setting all protected methods on WizardStepPage as virtual
[castle.git] / Facilities / AutomaticTransactionManagement / Castle.Facilities.AutomaticTransactionManagement.Tests / TransactionTestCase.cs
blobd5bbe84639503c90860d2f5add9726060cdec8e7
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using NUnit.Framework;
19 using Castle.MicroKernel.SubSystems.Configuration;
20 using Castle.Windsor;
21 using Castle.Services.Transaction;
23 [TestFixture]
24 public class FacilityBasicTestCase
26 [Test]
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"];
40 service.BaseMethod();
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);
50 [Test]
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);
73 try
75 service.Delete(1);
77 catch (Exception)
79 // Expected
82 Assert.AreEqual(2, transactionManager.TransactionCount);
83 Assert.AreEqual(1, transactionManager.CommittedCount);
84 Assert.AreEqual(1, transactionManager.RolledBackCount);
87 [Test]
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);
108 service.Delete(1);
110 catch (Exception)
112 // Expected
115 Assert.AreEqual(2, transactionManager.TransactionCount);
116 Assert.AreEqual(1, transactionManager.CommittedCount);
117 Assert.AreEqual(1, transactionManager.RolledBackCount);
120 [Test]
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();
143 catch (Exception)
145 // Expected
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>();
162 catch
164 //exepected
167 Assert.AreEqual(4, transactionManager.TransactionCount);
168 Assert.AreEqual(2, transactionManager.CommittedCount);
169 Assert.AreEqual(2, transactionManager.RolledBackCount);
172 [Test]
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");
182 comp1.Create();
184 comp1.Delete();
186 comp1.Save();
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);
196 /// <summary>
197 /// Tests the situation where the class uses
198 /// ATM, but grab the transaction manager and rollbacks the
199 /// transaction manually
200 /// </summary>
201 [Test]
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");
215 serv.Update(1);
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);