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);
            }
        }
コード例 #2
0
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="WrapHandler"/> based on an instance of <see cref="WrapHandlerData"/>.
        /// </summary>
        /// <seealso cref="ExceptionHandlerCustomFactory"/>
        /// <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. Must be an instance of <see cref="WrapHandlerData"/>.</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="WrapHandler"/>.</returns>
        public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            WrapHandlerData castedObjectConfiguration
                = (WrapHandlerData)objectConfiguration;

            WrapHandler createdObject
                = new WrapHandler(castedObjectConfiguration.ExceptionMessage, castedObjectConfiguration.WrapExceptionType);

            return(createdObject);
        }
        protected override void Arrange()
        {
            var data = new WrapHandlerData
            {
                Name = ExpectedName,
                ExceptionMessage = ExpectedExceptionMessage,
                WrapExceptionTypeName = ExpectedExceptionTypeName
            };

            HandlerData = data;
        }
コード例 #4
0
        public void WrapHandlerNodeDataTest()
        {
            string name = "some name";
            string message = "some message";
            Type wrapExceptionType = typeof(ApplicationException);

            WrapHandlerData wrapHandlerData = new WrapHandlerData();
            wrapHandlerData.Name = name;
            wrapHandlerData.ExceptionMessage = message;
            wrapHandlerData.WrapExceptionType = wrapExceptionType;

            WrapHandlerNode wrapHandlerNode = new WrapHandlerNode(wrapHandlerData);

            WrapHandlerData nodeData = (WrapHandlerData)wrapHandlerNode.ExceptionHandlerData;
            Assert.AreEqual(name, nodeData.Name);
            Assert.AreEqual(message, nodeData.ExceptionMessage);
            Assert.AreEqual(wrapExceptionType, nodeData.WrapExceptionType);
        }
コード例 #5
0
        public void WrapHandlerDataTest()
        {
            string name = "some name";
            string message = "some message";
            Type exceptionType = typeof(AppDomainUnloadedException);
            Type wrapExceptionType = typeof(ApplicationException);

            WrapHandlerData data = new WrapHandlerData();
            data.Name = name;
            data.ExceptionMessage = message;
            data.Type = exceptionType;
            data.WrapExceptionType = wrapExceptionType;

            WrapHandlerNode node = new WrapHandlerNode(data);
            Assert.AreEqual(name, node.Name);
            Assert.AreEqual(message, node.ExceptionMessage);
            Assert.AreEqual(wrapExceptionType, node.WrapExceptionType);
        }
コード例 #6
0
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="WrapHandler"/> based on an instance of <see cref="WrapHandlerData"/>.
        /// </summary>
        /// <seealso cref="ExceptionHandlerCustomFactory"/>
        /// <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. Must be an instance of <see cref="WrapHandlerData"/>.</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="WrapHandler"/>.</returns>
        public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            WrapHandlerData castedObjectConfiguration
                = (WrapHandlerData)objectConfiguration;

            string exceptionMessage = castedObjectConfiguration.ExceptionMessage;

            if (!string.IsNullOrEmpty(castedObjectConfiguration.ExceptionMessageResourceName))
            {
                Type exceptionMessageResourceType = Type.GetType(castedObjectConfiguration.ExceptionMessageResourceType, false);

                if (null != exceptionMessageResourceType)
                {
                    exceptionMessage = ResourceStringLoader.LoadString(exceptionMessageResourceType.FullName,
                                                                       castedObjectConfiguration.ExceptionMessageResourceName,
                                                                       exceptionMessageResourceType.Assembly);
                }
            }

            WrapHandler createdObject
                = new WrapHandler(exceptionMessage, castedObjectConfiguration.WrapExceptionType);

            return(createdObject);
        }
        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);
                }
                
            }
        }
コード例 #10
0
ファイル: WrapHandler.cs プロジェクト: bnantz/NCS-V1-1
        /// <summary>
        /// <para>Initializes the provider with a <see cref="ExceptionHandlingConfigurationView"/>.</para>
        /// </summary>
        /// <param name="configurationView">
        /// <para>A <see cref="ExceptionHandlingConfigurationView"/> object.</para>
        /// </param>
        public override void Initialize(ConfigurationView configurationView)
        {
            ArgumentValidation.CheckForNullReference(configurationView, "configurationView");
            ArgumentValidation.CheckExpectedType(configurationView, typeof(ExceptionHandlingConfigurationView));

            ExceptionHandlingConfigurationView exceptionHandlingConfigurationView = (ExceptionHandlingConfigurationView)configurationView;

            ExceptionHandlerData exceptionHandlerData = exceptionHandlingConfigurationView.GetExceptionHandlerData(CurrentPolicyName, CurrentExceptionTypeName, ConfigurationName);
            ArgumentValidation.CheckExpectedType(exceptionHandlerData, typeof(WrapHandlerData));

            wrapHandlerData = (WrapHandlerData)exceptionHandlerData;
        }
        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 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;
            }
        }