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 Castle
.ActiveRecord
.Framework
;
19 using Castle
.ActiveRecord
.Framework
.Scopes
;
20 using Castle
.ActiveRecord
.Tests
.Model
;
21 using Castle
.ActiveRecord
.Tests
.Model
.LazyModel
;
23 using NUnit
.Framework
;
26 public class SessionScopeTestCase
: AbstractActiveRecordTest
29 [ExpectedException(typeof(ActiveRecordException
), "A scope tried to registered itself within the framework, but the Active Record was not initialized")]
30 public void GoodErrorMessageIfTryingToUseScopeWithoutInitializingFramework()
32 //Need to do this because other tests may have already initialized the framework.
33 IThreadScopeInfo scope
= ThreadScopeAccessor
.Instance
.ScopeInfo
;
34 ThreadScopeAccessor
.Instance
.ScopeInfo
= null;
41 ThreadScopeAccessor
.Instance
.ScopeInfo
= scope
;
46 public void OneDatabaseSameSession()
48 ISession session1
, session2
, session3
, session4
;
50 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Post
), typeof(Blog
) );
54 // So no optimization, thus different sessions
56 session1
= Blog
.Holder
.CreateSession( typeof(Blog
) );
57 session2
= Blog
.Holder
.CreateSession( typeof(Blog
) );
59 Assert
.IsNotNull( session1
);
60 Assert
.IsNotNull( session2
);
61 Assert
.IsTrue( session1
!= session2
);
63 Blog
.Holder
.ReleaseSession(session1
);
64 Blog
.Holder
.ReleaseSession(session2
);
68 using(new SessionScope())
70 session1
= Blog
.Holder
.CreateSession( typeof(Blog
) );
71 session2
= Blog
.Holder
.CreateSession( typeof(Post
) );
72 session3
= Blog
.Holder
.CreateSession( typeof(Blog
) );
73 session4
= Blog
.Holder
.CreateSession( typeof(Post
) );
75 Assert
.IsNotNull( session1
);
76 Assert
.IsNotNull( session2
);
77 Assert
.IsNotNull( session3
);
78 Assert
.IsNotNull( session3
);
80 Assert
.IsTrue( session2
== session1
);
81 Assert
.IsTrue( session3
== session1
);
82 Assert
.IsTrue( session4
== session1
);
84 Blog
.Holder
.ReleaseSession(session1
);
85 Blog
.Holder
.ReleaseSession(session2
);
86 Blog
.Holder
.ReleaseSession(session3
);
87 Blog
.Holder
.ReleaseSession(session4
);
89 session1
= Blog
.Holder
.CreateSession( typeof(Post
) );
90 session2
= Blog
.Holder
.CreateSession( typeof(Post
) );
91 session3
= Blog
.Holder
.CreateSession( typeof(Blog
) );
92 session4
= Blog
.Holder
.CreateSession( typeof(Blog
) );
94 Assert
.IsNotNull( session1
);
95 Assert
.IsNotNull( session2
);
96 Assert
.IsNotNull( session3
);
97 Assert
.IsNotNull( session3
);
99 Assert
.IsTrue( session2
== session1
);
100 Assert
.IsTrue( session3
== session1
);
101 Assert
.IsTrue( session4
== session1
);
104 // Back to the old behavior
106 session1
= Blog
.Holder
.CreateSession( typeof(Blog
) );
107 session2
= Blog
.Holder
.CreateSession( typeof(Blog
) );
109 Assert
.IsNotNull( session1
);
110 Assert
.IsNotNull( session2
);
111 Assert
.IsTrue( session1
!= session2
);
113 Blog
.Holder
.ReleaseSession(session1
);
114 Blog
.Holder
.ReleaseSession(session2
);
118 public void SessionScopeUsage()
120 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Post
), typeof(Blog
) );
126 using(new SessionScope())
128 Blog blog
= new Blog();
129 blog
.Author
= "hammett";
130 blog
.Name
= "some name";
133 Post post
= new Post(blog
, "title", "post contents", "Castle");
137 Blog
[] blogs
= Blog
.FindAll();
138 Assert
.AreEqual( 1, blogs
.Length
);
140 Post
[] posts
= Post
.FindAll();
141 Assert
.AreEqual( 1, posts
.Length
);
145 public void NestedSessionScopeUsage()
147 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Post
), typeof(Blog
) );
153 using(new SessionScope())
155 Blog blog
= new Blog();
157 using(new SessionScope())
159 blog
.Author
= "hammett";
160 blog
.Name
= "some name";
164 using(new SessionScope())
166 Post post
= new Post(blog
, "title", "post contents", "Castle");
171 Blog
[] blogs
= Blog
.FindAll();
172 Assert
.AreEqual( 1, blogs
.Length
);
174 Post
[] posts
= Post
.FindAll();
175 Assert
.AreEqual( 1, posts
.Length
);
179 public void NestedSessionScopeAndLazyLoad()
181 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(ProductLazy
), typeof(CategoryLazy
) );
184 ProductLazy product
= new ProductLazy();
186 product
.Categories
.Add( new CategoryLazy("x") );
187 product
.Categories
.Add( new CategoryLazy("y") );
188 product
.Categories
.Add( new CategoryLazy("z") );
192 using(new SessionScope())
194 ProductLazy product1
= ProductLazy
.Find(product
.Id
);
195 Assert
.AreEqual( 3, product1
.Categories
.Count
);
197 foreach(CategoryLazy cat
in product1
.Categories
)
199 object dummy
= cat
.Name
;
202 ProductLazy product2
= ProductLazy
.Find(product
.Id
);
203 Assert
.AreEqual( 3, product2
.Categories
.Count
);
205 using(new SessionScope())
207 foreach(CategoryLazy cat
in product2
.Categories
)
209 object dummy
= cat
.Name
;
213 using(new SessionScope())
215 ProductLazy product3
= ProductLazy
.Find(product
.Id
);
216 Assert
.AreEqual( 3, product3
.Categories
.Count
);
218 foreach(CategoryLazy cat
in product3
.Categories
)
220 object dummy
= cat
.Name
;
227 public void AnExceptionInvalidatesTheScopeAndPreventItsFlushing()
229 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
239 using(new SessionScope())
242 blog
.Author
= "hammett";
243 blog
.Name
= "some name";
246 post
= new Post(blog
, "title", "contents", "castle");
250 using(SessionScope session
= new SessionScope())
252 Assert
.IsFalse(session
.HasSessionError
);
256 // Forcing an exception
257 post
= new Post(new Blog(100), "title", "contents", "castle");
259 Assert
.Fail("Expecting exception as this operation violates a FK constraint");
261 catch(ActiveRecordException
)
263 // Exception expected
266 Assert
.IsTrue(session
.HasSessionError
);
271 public void SessionScopeFlushModeNever()
273 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
279 using(new SessionScope(FlushAction
.Never
))
281 Blog blog
= new Blog();
282 blog
.Author
= "hammett";
283 blog
.Name
= "some name";
285 //This gets flushed automatically because of the identity field
288 Blog
[] blogs
= Blog
.FindAll();
289 Assert
.AreEqual(1, blogs
.Length
);
291 //This change won't be saved to the db
292 blog
.Author
= "A New Author";
295 //The change should not be in the db
296 blogs
= Blog
.FindByProperty("Author", "A New Author");
297 Assert
.AreEqual(0, blogs
.Length
);
299 SessionScope
.Current
.Flush();
301 //The change should now be in the db
302 blogs
= Blog
.FindByProperty("Author", "A New Author");
303 Assert
.AreEqual(1, blogs
.Length
);
305 //This change will be save to the db because it uses the SaveNow method
306 blog
.Name
= "A New Name";
309 //The change should now be in the db
310 blogs
= Blog
.FindByProperty("Name", "A New Name");
311 Assert
.AreEqual(1, blogs
.Length
);
313 //This deletion should not get to the db
316 blogs
= Blog
.FindAll();
317 Assert
.AreEqual(1, blogs
.Length
);
319 SessionScope
.Current
.Flush();
321 //The deletion should now be in the db
322 blogs
= Blog
.FindAll();
323 Assert
.AreEqual(0, blogs
.Length
);