#region CPL License /* Nuclex Framework Copyright (C) 2002-2007 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; #if UNITTEST using NUnit.Framework; using NMock2; using Nuclex.Support.Tracking; namespace Nuclex.Support.Scheduling { /// Unit Test for the queue operation class [TestFixture] public class QueueOperationTest { #region interface IQueueOperationSubscriber /// Interface used to test the set progression. public interface IQueueOperationSubscriber { /// Called when the queue operations's progress changes /// Queue operation whose progress has changed /// Contains the new progress achieved void ProgressUpdated(object sender, ProgressUpdateEventArgs e); /// Called when the queue operation has ended /// Queue operation that as ended /// Not used void Ended(object sender, EventArgs e); } #endregion // interface IQueueOperationSubscriber #region class ProgressUpdateEventArgsMatcher /// Compares two ProgressUpdateEventArgsInstances for NMock validation private class ProgressUpdateEventArgsMatcher : Matcher { /// Initializes a new ProgressUpdateEventArgsMatcher /// Expected progress update event arguments public ProgressUpdateEventArgsMatcher(ProgressUpdateEventArgs expected) { this.expected = expected; } /// /// Called by NMock to verfiy the ProgressUpdateEventArgs match the expected value /// /// Actual value to compare to the expected value /// /// True if the actual value matches the expected value; otherwise false /// public override bool Matches(object actualAsObject) { ProgressUpdateEventArgs actual = (actualAsObject as ProgressUpdateEventArgs); if(actual == null) return false; return (actual.Progress == this.expected.Progress); } /// Creates a string representation of the expected value /// Writer to write the string representation into public override void DescribeTo(TextWriter writer) { writer.Write(this.expected.Progress.ToString()); } /// Expected progress update event args value private ProgressUpdateEventArgs expected; } #endregion // class ProgressUpdateEventArgsMatcher #region class TestOperation /// Progression used for testing in this unit test private class TestOperation : Operation { /// Begins executing the operation. Yeah, sure :) public override void Begin() { } /// Moves the operation into the ended state public void SetEnded() { SetEnded(null); } /// Moves the operation into the ended state with an exception /// Exception public void SetEnded(Exception exception) { SetException(exception); OnAsyncEnded(); } /// Changes the testing progression's indicated progress /// /// New progress to be reported by the testing progression /// public void ChangeProgress(float progress) { OnAsyncProgressUpdated(progress); } } #endregion // class TestOperation /// Initialization routine executed before each test is run [SetUp] public void Setup() { this.mockery = new Mockery(); } /// Validates that the queue executes operations sequentially [Test] public void TestSequentialExecution() { TestOperation operation1 = new TestOperation(); TestOperation operation2 = new TestOperation(); QueueOperation testQueueOperation = new QueueOperation( new TestOperation[] { operation1, operation2 } ); IQueueOperationSubscriber mockedSubscriber = mockSubscriber(testQueueOperation); testQueueOperation.Begin(); Expect.Once.On(mockedSubscriber). Method("ProgressUpdated"). With( new Matcher[] { new NMock2.Matchers.TypeMatcher(typeof(QueueOperation)), new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.25f)) } ); operation1.ChangeProgress(0.5f); Expect.Once.On(mockedSubscriber). Method("ProgressUpdated"). With( new Matcher[] { new NMock2.Matchers.TypeMatcher(typeof(QueueOperation)), new ProgressUpdateEventArgsMatcher(new ProgressUpdateEventArgs(0.5f)) } ); operation1.SetEnded(); this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// Mocks a subscriber for the events of an operation /// Operation to mock an event subscriber for /// The mocked event subscriber private IQueueOperationSubscriber mockSubscriber(Operation operation) { IQueueOperationSubscriber mockedSubscriber = this.mockery.NewMock(); operation.AsyncEnded += new EventHandler(mockedSubscriber.Ended); operation.AsyncProgressUpdated += new EventHandler(mockedSubscriber.ProgressUpdated); return mockedSubscriber; } /// Mock object factory private Mockery mockery; } } // namespace Nuclex.Support.Tracking #endif // UNITTEST