Example #1
0
        public void FlushAndPauseShouldNotCloseTheSession()
        {
            NhConversation c = NewStartedConversation();

            c.FlushAndPause();
            AssertIsPaused(c.GetSession(sessions));
        }
		public void NeedsSessionFactoryAndContext()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			Assert.That(c != null);
		}
		public void DefaultCommitModeIsAutomatic()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			var cm = c.CommitMode;
			Assert.That(cm, Is.EqualTo(ConversationCommitMode.Automatic));
		}
		public void CanSetDifferentCommitMode()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var cm = ConversationCommitMode.Explicit;
			var c = new NhConversation(sf, cc) { CommitMode = cm };
			Assert.That(c.CommitMode, Is.EqualTo(cm));
		}
Example #5
0
        public void StartedUnitOfWorkShouldNotFlushToDatabaseUntilEndOfConversation()
        {
            NhConversation c = NewStartedConversation();

            ISession s = c.GetSession(sessions);

            Assert.That(s.FlushMode, Is.EqualTo(FlushMode.Never));
        }
Example #6
0
        public void StartShouldStartUnitOfWork()
        {
            NhConversation c = NewConversation();

            c.Start();
            ISession s = c.GetSession(sessions);

            AssertIsOpen(s);
        }
Example #7
0
        public void PauseShouldDisconnectButNotCloseUnitOfWork()
        {
            NhConversation c = NewStartedConversation();

            c.Pause();
            ISession s = c.GetSession(sessions);

            AssertIsPaused(s);
        }
Example #8
0
        private NhConversation NewPausedConversation()
        {
            NhConversation c = NewConversation();

            c.Start();
            c.Pause();
            AssertIsPaused(c.GetSession(sessions));
            return(c);
        }
Example #9
0
        public void ResumeShouldReconnectUnitOfWork()
        {
            NhConversation c = NewPausedConversation();

            c.Resume();
            ISession s = c.GetSession(sessions);

            AssertIsOpen(s);
            Assert.That(s.IsConnected);
        }
Example #10
0
        public void ResumeAfterStartShouldNotStatAnotherUnitOfWork()
        {
            NhConversation c          = NewStartedConversation();
            ISession       startedUoW = c.GetSession(sessions);

            c.Resume();
            ISession currentUoW = c.GetSession(sessions);

            Assert.That(currentUoW, Is.SameAs(startedUoW));
        }
Example #11
0
        public void PauseShouldNotFlushUnitOfWork()
        {
            NhConversation c            = NewStartedConversation();
            var            persistedObj = new Silly3();

            c.GetSession(sessions).Save(persistedObj);

            c.Pause();
            AssertDoesNotExistInDb(persistedObj);
        }
Example #12
0
        public void Destructor()
        {
            NhConversation c = NewStartedConversation();
            ISession       s = c.GetSession(sessions);

            c.Dispose();
            Assert.That(s, Is.Not.Null);
            Assert.That(s.IsOpen, Is.False);
            Assert.Throws <ConversationException>(() => s = c.GetSession(sessions));
        }
Example #13
0
        private NhConversation NewStartedConversation()
        {
            NhConversation c = NewConversation();

            c.Start();
            ISession s = c.GetSession(sessions);

            Assert.That(s.IsOpen, Is.True, "UoW expected to be started");
            return(c);
        }
Example #14
0
        public void EndShouldCloseStartedUnitOfWork()
        {
            NhConversation c = NewStartedConversation();

            c.End();
            ISession s = c.GetSession(sessions);

            Assert.That(s, Is.Not.Null);
            Assert.That(s.IsOpen, Is.False);
        }
Example #15
0
        public IConversation CreateConversation(string conversationId)
        {
            var result = new NhConversation(factoriesProvider, wrapper, conversationId);

            if (conversationId.StartsWith("Notify"))
            {
                result.Ended += EventSource.Conversation_Ended;
            }

            return(result);
        }
Example #16
0
        public void StartCalledTwiceWithoutEndShouldNotStartAnotherUnitOfWork()
        {
            NhConversation c = NewConversation();

            c.Start();
            ISession s = c.GetSession(sessions);

            c.Start();
            Assert.That(c.GetSession(sessions), Is.SameAs(s), "ReStart of the same conversation should not change the session.");
            Assert.That(s.IsOpen);
        }
