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(); } }
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(); } }
/// <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); } }
public void Dispose_causeAchievedRolesDisposition() { // arrange: FakeRole roleToReturn = GeneratePartialMock <FakeRole>(); roleToReturn.Expect(r => r.Dispose()).Repeat.Once(); FakeWorkingSession fakeSession = GeneratePartialMock <FakeWorkingSession>(); IPrincipal owner = fakeSession.Owner; fakeSession.Expect(s => s.CallIsAllowed <IFakeRole>()).Return(true).Repeat.Once(); FakeRoleBuilder <IFakeRole, FakeRole> roleBuilder = GeneratePartialMock <FakeRoleBuilder <IFakeRole, FakeRole> >(); roleBuilder.Expect(b => b.CallCreateRoleFor(owner)).Return(roleToReturn).Repeat.Once(); fakeSession.Expect(s => s.CallGetRoleBuilder <IFakeRole>()).Return(roleBuilder).Repeat.Once(); IFakeRole achievedRole = null; fakeSession.Achieve <IFakeRole>(out achievedRole); fakeSession.Expect(s => s.CallBeforeDispose()).Repeat.Once(); IWorkingSession session = fakeSession; IFakeRole roleRef = null; // act: fakeSession.Dispose(); // assert: Assert.Throws <ObjectDisposedException>(delegate { session.CanAchieve <IFakeRole>(); }); Assert.Throws <ObjectDisposedException>(delegate { session.Achieve <IFakeRole>(out roleRef); }); Assert.IsNull(roleRef); Assert.Throws <ObjectDisposedException>(delegate { session.Leave <IFakeRole>(ref roleRef); }); }
public void EndWorkingSession_withoutOwner_throwsArgumentNullException() { // arrange: IEnterprise enterprise = new Fakes.FakeEnterprise(); IWorkingSession session = MockRepository.GenerateStrictMock <IWorkingSession>(); // assert: Assert.Throws <ArgumentNullException>(delegate { enterprise.EndWorkingSession(null, session); }); }
public void EndWorkingSession_withWrongSessionType_throwsInvalidOperationException() { // arrange: IEnterprise enterprise = new Fakes.FakeEnterprise(); IPrincipal owner = new GenericPrincipal(new GenericIdentity("test", "test"), new string[] {}); IWorkingSession session = MockRepository.GenerateStrictMock <IWorkingSession>(); // assert: Assert.Throws <InvalidOperationException>(delegate { enterprise.EndWorkingSession(owner, session); }); }
public void StartWorkingSession_withoutOwner_throwsArgumentNullException() { // arrange: IEnterprise enterprise = new Fakes.FakeEnterprise(); IWorkingSession session = null; // assert: Assert.Throws <ArgumentNullException>(delegate { enterprise.StartWorkingSession(null, out session); }); Assert.IsNull(session); }
/// <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; }
public void Deserialize_works(string identifier) { // arrange: string userID = "FakeUser"; IPrincipal owner = new GenericPrincipal(new GenericIdentity(userID), new string[0]); IWorkingSession session = new FakeWorkingSession(identifier, owner); Stream stream = TestUtilities.Serialize(session); // act: IWorkingSession deserialized = TestUtilities.Deserialize <IWorkingSession>(stream); // assert: Assert.IsNotNull(deserialized); Assert.AreEqual(identifier, deserialized.Identifier); }
public void Dispose_callsBeforeDisposeTemplateMethod() { // arrange: FakeWorkingSession fakeSession = GeneratePartialMock <FakeWorkingSession>(); fakeSession.Expect(s => s.CallBeforeDispose()).Repeat.Once(); IWorkingSession session = fakeSession; IFakeRole roleRef = null; // act: fakeSession.Dispose(); // assert: Assert.Throws <ObjectDisposedException>(delegate { session.CanAchieve <IFakeRole>(); }); Assert.Throws <ObjectDisposedException>(delegate { session.Achieve <IFakeRole>(out roleRef); }); Assert.IsNull(roleRef); Assert.Throws <ObjectDisposedException>(delegate { session.Leave <IFakeRole>(ref roleRef); }); }
public void Deserialize_afterAnAchieve_works() { // arrange: IWorkingSession session = new FakeSerializableWorkingSession(); IFakeRole achievedRole = null; session.Achieve <IFakeRole>(out achievedRole); Stream stream = TestUtilities.Serialize(session); // act: IFakeRole deserializedRole = null; IWorkingSession deserialized = TestUtilities.Deserialize <IWorkingSession>(stream); deserialized.Achieve <IFakeRole>(out deserializedRole); // assert: Assert.IsNotNull(deserialized); Assert.AreEqual(((FakeSerializableWorkingSession)session).CalledMethods.Count(), ((FakeSerializableWorkingSession)deserialized).CalledMethods.Count()); }
/// <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; }