Esempio n. 1
0
        public void ProcessWithReturnValueReturnsCorrectValueWhenNoExceptionThrown()
        {
            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);

            int result = manager.Process(() => 42, "policy1");

            Assert.AreEqual(42, result);
        }
Esempio n. 2
0
        public void SetUp()
        {
            manager = new ExceptionManagerImpl(new Dictionary <string, ExceptionPolicyImpl>());
            ReflectionInstrumentationAttacher attacher
                = new ReflectionInstrumentationAttacher(
                      ((IInstrumentationEventProvider)manager).GetInstrumentationEventProvider(),
                      typeof(DefaultExceptionHandlingEventLogger),
                      new object[] { true, true, true, "ApplicationInstanceName" });

            attacher.BindInstrumentation();
        }
        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);
            }
        }
Esempio n. 6
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);
            }
        }
        public void ProcessWithReturnValueProcessesExceptionsOnThrow()
        {
            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);

            // is the exception rethrown?
            Exception thrownException = new ArithmeticException();
            try
            {
                Exception ex1 = thrownException;
                int result = manager.Process(() => { throw ex1;
#pragma warning disable 162 // Unreachable code
                                                       return 37;
#pragma warning restore 162
                }, "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e);
                Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
                Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);
            }

            // is the exception swallowed? action == None
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentOutOfRangeException();
            Exception ex3 = thrownException;
            int swallowedResult = manager.Process(() => { throw ex3;
#pragma warning disable 162 // Unreachable code
                                                   return 17; 
#pragma warning restore 162
            }, -20, "policy1");
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);
            Assert.AreEqual(-20, swallowedResult);            
        }
        public void ProcessWithReturnValueReturnsCorrectValueWhenNoExceptionThrown()
        {
            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);

            int result = manager.Process(() => 42, "policy1");
            Assert.AreEqual(42, result);
        }
        public void ProcessForwardsHandlingToConfiguredExceptionEntry()
        {
            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);

            // is the exception rethrown?
            Exception thrownException = new ArithmeticException();
            try
            {
                Exception ex1 = thrownException;
                manager.Process( () => { throw ex1; }, "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e);
                Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
                Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);
            }

            // is the new exception thrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentException();
            try
            {
                Exception ex2 = thrownException;
                manager.Process(() => { throw ex2; }, "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();
            Exception ex3 = thrownException;
            manager.Process(() => { throw ex3; }, "policy1");
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);

            // is the unknwon exception rethrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new Exception();
            try
            {
                manager.Process(
                    () => { throw (thrownException); },
                    "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e);
                Assert.AreEqual(0, TestExceptionHandler.HandlingNames.Count);
            }
        }
        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);
        }
Esempio n. 11
0
        public void ProcessWithReturnValueProcessesExceptionsOnThrow()
        {
            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);

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

            try
            {
                Exception ex1    = thrownException;
                int       result = manager.Process(() => { throw ex1;
#pragma warning disable 162 // Unreachable code
                                                           return(37);

#pragma warning restore 162
                                                   }, "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e);
                Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
                Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);
            }

            // is the exception swallowed? action == None
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentOutOfRangeException();
            Exception ex3             = thrownException;
            int       swallowedResult = manager.Process(() => { throw ex3;
#pragma warning disable 162 // Unreachable code
                                                                return(17);

#pragma warning restore 162
                                                        }, -20, "policy1");

            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);
            Assert.AreEqual(-20, swallowedResult);
        }
Esempio n. 12
0
        public void ProcessForwardsHandlingToConfiguredExceptionEntry()
        {
            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);

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

            try
            {
                Exception ex1 = thrownException;
                manager.Process(() => { throw ex1; }, "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e);
                Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
                Assert.AreEqual("handler11", TestExceptionHandler.HandlingNames[0]);
            }

            // is the new exception thrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new ArgumentException();
            try
            {
                Exception ex2 = thrownException;
                manager.Process(() => { throw ex2; }, "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();
            Exception ex3 = thrownException;

            manager.Process(() => { throw ex3; }, "policy1");
            Assert.AreEqual(1, TestExceptionHandler.HandlingNames.Count);
            Assert.AreEqual("handler13", TestExceptionHandler.HandlingNames[0]);

            // is the unknwon exception rethrown?
            TestExceptionHandler.HandlingNames.Clear();
            thrownException = new Exception();
            try
            {
                manager.Process(
                    () => { throw (thrownException); },
                    "policy1");
                Assert.Fail("should have thrown");
            }
            catch (Exception e)
            {
                Assert.AreSame(thrownException, e);
                Assert.AreEqual(0, TestExceptionHandler.HandlingNames.Count);
            }
        }
Esempio n. 13
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);
        }