public void CanSerializeAndDeserializeSettings()
        {
            const string policyName = "policyName";
            const string typeName = "typeName";
            const string handlerName = "handlerName";
            const string handler1Name = "handler1Name";

            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
            ExceptionPolicyData policyData = new ExceptionPolicyData(policyName);
            ExceptionTypeData typeData = new ExceptionTypeData(typeName, typeof(Exception), PostHandlingAction.None);
            typeData.ExceptionHandlers.Add(new WrapHandlerData(handlerName, "foo", typeof(InvalidCastException)));
            typeData.ExceptionHandlers.Add(new ReplaceHandlerData(handler1Name, "foo", typeof(InvalidCastException)));
            policyData.ExceptionTypes.Add(typeData);
            settings.ExceptionPolicies.Add(policyData);

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = fileName;
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            config.Sections.Add(ExceptionHandlingSettings.SectionName, settings);
            config.Save();

            config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            settings = (ExceptionHandlingSettings)config.Sections[ExceptionHandlingSettings.SectionName];

            Assert.AreEqual(1, settings.ExceptionPolicies.Count);
            Assert.AreEqual(1, settings.ExceptionPolicies.Get(0).ExceptionTypes.Count);
            Assert.AreEqual(2, settings.ExceptionPolicies.Get(0).ExceptionTypes.Get(0).ExceptionHandlers.Count);
        }
        public void CanGetExceptionMessageFromResource()
        {
            const string resourceName = "ExceptionMessage";

            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            WrapHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "wrapped", typeof(ArgumentException).AssemblyQualifiedName);
            exceptionHandlerData.ExceptionMessageResourceName = resourceName;
            exceptionHandlerData.ExceptionMessageResourceType = typeof(Resources).AssemblyQualifiedName;

            string resourceValue = Resources.ExceptionMessage;

            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

            Exception originalException = new Exception("to be wrapped");
            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(resourceValue, e.Message);
                Assert.AreSame(originalException, e.InnerException);
            }
        }
        public void CanCreatePoliciesForHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            FaultContractExceptionHandlerData exceptionHandlerData = new FaultContractExceptionHandlerData("handler1",
                typeof(MockFaultContract).AssemblyQualifiedName);
            exceptionHandlerData.ExceptionMessage = "fault message";
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Message", "{Message}"));
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Data", "{Data}"));
            exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("SomeNumber", "{OffendingNumber}"));
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            var manager = settings.BuildExceptionManager();

            NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 12341234123412);
            originalException.Data.Add("someKey", "someValue");
            try
            {
                manager.HandleException(originalException, "policy");
                Assert.Fail("a new exception should have been thrown");
            }
            catch (FaultContractWrapperException e)
            {
                MockFaultContract fault = (MockFaultContract)e.FaultContract;
                Assert.AreEqual(originalException.Message, fault.Message);
                Assert.AreEqual(originalException.Data.Count, fault.Data.Count);
                Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]);
                Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber);
            }
        }
        public void CanCreatePolicyWithEmptyTypes()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData1
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData1);
            ExceptionTypeData exceptionTypeData2
                = new ExceptionTypeData("type2", typeof(ArgumentException), PostHandlingAction.NotifyRethrow);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData2);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

                Assert.IsNotNull(policy);
                Assert.IsNotNull(policy.GetPolicyEntry(typeof(Exception)));
                Assert.IsNotNull(policy.GetPolicyEntry(typeof(ArgumentException)));
                Assert.IsNull(policy.GetPolicyEntry(typeof(InvalidOperationException)));

                // little detail is exposed for policy entries - need to probe its behavior for a proper assert
                Assert.IsFalse(policy.GetPolicyEntry(typeof(Exception)).Handle(new Exception()));
                Assert.IsTrue(policy.GetPolicyEntry(typeof(ArgumentException)).Handle(new ArgumentException()));
                
            }
        }
        public void CanCreatePoliciesForHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            ExceptionHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "wrapped", typeof(ArgumentException).AssemblyQualifiedName);
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

            Exception originalException = new Exception("to be wrapped");
            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual("wrapped", e.Message);
                Assert.AreSame(originalException, e.InnerException);
            }
        }
        public void CanCreatePoliciesForHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            CustomHandlerData exceptionHandlerData = new CustomHandlerData("handler1", typeof(TestCustomExceptionHandler));
            exceptionHandlerData.Attributes.Add(TestCustomExceptionHandler.AttributeKey, "custom handler");
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

            Exception originalException = new Exception("to be replaced");
            try
            {
                policy.HandleException(originalException);
                Assert.Fail("a new exception should have been thrown");
            }
            catch (Exception e)
            {
                Assert.AreEqual("custom handler", e.Message);
                Assert.AreSame(originalException, e.InnerException);
                Assert.AreSame(originalException, TestCustomExceptionHandler.handledException);
            }
        }
        public void CanCreateEmptyPolicy()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");
                Assert.IsNotNull(policy);
            }
        }
 private static ExceptionHandlingSettings CreateSettings()
 {
     LoggingExceptionHandlerData logData = new LoggingExceptionHandlerData("test", "cat1", 1, System.Diagnostics.TraceEventType.Error, "title", typeof(XmlExceptionFormatter), 4);
     ExceptionTypeData typeData = new ExceptionTypeData("test", typeof(Exception), PostHandlingAction.None);
     typeData.ExceptionHandlers.Add(logData);
     ExceptionPolicyData policy = new ExceptionPolicyData("test");
     policy.ExceptionTypes.Add(typeData);
     ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
     settings.ExceptionPolicies.Add(policy);
     return settings;
 }
 static Dictionary<Type, ExceptionPolicyEntry> GetEntries(ExceptionPolicyData policyData)
 {
     Dictionary<Type, ExceptionPolicyEntry> entries = new Dictionary<Type, ExceptionPolicyEntry>();
     List<IExceptionHandler> handlers = new List<IExceptionHandler>();
     handlers.Add(new MockThrowingExceptionHandler());
     handlers.Add(new MockExceptionHandler(new NameValueCollection()));
     foreach (ExceptionTypeData typeData in policyData.ExceptionTypes)
     {
         entries.Add(typeof(ArgumentException), new ExceptionPolicyEntry(typeof(ArgumentException), 
                                                                         typeData.PostHandlingAction,
                                                                         handlers));
     }
     return entries;
 }
        static ExceptionHandlingSettings CreateSettings()
        {
            FaultContractExceptionHandlerData logData = new FaultContractExceptionHandlerData("test");
            logData.FaultContractType = typeof(object).AssemblyQualifiedName;
            logData.ExceptionMessage = "my exception message";
            logData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("property1", "source1"));
            logData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("property2", "source2"));

            ExceptionTypeData typeData = new ExceptionTypeData("test", typeof(Exception), PostHandlingAction.None);
            typeData.ExceptionHandlers.Add(logData);
            ExceptionPolicyData policy = new ExceptionPolicyData("test");
            policy.ExceptionTypes.Add(typeData);
            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
            settings.ExceptionPolicies.Add(policy);
            return settings;
        }
        protected override void Arrange()
        {
            updatableConfigurationSource = new ConfigurationSourceUpdatable();

            exceptionPolicy1 = new ExceptionPolicyData("default");
            exceptionTypeData = new ExceptionTypeData("Exception", "System.Exception", PostHandlingAction.ThrowNewException);
            exceptionPolicy1.ExceptionTypes.Add(exceptionTypeData);

            ehabSettings = new ExceptionHandlingSettings();
            ehabSettings.ExceptionPolicies.Add(exceptionPolicy1);

            updatableConfigurationSource.Add(ExceptionHandlingSettings.SectionName, ehabSettings);

            container = new UnityContainer();
            containerConfigurator = new UnityContainerConfigurator(container);
            EnterpriseLibraryContainer.ConfigureContainer(containerConfigurator, updatableConfigurationSource);
        }
		public void CanCreatePoliciesForHandler()
		{
			ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
			settings.ExceptionPolicies.Add(exceptionPolicyData);

			ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
			exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

			FaultContractExceptionHandlerData exceptionHandlerData = new FaultContractExceptionHandlerData("handler1", 
				typeof(MockFaultContract).AssemblyQualifiedName);
			exceptionHandlerData.ExceptionMessage = "fault message";
			exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Message", "{Message}"));
			exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("Data", "{Data}"));
			exceptionHandlerData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("SomeNumber", "{OffendingNumber}"));
			exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

		    using(var container = new UnityContainer()
		        .AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
		    {
		        ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

		        NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 12341234123412);
		        originalException.Data.Add("someKey", "someValue");
		        try
		        {
		            policy.HandleException(originalException);
		            Assert.Fail("a new exception should have been thrown");
		        }
		        catch (FaultContractWrapperException e)
		        {
		            MockFaultContract fault = (MockFaultContract)e.FaultContract;
		            Assert.AreEqual(originalException.Message, fault.Message);
		            Assert.AreEqual(originalException.Data.Count, fault.Data.Count);
		            Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]);
		            Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber);
		        }
		    }
		}
		public void CanDeserializeSerializedConfiguration()
		{
			ExceptionHandlingSettings settings = new ExceptionHandlingSettings();

			ExceptionTypeData typeData11 = new ExceptionTypeData(typeName11, typeof(ArgumentNullException), PostHandlingAction.None);
			typeData11.ExceptionHandlers.Add(new LoggingExceptionHandlerData(handlerName111, handlerCategory111, 100, TraceEventType.Information, handlerMessage111, typeof(ExceptionFormatter), 101));

			ExceptionPolicyData policyData1 = new ExceptionPolicyData(policyName1);
			policyData1.ExceptionTypes.Add(typeData11);

			settings.ExceptionPolicies.Add(policyData1);

			IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>();
			sections[ExceptionHandlingSettings.SectionName] = settings;
			IConfigurationSource configurationSource
				= ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

			ExceptionHandlingSettings roSettigs = (ExceptionHandlingSettings)configurationSource.GetSection(ExceptionHandlingSettings.SectionName);

			Assert.IsNotNull(roSettigs);
			Assert.AreEqual(1, roSettigs.ExceptionPolicies.Count);

			Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1));
			Assert.AreEqual(1, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Count);

			Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11));
			Assert.AreSame(typeof(ArgumentNullException), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).Type);
			Assert.AreEqual(1, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Count);
			Assert.AreSame(typeof(LoggingExceptionHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111).GetType());
			Assert.AreEqual(100, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).EventId);
			Assert.AreEqual(typeof(ExceptionFormatter), ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).FormatterType);
			Assert.AreEqual(handlerCategory111, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).LogCategory);
			Assert.AreEqual(101, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).Priority);
			Assert.AreEqual(TraceEventType.Information, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).Severity);
			Assert.AreEqual(handlerMessage111, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).Title);
		}
 /// <summary>
 /// <para>Adds an <see cref="ExceptionPolicyData"/> into the collection.</para>
 /// </summary>
 /// <param name="providerData">
 /// <para>The <see cref="ExceptionPolicyData"/> to add. The value can not be a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </param>
 /// <remarks>
 /// <para>If a reference already exists in the collection by <seealso cref="ProviderData.Name"/>, it will be replaced with the new reference.</para>
 /// </remarks>
 /// <exception cref="System.ArgumentNullException">
 /// <para><paramref name="providerData"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// <para>- or -</para>
 /// <para><seealso cref="ProviderData.Name"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </exception>
 public void Add(ExceptionPolicyData providerData)
 {
     AddProvider(providerData);
 }
        public void TestCallHandlerCustomFactory()
        {
            PolicyInjectionSettings settings = new PolicyInjectionSettings();
            PolicyData policyData = new PolicyData("policy");
            ExceptionCallHandlerData data = new ExceptionCallHandlerData("exceptionhandler", "Swallow Exceptions");
            data.Order = 5;
            policyData.Handlers.Add(data);
            policyData.MatchingRules.Add(new CustomMatchingRuleData("matchesEverything", typeof(AlwaysMatchingRule)));
            settings.Policies.Add(policyData);

            ExceptionHandlingSettings ehabSettings = new ExceptionHandlingSettings();
            ExceptionPolicyData swallowExceptions = new ExceptionPolicyData("Swallow Exceptions");
            swallowExceptions.ExceptionTypes.Add(new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.None));
            ehabSettings.ExceptionPolicies.Add(swallowExceptions);

            DictionaryConfigurationSource dictConfigurationSource = new DictionaryConfigurationSource();
            dictConfigurationSource.Add(PolicyInjectionSettings.SectionName, settings);

            IUnityContainer container = new UnityContainer().AddNewExtension<Interception>();
            settings.ConfigureContainer(container, dictConfigurationSource);
            new UnityContainerConfigurator(container).RegisterAll(dictConfigurationSource, ehabSettings);

            InjectionFriendlyRuleDrivenPolicy policy = container.Resolve<InjectionFriendlyRuleDrivenPolicy>("policy");

            ICallHandler handler
                = (policy.GetHandlersFor(new MethodImplementationInfo(null, (MethodInfo)MethodBase.GetCurrentMethod()), container)).ElementAt(0);

            Assert.IsNotNull(handler);
            Assert.AreEqual(handler.Order, data.Order);
        }
        public void CanCreateExceptionHandlerFromConfiguration()
        {
            PolicyInjectionSettings settings = new PolicyInjectionSettings();
            PolicyData policyData = new PolicyData("policy");
            policyData.Handlers.Add(new ExceptionCallHandlerData("exceptionhandler", "Swallow Exceptions"));
            policyData.MatchingRules.Add(new CustomMatchingRuleData("matchesEverything", typeof(AlwaysMatchingRule)));
            settings.Policies.Add(policyData);

            ExceptionHandlingSettings ehabSettings = new ExceptionHandlingSettings();
            ExceptionPolicyData swallowExceptions = new ExceptionPolicyData("Swallow Exceptions");
            swallowExceptions.ExceptionTypes.Add(new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.None));
            ehabSettings.ExceptionPolicies.Add(swallowExceptions);
            DictionaryConfigurationSource dictConfigurationSource = new DictionaryConfigurationSource();
            dictConfigurationSource.Add(PolicyInjectionSettings.SectionName, settings);
            dictConfigurationSource.Add(ExceptionHandlingSettings.SectionName, ehabSettings);

            using (PolicyInjector injector = new PolicyInjector(dictConfigurationSource))
            {
                TargetType target = injector.Create<TargetType>();
                target.WillThrowException();
            }
        }
        public void PoliciesForExceptionPoliciesCanHandleRepeatedTypes()
        {
            ExceptionPolicyData exceptionPolicy1Data = new ExceptionPolicyData("policy1");
            settings.ExceptionPolicies.Add(exceptionPolicy1Data);

            ExceptionTypeData exceptionTypeData11
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);
            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData11);
            ExceptionTypeData exceptionTypeData12
                = new ExceptionTypeData("type2", typeof(ArgumentException), PostHandlingAction.NotifyRethrow);
            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData12);

            ExceptionPolicyData exceptionPolicy2Data = new ExceptionPolicyData("policy2");
            settings.ExceptionPolicies.Add(exceptionPolicy2Data);

            ExceptionTypeData exceptionTypeData21
                = new ExceptionTypeData("type1", typeof(InvalidOperationException), PostHandlingAction.NotifyRethrow);
            exceptionPolicy2Data.ExceptionTypes.Add(exceptionTypeData21);
            ExceptionTypeData exceptionTypeData22
                = new ExceptionTypeData("type2", typeof(ArgumentNullException), PostHandlingAction.NotifyRethrow);
            exceptionPolicy2Data.ExceptionTypes.Add(exceptionTypeData22);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy1");

                Assert.IsNotNull(policy);
                Assert.IsNotNull(policy.GetPolicyEntry(typeof(Exception)));
                Assert.IsNotNull(policy.GetPolicyEntry(typeof(ArgumentException)));
                Assert.IsNull(policy.GetPolicyEntry(typeof(InvalidOperationException)));

                // this breaks if the type1 for policy2 overrides the policy for policy1's type
                Assert.IsFalse(policy.GetPolicyEntry(typeof(Exception)).Handle(new Exception()));
                Assert.IsTrue(policy.GetPolicyEntry(typeof(ArgumentException)).Handle(new ArgumentException()));
            }
        }
