#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Nuclex.Support.Scheduling;
#if UNITTEST
using NUnit.Framework;
using NMock2;
namespace Nuclex.Support.Tracking {
/// Unit Test for the request class
[TestFixture]
public class RequestTest {
#region class CustomWaitRequest
///
/// Request with a custom wait implementation that completes the request instead
/// of waiting for it complete by outside means
///
private class CustomWaitRequest : Request {
/// Waits until the background process finishes
public override void Wait() {
// This could be a race condition if this was used for anything but this simple
// unit test. Might be neccessary to refactor this when writing advanced tests.
if(!base.Ended) {
OnAsyncEnded();
}
}
}
#endregion // class CustomWaitRequest
///
/// Verifies that the SucceededDummy request is in the ended state
///
[Test]
public void TestSucceededDummy() {
Request dummy = Request.SucceededDummy;
Assert.IsTrue(dummy.Ended);
dummy.Join(); // should not throw
}
///
/// Verifies that the FailedDummy request is in the ended state and throws
/// an exception when Join()ing
///
[Test, ExpectedException(typeof(AbortedException))]
public void TestFailedDummy() {
Request failedDummy = Request.CreateFailedDummy(
new AbortedException("Hello World")
);
Assert.IsTrue(failedDummy.Ended);
failedDummy.Join();
}
///
/// Verifies that the Request's Wait() method is invoked if the request is joined
/// before the request has finished.
///
[Test]
public void TestJoinWithWaiting() {
CustomWaitRequest waitRequest = new CustomWaitRequest();
waitRequest.Join();
Assert.IsTrue(waitRequest.Ended);
}
}
/// Unit Test for the generic request class
[TestFixture]
public class GenericRequestTest {
///
/// Verifies that the SucceededDummy request is in the ended state
///
[Test]
public void TestSucceededDummy() {
Request dummy = Request.CreateSucceededDummy(12345);
Assert.IsTrue(dummy.Ended);
Assert.AreEqual(12345, dummy.Join()); // should not throw
}
///
/// Verifies that the FailedDummy request is in the ended state and throws
/// an exception when Join()ing
///
[Test, ExpectedException(typeof(AbortedException))]
public void TestFailedDummy() {
Request failedDummy = Request.CreateFailedDummy(
new AbortedException("Hello World")
);
Assert.IsTrue(failedDummy.Ended);
failedDummy.Join();
}
}
} // namespace Nuclex.Support.Tracking
#endif // UNITTEST