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
.Facilities
.AutomaticTransactionManagement
.Tests
18 using NUnit
.Framework
;
19 using Castle
.MicroKernel
.SubSystems
.Configuration
;
21 using Castle
.Services
.Transaction
;
24 public class FacilityBasicTestCase
27 public void TestReportedBug()
29 WindsorContainer container
= new WindsorContainer(new DefaultConfigurationStore());
31 container
.AddFacility("transactionmanagement", new TransactionFacility());
33 container
.AddComponent("transactionmanager",
34 typeof(ITransactionManager
), typeof(MockTransactionManager
));
36 container
.AddComponent("comp", typeof(SubTransactionalComp
));
38 SubTransactionalComp service
= (SubTransactionalComp
)container
["comp"];
42 MockTransactionManager transactionManager
= (MockTransactionManager
)
43 container
["transactionmanager"];
45 Assert
.AreEqual(1, transactionManager
.TransactionCount
);
46 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
47 Assert
.AreEqual(0, transactionManager
.RolledBackCount
);
51 public void TestBasicOperations()
53 WindsorContainer container
= new WindsorContainer(new DefaultConfigurationStore());
55 container
.AddFacility("transactionmanagement", new TransactionFacility());
57 container
.AddComponent("transactionmanager",
58 typeof(ITransactionManager
), typeof(MockTransactionManager
));
60 container
.AddComponent("services.customer", typeof(CustomerService
));
62 CustomerService service
= (CustomerService
)container
["services.customer"];
64 service
.Insert("TestCustomer", "Rua P Leite, 33");
66 MockTransactionManager transactionManager
= (MockTransactionManager
)
67 container
["transactionmanager"];
69 Assert
.AreEqual(1, transactionManager
.TransactionCount
);
70 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
71 Assert
.AreEqual(0, transactionManager
.RolledBackCount
);
82 Assert
.AreEqual(2, transactionManager
.TransactionCount
);
83 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
84 Assert
.AreEqual(1, transactionManager
.RolledBackCount
);
88 public void TestBasicOperationsWithInterfaceService()
90 WindsorContainer container
= new WindsorContainer(new DefaultConfigurationStore());
92 container
.AddFacility("transactionmanagement", new TransactionFacility());
93 container
.AddComponent("transactionmanager", typeof(ITransactionManager
), typeof(MockTransactionManager
));
94 container
.AddComponent("services.customer", typeof(ICustomerService
), typeof(AnotherCustomerService
));
96 ICustomerService service
= (ICustomerService
)container
["services.customer"];
98 service
.Insert("TestCustomer", "Rua P Leite, 33");
100 MockTransactionManager transactionManager
= (MockTransactionManager
)container
["transactionmanager"];
102 Assert
.AreEqual(1, transactionManager
.TransactionCount
);
103 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
104 Assert
.AreEqual(0, transactionManager
.RolledBackCount
);
115 Assert
.AreEqual(2, transactionManager
.TransactionCount
);
116 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
117 Assert
.AreEqual(1, transactionManager
.RolledBackCount
);
121 public void TestBasicOperationsWithGenericService()
123 WindsorContainer container
= new WindsorContainer(new DefaultConfigurationStore());
125 container
.AddFacility("transactionmanagement", new TransactionFacility());
126 container
.AddComponent("transactionmanager", typeof(ITransactionManager
), typeof(MockTransactionManager
));
127 container
.AddComponent("generic.services", typeof(GenericService
<>));
129 GenericService
<string> genericService
= container
.Resolve
<GenericService
<string>>();
131 genericService
.Foo();
133 MockTransactionManager transactionManager
= (MockTransactionManager
)container
["transactionmanager"];
135 Assert
.AreEqual(1, transactionManager
.TransactionCount
);
136 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
137 Assert
.AreEqual(0, transactionManager
.RolledBackCount
);
141 genericService
.Throw();
148 Assert
.AreEqual(2, transactionManager
.TransactionCount
);
149 Assert
.AreEqual(1, transactionManager
.CommittedCount
);
150 Assert
.AreEqual(1, transactionManager
.RolledBackCount
);
152 genericService
.Bar
<int>();
154 Assert
.AreEqual(3, transactionManager
.TransactionCount
);
155 Assert
.AreEqual(2, transactionManager
.CommittedCount
);
156 Assert
.AreEqual(1, transactionManager
.RolledBackCount
);
160 genericService
.Throw
<float>();
167 Assert
.AreEqual(4, transactionManager
.TransactionCount
);
168 Assert
.AreEqual(2, transactionManager
.CommittedCount
);
169 Assert
.AreEqual(2, transactionManager
.RolledBackCount
);
173 public void TestBasicOperationsWithConfigComponent()
175 WindsorContainer container
= new WindsorContainer(ConfigHelper
.ResolvePath("../HasConfiguration.xml"));
177 container
.AddComponent("transactionmanager",
178 typeof(ITransactionManager
), typeof(MockTransactionManager
));
180 TransactionalComp1 comp1
= (TransactionalComp1
)container
.Resolve("mycomp");
188 MockTransactionManager transactionManager
= (MockTransactionManager
)
189 container
["transactionmanager"];
191 Assert
.AreEqual(3, transactionManager
.TransactionCount
);
192 Assert
.AreEqual(3, transactionManager
.CommittedCount
);
193 Assert
.AreEqual(0, transactionManager
.RolledBackCount
);
197 /// Tests the situation where the class uses
198 /// ATM, but grab the transaction manager and rollbacks the
199 /// transaction manually
202 public void RollBackExplicitOnClass()
204 WindsorContainer container
= new WindsorContainer();
206 container
.AddFacility("transactionmanagement", new TransactionFacility());
208 container
.AddComponent("transactionmanager",
209 typeof(ITransactionManager
), typeof(MockTransactionManager
));
211 container
.AddComponent("mycomp", typeof(CustomerService
));
213 CustomerService serv
= (CustomerService
) container
.Resolve("mycomp");
217 MockTransactionManager transactionManager
= (MockTransactionManager
)
218 container
["transactionmanager"];
220 Assert
.AreEqual(1, transactionManager
.TransactionCount
);
221 Assert
.AreEqual(1, transactionManager
.RolledBackCount
);
222 Assert
.AreEqual(0, transactionManager
.CommittedCount
);