Exemple #18
0
 /// <summary>
 /// <para>Adds an <see cref="ExceptionPolicyData"/> into the collection.</para>
 /// </summary>
 /// <param name="providerData">
 /// <para>The <see cref="ExceptionPolicyData"/> to add. The value can not be a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </param>
 /// <remarks>
 /// <para>If a reference already exists in the collection by <seealso cref="ProviderData.Name"/>, it will be replaced with the new reference.</para>
 /// </remarks>
 /// <exception cref="System.ArgumentNullException">
 /// <para><paramref name="providerData"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// <para>- or -</para>
 /// <para><seealso cref="ProviderData.Name"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </exception>
 public void Add(ExceptionPolicyData providerData)
 {
     AddProvider(providerData);
 }
        public void PoliciesForExceptionManagerAreCreated()
        {
            ExceptionPolicyData exceptionPolicy1Data = new ExceptionPolicyData("policy1");
            settings.ExceptionPolicies.Add(exceptionPolicy1Data);

            ExceptionTypeData exceptionTypeData11
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData11);
            CustomHandlerData exceptionHandlerData11 = new CustomHandlerData("handler1", typeof(TestCustomExceptionHandler));
            exceptionHandlerData11.Attributes.Add(TestCustomExceptionHandler.AttributeKey, "handler1");
            exceptionTypeData11.ExceptionHandlers.Add(exceptionHandlerData11);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionManager manager = container.Resolve<ExceptionManager>();
                Assert.IsNotNull(manager);

                Exception exceptionToThrow = new Exception("some message");

                try
                {
                    manager.Process(() => { throw exceptionToThrow; }, "policy1");
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (Exception e)
                {
                    Assert.AreSame(exceptionToThrow, e.InnerException);
                    Assert.AreEqual("handler1", e.Message);
                }
            }
        }
        public void PoliciesForExceptionPoliciesCanHandleRepeatedTypesWithRepeatedHandlers()
        {
            ExceptionPolicyData exceptionPolicy1Data = new ExceptionPolicyData("policy1");
            settings.ExceptionPolicies.Add(exceptionPolicy1Data);

            ExceptionTypeData exceptionTypeData11
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicy1Data.ExceptionTypes.Add(exceptionTypeData11);
            ExceptionHandlerData exceptionHandlerData11 = new WrapHandlerData("handler1", "message1",
                typeof(ArgumentException).AssemblyQualifiedName);
            exceptionTypeData11.ExceptionHandlers.Add(exceptionHandlerData11);

            ExceptionPolicyData exceptionPolicy2Data = new ExceptionPolicyData("policy2");
            settings.ExceptionPolicies.Add(exceptionPolicy2Data);

            ExceptionTypeData exceptionTypeData21
                = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicy2Data.ExceptionTypes.Add(exceptionTypeData21);
            ExceptionHandlerData exceptionHandlerData21 = new WrapHandlerData("handler1", "message2",
                typeof(ArgumentException).AssemblyQualifiedName);
            exceptionTypeData21.ExceptionHandlers.Add(exceptionHandlerData21);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy1");

                try
                {
                    policy.HandleException(new Exception("to be wrapped"));
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual("message1", e.Message, "Policy 1 is using the handler definitions from policy 2");
                }
                
            }
        }
        public void WrapperHandlerCanGetExceptionMessageFromResource()
        {
            const string resourceName = "ExceptionMessage";

            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            WrapHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "message",
                typeof(ArgumentException).AssemblyQualifiedName);

            exceptionHandlerData.ExceptionMessageResourceName = resourceName;
            exceptionHandlerData.ExceptionMessageResourceType = typeof(Resources).AssemblyQualifiedName;

            string resourceValue = Resources.ExceptionMessage;

            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

                try
                {
                    policy.HandleException(new Exception("to be wrapped"));
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual(resourceValue, e.Message);
                }
                
            }
        }
        public void CanCreatePolicyWithSimpleExceptionHandler()
        {
            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            ExceptionHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "message",
                typeof(ArgumentException).AssemblyQualifiedName);
            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            using(var container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension(configurationSource)))
            {
                ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

                try
                {
                    policy.HandleException(new Exception("to be wrapped"));
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual("message", e.Message);
                }
                
            }
        }
