Achieved 100% test coverage for all classes in the Nuclex.Support.Tracking namespace; wrote unit tests for the AbortedException class

git-svn-id: file:///srv/devel/repo-conversion/nusu@102 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2008-12-07 19:01:43 +00:00
parent e785fdf57d
commit 00caebf7e9
3 changed files with 220 additions and 0 deletions

View File

@ -156,6 +156,7 @@
<DependentUpon>PluginRepository.cs</DependentUpon>
</Compile>
<Compile Include="Source\Scheduling\AbortedException.cs" />
<Compile Include="Source\Scheduling\AbortedException.Test.cs" />
<Compile Include="Source\Scheduling\IAbortable.cs" />
<Compile Include="Source\Scheduling\Operation.cs" />
<Compile Include="Source\Scheduling\OperationQueue.cs" />
@ -199,6 +200,9 @@
<DependentUpon>ProgressTracker.cs</DependentUpon>
</Compile>
<Compile Include="Source\Tracking\Request.cs" />
<Compile Include="Source\Tracking\Request.Test.cs">
<DependentUpon>Request.cs</DependentUpon>
</Compile>
<Compile Include="Source\Tracking\StatusReportEventArgs.Test.cs">
<DependentUpon>StatusReportEventArgs.cs</DependentUpon>
</Compile>

View File

@ -0,0 +1,84 @@
#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 System.Runtime.Serialization.Formatters.Binary;
#if UNITTEST
using NUnit.Framework;
using NMock2;
namespace Nuclex.Support.Scheduling {
/// <summary>Unit Test for the request class</summary>
[TestFixture]
public class AbortedExceptionTest {
/// <summary>
/// Verifies that the exception's default constructor is working
/// </summary>
[Test]
public void TestDefaultConstructor() {
AbortedException testException = new AbortedException();
string testExceptionString = testException.ToString();
}
/// <summary>
/// Checks whether the exception correctly stores its inner exception
/// </summary>
[Test]
public void TestInnerException() {
Exception inner = new Exception("This is a test");
AbortedException testException = new AbortedException(
"Hello World", inner
);
Assert.AreSame(inner, testException.InnerException);
}
/// <summary>
/// Test whether the exception can be serialized
/// </summary>
[Test]
public void TestSerialization() {
BinaryFormatter formatter = new BinaryFormatter();
using(MemoryStream memory = new MemoryStream()) {
AbortedException exception1 = new AbortedException("Hello World");
formatter.Serialize(memory, exception1);
memory.Position = 0;
object exception2 = formatter.Deserialize(memory);
Assert.IsInstanceOfType(typeof(AbortedException), exception2);
Assert.AreEqual(exception1.Message, ((AbortedException)exception2).Message);
}
}
}
} // namespace Nuclex.Support.Scheduling
#endif // UNITTEST

View File

@ -0,0 +1,132 @@
#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 {
/// <summary>Unit Test for the request class</summary>
[TestFixture]
public class RequestTest {
#region class CustomWaitRequest
/// <summary>
/// Request with a custom wait implementation that completes the request instead
/// of waiting for it complete by outside means
/// </summary>
private class CustomWaitRequest : Request {
/// <summary>Waits until the background process finishes</summary>
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
/// <summary>
/// Verifies that the SucceededDummy request is in the ended state
/// </summary>
[Test]
public void TestSucceededDummy() {
Request dummy = Request.SucceededDummy;
Assert.IsTrue(dummy.Ended);
dummy.Join(); // should not throw
}
/// <summary>
/// Verifies that the FailedDummy request is in the ended state and throws
/// an exception when Join()ing
/// </summary>
[Test, ExpectedException(typeof(AbortedException))]
public void TestFailedDummy() {
Request failedDummy = Request.CreateFailedDummy(
new AbortedException("Hello World")
);
Assert.IsTrue(failedDummy.Ended);
failedDummy.Join();
}
/// <summary>
/// Verifies that the Request's Wait() method is invoked if the request is joined
/// before the request has finished.
/// </summary>
[Test]
public void TestJoinWithWaiting() {
CustomWaitRequest waitRequest = new CustomWaitRequest();
waitRequest.Join();
Assert.IsTrue(waitRequest.Ended);
}
}
/// <summary>Unit Test for the generic request class</summary>
[TestFixture]
public class GenericRequestTest {
/// <summary>
/// Verifies that the SucceededDummy request is in the ended state
/// </summary>
[Test]
public void TestSucceededDummy() {
Request<int> dummy = Request<int>.CreateSucceededDummy(12345);
Assert.IsTrue(dummy.Ended);
Assert.AreEqual(12345, dummy.Join()); // should not throw
}
/// <summary>
/// Verifies that the FailedDummy request is in the ended state and throws
/// an exception when Join()ing
/// </summary>
[Test, ExpectedException(typeof(AbortedException))]
public void TestFailedDummy() {
Request<int> failedDummy = Request<int>.CreateFailedDummy(
new AbortedException("Hello World")
);
Assert.IsTrue(failedDummy.Ended);
failedDummy.Join();
}
}
} // namespace Nuclex.Support.Tracking
#endif // UNITTEST