public void HandlerInChainThrowsExceptionWhenProduceError()
 {
     ExceptionPolicyData policyData = PolicyData;
     Dictionary<Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
     ExceptionPolicyDefinition policyIml = new ExceptionPolicyDefinition(policyData.Name, entries);
     policyIml.HandleException(new ArgumentException());
 }
 public void HandleExceptionWithNullExceptionThrows()
 {
     ExceptionPolicyData policyData = PolicyData;
     Dictionary<Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
     ExceptionPolicyDefinition policyIml = new ExceptionPolicyDefinition(policyData.Name, entries);
     policyIml.HandleException(null);
 }
示例#3
0
        public void HandleExceptionWithNullExceptionThrows()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyDefinition policyIml             = new ExceptionPolicyDefinition(policyData.Name, entries);

            policyIml.HandleException(null);
        }
示例#4
0
        public void HandlerInChainThrowsExceptionWhenProduceError()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyDefinition policyIml             = new ExceptionPolicyDefinition(policyData.Name, entries);

            policyIml.HandleException(new ArgumentException());
        }
 public void HandleExceptionThatHasNoEntryReturnsTrue()
 {
     ExceptionPolicyData policyData = PolicyData;
     Dictionary<Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
     ExceptionPolicyDefinition policyIml = new ExceptionPolicyDefinition(policyData.Name, entries);
     bool handled = policyIml.HandleException(new InvalidCastException());
     Assert.IsTrue(handled);
 }
示例#6
0
        public void HandleExceptionThatHasNoEntryReturnsTrue()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyDefinition policyIml             = new ExceptionPolicyDefinition(policyData.Name, entries);
            bool handled = policyIml.HandleException(new InvalidCastException());

            Assert.IsTrue(handled);
        }
示例#7
0
        public void WhenRegisteredAgainstExceptionType_OnlyCorrectHandlerIsCalled()
        {
            ExceptionPolicyDefinition policyImpl = new ExceptionPolicyDefinition("APolicyName",
                                                                                 new List <ExceptionPolicyEntry>()
            {
                entry1, entry2
            });

            policyImpl.HandleException(new ArgumentNullException("TestException"));
            Assert.AreEqual(1, handler1.instanceHandledExceptionCount);
            Assert.AreEqual(0, handler2.instanceHandledExceptionCount);
        }
        public void ShouldCreateHandlerWithCorrectPolicy()
        {
            string policyName = "Swallow Exceptions";
            ExceptionCallHandlerAttribute attribute = new ExceptionCallHandlerAttribute(policyName);

            attribute.Order = 400;

            IUnityContainer container = new UnityContainer();
            var             policy    = new ExceptionPolicyDefinition(policyName, new ExceptionPolicyEntry[0]);

            container.RegisterInstance(policyName, policy);

            ExceptionCallHandler handler = (ExceptionCallHandler)attribute.CreateHandler(container);

            Assert.AreEqual(policyName, handler.ExceptionPolicyName);
            Assert.AreEqual(400, handler.Order);
        }
        public void WhenConfiguredContainer_ThenCanResolveCallHandler()
        {
            using (var container = new UnityContainer())
            {
                // TODO review after updating call handler
                var policy = new ExceptionPolicyDefinition("policy", new ExceptionPolicyEntry[0]);
                container.RegisterInstance("policy", policy);

                this.callHandlerData.ConfigureContainer(container, "-suffix");

                var handler = (ExceptionCallHandler)container.Resolve <ICallHandler>("exception-suffix");

                Assert.AreEqual(400, handler.Order);
                Assert.AreSame("policy", handler.ExceptionPolicyName);

                Assert.AreNotSame(handler, container.Resolve <ICallHandler>("exception-suffix"));
            }
        }
        public void WhenConfiguredContainer_ThenCanResolveCallHandler()
        {
            using (var container = new UnityContainer())
            {
                // TODO review after updating call handler
                var policy = new ExceptionPolicyDefinition("policy", new ExceptionPolicyEntry[0]);
                container.RegisterInstance("policy", policy);

                this.callHandlerData.ConfigureContainer(container, "-suffix");

                var handler = (ExceptionCallHandler)container.Resolve<ICallHandler>("exception-suffix");

                Assert.AreEqual(400, handler.Order);
                Assert.AreSame("policy", handler.ExceptionPolicyName);

                Assert.AreNotSame(handler, container.Resolve<ICallHandler>("exception-suffix"));
            }
        }