Exemple #23
0
        private void AddPolicyEntriesToCache(ExceptionPolicyData policyData, ConfigurationContext context)
        {
            this.policyEntries = new Hashtable(policyData.ExceptionTypes.Count);

            foreach (ExceptionTypeData typeData in policyData.ExceptionTypes)
            {
                Type exceptionType = GetExceptionType(typeData, policyData.Name);
                ExceptionPolicyEntry exceptionEntry = new ExceptionPolicyEntry(policyData.Name, typeData, context);
                this.policyEntries.Add(exceptionType, exceptionEntry);
            }
        }
        public void CanGetExceptionMessageFromResourceForDifferentLocales()
        {
            const string resourceName = "ExceptionMessage";

            ExceptionPolicyData exceptionPolicyData = new ExceptionPolicyData("policy");
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            ExceptionTypeData exceptionTypeData = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.ThrowNewException);
            exceptionPolicyData.ExceptionTypes.Add(exceptionTypeData);

            WrapHandlerData exceptionHandlerData = new WrapHandlerData("handler1", "wrapped", typeof(ArgumentException).AssemblyQualifiedName);
            exceptionHandlerData.ExceptionMessageResourceName = resourceName;
            exceptionHandlerData.ExceptionMessageResourceType = typeof(Resources).AssemblyQualifiedName;

            exceptionTypeData.ExceptionHandlers.Add(exceptionHandlerData);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            ExceptionPolicyImpl policy = container.Resolve<ExceptionPolicyImpl>("policy");

            Exception originalException = new Exception("to be wrapped");
            string enMessage = null;
            string esMessage = null;
            CultureInfo currentUICulture = CultureInfo.CurrentUICulture;

            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

                try
                {
                    policy.HandleException(originalException);
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual(Resources.ExceptionMessage, e.Message);
                    Assert.AreSame(originalException, e.InnerException);
                    enMessage = e.Message;
                }

                Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
                try
                {
                    policy.HandleException(originalException);
                    Assert.Fail("a new exception should have been thrown");
                }
                catch (ArgumentException e)
                {
                    Assert.AreEqual(Resources.ExceptionMessage, e.Message);
                    Assert.AreSame(originalException, e.InnerException);
                    esMessage = e.Message;
                }

                Assert.AreNotEqual(enMessage, esMessage);
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = currentUICulture;
            }
        }