Example #17
0
        public void EndShouldFlushStartedUnitOfWork()
        {
            NhConversation c            = NewStartedConversation();
            ISession       s            = c.GetSession(sessions);
            var            persistedObj = new Silly3();

            s.Save(persistedObj);

            c.End();
            AssertExistsInDb(persistedObj);
        }
Example #18
0
        public void EndShouldNotFlushPausedUnitOfWork()
        {
            NhConversation c            = NewPausedConversation();
            ISession       s            = c.GetSession(sessions);
            var            persistedObj = new Silly3();

            s.Save(persistedObj);

            c.End();
            AssertDoesNotExistInDb(persistedObj);
        }
Example #19
0
        public void ManualUnbindSessionDoNotCreateProblemsInARunningConversation()
        {
            NhConversation c = NewStartedConversation();

            ISession s = c.GetSession(sessions);

            c.Unbind(s);
            Assert.That(c.GetSession(sessions), Is.Null, "A session still bind after manual Unbind.");
            c.Resume();
            Assert.That(c.GetSession(sessions).IsOpen);
            c.End();
        }
Example #20
0
        public void StartAfterEndShouldStartAnotherUnitOfWork()
        {
            NhConversation c           = NewStartedConversation();
            ISession       previousUoW = c.GetSession(sessions);

            c.End();

            c.Start();
            ISession currentUoW = c.GetSession(sessions);

            AssertIsOpen(currentUoW);
            Assert.That(currentUoW, Is.Not.SameAs(previousUoW));
        }
Example #21
0
        public void ManualBindSessionToConversationShouldUnbindOrphanedSession()
        {
            // TODO: recreate this test in ConversationFixtureBase to check the wrapper
            NhConversation c = NewStartedConversation();

            ISession s = sessions.OpenSession();

            Assert.That(Spying.Logger <NhConversation>().Execute(() => c.Bind(s)).WholeMessage,
                        Text.Contains("Already session bound on call to Bind()"));
            ISession sessionFromConversation = c.GetSession(sessions);

            Assert.That(c.Wrapper.IsWrapped(sessionFromConversation), "The new bind session should be wrapped and managed by the Wrapper.");
            Assert.That(sessionFromConversation.FlushMode, Is.EqualTo(FlushMode.Never), "The FlushMode of new bind session should be changed to Never.");
        }
Example #22
0
        public void SessionCloseOutsideTheConversation()
        {
            NhConversation c       = NewStartedConversation();
            ISession       session = c.GetSession(sessions);

            session.Close();
            Assert.That(c.GetSession(sessions), Is.SameAs(session));
            Assert.DoesNotThrow(c.Pause);
            Assert.That(Spying.Logger <NhConversation>().Execute(c.Resume).WholeMessage,
                        Text.DoesNotContain("Already session bound on call to Bind()"),
                        "No warning is needed for a closed session.");
            Assert.That(c.GetSession(sessions), Is.Not.SameAs(session));
            c.Dispose();
        }
		public void CannotSetCommitModeAfterSessionStarted()
		{
			var t = MockRepository.GenerateStub<ITransaction>();
			var s = MockRepository.GenerateStub<ISession>();
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			sf.Stub(x => x.OpenSession()).Return(s);
			s.Stub(x => x.BeginTransaction()).IgnoreArguments().Return(t);
			var cm = ConversationCommitMode.Explicit;
			var c = new NhConversation(sf, cc);
			c.Execute(x => { }); // Call doesn't matter, it's a stub
			var e = Assert.Throws<NotSupportedException>(() => c.CommitMode = cm);
			Assert.That(e.Message, Contains.Substring("CommitMode"));
			Assert.That(e.Message, Contains.Substring("started"));
		}
		public void CancelingRaisesEvent()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			ConversationCanceledEventArgs eventRaised = null;
			object eventRaiser = null;
			c.Canceled += (o, a) =>
			{
				eventRaiser = o;
				eventRaised = a;
			};
			c.Cancel();
			Assert.That(eventRaised, Is.Not.Null);
			Assert.That(eventRaised.CanceledByUser);
			Assert.That(eventRaised.Exception, Is.Null);
			Assert.That(eventRaiser, Is.SameAs(c));
		}
