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 ReplaceHandlerData("handler1", "replaced", typeof(ArgumentException).AssemblyQualifiedName); 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 (ArgumentException e) { Assert.AreEqual("replaced", e.Message); Assert.IsNull(e.InnerException); } }
/// <summary> /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. /// Builds a <see cref="ReplaceHandler"/> based on an instance of <see cref="ReplaceHandlerData"/>. /// </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="ReplaceHandlerData"/>.</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="ReplaceHandler"/>.</returns> public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { ReplaceHandlerData castedObjectConfiguration = (ReplaceHandlerData)objectConfiguration; ReplaceHandler createdObject = new ReplaceHandler(castedObjectConfiguration.ExceptionMessage, castedObjectConfiguration.ReplaceExceptionType); return(createdObject); }
public void ReplaceHandlerDataTest() { string name = "some name"; string message = "some message"; Type replaceExceptionType = typeof(ApplicationException); ReplaceHandlerData data = new ReplaceHandlerData(); data.Name = name; data.ExceptionMessage = message; data.ReplaceExceptionType = replaceExceptionType; ReplaceHandlerNode node = new ReplaceHandlerNode(data); Assert.AreEqual(name, node.Name); Assert.AreEqual(message, node.ExceptionMessage); Assert.AreEqual(replaceExceptionType, node.ReplaceExceptionType); }
/// <summary> /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. /// Builds a <see cref="ReplaceHandler"/> based on an instance of <see cref="ReplaceHandlerData"/>. /// </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="ReplaceHandlerData"/>.</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="ReplaceHandler"/>.</returns> public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { ReplaceHandlerData castedObjectConfiguration = (ReplaceHandlerData)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); } } ReplaceHandler createdObject = new ReplaceHandler(exceptionMessage, castedObjectConfiguration.ReplaceExceptionType); return(createdObject); }
/// <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(ReplaceHandlerData)); replaceHandlerData = (ReplaceHandlerData)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); ReplaceHandlerData exceptionHandlerData = new ReplaceHandlerData("handler1", "replaced", 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 replaced"); try { policy.HandleException(originalException); Assert.Fail("a new exception should have been thrown"); } catch (ArgumentException e) { Assert.AreEqual(resourceValue, e.Message); Assert.IsNull(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); ReplaceHandlerData exceptionHandlerData = new ReplaceHandlerData("handler1", "replaced", 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 replaced"); 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.IsNull(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.IsNull(e.InnerException); esMessage = e.Message; } Assert.AreNotEqual(enMessage, esMessage); } finally { Thread.CurrentThread.CurrentUICulture = currentUICulture; } }