//[Description("Creates two policies which both handle the same exception type.")]
        public void FailureBehaviour02()
        {
            var policy1 = PolicyGroupBuilder
                          .Create <AppleException>
                              (d => d.SetTargetForDefaultContext <BerlinException>()
                              .StartAndComplete(c => c.Set <ExceptionHandler <AppleException, BerlinException> >())
                              .WithoutTerminator()
                              );

            var policy2 = PolicyGroupBuilder
                          .Create <AppleException>
                              (d => d.SetTargetForDefaultContext <BeirutException>()
                              .StartAndComplete(c => c.Set <ExceptionHandler <AppleException, BeirutException> >())
                              .WithoutTerminator()
                              );

            var policyGroup = new IExceptionPolicyGroup[]
            {
                policy1,
                policy2
            };

            Action ctor = () => new ExceptionManager(policyGroup);

            ctor.Should().Throw <ExceptionManagerConfigurationException>();
        }
        public void UnWrappRuleTest01()
        {
            var policy2 = PolicyGroupBuilder
                          .Create <AggregateException>
                          (
                bd => bd.SetTargetForDefaultContext <AppleException>()
                .StartAndComplete(c => c.Set <TestExceptionHandlerB <AggregateException> >())
                .WithoutTerminator(),
                b1 => b1.SetTargetForContext <AppleException>("marten")
                .StartAndComplete(c => c.Set <TestExceptionHandlerB <AggregateException> >())
                .WithoutTerminator()
                          );

            var exception = new AggregateException("Greetings from outer exception",
                                                   Enumerable
                                                   .Repeat(new AppleException("Greetings from inner Exception."),
                                                           1));

            policy2.Should().NotBeNull();
            var manager = new ExceptionManager
                              (new[]
            {
                policy2
            }
                              , new PolicyMissingDefaultRule()
                              , new DefaultPolicyMatchingStrategy());

            manager.Invoking(m => m.Handle(exception))
            .Should().ThrowExactly <AppleException>();
        }
        public void ThrowsExceptionWhenNoTerminatorDefined()
        {
            var policy1 = PolicyGroupBuilder
                          .Create <AppleException>
                              (d => d.SetTargetForDefaultContext <AppleException>()
                              .StartAndComplete(c => c.Set <TestExceptionHandler <AppleException> >())
                              .WithoutTerminator());

            policy1.Should().NotBeNull();
            var manager = new ExceptionManager(new[]
            {
                policy1
            }, new PolicyMissingDefaultRule(),
                                               new DefaultPolicyMatchingStrategy());

            manager.Invoking(m => m.Handle(new BananaException()))
            .Should().ThrowExactly <PolicyMissingException>();
        }
        public void Handle_ShouldThrowArgumentNullException_WhenExceptionIsNull()
        {
            var policy1 = PolicyGroupBuilder
                          .Create <AppleException>
                              (d => d.SetTargetForDefaultContext <PearException>()
                              .StartAndComplete(c => c.Set <ExceptionHandler <AppleException, PearException> >())
                              .WithTerminator <VoidTerminator <PearException> >()
                              );

            policy1.Should().NotBeNull();
            var manager = new ExceptionManager(new[]
            {
                policy1
            });


            manager.Invoking(m => m.Handle(null as Exception))
            .Should().ThrowExactly <ArgumentNullException>();
        }
        public void Handle_ShouldNotThrowTheResultingException_WhenThePolicyIsTerminated()
        {
            var policy1 = PolicyGroupBuilder
                          .Create <AppleException>
                              (d => d.SetTargetForDefaultContext <PearException>()
                              .StartAndComplete(c => c.Set <ExceptionHandler <AppleException, PearException> >())
                              .WithTerminator <VoidTerminator <PearException> >()
                              );

            policy1.Should().NotBeNull();
            var manager = new ExceptionManager(new[]
            {
                policy1
            });


            manager.Invoking(m => m.Handle(new AppleException()))
            .Should().NotThrow();
        }
        //[Description(@"Tests the building a simple policy with a single conversion step.")]
        public void T1()
        {
            var policy1 = PolicyGroupBuilder
                          .Create <PearException>
                              (d => d.SetTargetForDefaultContext <BananaException>()
                              .StartAndComplete(c => c.Set <ExceptionHandler <PearException, BananaException> >())
                              .WithoutTerminator()
                              );

            policy1.Should().NotBeNull();
            var manager = new ExceptionManager(new[]
            {
                policy1
            });


            manager.Invoking(m => m.Handle(new PearException()))
            .Should().ThrowExactly <BananaException>();
        }
        public void T3()
        {
            var policy3 = PolicyGroupBuilder
                          .Create <CherryException>
                              (d => d.SetTargetForDefaultContext <BananaException>()
                              .Start <PearException>(c => c.Set <ExceptionHandler <CherryException, PearException> >())
                              .Then <MissingFieldException>(c => c.Set <ExceptionHandler <PearException, MissingFieldException> >())
                              .ThenComplete(c => c.Set <ExceptionHandler <MissingFieldException, BananaException> >())
                              .WithoutTerminator()
                              );

            policy3.Should().NotBeNull();
            var manager = new ExceptionManager(new[]
            {
                policy3
            }, new PolicyMissingDefaultRule(),
                                               new DefaultPolicyMatchingStrategy());

            manager.Invoking(m => m.Handle(new CherryException()))
            .Should().ThrowExactly <BananaException>();
        }
Example #8
0
        public void HonorsDefaultRuleOverrideWhenNoPolicyDefined()
        {
            var defaultRuleMock = new Mock <IUnconfiguredExceptionRule>();

            defaultRuleMock
            .Setup(_ => _.Apply(It.IsAny <Exception>()))
            .Throws <PolicyMissingException>()
            ;

            var policyGroup = PolicyGroupBuilder
                              .Create <AppleException>
                              (
                d => d.SetTargetForDefaultContext <AppleException>()
                .StartAndComplete(c => c.Set <ExceptionManagerTests.TestExceptionHandler <AppleException> >())
                .WithoutTerminator()
                              );

            var manager = new ExceptionManager(new[] { policyGroup }, defaultRuleMock.Object);

            manager.Invoking(m => m.Handle(new OutOfMemoryException()))
            .Should().ThrowExactly <PolicyMissingException>();

            defaultRuleMock.Verify();
        }