public void ParticipatingTransactionWithCommit() { IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory)); IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection)); ISession session = (ISession)mocks.CreateMock(typeof(ISession)); using (mocks.Ordered()) { SetupCommitExpectations(connection, connectionFactory, session); } mocks.ReplayAll(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); TransactionTemplate tt = new TransactionTemplate(tm); tt.Execute(delegate(ITransactionStatus status) { nt.Execute(new AssertSessionCallback(session)); return(null); }); tm.Commit(ts); mocks.VerifyAll(); }
public void ParticipatingTransactionWithCommit() { IConnectionFactory connectionFactory = A.Fake <IConnectionFactory>(); IConnection connection = A.Fake <IConnection>(); ISession session = A.Fake <ISession>(); SetupCreateSession(connection, connectionFactory, session); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); TransactionTemplate tt = new TransactionTemplate(tm); tt.Execute(status => { nt.Execute(new AssertSessionCallback(session)); return(null); }); tm.Commit(ts); AssertCommitExpectations(connection, connectionFactory, session); }
public void TransactionSuspension() { IConnectionFactory connectionFactory = A.Fake <IConnectionFactory>(); IConnection connection = A.Fake <IConnection>(); ISession session = A.Fake <ISession>(); ISession session2 = A.Fake <ISession>(); A.CallTo(() => connectionFactory.CreateConnection()).Returns(connection).Twice(); A.CallTo(() => connection.CreateSession(AcknowledgementMode.Transactional)) .Returns(session).Once() .Then.Returns(session2).Once(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.RequiresNew; tt.Execute(status => { nt.Execute(new AssertNotSameSessionCallback(session)); return(null); }); nt.Execute(new AssertSessionCallback(session)); tm.Commit(ts); A.CallTo(() => session.Commit()).MustHaveHappenedOnceExactly(); A.CallTo(() => session2.Commit()).MustHaveHappenedOnceExactly(); A.CallTo(() => session.Close()).MustHaveHappenedOnceExactly(); A.CallTo(() => session2.Close()).MustHaveHappenedOnceExactly(); A.CallTo(() => connection.Close()).MustHaveHappenedTwiceExactly(); }
public void ParticipatingTransactionWithRollback() { IConnectionFactory connectionFactory = A.Fake <IConnectionFactory>(); IConnection connection = A.Fake <IConnection>(); ISession session = A.Fake <ISession>(); SetupCreateSession(connection, connectionFactory, session); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); TransactionTemplate tt = new TransactionTemplate(tm); tt.Execute(status => { nt.Execute(new AssertSessionCallback(session)); status.SetRollbackOnly(); return(null); }); try { tm.Commit(ts); Assert.Fail("Should have thrown UnexpectedRollbackException"); } catch (UnexpectedRollbackException) { } AssertRollbackExpectations(connection, connectionFactory, session); }
public void ProducerCallbackWithIdAndTimestampDisabled() { NmsTemplate template = CreateTemplate(); template.ConnectionFactory = mockConnectionFactory; template.MessageIdEnabled = false; template.MessageTimestampEnabled = false; IMessageProducer mockProducer = mocks.StrictMock <IMessageProducer>(); Expect.Call(mockSession.CreateProducer(null)).Return(mockProducer); mockProducer.DisableMessageID = true; LastCall.On(mockProducer).Repeat.Once(); mockProducer.DisableMessageTimestamp = true; LastCall.On(mockProducer).Repeat.Once(); Expect.Call(mockProducer.Priority).Return(MsgPriority.Normal); CloseProducerSessionConnection(mockProducer); mocks.ReplayAll(); template.Execute((session, producer) => { bool b = session.Transacted; MsgPriority priority = producer.Priority; return(null); }); mocks.VerifyAll(); }
public void ProducerCallback() { NmsTemplate template = CreateTemplate(); template.ConnectionFactory = mockConnectionFactory; IMessageProducer mockProducer = mocks.StrictMock <IMessageProducer>(); Expect.Call(mockSession.CreateProducer(null)).Return(mockProducer); Expect.Call(mockProducer.Priority).Return(MsgPriority.Normal); CloseProducerSessionConnection(mockProducer); mocks.ReplayAll(); MsgPriority priority = MsgPriority.Highest; template.Execute((session, producer) => { bool b = session.Transacted; priority = producer.Priority; return(null); }); Assert.AreEqual(priority, MsgPriority.Normal); mocks.VerifyAll(); }
public void ProducerCallbackWithIdAndTimestampDisabled() { NmsTemplate template = CreateTemplate(); template.ConnectionFactory = mockConnectionFactory; template.MessageIdEnabled = false; template.MessageTimestampEnabled = false; IMessageProducer mockProducer = A.Fake <IMessageProducer>(); A.CallTo(() => mockSession.CreateProducer(null)).Returns(mockProducer); A.CallTo(() => mockProducer.Priority).Returns(MsgPriority.Normal); template.Execute((session, producer) => { bool b = session.Transacted; MsgPriority priority = producer.Priority; return(null); }); AssertCloseProducerSessionConnection(mockProducer); A.CallToSet(() => mockProducer.DisableMessageID).WhenArgumentsMatch(x => x.Get <bool>(0) == true).MustHaveHappenedOnceExactly(); A.CallToSet(() => mockProducer.DisableMessageTimestamp).WhenArgumentsMatch(x => x.Get <bool>(0) == true).MustHaveHappenedOnceExactly(); }
public void TransactionSuspension() { IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory)); IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection)); ISession session = (ISession)mocks.CreateMock(typeof(ISession)); ISession session2 = (ISession)mocks.CreateMock(typeof(ISession)); Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Twice(); Expect.Call(connection.CreateSession(AcknowledgementMode.Transactional)).Return(session).Repeat.Once(); Expect.Call(connection.CreateSession(AcknowledgementMode.Transactional)).Return(session2).Repeat.Once(); session.Commit(); LastCall.On(session).Repeat.Once(); session2.Commit(); LastCall.On(session2).Repeat.Once(); session.Close(); LastCall.On(session).Repeat.Once(); session2.Close(); LastCall.On(session2).Repeat.Once(); connection.Close(); LastCall.On(connection).Repeat.Twice(); mocks.ReplayAll(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.RequiresNew; tt.Execute(delegate(ITransactionStatus status) { nt.Execute(new AssertNotSameSessionCallback(session)); return(null); }); nt.Execute(new AssertSessionCallback(session)); tm.Commit(ts); mocks.VerifyAll(); }
public void SuspendedTransaction() { IConnectionFactory connectionFactory = mocks.StrictMock <IConnectionFactory>(); IConnection connection = mocks.StrictMock <IConnection>(); ISession session = mocks.StrictMock <ISession>(); ISession session2 = mocks.StrictMock <ISession>(); Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Twice(); Expect.Call(connection.CreateSession(AcknowledgementMode.Transactional)).Return(session).Repeat.Once(); Expect.Call(connection.CreateSession(AcknowledgementMode.AutoAcknowledge)).Return(session2).Repeat.Once(); session.Commit(); LastCall.On(session).Repeat.Once(); session.Close(); LastCall.On(session).Repeat.Once(); session2.Close(); LastCall.On(session2).Repeat.Once(); connection.Close(); LastCall.On(connection).Repeat.Twice(); mocks.ReplayAll(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.NotSupported; tt.Execute(status => { nt.Execute(new AssertNotSameSessionCallback(session)); return(null); }); nt.Execute(new AssertSessionCallback(session)); tm.Commit(ts); mocks.VerifyAll(); }
public void ParticipatingTransactionWithRollback() { IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory)); IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection)); ISession session = (ISession)mocks.CreateMock(typeof(ISession)); using (mocks.Ordered()) { SetupRollbackExpectations(connection, connectionFactory, session); } mocks.ReplayAll(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); TransactionTemplate tt = new TransactionTemplate(tm); tt.Execute(delegate(ITransactionStatus status) { nt.Execute(new AssertSessionCallback(session)); status.SetRollbackOnly(); return(null); }); try { tm.Commit(ts); Assert.Fail("Should have thrown UnexpectedRollbackException"); } catch (UnexpectedRollbackException) { } mocks.VerifyAll(); }
public void SessionCallback() { NmsTemplate template = CreateTemplate(); template.ConnectionFactory = mockConnectionFactory; template.Execute(session => { bool b = session.Transacted; return(null); }); A.CallTo(() => mockSession.Close()).MustHaveHappenedOnceExactly(); A.CallTo(() => mockConnection.Close()).MustHaveHappenedOnceExactly(); }
public void TransactionRollback() { IConnectionFactory connectionFactory = A.Fake <IConnectionFactory>(); IConnection connection = A.Fake <IConnection>(); ISession session = A.Fake <ISession>(); SetupCreateSession(connection, connectionFactory, session); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); tm.Rollback(ts); AssertRollbackExpectations(connection, connectionFactory, session); }
public void TransactionRollback() { IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory)); IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection)); ISession session = (ISession)mocks.CreateMock(typeof(ISession)); SetupRollbackExpectations(connection, connectionFactory, session); mocks.ReplayAll(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); tm.Rollback(ts); mocks.VerifyAll(); }
public void SessionCallback() { NmsTemplate template = CreateTemplate(); template.ConnectionFactory = mockConnectionFactory; mockSession.Close(); LastCall.On(mockSession).Repeat.Once(); mockConnection.Close(); LastCall.On(mockConnection).Repeat.Once(); mocks.ReplayAll(); template.Execute(session => { bool b = session.Transacted; return(null); }); mocks.VerifyAll(); }
public void TransactionCommit() { IConnectionFactory connectionFactory = mocks.StrictMock <IConnectionFactory>(); IConnection connection = mocks.StrictMock <IConnection>(); ISession session = mocks.StrictMock <ISession>(); using (mocks.Ordered()) { SetupCommitExpectations(connection, connectionFactory, session); } mocks.ReplayAll(); NmsTransactionManager tm = new NmsTransactionManager(connectionFactory); ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition()); NmsTemplate nt = new NmsTemplate(connectionFactory); nt.Execute(new AssertSessionCallback(session)); tm.Commit(ts); mocks.VerifyAll(); }
public void ProducerCallback() { NmsTemplate template = CreateTemplate(); template.ConnectionFactory = mockConnectionFactory; IMessageProducer mockProducer = A.Fake <IMessageProducer>(); A.CallTo(() => mockSession.CreateProducer(null)).Returns(mockProducer); A.CallTo(() => mockProducer.Priority).Returns(MsgPriority.Normal); MsgPriority priority = MsgPriority.Highest; template.Execute((session, producer) => { bool b = session.Transacted; priority = producer.Priority; return(null); }); Assert.AreEqual(priority, MsgPriority.Normal); AssertCloseProducerSessionConnection(mockProducer); }
public void SessionCallbackWithinSynchronizedTransaction() { SingleConnectionFactory scf = new SingleConnectionFactory(mockConnectionFactory); NmsTemplate template = CreateTemplate(); template.ConnectionFactory = scf; mockConnection.Start(); LastCall.On(mockConnection).Repeat.Times(2); // We're gonna call getTransacted 3 times, i.e. 2 more times. Expect.Call(mockSession.Transacted).Return(UseTransactedSession).Repeat.Twice(); if (UseTransactedTemplate) { mockSession.Commit(); LastCall.On(mockSession).Repeat.Once(); } mockSession.Close(); LastCall.On(mockSession).Repeat.Once(); mockConnection.Stop(); LastCall.On(mockConnection).Repeat.Once(); mockConnection.Close(); LastCall.On(mockConnection).Repeat.Once(); mocks.ReplayAll(); TransactionSynchronizationManager.InitSynchronization(); try { template.Execute(session => { bool b = session.Transacted; return(null); }); template.Execute(session => { bool b = session.Transacted; return(null); }); Assert.AreSame(mockSession, ConnectionFactoryUtils.GetTransactionalSession(scf, null, false)); Assert.AreSame(mockSession, ConnectionFactoryUtils.GetTransactionalSession(scf, scf.CreateConnection(), false)); //In Java this test was doing 'double-duty' and testing TransactionAwareConnectionFactoryProxy, which has //not been implemented in .NET template.Execute(session => { bool b = session.Transacted; return(null); }); IList synchs = TransactionSynchronizationManager.Synchronizations; Assert.AreEqual(1, synchs.Count); ITransactionSynchronization synch = (ITransactionSynchronization)synchs[0]; synch.BeforeCommit(false); synch.BeforeCompletion(); synch.AfterCommit(); synch.AfterCompletion(TransactionSynchronizationStatus.Unknown); } finally { TransactionSynchronizationManager.ClearSynchronization(); //Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0); //Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive); scf.Dispose(); } Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0); mocks.VerifyAll(); }