2017-07-21 12:05:31 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-21 12:05:31 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Moq;
|
2018-08-31 10:06:27 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
2017-07-21 12:05:31 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2018-08-31 10:06:27 +02:00
|
|
|
|
using SafeExamBrowser.Core.OperationModel;
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.UnitTests.OperationModel
|
2017-07-21 12:05:31 +02:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public class OperationSequenceTests
|
2017-07-21 12:05:31 +02:00
|
|
|
|
{
|
|
|
|
|
private Mock<ILogger> loggerMock;
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
loggerMock = new Mock<ILogger>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCreateCopyOfOperationQueue()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
operations.Clear();
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
#region Perform Tests
|
|
|
|
|
|
2018-01-19 14:04:12 +01:00
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustCorrectlyAbortPerform()
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Aborted);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Aborted, result);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustPerformOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Never);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Never);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustPerformOperationsInSequence()
|
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success).Callback(() => a = ++current);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success).Callback(() => c = ++current);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
Assert.IsTrue(a == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(c == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRevertOperationsInCaseOfError()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operationD = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationC.Setup(o => o.Perform()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
operations.Enqueue(operationD.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationD.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationD.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public void MustRevertOperationsInSequenceAfterPerformError()
|
2017-07-21 12:05:31 +02:00
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0, d = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operationD = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationA.Setup(o => o.Revert()).Callback(() => a = ++current);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationB.Setup(o => o.Revert()).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Throws<Exception>();
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationC.Setup(o => o.Revert()).Callback(() => c = ++current);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
operationD.Setup(o => o.Revert()).Callback(() => d = ++current);
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
operations.Enqueue(operationD.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
Assert.IsTrue(d == 0);
|
|
|
|
|
Assert.IsTrue(c == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(a == 3);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
[TestMethod]
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public void MustContinueToRevertOperationsAfterPerformError()
|
2017-10-09 12:11:33 +02:00
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
2017-10-09 12:11:33 +02:00
|
|
|
|
operationC.Setup(o => o.Perform()).Throws<Exception>();
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
operationC.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationB.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationA.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
var success = sut.TryPerform();
|
2017-10-09 12:11:33 +02:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSucceedWithEmptyQueue()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2017-07-21 12:05:31 +02:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailInCaseOfUnexpectedError()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
var indicatorMock = new Mock<IProgressIndicator>();
|
|
|
|
|
|
|
|
|
|
indicatorMock.Setup(i => i.SetMaxValue(It.IsAny<int>())).Throws<Exception>();
|
|
|
|
|
sut.ProgressIndicator = indicatorMock.Object;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Repeat Tests
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustCorrectlyAbortRepeat()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Aborted);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationB.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Repeat(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Aborted, result);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRepeatOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationA.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationB.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationB.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Perform(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRepeatOperationsInSequence()
|
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => a = ++current);
|
|
|
|
|
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => c = ++current);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
Assert.IsTrue(a == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(c == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotRevertOperationsInCaseOfError()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operationD = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
operationC.Setup(o => o.Repeat()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
operations.Enqueue(operationD.Object);
|
|
|
|
|
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationB.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationC.Verify(o => o.Repeat(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
operationD.Verify(o => o.Repeat(), Times.Never);
|
|
|
|
|
operationD.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSucceedRepeatingWithEmptyQueue()
|
|
|
|
|
{
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSucceedRepeatingWithoutCallingPerform()
|
|
|
|
|
{
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailInCaseOfUnexpectedErrorWhenRepeating()
|
|
|
|
|
{
|
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
var indicatorMock = new Mock<IProgressIndicator>();
|
|
|
|
|
|
|
|
|
|
indicatorMock.Setup(i => i.SetMaxValue(It.IsAny<int>())).Throws<Exception>();
|
|
|
|
|
sut.ProgressIndicator = indicatorMock.Object;
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = sut.TryRepeat();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Revert Tests
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRevertOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
var success = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRevertOperationsInSequence()
|
|
|
|
|
{
|
|
|
|
|
int current = 0, a = 0, b = 0, c = 0;
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
operationA.Setup(o => o.Revert()).Callback(() => a = ++current);
|
|
|
|
|
operationB.Setup(o => o.Revert()).Callback(() => b = ++current);
|
|
|
|
|
operationC.Setup(o => o.Revert()).Callback(() => c = ++current);
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
var success = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(success);
|
|
|
|
|
Assert.IsTrue(c == 1);
|
|
|
|
|
Assert.IsTrue(b == 2);
|
|
|
|
|
Assert.IsTrue(a == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustContinueToRevertOperationsInCaseOfError()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
operationA.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationB.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
operationC.Setup(o => o.Revert()).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
var success = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustOnlyRevertPerformedOperations()
|
|
|
|
|
{
|
|
|
|
|
var operationA = new Mock<IOperation>();
|
|
|
|
|
var operationB = new Mock<IOperation>();
|
|
|
|
|
var operationC = new Mock<IOperation>();
|
|
|
|
|
var operations = new Queue<IOperation>();
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
operationA.Setup(o => o.Perform()).Returns(OperationResult.Success);
|
|
|
|
|
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
|
|
|
|
operationB.Setup(o => o.Perform()).Returns(OperationResult.Aborted);
|
|
|
|
|
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
operations.Enqueue(operationA.Object);
|
|
|
|
|
operations.Enqueue(operationB.Object);
|
|
|
|
|
operations.Enqueue(operationC.Object);
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, operations);
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
|
|
|
|
|
var success = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
operationA.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationB.Verify(o => o.Revert(), Times.Once);
|
|
|
|
|
operationC.Verify(o => o.Revert(), Times.Never);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustSucceedWithEmptyQueueWhenReverting()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
|
|
|
|
|
sut.TryPerform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
sut.TryRevert();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public void MustSucceedRevertingWithoutCallingPerform()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
2018-02-01 08:37:12 +01:00
|
|
|
|
var success = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailInCaseOfUnexpectedErrorWhenReverting()
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
|
|
|
|
|
var indicatorMock = new Mock<IProgressIndicator>();
|
|
|
|
|
|
2018-02-07 13:25:49 +01:00
|
|
|
|
indicatorMock.Setup(i => i.SetIndeterminate()).Throws<Exception>();
|
2018-02-02 09:18:35 +01:00
|
|
|
|
sut.ProgressIndicator = indicatorMock.Object;
|
|
|
|
|
|
|
|
|
|
var success = sut.TryRevert();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(success);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2017-07-21 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|