Example #25
0
        public void SessionDisposeOutsideTheConversation()
        {
            NhConversation c = NewStartedConversation();

            using (c.GetSession(sessions))
            {
                // Do nothing only simulate a possible usage
                // of sessions.GetCurrentSession() outside conversation management
            }
            // Need some new events in NH about session.Close (Event/Listener)
            // Assert.That(c.GetSession(sessions), Is.Null, "should auto unbind the session.");
            Assert.DoesNotThrow(c.Pause);
            Assert.That(Spying.Logger <NhConversation>().Execute(c.Resume).WholeMessage,
                        Text.DoesNotContain("Already session bound on call to Bind()"),
                        "No warning is needed for a closed session.");
            Assert.That(c.GetSession(sessions), Is.Not.Null);
            c.Dispose();
        }
Example #26
0
        public void ConversationUsage()
        {
            CommitInNewSession(session =>
            {
                var o = new Other3 {
                    Name = "some other silly"
                };
                var e = new Silly3 {
                    Name = "somebody", Other = o
                };
                session.Save(e);
            });

            using (NhConversation c = NewConversation())
            {
                c.Start();
                ISession       s  = c.GetSession(sessions);
                IList <Silly3> sl = s.CreateQuery("from Silly3").List <Silly3>();
                c.Pause();
                Assert.That(sl.Count == 1);
                Assert.That(!NHibernateUtil.IsInitialized(sl[0].Other));
                // working with entities, even using lazy loading
                Assert.That(!s.Transaction.IsActive);
                Assert.That("some other silly", Is.EqualTo(sl[0].Other.Name));
                Assert.That(NHibernateUtil.IsInitialized(sl[0].Other));
                sl[0].Other.Name = "nobody";
                c.Resume();
                s = c.GetSession(sessions);
                s.SaveOrUpdate(sl[0]);
                // the dispose auto-end the conversation
            }

            using (NhConversation c = NewConversation())
            {
                c.Start();
                ISession s = c.GetSession(sessions);
                s.Delete("from Silly3");
                c.End();
            }
        }
Example #27
0
        public void ResumeWithoutStartShouldNotThrow()
        {
            NhConversation c = NewConversation();

            Assert.DoesNotThrow(c.Resume);
        }
		public void ScopesAreNotLeaking()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = new NhConversationContext();
			var c = new NhConversation(sf, cc);
			WeakReference weakRef = CreateScope(c);
			GC.Collect();
			GC.WaitForPendingFinalizers();
			Assert.That(weakRef.Target, Is.Null);
		}
		public void ScopesAreInvalidatedOnConversationDisposal()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			INhScope s = (INhScope)c.Scope();
			Assert.That(s.IsValid);
			c.Dispose();
			Assert.That(s.IsValid, Is.False);
			s.Dispose(); // Must not throw
		}
		public void ExceptionsInExecuteSilentlyCancelTheConversationWithoutThrowing()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			var e = new Exception("foo");
			ConversationCanceledEventArgs eventRaised = null;
			object eventRaiser = null;
			c.Canceled += (o, a) =>
			{
				eventRaiser = o;
				eventRaised = a;
			};
			c.ExecuteSilently(() => { throw e; }); // Doesn't throw.
			Assert.That(eventRaised, Is.Not.Null);
			Assert.That(eventRaised.CanceledByUser, Is.False);
			Assert.That(eventRaised.Exception, Is.SameAs(e));
			Assert.That(eventRaiser, Is.SameAs(c));
		}
		public void ExceptionsInExecuteCancelTheConversation()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			ConversationCanceledEventArgs eventRaised = null;
			object eventRaiser = null;
			c.Canceled += (o, a) =>
			{
				eventRaiser = o;
				eventRaised = a;
			};
			var e = Assert.Throws<Exception>(() => c.Execute(() => { throw new Exception("foo"); }));
			Assert.That(e.Message, Is.EqualTo("foo"));
			Assert.That(eventRaised, Is.Not.Null);
			Assert.That(eventRaised.CanceledByUser, Is.False);
			Assert.That(eventRaised.Exception, Is.SameAs(e));
			Assert.That(eventRaiser, Is.SameAs(c));
		}
		public void CancelStateCanBeQueried()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			c.Cancel();
			Assert.That(c.IsCanceled);
		}
		public void CanUseExecuteToSwitchConversation()
		{
			NhConversationContext context = new NhConversationContext();
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var conv1 = new NhConversation(sf, context);
			var conv2 = new NhConversation(sf, context);

			Assert.That(context.CurrentConversation, Is.Null);
			conv1.Execute(() => Assert.That(context.CurrentConversation, Is.SameAs(conv1)));
			conv2.Execute(() => Assert.That(context.CurrentConversation, Is.SameAs(conv2)));
			Assert.That(context.CurrentConversation, Is.Null);
		}
		public void CanSwitchBetweenConversations()
		{
			NhConversationContext context = new NhConversationContext();
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var conv1 = new NhConversation(sf, context);
			var conv2 = new NhConversation(sf, context);

			Assert.That(context.CurrentConversation, Is.Null);
			using (conv1.Scope())
			{
				Assert.That(context.CurrentConversation, Is.SameAs(conv1));
				using (conv2.Scope())
				{
					Assert.That(context.CurrentConversation, Is.SameAs(conv2));
				}
				Assert.That(context.CurrentConversation, Is.SameAs(conv1));
			}
			Assert.That(context.CurrentConversation, Is.Null);
		}
		public void ExceptionsCancelTheConversation()
		{
			var t = MockRepository.GenerateStub<ITransaction>();
			var s = MockRepository.GenerateStub<ISession>();
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			sf.Stub(x => x.OpenSession()).Return(s);
			s.Stub(x => x.BeginTransaction()).IgnoreArguments().Return(t);
			var c = new NhConversation(sf, cc);
			ConversationCanceledEventArgs eventRaised = null;
			object eventRaiser = null;
			c.Canceled += (o, a) =>
			{
				eventRaiser = o;
				eventRaised = a;
			};
			var e = Assert.Throws<Exception>(() => c.Execute(x => { throw new Exception("foo"); }));
			Assert.That(e.Message, Is.EqualTo("foo"));
			Assert.That(eventRaised, Is.Not.Null);
			Assert.That(eventRaised.CanceledByUser, Is.False);
			Assert.That(eventRaised.Exception, Is.SameAs(e));
			Assert.That(eventRaiser, Is.SameAs(c));
		}
