Exemplo n.º 1
0
        public void ContinueCheckingStrategiesAfterBeingHandled()
        {
            var counter = 0;
            var context = new ErrorHandlingContext(this, ErrorSeverityLevel.NonFatal, new Exception());

            var strategy1 = new Mock <IErrorHandlingStrategy>();

            strategy1.Setup(o => o.Handle(context)).Callback(() =>
            {
                context.MarkErrorAsHandled();
                counter++;
            });

            var strategy2 = new Mock <IErrorHandlingStrategy>();

            strategy2.Setup(o => o.Handle(context)).Callback(() =>
            {
                counter++;
            });

            var target = new CompositeErrorHandlingStrategy();

            target.AddStrategy(strategy1.Object);
            target.AddStrategy(strategy2.Object);

            target.Handle(context);

            Assert.AreEqual(2, counter);
        }
Exemplo n.º 2
0
        public void ExecutesAllStrategiesInTheOriginalOrder()
        {
            var counter = 0;
            var context = new ErrorHandlingContext(this, ErrorSeverityLevel.NonFatal, new Exception());

            var strategy1 = new Mock <IErrorHandlingStrategy>();

            strategy1.Setup(o => o.Handle(context)).Callback(() =>
            {
                Assert.AreEqual(0, counter);
                counter++;
            });

            var strategy2 = new Mock <IErrorHandlingStrategy>();

            strategy2.Setup(o => o.Handle(context)).Callback(() =>
            {
                Assert.AreEqual(1, counter);
                counter++;
            });

            var target = new CompositeErrorHandlingStrategy();

            target.AddStrategy(strategy1.Object);
            target.AddStrategy(strategy2.Object);

            target.Handle(context);

            Assert.AreEqual(2, counter);
        }
Exemplo n.º 3
0
        public void ThrowAnExceptionWhenRemovingAStrategyThatDoesNotExist()
        {
            var strategy = new Mock <IErrorHandlingStrategy>();

            var target = new CompositeErrorHandlingStrategy();

            Assert.Throws <ArgumentException>(() => target.RemoveStrategy(strategy.Object));
        }
Exemplo n.º 4
0
        public void ThrowAnExceptionWhenAddingADuplicateStrategy()
        {
            var strategy = new Mock <IErrorHandlingStrategy>();

            var target = new CompositeErrorHandlingStrategy();

            target.AddStrategy(strategy.Object);

            Assert.Throws <ArgumentException>(() => target.AddStrategy(strategy.Object));
        }
Exemplo n.º 5
0
        public void ThrowAnExceptionForDuplicateStrategies2()
        {
            var strategy = new Mock <IErrorHandlingStrategy>();
            IEnumerable <IErrorHandlingStrategy> strategies = new[]
            {
                strategy.Object,
                strategy.Object
            };

            Assert.Throws <ArgumentException>(() => CompositeErrorHandlingStrategy.Create(strategies));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a <see cref="CompositeErrorHandlingStrategy"/> from the strategies provided.
        /// </summary>
        /// <param name="strategies">The strategies to add to the composite result.</param>
        /// <returns>A <see cref="CompositeErrorHandlingStrategy"/> instance.</returns>
        public static CompositeErrorHandlingStrategy Create(IEnumerable <IErrorHandlingStrategy> strategies)
        {
            if (strategies == null)
            {
                throw new ArgumentNullException(nameof(strategies));
            }

            var result = new CompositeErrorHandlingStrategy();

            foreach (var strategy in strategies)
            {
                result.AddStrategy(strategy);
            }

            return(result);
        }
Exemplo n.º 7
0
        public void ThrowAnExceptionWhenAddingANullStrategy()
        {
            var target = new CompositeErrorHandlingStrategy();

            Assert.Throws <ArgumentNullException>(() => target.AddStrategy(null));
        }
Exemplo n.º 8
0
        public void ThrowAnExceptionForDuplicateStrategies1()
        {
            var strategy = new Mock <IErrorHandlingStrategy>();

            Assert.Throws <ArgumentException>(() => CompositeErrorHandlingStrategy.Create(strategy.Object, strategy.Object));
        }