public void HandleForwardsHandlingToConfiguredExceptionEntry()
        {
            Dictionary <string, ExceptionPolicyImpl> policies       = new Dictionary <string, ExceptionPolicyImpl>();
            Dictionary <Type, ExceptionPolicyEntry>  policy1Entries = new Dictionary <Type, ExceptionPolicyEntry>();

            policy1Entries.Add(
                typeof(ArithmeticException),
                new ExceptionPolicyEntry(PostHandlingAction.NotifyRethrow, new IExceptionHandler[] { new TestExceptionHandler("handler11") }));
            policy1Entries.Add(
                typeof(ArgumentException),
                new ExceptionPolicyEntry(PostHandlingAction.ThrowNewException, new IExceptionHandler[] { new TestExceptionHandler("handler12") }));
            policy1Entries.Add(
                typeof(ArgumentOutOfRangeException),
                new ExceptionPolicyEntry(PostHandlingAction.None, new IExceptionHandler[] { new TestExceptionHandler("handler13") }));
            policies.Add("policy1", new ExceptionPolicyImpl("policy1", policy1Entries));

            ExceptionManager manager = new ExceptionManagerImpl(policies);

            Exception thrownException;

            // is the exception rethrown?
            thrownException = new ArithmeticException();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1"));
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);

            // is the new exception thrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentException();
            try
            {
                manager.HandleException(thrownException, "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e.InnerException);
                Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
                Assert.AreEqual("handler12", TestExceptionHandler.HandlingNames[0]);
            }

            // is the exception swallowed? action == None
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentOutOfRangeException();
            Assert.IsFalse(manager.HandleException(thrownException, "policy1"));
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);

            // is the unknwon exception rethrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new Exception();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1"));
            Assert.AreEqual(0, TestExceptionHandler.HandlingNames.Count);
        }
        public void HandleWithNonExistingPolicyFiresInstrumentation()
        {
            ExceptionManager manager = new ExceptionManagerImpl(new Dictionary <string, ExceptionPolicyImpl>());

            ((DefaultExceptionHandlingInstrumentationProvider)((IInstrumentationEventProvider)manager).GetInstrumentationEventProvider())
            .exceptionHandlingErrorOccurred += (sender, args) => { firedEvent = args; };
            try
            {
                manager.HandleException(new Exception(), "policy");
                Assert.Fail("should have thrown");
            }
            catch (ExceptionHandlingException)
            {
                Assert.IsNotNull(this.firedEvent);
                Assert.AreEqual("policy", this.firedEvent.PolicyName);
            }
        }
        public void HandleWithNonExistingPolicyFiresInstrumentation()
        {
            ExceptionManager manager = new ExceptionManagerImpl(
                new Dictionary<string, ExceptionPolicyImpl>(),
                new TestInstrumentationProvider(this));

            try
            {
                manager.HandleException(new Exception(), "policy");
                Assert.Fail("should have thrown");
            }
            catch (ExceptionHandlingException)
            {
                Assert.IsNotNull(policyName);
                Assert.AreEqual("policy", policyName);
            }
        }
Beispiel #4
0
        public void HandleWithNonExistingPolicyFiresInstrumentation()
        {
            ExceptionManager manager = new ExceptionManagerImpl(
                new Dictionary <string, ExceptionPolicyImpl>(),
                new TestInstrumentationProvider(this));

            try
            {
                manager.HandleException(new Exception(), "policy");
                Assert.Fail("should have thrown");
            }
            catch (ExceptionHandlingException)
            {
                Assert.IsNotNull(policyName);
                Assert.AreEqual("policy", policyName);
            }
        }