Example #36
0
        public void EndWithoutStartShouldNotThrow()
        {
            NhConversation c = NewConversation();

            Assert.DoesNotThrow(c.End);
        }
		public void CantCommitAfterCanceling()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			c.Cancel();
			Assert.Throws<InvalidOperationException>(() => c.Commit());
		}
Example #38
0
        public void GetSessionsShouldThrowWhenConversationNotYetStarted()
        {
            NhConversation c = NewConversation();

            Assert.Throws <ConversationException>(() => c.GetSession(sessions));
        }
		public void CannotCallExecuteWhenConversationIsCanceled()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			c.Cancel();
			var e = Assert.Throws<InvalidOperationException>(() => c.Execute(() => { ; })); // Call doesn't matter, it's a stub
			Assert.That(e.Message, Contains.Substring("canceled"));
		}
		public void ScopeIsRegistered()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateMock<INhConversationContext>();
			cc.Expect(m => m.RegisterScope(null)).IgnoreArguments();
			var c = new NhConversation(sf, cc);
			using (c.Scope()) { }
			cc.VerifyAllExpectations();
		}
		public void RestartingTheConversationResetsCancelState()
		{
			var t = MockRepository.GenerateStub<ITransaction>();
			var s = MockRepository.GenerateStub<ISession>();
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			sf.Stub(x => x.OpenSession()).Return(s);
			s.Stub(x => x.BeginTransaction()).IgnoreArguments().Return(t);
			var c = new NhConversation(sf, cc);
			c.Cancel();
			c.Restart();
			bool called = false;
			c.Execute(x => called = true); // Call doesn't matter, it's a stub
			Assert.That(called);
		}
		public void CanCreateScope()
		{
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateStub<INhConversationContext>();
			var c = new NhConversation(sf, cc);
			using (c.Scope()) { }
		}
		public void ScopeIsDeregistered()
		{
			INhScope registeredScope = null;
			var sf = MockRepository.GenerateStub<ISessionFactory>();
			var cc = MockRepository.GenerateMock<INhConversationContext>();
			cc.Expect(m => m.RegisterScope(null)).IgnoreArguments().WhenCalled(i => registeredScope = (INhScope)i.Arguments[0]);
			var c = new NhConversation(sf, cc);
			using (c.Scope())
			{
				cc.Expect(m => m.ReleaseScope(registeredScope));
			}
			cc.VerifyAllExpectations();
		}