Exemple #1
0
        public void AcquireWorkingSession_withValidArguments_callTemplateMethod()
        {
            // arrange:
            System.Collections.Generic.List <object> mocks = new System.Collections.Generic.List <object>();
            string identifier = "testWorkingSession";

            Fakes.FakeEnterprise enterprise = MockRepository.GeneratePartialMock <Fakes.FakeEnterprise>();
            mocks.Add(enterprise);
            IPrincipal owner = MockRepository.GenerateStrictMock <IPrincipal>();

            mocks.Add(owner);
            WorkingSessionBase session = MockRepository.GeneratePartialMock <WorkingSessionBase>("MockWorkingSession", owner);

            mocks.Add(session);
            enterprise.Expect(e => e.CallAcquireWorkingSessionReal(owner, identifier)).Return(session).Repeat.Once();

            // act:
            IWorkingSession returnedWorkingSession = enterprise.AcquireWorkingSession(owner, identifier);

            // assert:
            Assert.IsNotNull(returnedWorkingSession);
            Assert.AreSame(session, returnedWorkingSession);
            foreach (object m in mocks)
            {
                m.VerifyAllExpectations();
            }
        }
Exemple #2
0
        public void EndWorkingSession_onSubclassException_dontWrapInvalidOperationException()
        {
            // arrange:
            System.Collections.Generic.List <object> mocks = new System.Collections.Generic.List <object>();
            InvalidOperationException thrownException      = new InvalidOperationException("thrownException");

            Fakes.FakeEnterprise enterprise = MockRepository.GeneratePartialMock <Fakes.FakeEnterprise>();
            mocks.Add(enterprise);
            IPrincipal owner = MockRepository.GenerateStrictMock <IPrincipal>();

            mocks.Add(owner);
            WorkingSessionBase session = MockRepository.GeneratePartialMock <WorkingSessionBase>("MockWorkingSession", owner);

            mocks.Add(session);
            enterprise.Expect(e => e.CallBeforeWorkingSessionEnd(owner, session)).Throw(thrownException).Repeat.Once();
            InvalidOperationException cought = null;

            // act:
            try
            {
                enterprise.EndWorkingSession(owner, session);
            }
            catch (InvalidOperationException e)
            {
                cought = e;
            }

            // assert:
            Assert.IsNotNull(cought);
            Assert.AreSame(thrownException, cought);
            foreach (object m in mocks)
            {
                m.VerifyAllExpectations();
            }
        }
Exemple #3
0
        public void StartWorkingSession_withOwner_callTemplateMethod()
        {
            // arrange:
            System.Collections.Generic.List <object> mocks = new System.Collections.Generic.List <object>();
            Fakes.FakeEnterprise enterprise = MockRepository.GeneratePartialMock <Fakes.FakeEnterprise>();
            mocks.Add(enterprise);
            IPrincipal owner = MockRepository.GenerateStrictMock <IPrincipal>();

            mocks.Add(owner);
            WorkingSessionBase outWorkingSessionBase = null;
            WorkingSessionBase session = MockRepository.GeneratePartialMock <WorkingSessionBase>("MockWorkingSession", owner);

            mocks.Add(session);
            enterprise.Expect(e => e.CallStartWorkingSession(owner, out outWorkingSessionBase)).OutRef(session).Repeat.Once();
            IWorkingSession outWorkingSession = null;

            // act:
            enterprise.StartWorkingSession(owner, out outWorkingSession);

            // assert:
            Assert.IsNotNull(outWorkingSession);
            Assert.AreSame(session, outWorkingSession);
            foreach (object m in mocks)
            {
                m.VerifyAllExpectations();
            }
        }
        /// <summary>
        /// Ends the working session. Template method.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Before disposing the working session will call the
        /// <see cref="EnterpriseBase.BeforeWorkingSessionEnd(IPrincipal, IWorkingSession)"/> so that
        /// derived class will be able to log the operation.
        /// </para>
        /// <para>
        /// This could be usefult if you have to lock accesses to the application to specific users.
        /// </para>
        /// </remarks>
        /// <param name='owner'>
        /// The owner.
        /// </param>
        /// <param name='workingSession'>
        /// The working session to end.
        /// </param>
        /// <exception cref="ArgumentNullException">Either <paramref name="owner"/> or
        /// <paramref name="workingSession"/> are <value>null</value>.</exception>
        /// <exception cref="InvalidOperationException"><paramref name="owner"/> can not end
        /// <paramref name="workingSession"/>.</exception>
        public void EndWorkingSession(IPrincipal owner, IWorkingSession workingSession)
        {
            if (null == owner)
            {
                throw new ArgumentNullException("owner");
            }
            if (null == workingSession)
            {
                throw new ArgumentNullException("workingSession");
            }
            WorkingSessionBase session = workingSession as WorkingSessionBase;

            if (null == session)
            {
                ArgumentException inner = new ArgumentException("The working session provided do not extend WorkingSessionBase.", "workingSession");
                throw new InvalidOperationException("Unknown working session.", inner);
            }
            try
            {
                BeforeWorkingSessionEnd(owner, session);

                session.Dispose();
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                {
                    throw e;
                }
                string message = string.Format("The owner ({0}) can not end the working session {1}.", owner.Identity.Name, workingSession.Identifier);
                throw new InvalidOperationException(message, e);
            }
        }
        /// <summary>
        /// Starts a new working session. Template method.
        /// </summary>
        /// <param name='owner'>
        /// Owner of the new session.
        /// </param>
        /// <param name='workingSession'>
        /// The new working session.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="owner"/> is <value>null</value>.</exception>
        /// <exception cref="InvalidOperationException"><paramref name="owner"/> can not create a
        /// new <see cref="IWorkingSession"/>.</exception>
        /// <seealso cref="EnterpriseBase.StartWorkingSession(IPrincipal, WorkingSessionBase)"/>
        public void StartWorkingSession(IPrincipal owner, out IWorkingSession workingSession)
        {
            if (null == owner)
            {
                throw new ArgumentNullException("owner");
            }
            WorkingSessionBase newSession = null;

            StartWorkingSession(owner, out newSession);
            workingSession = newSession;
        }
 /// <summary>
 /// Starts a new working session.
 /// </summary>
 /// <param name='owner'>
 /// Owner of the new session. Will never be <value>null</value>.
 /// </param>
 /// <param name='workingSession'>
 /// The new working session.
 /// </param>
 /// <exception cref="InvalidOperationException"><paramref name="owner"/> can not create a
 /// new <see cref="IWorkingSession"/>.</exception>
 protected abstract void StartWorkingSession(IPrincipal owner, out WorkingSessionBase workingSession);
 /// <summary>
 /// Called before that <paramref name="workingSession"/> has been disposed.
 /// </summary>
 /// <param name='owner'>
 /// The owner.
 /// </param>
 /// <param name='workingSession'>
 /// The working session to end.
 /// </param>
 /// <exception cref="InvalidOperationException"><paramref name="owner"/> can not end
 /// <paramref name="workingSession"/>.</exception>
 protected abstract void BeforeWorkingSessionEnd(IPrincipal owner, WorkingSessionBase workingSession);