示例#11
0
        /// <summary>
        /// Create <see cref="ExceptionPolicyDefinition"/> to implement exception shielding for component.
        /// 1. rethrow all exceptions derived from component base exception <typeparamref name="TBaseException"/>
        /// 2. rethrow <see cref="OperationCanceledException"/>
        /// 3. wrap generic exceptions to component specific exception <typeparamref name="TBaseException"/>
        /// </summary>
        /// <param name="policyDefinitionName">Policy definition name</param>
        /// <param name="wrapExceptionMessage">string resolver to be used in case of wrapping generic exception to component specific exception</param>
        /// <param name="wrapExceptionBeforeHandlers">additional handlers <see cref="IExceptionHandler"/> which will be executed before wrapping generic exception to component specific exception</param>
        /// <param name="additionalPolicyEntries">additional <see cref="ExceptionPolicyEntry"/> which will be added to policy definition before default ones</param>
        /// <typeparam name="TBaseException">Type of base class for component specific exceptions</typeparam>
        /// <returns>Policy definition <see cref="ExceptionPolicyDefinition"/></returns>
        public static ExceptionPolicyDefinition CreateShieldingForComponent <TBaseException>(
            string policyDefinitionName          = null,
            IStringResolver wrapExceptionMessage = null,
            IEnumerable <IExceptionHandler> wrapExceptionBeforeHandlers = null,
            IEnumerable <ExceptionPolicyEntry> additionalPolicyEntries  = null) where TBaseException : Exception
        {
            if (policyDefinitionName == null)
            {
                policyDefinitionName = "ShieldingFor" + typeof(TBaseException).FullName;
            }

            List <IExceptionHandler> handlers = wrapExceptionBeforeHandlers?.ToList() ?? new List <IExceptionHandler>();

            WrapHandler wrapHandler = wrapExceptionMessage == null
                                          ? new WrapHandler(
                "Unhandled exception. See inner for details",
                typeof(TBaseException))
                                          : new WrapHandler(wrapExceptionMessage, typeof(TBaseException));

            handlers.Add(wrapHandler);

            List <ExceptionPolicyEntry> policyEntries = additionalPolicyEntries?.ToList()
                                                        ?? new List <ExceptionPolicyEntry>();

            policyEntries.Add(
                new ExceptionPolicyEntry(
                    typeof(TBaseException),
                    PostHandlingAction.NotifyRethrow,
                    Enumerable.Empty <IExceptionHandler>()));

            policyEntries.Add(
                new ExceptionPolicyEntry(
                    typeof(OperationCanceledException),
                    PostHandlingAction.NotifyRethrow,
                    Enumerable.Empty <IExceptionHandler>()));

            policyEntries.Add(
                new ExceptionPolicyEntry(typeof(Exception), PostHandlingAction.ThrowNewException, handlers));

            ExceptionPolicyDefinition result = new ExceptionPolicyDefinition(policyDefinitionName, policyEntries);

            return(result);
        }
示例#12
0
 protected override void Act()
 {
     this.policy = this.factory.CreatePolicy("existing");
 }
 protected override void Act()
 {
     this.policy = this.factory.CreatePolicy("existing");
 }
        public void WhenRegisteredAgainstExceptionType_OnlyCorrectHandlerIsCalled()
        {

            ExceptionPolicyDefinition policyImpl = new ExceptionPolicyDefinition("APolicyName",
                                                                     new List<ExceptionPolicyEntry>() {entry1, entry2});

            policyImpl.HandleException(new ArgumentNullException("TestException"));
            Assert.AreEqual(1, handler1.instanceHandledExceptionCount);
            Assert.AreEqual(0, handler2.instanceHandledExceptionCount);
        }