Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / SessionScopeTestCase.cs
blob5adc0c5f2fe89a7f0ab97b93188d3ece88674188
1 // Copyright 2004-2007 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.ActiveRecord.Tests
17 using System;
18 using Castle.ActiveRecord.Framework;
19 using Castle.ActiveRecord.Framework.Scopes;
20 using Castle.ActiveRecord.Tests.Model;
21 using Castle.ActiveRecord.Tests.Model.LazyModel;
22 using NHibernate;
23 using NUnit.Framework;
25 [TestFixture]
26 public class SessionScopeTestCase : AbstractActiveRecordTest
28 [Test]
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;
35 try
37 new SessionScope();
39 finally
41 ThreadScopeAccessor.Instance.ScopeInfo = scope;
45 [Test]
46 public void OneDatabaseSameSession()
48 ISession session1, session2, session3, session4;
50 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Post), typeof(Blog) );
51 Recreate();
53 // No scope here
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);
66 // With scope
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);
117 [Test]
118 public void SessionScopeUsage()
120 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Post), typeof(Blog) );
121 Recreate();
123 Post.DeleteAll();
124 Blog.DeleteAll();
126 using(new SessionScope())
128 Blog blog = new Blog();
129 blog.Author = "hammett";
130 blog.Name = "some name";
131 blog.Save();
133 Post post = new Post(blog, "title", "post contents", "Castle");
134 post.Save();
137 Blog[] blogs = Blog.FindAll();
138 Assert.AreEqual( 1, blogs.Length );
140 Post[] posts = Post.FindAll();
141 Assert.AreEqual( 1, posts.Length );
144 [Test]
145 public void NestedSessionScopeUsage()
147 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Post), typeof(Blog) );
148 Recreate();
150 Post.DeleteAll();
151 Blog.DeleteAll();
153 using(new SessionScope())
155 Blog blog = new Blog();
157 using(new SessionScope())
159 blog.Author = "hammett";
160 blog.Name = "some name";
161 blog.Save();
164 using(new SessionScope())
166 Post post = new Post(blog, "title", "post contents", "Castle");
167 post.Save();
171 Blog[] blogs = Blog.FindAll();
172 Assert.AreEqual( 1, blogs.Length );
174 Post[] posts = Post.FindAll();
175 Assert.AreEqual( 1, posts.Length );
178 [Test]
179 public void NestedSessionScopeAndLazyLoad()
181 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(ProductLazy), typeof(CategoryLazy) );
182 Recreate();
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") );
190 product.Save();
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;
226 [Test]
227 public void AnExceptionInvalidatesTheScopeAndPreventItsFlushing()
229 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
230 Recreate();
232 Post.DeleteAll();
233 Blog.DeleteAll();
235 Blog blog;
236 Post post;
238 // Prepare
239 using(new SessionScope())
241 blog = new Blog();
242 blog.Author = "hammett";
243 blog.Name = "some name";
244 blog.Save();
246 post = new Post(blog, "title", "contents", "castle");
247 post.Save();
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");
258 post.SaveAndFlush();
259 Assert.Fail("Expecting exception as this operation violates a FK constraint");
261 catch(ActiveRecordException)
263 // Exception expected
266 Assert.IsTrue(session.HasSessionError);
270 [Test]
271 public void SessionScopeFlushModeNever()
273 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
274 Recreate();
276 Post.DeleteAll();
277 Blog.DeleteAll();
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
286 blog.Save();
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";
293 blog.Save();
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";
307 blog.SaveAndFlush();
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
314 blog.Delete();
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);