1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Tests
18 using System
.Collections
;
20 using NUnit
.Framework
;
22 using Castle
.ActiveRecord
.Tests
.Model
;
23 using Iesi
.Collections
;
26 public class TableHierarchyTestCase
: AbstractActiveRecordTest
29 public void CompanyFirmAndClient()
31 ActiveRecordStarter
.Initialize( GetConfigSource(),
32 typeof(Company
), typeof(Client
), typeof(Firm
), typeof(Person
) );
40 Firm firm
= new Firm("keldor");
43 Client client
= new Client("castle", firm
);
46 Client
[] clients
= Client
.FindAll();
47 Assert
.AreEqual( 1, clients
.Length
);
49 Firm
[] firms
= Firm
.FindAll();
50 Assert
.AreEqual( 1, firms
.Length
);
52 Assert
.AreEqual( firm
.Id
, firms
[0].Id
);
53 Assert
.AreEqual( client
.Id
, clients
[0].Id
);
55 Assert
.IsNotNull( clients
[0].Firm
);
56 Assert
.AreEqual( firm
.Id
, clients
[0].Firm
.Id
);
60 public void ManyToMany()
62 ActiveRecordStarter
.Initialize( GetConfigSource(),
63 typeof(Company
), typeof(Client
), typeof(Firm
), typeof(Person
) );
71 Firm firm
= new Firm("keldor");
72 Client client
= new Client("castle", firm
);
73 Company company
= new Company("vs");
75 using(new SessionScope())
81 Person person
= new Person();
82 person
.Name
= "hammett";
84 person
.Companies
.Add( firm
);
85 person
.Companies
.Add( client
);
86 person
.Companies
.Add( company
);
90 company
= Company
.Find( company
.Id
);
91 Assert
.AreEqual(1, company
.People
.Count
);
95 public void ManyToManyUsingSet()
97 ActiveRecordStarter
.Initialize(GetConfigSource(),
98 typeof(Order
), typeof(Product
)/*, typeof(LineItem)*/);
104 Order myOrder
= new Order();
105 myOrder
.OrderedDate
= DateTime
.Parse("05/09/2004");
106 Product coolGadget
= new Product();
107 coolGadget
.Name
= "PSP";
108 coolGadget
.Price
= 250.39f
;
110 using (new SessionScope())
113 ISet products
= new ListSet();
114 products
.Add(coolGadget
);
115 myOrder
.Products
= products
;
119 Order secondRef2Order
= Order
.Find(myOrder
.ID
);
120 Assert
.IsFalse(secondRef2Order
.Products
.IsEmpty
);
122 Product secondRef2Product
= Product
.Find(coolGadget
.ID
);
123 Assert
.AreEqual(1, secondRef2Product
.Orders
.Count
);
127 [Ignore("Jira issue for NH team")]
128 public void InvalidSessionCache()
130 ActiveRecordStarter
.Initialize( GetConfigSource(),
131 typeof(Company
), typeof(Client
), typeof(Firm
), typeof(Person
) );
139 Firm firm
= new Firm("keldor");
140 Client client
= new Client("castle", firm
);
141 Company company
= new Company("vs");
143 using(new SessionScope())
150 using(new SessionScope())
154 Client c
= Client
.Find( firm
.Id
);
156 Assert
.Fail("Exception was expected");
163 Firm firm2
= Firm
.Find( firm
.Id
);
165 Assert
.IsNotNull(firm2
);
170 [Ignore("Create schema does not create all necessary tables for this test case")]
171 public void ManyToManyUsingIDBag()
173 ActiveRecordStarter
.Initialize(GetConfigSource(),
174 typeof(OrderWithIDBag
), typeof(ProductWithIDBag
));
177 OrderWithIDBag
.DeleteAll();
178 ProductWithIDBag
.DeleteAll();
180 OrderWithIDBag myOrder
= new OrderWithIDBag();
181 myOrder
.OrderedDate
= new DateTime(2006, 12, 25);
182 ProductWithIDBag coolGadget
= new ProductWithIDBag();
183 coolGadget
.Name
= "Xbox 2";
184 coolGadget
.Price
= 330.23f
;
186 using (new SessionScope())
189 IList products
= new ArrayList();
190 products
.Add(coolGadget
);
191 myOrder
.Products
= products
;
195 OrderWithIDBag secondRef2Order
= OrderWithIDBag
.Find(myOrder
.ID
);
196 Assert
.IsTrue(secondRef2Order
.Products
.Count
> 0);
198 ProductWithIDBag secondRef2Product
= ProductWithIDBag
.Find(coolGadget
.ID
);
199 Assert
.AreEqual(1, secondRef2Product
.Orders
.Count
);