public void OnError_ProcessorResult_Is_Not_Changed_By_Execute()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();

            Exception executionException = new Exception();

            object[] executionOutput = new object[0];
            processor.OnExecuteAction = input => new ProcessorResult
            {
                Status = ProcessorStatus.Error,
                Output = executionOutput,
                Error  = executionException
            };

            Exception onErrorException = new Exception();

            object[] onErrorOutput = new object[0];
            processor.OnErrorAction = result =>
            {
                Assert.AreEqual(ProcessorStatus.Error, result.Status, "The result status should have been ProcessorStatus.Error.");
                Assert.AreSame(result.Error, executionException, "The Error should have been the same instance as the one set in OnExecute.");
                Assert.AreSame(result.Output, executionOutput, "The Output should have been the same instance as the one set in OnExecute.");
                result.Error  = onErrorException;
                result.Output = onErrorOutput;
            };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNotNull(resultFromExecute, "There should have been a non-null result.");
            Assert.AreEqual(ProcessorStatus.Error, resultFromExecute.Status, "The result status should have been ProcessorStatus.Error.");
            Assert.AreSame(resultFromExecute.Error, onErrorException, "The Error should have been the same instance as the one set in OnError.");
            Assert.AreSame(resultFromExecute.Output, onErrorOutput, "The Output should have been the same instance as the one set in OnError.");
        }
        public void Processor_Initializes_In_Correct_Order()
        {
            MockNonGenericProcessor mock = new MockNonGenericProcessor();
            int count = 0;

            mock.Initializing = new EventHandler(
                (obj, eArgs) =>
            {
                Assert.AreEqual(0, count, "Count should have been 0 because the Initializing event should have been called first.");
                Assert.IsFalse(mock.IsInitialized, "IsInitialized should be false.");
                count++;
            });

            mock.OnInitializeAction = () =>
            {
                Assert.AreEqual(1, count, "Count should have been 1 because the OnInitilize override should have been called second.");
                Assert.IsFalse(mock.IsInitialized, "IsInitialized should be false.");
                count++;
            };

            mock.Initialized = new EventHandler(
                (obj, eArgs) =>
            {
                Assert.AreEqual(2, count, "Count should have been 2 because the Initialized event should have been called third.");
                Assert.IsTrue(mock.IsInitialized, "IsInitialized should be true.");
                count++;
            });

            Assert.IsFalse(mock.IsInitialized, "IsInitialized should be false.");
            mock.Initialize();
            Assert.IsTrue(mock.IsInitialized, "IsInitialized should be true.");

            Assert.AreEqual(3, count, "Count should have been 3 because the Initialized event should have been called.");
        }
        public void Execute_Allows_Null_Output_And_Null_Error_On_ProcessResult()
        {
            MockNonGenericProcessor processor   = new MockNonGenericProcessor();
            Exception       exception           = new Exception();
            ProcessorResult resultFromOnExecute = null;

            processor.OnExecuteAction = input =>
            {
                resultFromOnExecute = new ProcessorResult()
                {
                    Status = ProcessorStatus.Ok, Output = null, Error = null
                };
                return(resultFromOnExecute);
            };

            processor.Initialize();

            object[] inputObjArray = new object[1] {
                "hello"
            };
            ProcessorResult result = processor.Execute(inputObjArray);

            Assert.IsNotNull(result, "Processor.Execute should never return null.");
            Assert.AreEqual(ProcessorStatus.Ok, result.Status, "Processor.Execute should have returned the same value as returned from OnExecute.");
            Assert.AreSame(resultFromOnExecute, result, "Processor.Execute should have returned the same instance returned from OnExecute.");
            Assert.IsNull(result.Output, "Processor.Execute should have returned the same instance returned from OnExecute.");
            Assert.IsNull(result.Error, "Processor.Execute should have returned the same instance returned from OnExecute.");
        }
        public void Execute_Throws_If_Input_Is_Null()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();

            processor.Initialize();

            ExceptionAssert.ThrowsArgumentNull(
                "Execute should have thrown since the input parameter is null.",
                "input",
                () =>
            {
                ProcessorResult resultFromExecute = processor.Execute(null);
            });
        }
        public void Execute_Default_ProcessResult_Is_Status_Ok_With_Null_Output()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            Exception exception = new Exception();
            processor.OnExecuteAction = input => new ProcessorResult();

            processor.Initialize();
            ProcessorResult result = processor.Execute(new object[0]);

            Assert.IsNotNull(result, "Processor.Execute should never return null.");
            Assert.AreEqual(ProcessorStatus.Ok, result.Status, "Processor.Execute should have returned the ProcessorStatus.Ok.");
            Assert.IsNull(result.Output, "Processor.Execute should have returned a null Output.");
            Assert.IsNull(result.Error, "Processor.Execute should have returned a null exception.");
        }
        public void Execute_Default_ProcessResult_Is_Status_Ok_With_Null_Output()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            Exception exception = new Exception();

            processor.OnExecuteAction = input => new ProcessorResult();

            processor.Initialize();
            ProcessorResult result = processor.Execute(new object[0]);

            Assert.IsNotNull(result, "Processor.Execute should never return null.");
            Assert.AreEqual(ProcessorStatus.Ok, result.Status, "Processor.Execute should have returned the ProcessorStatus.Ok.");
            Assert.IsNull(result.Output, "Processor.Execute should have returned a null Output.");
            Assert.IsNull(result.Error, "Processor.Execute should have returned a null exception.");
        }
        public void OnError_Not_Called_If_Execute_Returns_ProcessorStatus_Ok()
        {
            MockNonGenericProcessor processor         = new MockNonGenericProcessor();
            ProcessorResult         resultFromOnError = null;

            processor.OnErrorAction   = result => resultFromOnError = result;
            processor.OnExecuteAction = input => new ProcessorResult {
                Status = ProcessorStatus.Ok
            };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNull(resultFromOnError, "OnError should not have run.");
            Assert.IsNotNull(resultFromExecute, "There should have been a non-null result.");
        }
        public void OnError_Called_If_Execute_Returns_Null()
        {
            MockNonGenericProcessor processor         = new MockNonGenericProcessor();
            ProcessorResult         resultFromOnError = null;

            processor.OnErrorAction   = result => resultFromOnError = result;
            processor.OnExecuteAction = input => null;

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNotNull(resultFromExecute, "OnError should have been called.");
            Assert.AreEqual(ProcessorStatus.Error, resultFromOnError.Status, "The Status should have been ProcessorStatus.Error");
            Assert.IsNotNull(resultFromOnError.Error, "The Exception should have been set of the ProcessorResult.");
            Assert.IsInstanceOfType(resultFromOnError.Error, typeof(InvalidOperationException), "The Exception should have been an InvalidOperationException.");
            Assert.IsNull(resultFromOnError.Output, "The Output should not have been set of the ProcessorResult.");
            Assert.AreSame(resultFromOnError, resultFromExecute, "The ProcessorResult from Execute should be the same instance as the one from OnError");
        }
        public void OnError_Called_If_Execute_Throws()
        {
            MockNonGenericProcessor   processor         = new MockNonGenericProcessor();
            ProcessorResult           resultFromOnError = null;
            InvalidOperationException exception         = new InvalidOperationException();

            processor.OnErrorAction   = result => resultFromOnError = result;
            processor.OnExecuteAction = input => { throw exception; };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNotNull(resultFromExecute, "OnError should have been called.");
            Assert.AreEqual(ProcessorStatus.Error, resultFromOnError.Status, "The Status should have been ProcessorStatus.Error");
            Assert.IsNotNull(resultFromOnError.Error, "The Exception should have been set of the ProcessorResult.");
            Assert.AreSame(exception, resultFromOnError.Error, "The Exception should have been the same instance as the one thrown from within OnExecute.");
            Assert.IsNull(resultFromOnError.Output, "The Output should not have been set of the ProcessorResult.");
            Assert.AreSame(resultFromOnError, resultFromExecute, "The ProcessorResult from Execute should be the same instance as the one from OnError");
        }
        public void Execute_Does_Not_Change_ProcessResult_If_Not_Null()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            Exception exception = new Exception();
            ProcessorResult resultFromOnExecute = null;
            processor.OnExecuteAction = input =>
                {
                    resultFromOnExecute = new ProcessorResult() { Status = ProcessorStatus.Ok, Output = input, Error = exception };
                    return resultFromOnExecute;
                };

            processor.Initialize();

            object[] inputObjArray = new object[1] { "hello" };
            ProcessorResult result = processor.Execute(inputObjArray);

            Assert.IsNotNull(result, "Processor.Execute should never return null.");
            Assert.AreEqual(ProcessorStatus.Ok, result.Status, "Processor.Execute should have returned the same value as returned from OnExecute.");
            Assert.AreSame(resultFromOnExecute, result, "Processor.Execute should have returned the same instance returned from OnExecute.");
            Assert.AreSame(inputObjArray, result.Output, "Processor.Execute should have returned the same instance returned from OnExecute.");
            Assert.AreSame(exception, result.Error, "Processor.Execute should have returned the same instance returned from OnExecute.");
        }
        public void Processor_Initializes_In_Correct_Order()
        {
            MockNonGenericProcessor mock = new MockNonGenericProcessor();
            int count = 0;

            mock.Initializing = new EventHandler(
                (obj, eArgs) =>
                {
                    Assert.AreEqual(0, count, "Count should have been 0 because the Initializing event should have been called first.");
                    Assert.IsFalse(mock.IsInitialized, "IsInitialized should be false.");
                    count++;
                });

            mock.OnInitializeAction = () =>
                {
                    Assert.AreEqual(1, count, "Count should have been 1 because the OnInitilize override should have been called second.");
                    Assert.IsFalse(mock.IsInitialized, "IsInitialized should be false.");
                    count++;
                };

            mock.Initialized = new EventHandler(
                (obj, eArgs) =>
                {
                    Assert.AreEqual(2, count, "Count should have been 2 because the Initialized event should have been called third.");
                    Assert.IsTrue(mock.IsInitialized, "IsInitialized should be true.");
                    count++;
                });

            Assert.IsFalse(mock.IsInitialized, "IsInitialized should be false.");
            mock.Initialize();
            Assert.IsTrue(mock.IsInitialized, "IsInitialized should be true.");

            Assert.AreEqual(3, count, "Count should have been 3 because the Initialized event should have been called.");
        }
        public void OnError_ProcessorResult_Is_Not_Changed_By_Execute()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();

            Exception executionException = new Exception();
            object[] executionOutput = new object[0];
            processor.OnExecuteAction = input => new ProcessorResult
            {
                Status = ProcessorStatus.Error,
                Output = executionOutput,
                Error = executionException
            };

            Exception onErrorException = new Exception();
            object[] onErrorOutput = new object[0];
            processor.OnErrorAction = result =>
            {
                Assert.AreEqual(ProcessorStatus.Error, result.Status, "The result status should have been ProcessorStatus.Error.");
                Assert.AreSame(result.Error, executionException, "The Error should have been the same instance as the one set in OnExecute.");
                Assert.AreSame(result.Output, executionOutput, "The Output should have been the same instance as the one set in OnExecute.");
                result.Error = onErrorException;
                result.Output = onErrorOutput;
            };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNotNull(resultFromExecute, "There should have been a non-null result.");
            Assert.AreEqual(ProcessorStatus.Error, resultFromExecute.Status, "The result status should have been ProcessorStatus.Error.");
            Assert.AreSame(resultFromExecute.Error, onErrorException, "The Error should have been the same instance as the one set in OnError.");
            Assert.AreSame(resultFromExecute.Output, onErrorOutput, "The Output should have been the same instance as the one set in OnError.");
        }
        public void OnError_Not_Called_If_Execute_Returns_ProcessorStatus_Ok()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            ProcessorResult resultFromOnError = null;
            processor.OnErrorAction = result => resultFromOnError = result;
            processor.OnExecuteAction = input => new ProcessorResult { Status = ProcessorStatus.Ok };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNull(resultFromOnError, "OnError should not have run.");
            Assert.IsNotNull(resultFromExecute, "There should have been a non-null result.");
        }
        public void OnError_Called_If_Execute_Throws()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            ProcessorResult resultFromOnError = null;
            InvalidOperationException exception = new InvalidOperationException();
            processor.OnErrorAction = result => resultFromOnError = result;
            processor.OnExecuteAction = input => { throw exception; };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNotNull(resultFromExecute, "OnError should have been called.");
            Assert.AreEqual(ProcessorStatus.Error, resultFromOnError.Status, "The Status should have been ProcessorStatus.Error");
            Assert.IsNotNull(resultFromOnError.Error, "The Exception should have been set of the ProcessorResult.");
            Assert.AreSame(exception, resultFromOnError.Error, "The Exception should have been the same instance as the one thrown from within OnExecute.");
            Assert.IsNull(resultFromOnError.Output, "The Output should not have been set of the ProcessorResult.");
            Assert.AreSame(resultFromOnError, resultFromExecute, "The ProcessorResult from Execute should be the same instance as the one from OnError");
        }
        public void OnError_Called_If_Execute_Returns_ProcessorStatus_Error()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            ProcessorResult resultFromOnError = null;
            processor.OnErrorAction = result => resultFromOnError = result;
            processor.OnExecuteAction = input => new ProcessorResult { Status = ProcessorStatus.Error };

            processor.Initialize();
            ProcessorResult resultFromExecute = processor.Execute(new object[0]);

            Assert.IsNotNull(resultFromExecute, "OnError should have been called.");
            Assert.AreEqual(ProcessorStatus.Error, resultFromOnError.Status, "The Status should have been ProcessorStatus.Error");
            Assert.IsNull(resultFromOnError.Error, "The Exception should not have been set of the ProcessorResult.");
            Assert.IsNull(resultFromOnError.Output, "The Output should not have been set of the ProcessorResult.");
            Assert.AreSame(resultFromOnError, resultFromExecute, "The ProcessorResult from Execute should be the same instance as the one from OnError");
        }
        public void Execute_Throws_If_Input_Is_Null()
        {
            MockNonGenericProcessor processor = new MockNonGenericProcessor();
            processor.Initialize();

            ExceptionAssert.ThrowsArgumentNull(
                "Execute should have thrown since the input parameter is null.",
                "input",
                () =>
                {
                    ProcessorResult resultFromExecute = processor.Execute(null);
                });
        }