From 00caebf7e940b7504a7c630e7e29858d2d92807d Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Sun, 7 Dec 2008 19:01:43 +0000 Subject: [PATCH] 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 --- Nuclex.Support.csproj | 4 + Source/Scheduling/AbortedException.Test.cs | 84 +++++++++++++ Source/Tracking/Request.Test.cs | 132 +++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 Source/Scheduling/AbortedException.Test.cs create mode 100644 Source/Tracking/Request.Test.cs diff --git a/Nuclex.Support.csproj b/Nuclex.Support.csproj index 23bfbb8..1f7bbd4 100644 --- a/Nuclex.Support.csproj +++ b/Nuclex.Support.csproj @@ -156,6 +156,7 @@ PluginRepository.cs + @@ -199,6 +200,9 @@ ProgressTracker.cs + + Request.cs + StatusReportEventArgs.cs diff --git a/Source/Scheduling/AbortedException.Test.cs b/Source/Scheduling/AbortedException.Test.cs new file mode 100644 index 0000000..d72f1e2 --- /dev/null +++ b/Source/Scheduling/AbortedException.Test.cs @@ -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 { + + /// Unit Test for the request class + [TestFixture] + public class AbortedExceptionTest { + + /// + /// Verifies that the exception's default constructor is working + /// + [Test] + public void TestDefaultConstructor() { + AbortedException testException = new AbortedException(); + + string testExceptionString = testException.ToString(); + } + + /// + /// Checks whether the exception correctly stores its inner exception + /// + [Test] + public void TestInnerException() { + Exception inner = new Exception("This is a test"); + AbortedException testException = new AbortedException( + "Hello World", inner + ); + + Assert.AreSame(inner, testException.InnerException); + } + + + /// + /// Test whether the exception can be serialized + /// + [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 diff --git a/Source/Tracking/Request.Test.cs b/Source/Tracking/Request.Test.cs new file mode 100644 index 0000000..4b1e6cc --- /dev/null +++ b/Source/Tracking/Request.Test.cs @@ -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 { + + /// 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