Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / Facilities / ActiveRecordIntegration / Castle.Facilities.ActiveRecordIntegration.Tests / Transaction2TestCase.cs
blob8e3883d15ab10fd47344ccaea87f253d1547c303
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.ActiveRecordIntegration.Tests
17 using System;
18 using Castle.ActiveRecord;
19 using NUnit.Framework;
21 using Castle.Facilities.ActiveRecordIntegration.Tests.Model;
24 [TestFixture]
25 public class Transaction2TestCase : AbstractActiveRecordTest
27 [Test]
28 public void OneLevelTransaction()
30 Post.DeleteAll();
31 Blog.DeleteAll();
33 using(new SessionScope())
35 Blog.FindAll(); // side effects only
37 BlogService service = (BlogService) container[ typeof(BlogService) ];
38 service.Create( "name", "author" );
41 Assert.AreEqual( 1, Blog.FindAll().Length );
44 [Test]
45 public void TransactionUsingParentSessionScope1()
47 Post.DeleteAll();
48 Blog.DeleteAll();
50 using(new SessionScope())
52 Blog.FindAll(); // side effects only
54 BlogService service = (BlogService) container[ typeof(BlogService) ];
55 Blog blog = service.Create( "name", "author" );
57 try
59 service.ModifyAndThrowException( blog, "name2" );
60 Assert.Fail("Exception expected");
61 } catch(Exception) {}
64 Assert.AreEqual( 1, Blog.FindAll().Length );
65 Blog blog2 = Blog.FindAll()[0];
66 Assert.AreEqual( "name", blog2.Name );
69 [Test]
70 public void TransactionUsingParentSessionScope2()
72 Post.DeleteAll();
73 Blog.DeleteAll();
75 using(new SessionScope())
77 Blog.FindAll(); // side effects only
79 BlogService service = (BlogService) container[ typeof(BlogService) ];
80 Blog blog = service.Create( "name", "author" );
82 try
84 service.ModifyAndThrowException2( blog, "name2" );
85 Assert.Fail("Exception expected");
87 catch(Exception) {}
90 Assert.AreEqual( 1, Blog.FindAll().Length );
91 Blog blog2 = Blog.FindAll()[0];
92 Assert.AreEqual( "name", blog2.Name );
95 [Test]
96 public void OneLevelTransactionRollback1()
98 Post.DeleteAll();
99 Blog.DeleteAll();
101 BlogService service = (BlogService) container[ typeof(BlogService) ];
103 using(new SessionScope())
105 Blog.FindAll(); // side effects only
109 service.CreateAndThrowException( "name", "author" );
111 catch(Exception)
116 Assert.AreEqual( 0, Blog.FindAll().Length );
119 [Test]
120 public void OneLevelTransactionRollback2()
122 Post.DeleteAll();
123 Blog.DeleteAll();
125 BlogService service = (BlogService) container[ typeof(BlogService) ];
127 using(new SessionScope())
129 Blog.FindAll(); // side effects only
133 service.CreateAndThrowException2( "name", "author" );
135 catch(Exception)
140 Assert.AreEqual( 0, Blog.FindAll().Length );
143 [Test]
144 public void OneLevelTransactionRollback3()
146 Post.DeleteAll();
147 Blog.DeleteAll();
149 BlogService service = (BlogService) container[ typeof(BlogService) ];
151 using(new SessionScope())
153 Blog.FindAll(); // side effects only
157 service.CreateAndThrowException3( "name", "author" );
159 catch(Exception)
164 Assert.AreEqual( 0, Blog.FindAll().Length );
167 [Test]
168 public void TwoLevelTransaction()
170 Post.DeleteAll();
171 Blog.DeleteAll();
173 FirstService service = (FirstService) container[ typeof(FirstService) ];
175 using(new SessionScope())
177 Blog.FindAll(); // side effects only
179 service.CreateBlogAndPost();
182 Assert.AreEqual( 1, Blog.FindAll().Length );
183 Assert.AreEqual( 1, Post.FindAll().Length );
186 [Test]
187 public void TwoLevelTransaction2()
189 Post.DeleteAll();
190 Blog.DeleteAll();
192 FirstService service = (FirstService) container[ typeof(FirstService) ];
194 using(new SessionScope())
196 Blog.FindAll(); // side effects only
200 service.CreateBlogAndPost2();
202 catch(Exception)
208 Assert.AreEqual( 0, Blog.FindAll().Length );
209 Assert.AreEqual( 0, Post.FindAll().Length );