Beispiel #5
0
 public void GetsHookedUpAndRaisesEvents()
 {
     using (WmiEventWatcher eventListener = new WmiEventWatcher(1))
     {
         try
         {
             manager.HandleException(new Exception(), "non-existing policy");
         }
         catch (ExceptionHandlingException)
         {
             eventListener.WaitForEvents();
             Assert.AreEqual(1, eventListener.EventsReceived.Count);
             Assert.AreEqual(typeof(ExceptionHandlingFailureEvent).Name,
                             eventListener.EventsReceived[0].ClassPath.ClassName);
             Assert.AreEqual("non-existing policy", eventListener.EventsReceived[0].GetPropertyValue("InstanceName"));
         }
     }
 }
        public void HandleWithReturnForwardsHandlingToConfiguredExceptionEntry()
        {
            var policies = new Dictionary<string, ExceptionPolicyImpl>();
            var policy1Entries = new Dictionary<Type, ExceptionPolicyEntry>
            {
                {
                    typeof (ArithmeticException),
                    new ExceptionPolicyEntry(typeof (ArithmeticException),
                        PostHandlingAction.NotifyRethrow,
                        new IExceptionHandler[] {new TestExceptionHandler("handler11")})
                },
                {
                    typeof (ArgumentException),
                    new ExceptionPolicyEntry(typeof (ArgumentException),
                        PostHandlingAction.ThrowNewException,
                        new IExceptionHandler[] {new TestExceptionHandler("handler12")})
                },
                {
                    typeof (ArgumentOutOfRangeException),
                    new ExceptionPolicyEntry(typeof (ArgumentOutOfRangeException),
                        PostHandlingAction.None,
                        new IExceptionHandler[] {new TestExceptionHandler("handler13")})
                }
            };

            policies.Add("policy1", new ExceptionPolicyImpl("policy1", policy1Entries));

            ExceptionManager manager = new ExceptionManagerImpl(policies);

            Exception exceptionToThrow;

            // is the exception rethrown?
            Exception thrownException = new ArithmeticException();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.IsNull(exceptionToThrow);
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);

            // is the new exception thrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentException();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.AreSame(thrownException, exceptionToThrow.InnerException);
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler12", TestExceptionHandler.HandlingNames[0]);

            // is the exception swallowed? action == None
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentOutOfRangeException();
            Assert.IsFalse(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.IsNull(exceptionToThrow);
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);

            // is the unknwon exception rethrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new Exception();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.IsNull(exceptionToThrow);
            Assert.AreEqual(0, TestExceptionHandler.HandlingNames.Count);
        }
Beispiel #7
0
        public void HandleWithReturnForwardsHandlingToConfiguredExceptionEntry()
        {
            var policies       = new Dictionary <string, ExceptionPolicyImpl>();
            var policy1Entries = new Dictionary <Type, ExceptionPolicyEntry>
            {
                {
                    typeof(ArithmeticException),
                    new ExceptionPolicyEntry(typeof(ArithmeticException),
                                             PostHandlingAction.NotifyRethrow,
                                             new IExceptionHandler[] { new TestExceptionHandler("handler11") })
                },
                {
                    typeof(ArgumentException),
                    new ExceptionPolicyEntry(typeof(ArgumentException),
                                             PostHandlingAction.ThrowNewException,
                                             new IExceptionHandler[] { new TestExceptionHandler("handler12") })
                },
                {
                    typeof(ArgumentOutOfRangeException),
                    new ExceptionPolicyEntry(typeof(ArgumentOutOfRangeException),
                                             PostHandlingAction.None,
                                             new IExceptionHandler[] { new TestExceptionHandler("handler13") })
                }
            };

            policies.Add("policy1", new ExceptionPolicyImpl("policy1", policy1Entries));

            ExceptionManager manager = new ExceptionManagerImpl(policies);

            Exception exceptionToThrow;

            // is the exception rethrown?
            Exception thrownException = new ArithmeticException();

            Assert.IsTrue(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.IsNull(exceptionToThrow);
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);

            // is the new exception thrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentException();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.AreSame(thrownException, exceptionToThrow.InnerException);
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler12", TestExceptionHandler.HandlingNames[0]);

            // is the exception swallowed? action == None
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentOutOfRangeException();
            Assert.IsFalse(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.IsNull(exceptionToThrow);
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);

            // is the unknwon exception rethrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new Exception();
            Assert.IsTrue(manager.HandleException(thrownException, "policy1", out exceptionToThrow));
            Assert.IsNull(exceptionToThrow);
            Assert.AreEqual(0, TestExceptionHandler.HandlingNames.Count);
        }