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
.ActiveRecordIntegration
.Tests
18 using Castle
.ActiveRecord
;
19 using NUnit
.Framework
;
21 using Castle
.Facilities
.ActiveRecordIntegration
.Tests
.Model
;
25 public class Transaction2TestCase
: AbstractActiveRecordTest
28 public void OneLevelTransaction()
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
);
45 public void TransactionUsingParentSessionScope1()
50 using(new SessionScope())
52 Blog
.FindAll(); // side effects only
54 BlogService service
= (BlogService
) container
[ typeof(BlogService
) ];
55 Blog blog
= service
.Create( "name", "author" );
59 service
.ModifyAndThrowException( blog
, "name2" );
60 Assert
.Fail("Exception expected");
64 Assert
.AreEqual( 1, Blog
.FindAll().Length
);
65 Blog blog2
= Blog
.FindAll()[0];
66 Assert
.AreEqual( "name", blog2
.Name
);
70 public void TransactionUsingParentSessionScope2()
75 using(new SessionScope())
77 Blog
.FindAll(); // side effects only
79 BlogService service
= (BlogService
) container
[ typeof(BlogService
) ];
80 Blog blog
= service
.Create( "name", "author" );
84 service
.ModifyAndThrowException2( blog
, "name2" );
85 Assert
.Fail("Exception expected");
90 Assert
.AreEqual( 1, Blog
.FindAll().Length
);
91 Blog blog2
= Blog
.FindAll()[0];
92 Assert
.AreEqual( "name", blog2
.Name
);
96 public void OneLevelTransactionRollback1()
101 BlogService service
= (BlogService
) container
[ typeof(BlogService
) ];
103 using(new SessionScope())
105 Blog
.FindAll(); // side effects only
109 service
.CreateAndThrowException( "name", "author" );
116 Assert
.AreEqual( 0, Blog
.FindAll().Length
);
120 public void OneLevelTransactionRollback2()
125 BlogService service
= (BlogService
) container
[ typeof(BlogService
) ];
127 using(new SessionScope())
129 Blog
.FindAll(); // side effects only
133 service
.CreateAndThrowException2( "name", "author" );
140 Assert
.AreEqual( 0, Blog
.FindAll().Length
);
144 public void OneLevelTransactionRollback3()
149 BlogService service
= (BlogService
) container
[ typeof(BlogService
) ];
151 using(new SessionScope())
153 Blog
.FindAll(); // side effects only
157 service
.CreateAndThrowException3( "name", "author" );
164 Assert
.AreEqual( 0, Blog
.FindAll().Length
);
168 public void TwoLevelTransaction()
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
);
187 public void TwoLevelTransaction2()
192 FirstService service
= (FirstService
) container
[ typeof(FirstService
) ];
194 using(new SessionScope())
196 Blog
.FindAll(); // side effects only
200 service
.CreateBlogAndPost2();
208 Assert
.AreEqual( 0, Blog
.FindAll().Length
);
209 Assert
.AreEqual( 0, Post
.FindAll().Length
);