Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / Facilities / NHibernateIntegration / Castle.Facilities.NHibernateIntegration.Tests / Internals / SessionManagerTestCase.cs
blob497f4f89d81c39a77236b3a23399a58ba82bcd86
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 using Castle.Facilities.NHibernateIntegration.Tests.Common;
17 namespace Castle.Facilities.NHibernateIntegration.Tests.Internals
19 using System;
21 using NUnit.Framework;
23 using NHibernate;
25 using Castle.MicroKernel.Facilities;
26 using Castle.Services.Transaction;
27 using ITransaction = Castle.Services.Transaction.ITransaction;
29 /// <summary>
30 /// Tests the default implementation of ISessionStore
31 /// </summary>
32 [TestFixture]
33 public class SessionManagerTestCase : AbstractNHibernateTestCase
35 [Test]
36 public void TwoDatabases()
38 ISessionManager manager = (ISessionManager)
39 container[typeof(ISessionManager)];
41 ISession session1 = manager.OpenSession();
42 ISession session2 = manager.OpenSession("db2");
44 Assert.IsNotNull(session1);
45 Assert.IsNotNull(session2);
47 Assert.IsFalse( Object.ReferenceEquals(session1, session2) );
49 session2.Dispose();
50 session1.Dispose();
52 Assert.IsTrue( (container[typeof(ISessionStore)]
53 as ISessionStore).IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
56 [Test]
57 public void NonInterceptedSession()
59 ISessionManager manager = (ISessionManager)
60 container[typeof(ISessionManager)];
61 string sessionAlias = "db2";
63 ISession session = manager.OpenSession(sessionAlias);
64 Order o = new Order();
65 o.Value = 9.3f;
66 session.SaveOrUpdate(o);
67 session.Close();
69 session = manager.OpenSession(sessionAlias);
70 session.Get(typeof(Order), 1);
71 session.Close();
73 TestInterceptor interceptor = container["nhibernate.session.interceptor.intercepted"] as TestInterceptor;
74 Assert.IsNotNull(interceptor);
75 Assert.IsFalse(interceptor.ConfirmOnSaveCall());
76 Assert.IsFalse(interceptor.ConfirmInstantiationCall());
77 interceptor.ResetState();
80 [Test]
81 public void InterceptedSessionByConfiguration()
83 ISessionManager manager = (ISessionManager)
84 container[typeof(ISessionManager)];
86 string sessionAlias = "intercepted";
88 ISession session = manager.OpenSession(sessionAlias);
89 Order o = new Order();
90 o.Value = 9.3f;
91 session.SaveOrUpdate(o);
92 session.Close();
94 session = manager.OpenSession(sessionAlias);
95 session.Get(typeof(Order), 1);
96 session.Close();
98 TestInterceptor interceptor = container["nhibernate.session.interceptor.intercepted"] as TestInterceptor;
99 Assert.IsNotNull(interceptor);
100 Assert.IsTrue(interceptor.ConfirmOnSaveCall());
101 Assert.IsTrue(interceptor.ConfirmInstantiationCall());
102 interceptor.ResetState();
105 [Test]
106 [ExpectedException(typeof(FacilityException))]
107 public void NonExistentAlias()
109 ISessionManager manager = (ISessionManager)
110 container[typeof(ISessionManager)];
112 manager.OpenSession("something in the way she moves");
115 [Test]
116 public void SharedSession()
118 ISessionManager manager = (ISessionManager)
119 container[typeof(ISessionManager)];
121 ISession session1 = manager.OpenSession();
122 ISession session2 = manager.OpenSession();
123 ISession session3 = manager.OpenSession();
125 Assert.IsNotNull(session1);
126 Assert.IsNotNull(session2);
127 Assert.IsNotNull(session3);
129 Assert.IsTrue(SessionDelegate.AreEqual(session1, session2));
130 Assert.IsTrue(SessionDelegate.AreEqual(session1, session3));
132 session3.Dispose();
133 session2.Dispose();
134 session1.Dispose();
136 Assert.IsTrue( (container[typeof(ISessionStore)]
137 as ISessionStore).IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
140 /// <summary>
141 /// This test ensures that the transaction takes
142 /// ownership of the session and disposes it at the end
143 /// of the transaction
144 /// </summary>
145 [Test]
146 // [Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
147 public void NewTransactionBeforeUsingSession()
149 ISessionManager manager = (ISessionManager)
150 container[typeof(ISessionManager)];
152 ITransactionManager tmanager = (ITransactionManager)
153 container[typeof(ITransactionManager)];
155 ITransaction transaction = tmanager.CreateTransaction(
156 TransactionMode.Requires, IsolationMode.Serializable );
158 transaction.Begin();
160 ISession session = manager.OpenSession();
162 Assert.IsNotNull(session);
163 Assert.IsNotNull(session.Transaction);
165 transaction.Commit();
167 // TODO: Assert transaction was committed
168 // Assert.IsTrue(session.Transaction.WasCommitted);
169 // Assert.IsTrue(session.IsConnected);
171 session.Dispose();
173 Assert.IsTrue( (container[typeof(ISessionStore)]
174 as ISessionStore).IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
177 /// <summary>
178 /// In this case the transaction should not take
179 /// ownership of the session (not dipose it at the
180 /// end of the transaction)
181 /// </summary>
182 [Test]
183 // [Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
184 public void NewTransactionAfterUsingSession()
186 ISessionManager manager = (ISessionManager)
187 container[typeof(ISessionManager)];
189 ISession session1 = manager.OpenSession();
191 ITransactionManager tmanager = (ITransactionManager)
192 container[typeof(ITransactionManager)];
194 ITransaction transaction = tmanager.CreateTransaction(
195 TransactionMode.Requires, IsolationMode.Serializable );
197 transaction.Begin();
199 // Nested
200 using(ISession session2 = manager.OpenSession())
202 Assert.IsNotNull(session2);
204 Assert.IsNotNull(session1);
205 Assert.IsNotNull(session1.Transaction, "After requesting compatible session, first session is enlisted in transaction too.");
206 Assert.IsTrue(session1.Transaction.IsActive, "After requesting compatible session, first session is enlisted in transaction too.");
208 using(ISession session3 = manager.OpenSession())
210 Assert.IsNotNull(session3);
211 Assert.IsNotNull(session3.Transaction);
212 Assert.IsTrue(session3.Transaction.IsActive);
215 SessionDelegate delagate1 = (SessionDelegate) session1;
216 SessionDelegate delagate2 = (SessionDelegate)session2;
217 Assert.AreSame(delagate1.InnerSession, delagate2.InnerSession);
220 transaction.Commit();
222 // TODO: Assert transaction was committed
223 // Assert.IsTrue(session1.Transaction.WasCommitted);
224 Assert.IsTrue(session1.IsConnected);
226 session1.Dispose();
228 Assert.IsTrue( (container[typeof(ISessionStore)]
229 as ISessionStore).IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
232 /// <summary>
233 /// This test ensures that the transaction enlists the
234 /// the sessions of both database connections
235 /// </summary>
236 [Test]
237 //[Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
238 public void NewTransactionBeforeUsingSessionWithTwoDatabases()
240 ISessionManager manager = (ISessionManager)
241 container[typeof(ISessionManager)];
243 ITransactionManager tmanager = (ITransactionManager)
244 container[typeof(ITransactionManager)];
246 ITransaction transaction = tmanager.CreateTransaction(
247 TransactionMode.Requires, IsolationMode.Serializable );
249 transaction.Begin();
251 ISession session1 = manager.OpenSession();
252 Assert.IsNotNull(session1);
253 Assert.IsNotNull(session1.Transaction);
255 ISession session2 = manager.OpenSession("db2");
256 Assert.IsNotNull(session2);
257 Assert.IsNotNull(session2.Transaction);
259 transaction.Commit();
261 // TODO: Assert transaction was committed
262 // Assert.IsTrue(session1.Transaction.WasCommitted);
263 // Assert.IsTrue(session1.IsConnected);
264 // TODO: Assert transaction was committed
265 // Assert.IsTrue(session2.Transaction.WasCommitted);
266 // Assert.IsTrue(session2.IsConnected);
268 session2.Dispose();
269 session1.Dispose();
271 Assert.IsTrue( (container[typeof(ISessionStore)]
272 as ISessionStore).IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
275 /// <summary>
276 /// This test ensures that the session is enlisted in actual transaction
277 /// only once for second database session
278 /// </summary>
279 [Test]
280 //[Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
281 public void SecondDatabaseSessionEnlistedOnlyOnceInActualTransaction()
283 ISessionManager manager = (ISessionManager)
284 container[typeof(ISessionManager)];
286 ITransactionManager tmanager = (ITransactionManager)
287 container[typeof(ITransactionManager)];
289 ITransaction transaction = tmanager.CreateTransaction(
290 TransactionMode.Requires, IsolationMode.Serializable );
292 transaction.Begin();
294 // open connection to first database and enlist session in running transaction
295 ISession session1 = manager.OpenSession();
297 // open connection to second database and enlist session in running transaction
298 using(ISession session2 = manager.OpenSession("db2"))
300 Assert.IsNotNull(session2);
301 Assert.IsNotNull(session2.Transaction);
303 // "real" NH session2 was not disposed because its in active transaction
305 // request compatible session for db2 --> we must get existing NH session to db2 which should be already enlisted in active transaction
306 using (ISession session3 = manager.OpenSession("db2"))
308 Assert.IsNotNull(session3);
309 Assert.IsTrue(session3.Transaction.IsActive);
312 transaction.Commit();
314 // TODO: Assert transaction was committed
315 // Assert.IsTrue(session1.Transaction.WasCommitted);
316 // Assert.IsTrue(session1.IsConnected);
318 session1.Dispose();
320 Assert.IsTrue( (container[typeof(ISessionStore)]
321 as ISessionStore).IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );