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 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 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 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 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);
            }
        }
Example #6
0
 /// <summary>
 /// Instantiates a new instance of the 
 /// <see cref="ExceptionPolicyEntry"/> class.
 /// </summary>
 internal ExceptionPolicyEntry(string policyName, ExceptionTypeData typeData, ConfigurationContext context)
 {
     this.policyName = policyName;
     this.postHandlingAction = typeData.PostHandlingAction;
     this.typeDataName = typeData.Name;
     this.factory = new ExceptionHandlerFactory(context);
 }
Example #7
0
 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;
 }
Example #8
0
 /// <summary>
 /// <para>Adds an <see cref="ExceptionTypeData"/> into the collection.</para>
 /// </summary>
 /// <param name="exceptionTypeData">
 /// <para>The <see cref="ExceptionTypeData"/> 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="ExceptionTypeData.Name"/>, it will be replaced with the new reference.</para>
 /// </remarks>
 /// <exception cref="ArgumentNullException">
 /// <para><paramref name="exceptionTypeData"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// <para><seealso cref="ExceptionTypeData.Name"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </exception>
 public void Add(ExceptionTypeData exceptionTypeData)
 {
     if (exceptionTypeData == null)
     {
         throw new ArgumentNullException("configurationSection");
     }
     if (exceptionTypeData.Name == null)
     {
         throw new InvalidOperationException(SR.ExceptionExceptionTypeDataName);
     }
     BaseAdd(exceptionTypeData.Name, exceptionTypeData);
 }
        public void ExceptionTypeNameBecomesName()
        {
            Type exceptionType = typeof(AppDomainUnloadedException);
            string name = "some name";

            ExceptionTypeData data = new ExceptionTypeData();
            data.Type = exceptionType;
            data.Name = name;

            ExceptionTypeNode node = new ExceptionTypeNode(data);
            Assert.AreEqual(exceptionType.Name, node.Name);
        }
        public void ExceptionTypeDataTest()
        {
            Type exceptionType = typeof(AppDomainUnloadedException);
            PostHandlingAction action = PostHandlingAction.None;

            ExceptionTypeData data = new ExceptionTypeData();
            data.Type = exceptionType;
            data.PostHandlingAction = action;

            ExceptionTypeNode node = new ExceptionTypeNode(data);
            Assert.AreEqual(exceptionType.Name, node.Name);
            Assert.AreEqual(action, node.PostHandlingAction);
        }
        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);
        }
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="ExceptionPolicyEntry"/> based on an instance of <see cref="ExceptionTypeData"/>.
        /// </summary>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of <see cref="ExceptionPolicyEntry"/>.</returns>
		public ExceptionPolicyEntry Create(IBuilderContext context, ExceptionTypeData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
		{
			List<IExceptionHandler> handlers = new List<IExceptionHandler>();
			foreach (ExceptionHandlerData handlerData in objectConfiguration.ExceptionHandlers)
			{
				IExceptionHandler handler
					= ExceptionHandlerCustomFactory.Instance.Create(context, handlerData, configurationSource, reflectionCache);
				handlers.Add(handler);
			}

			ExceptionPolicyEntry createdObject
				= new ExceptionPolicyEntry(
					objectConfiguration.PostHandlingAction,
					handlers);

			return createdObject;
		}
		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);
		}
        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);
            }
        }
Example #17
0
 private Type GetExceptionType(ExceptionTypeData typeData, string policyName)
 {
     try
     {
         return Type.GetType(typeData.TypeName, true);
     }
     catch (TypeLoadException e)
     {
         ExceptionUtility.LogHandlingException(policyName, null, null, e);
         ExceptionNotHandledEvent.Fire();
         throw new ExceptionHandlingException(SR.ExceptionUnknownExceptionTypeInConfiguration(typeData.TypeName), e);
     }
 }
 /// <summary>
 /// <para>Adds an <see cref="ExceptionTypeData"/> into the collection.</para>
 /// </summary>
 /// <param name="exceptionTypeData">
 /// <para>The <see cref="ExceptionTypeData"/> 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="ExceptionTypeData.Name"/>, it will be replaced with the new reference.</para>
 /// </remarks>
 /// <exception cref="ArgumentNullException">
 /// <para><paramref name="exceptionTypeData"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// <para><seealso cref="ExceptionTypeData.Name"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</para>
 /// </exception>
 public void Add(ExceptionTypeData exceptionTypeData)
 {
     if (exceptionTypeData == null)
     {
         throw new ArgumentNullException("configurationSection");
     }
     if (exceptionTypeData.Name == null)
     {
         throw new InvalidOperationException(SR.ExceptionExceptionTypeDataName);
     }
     BaseAdd(exceptionTypeData.Name, exceptionTypeData);
 }
        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()));
            }
        }
        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);
                }
                
            }
        }
        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;
            }
        }