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 using Castle
.Facilities
.NHibernateIntegration
.Tests
.Common
;
17 namespace Castle
.Facilities
.NHibernateIntegration
.Tests
.Internals
21 using NUnit
.Framework
;
25 using Castle
.MicroKernel
.Facilities
;
26 using Castle
.Services
.Transaction
;
27 using ITransaction
= Castle
.Services
.Transaction
.ITransaction
;
30 /// Tests the default implementation of ISessionStore
33 public class SessionManagerTestCase
: AbstractNHibernateTestCase
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
) );
52 Assert
.IsTrue( (container
[typeof(ISessionStore
)]
53 as ISessionStore
).IsCurrentActivityEmptyFor( Constants
.DefaultAlias
) );
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();
66 session
.SaveOrUpdate(o
);
69 session
= manager
.OpenSession(sessionAlias
);
70 session
.Get(typeof(Order
), 1);
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();
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();
91 session
.SaveOrUpdate(o
);
94 session
= manager
.OpenSession(sessionAlias
);
95 session
.Get(typeof(Order
), 1);
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();
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");
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
));
136 Assert
.IsTrue( (container
[typeof(ISessionStore
)]
137 as ISessionStore
).IsCurrentActivityEmptyFor( Constants
.DefaultAlias
) );
141 /// This test ensures that the transaction takes
142 /// ownership of the session and disposes it at the end
143 /// of the transaction
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
);
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);
173 Assert
.IsTrue( (container
[typeof(ISessionStore
)]
174 as ISessionStore
).IsCurrentActivityEmptyFor( Constants
.DefaultAlias
) );
178 /// In this case the transaction should not take
179 /// ownership of the session (not dipose it at the
180 /// end of the transaction)
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
);
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
);
228 Assert
.IsTrue( (container
[typeof(ISessionStore
)]
229 as ISessionStore
).IsCurrentActivityEmptyFor( Constants
.DefaultAlias
) );
233 /// This test ensures that the transaction enlists the
234 /// the sessions of both database connections
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
);
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);
271 Assert
.IsTrue( (container
[typeof(ISessionStore
)]
272 as ISessionStore
).IsCurrentActivityEmptyFor( Constants
.DefaultAlias
) );
276 /// This test ensures that the session is enlisted in actual transaction
277 /// only once for second database session
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
);
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);
320 Assert
.IsTrue( (container
[typeof(ISessionStore
)]
321 as ISessionStore
).IsCurrentActivityEmptyFor( Constants
.DefaultAlias
) );