Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / Facilities / NHibernateIntegration / Castle.Facilities.NHibernateIntegration.Tests / Internals / SessionStoreTestCase.cs
blobd0fac18456e58696e76f6377d3cc312a07bb0a10
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 namespace Castle.Facilities.NHibernateIntegration.Tests.Internals
17 using System;
18 using System.Threading;
19 using NUnit.Framework;
21 using NHibernate;
23 /// <summary>
24 /// Tests the default implementation of ISessionStore
25 /// </summary>
26 [TestFixture]
27 public class SessionStoreTestCase : AbstractNHibernateTestCase
29 private AutoResetEvent arEvent = new AutoResetEvent(false);
31 [Test]
32 [ExpectedException( typeof(ArgumentNullException) )]
33 public void NullAlias()
35 ISessionStore store = (ISessionStore) container[typeof(ISessionStore)];
37 store.FindCompatibleSession(null);
40 [Test]
41 public void FindCompatibleSession()
43 ISessionStore store = (ISessionStore) container[typeof(ISessionStore)];
44 ISessionFactory factory = (ISessionFactory) container[typeof(ISessionFactory)];
46 ISession session = store.FindCompatibleSession( Constants.DefaultAlias );
48 Assert.IsNull(session);
50 session = factory.OpenSession();
52 SessionDelegate sessDelegate = new SessionDelegate(true, session, store);
54 store.Store( Constants.DefaultAlias, sessDelegate );
56 Assert.IsNotNull(sessDelegate.SessionStoreCookie);
58 ISession session2 = store.FindCompatibleSession( "something in the way she moves" );
60 Assert.IsNull(session2);
62 session2 = store.FindCompatibleSession( Constants.DefaultAlias );
64 Assert.IsNotNull(session2);
65 Assert.AreSame(sessDelegate, session2);
67 session.Dispose();
69 store.Remove(sessDelegate);
71 session = store.FindCompatibleSession( Constants.DefaultAlias );
73 Assert.IsNull(session);
75 Assert.IsTrue( store.IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
78 [Test]
79 public void FindCompatibleSessionWithTwoThreads()
81 ISessionStore store = (ISessionStore) container[typeof(ISessionStore)];
82 ISessionFactory factory = (ISessionFactory) container[typeof(ISessionFactory)];
84 ISession session = factory.OpenSession();
86 SessionDelegate sessDelegate = new SessionDelegate(true, session, store);
88 store.Store( Constants.DefaultAlias, sessDelegate );
90 ISession session2 = store.FindCompatibleSession( Constants.DefaultAlias );
92 Assert.IsNotNull(session2);
93 Assert.AreSame(sessDelegate, session2);
95 Thread newThread = new Thread(new ThreadStart(FindCompatibleSessionOnOtherThread));
96 newThread.Start();
98 arEvent.WaitOne();
100 sessDelegate.Dispose();
102 Assert.IsTrue( store.IsCurrentActivityEmptyFor( Constants.DefaultAlias ) );
105 private void FindCompatibleSessionOnOtherThread()
107 ISessionStore store = (ISessionStore) container[typeof(ISessionStore)];
109 ISession session = store.FindCompatibleSession( "something in the way she moves" );
111 Assert.IsNull(session);
113 ISession session2 = store.FindCompatibleSession( Constants.DefaultAlias );
115 Assert.IsNull(session2);
117 arEvent.Set();