예제 #1
0
        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 static ErrorType GetErrorType(string exceptionType, string opCoCode, string policyName)
        {
            ErrorType errorType = null;

            //read the config details from file or other source (db) to find the error handling section

            //find all the exception policies
            NamedElementCollection <ExceptionPolicyData> policies =
                ((ExceptionHandlingSettings)ConfigurationSourceFactory.Create().GetSection("exceptionHandling")).ExceptionPolicies;

            //find just the one specified
            if (policies != null)
            {
                ExceptionPolicyData specifiedPolicy = policies.Get(policyName);

                if (specifiedPolicy != null)
                {
                    specifiedPolicy.ExceptionTypes.ForEach(delegate(ExceptionTypeData currentExceptionType)
                    {
                        if (currentExceptionType.Type.ToString() == exceptionType)
                        {
                            errorType = PopulateErrorType(specifiedPolicy.Name, opCoCode, currentExceptionType);
                        }
                    }

                                                           );
                }
            }

            return(errorType);
        }
예제 #3
0
        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);

            ExceptionPolicyData swallowExceptions = new ExceptionPolicyData("Swallow Exceptions");

            swallowExceptions.ExceptionTypes.Add(new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.None));

            DictionaryConfigurationSource dictConfigurationSource = new DictionaryConfigurationSource();

            IUnityContainer container = new UnityContainer().AddNewExtension <Interception>();

            settings.ConfigureContainer(container);
            container.RegisterInstance("Swallow Exceptions", swallowExceptions.BuildExceptionPolicy());


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

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

            Assert.IsNotNull(handler);
            Assert.AreEqual(handler.Order, data.Order);
        }
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="ExceptionPolicyImpl"/> based on an instance of <see cref="ExceptionPolicyData"/>.
        /// </summary>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="name">The name of the <see cref="ExceptionPolicyImpl"/> that should be created.</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="ExceptionPolicyImpl"/>.</returns>
        public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            ExceptionPolicyData objectConfiguration = GetConfiguration(name, configurationSource);

            if (objectConfiguration == null)
            {
                throw new ConfigurationErrorsException();                              //TODO: temp fix. this whole class needs to be removed
            }
            Dictionary <Type, ExceptionPolicyEntry> policyEntries = new Dictionary <Type, ExceptionPolicyEntry>();

            foreach (ExceptionTypeData exceptionTypeData in objectConfiguration.ExceptionTypes)
            {
                ExceptionPolicyEntry entry
                    = ExceptionPolicyEntryCustomFactory.Instance.Create(context, exceptionTypeData, configurationSource, reflectionCache);

                policyEntries.Add(exceptionTypeData.Type, entry);
            }

            ExceptionPolicyImpl createdObject
                = new ExceptionPolicyImpl(
                      objectConfiguration.Name,
                      policyEntries);

            return(createdObject);
        }
        public void CanApplyMachinePolicyOverridesToExistingPolicyType()
        {
            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");

            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);

            policy1.ExceptionTypes.Add(exceptionType1);

            MockRegistryKey machinePoliciesKey = new MockRegistryKey(false);

            machineKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, machinePoliciesKey);
            MockRegistryKey machinePolicy1Key = new MockRegistryKey(false);

            machinePoliciesKey.AddSubKey("policy1", machinePolicy1Key);
            MockRegistryKey machinePolicy1TypesKey = new MockRegistryKey(false);

            machinePolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName,
                                        machinePolicy1TypesKey);
            MockRegistryKey machinePolicy1Type1Key = new MockRegistryKey(false);

            machinePolicy1TypesKey.AddSubKey("type1", machinePolicy1Type1Key);
            machinePolicy1Type1Key.AddStringValue(
                ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName,
                PostHandlingAction.NotifyRethrow.ToString());

            provider.OverrideWithGroupPoliciesAndGenerateWmiObjects(section, true, machineKey, null, false, wmiSettings);

            Assert.AreEqual(PostHandlingAction.NotifyRethrow, exceptionType1.PostHandlingAction);

            Assert.IsTrue(
                MockRegistryKey.CheckAllClosed(machinePoliciesKey, machinePolicy1Key, machinePolicy1TypesKey, machinePolicy1Type1Key));
        }
        public void UserOverridesForMissingPolicyCausesNoProblems()
        {
            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");

            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);

            policy1.ExceptionTypes.Add(exceptionType1);

            MockRegistryKey userPoliciesKey = new MockRegistryKey(false);

            userKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, userPoliciesKey);
            MockRegistryKey userPolicy1Key = new MockRegistryKey(false);

            userPoliciesKey.AddSubKey("policy2", userPolicy1Key);
            MockRegistryKey userPolicy1TypesKey = new MockRegistryKey(false);

            userPolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName, userPolicy1TypesKey);
            MockRegistryKey userPolicy1Type1Key = new MockRegistryKey(false);

            userPolicy1TypesKey.AddSubKey("type1", userPolicy1Type1Key);
            userPolicy1Type1Key.AddStringValue(
                ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName,
                PostHandlingAction.NotifyRethrow.ToString());

            provider.OverrideWithGroupPoliciesAndGenerateWmiObjects(section, true, null, userKey, false, wmiSettings);

            Assert.AreEqual(PostHandlingAction.None, exceptionType1.PostHandlingAction);

            Assert.IsTrue(
                MockRegistryKey.CheckAllClosed(userPoliciesKey, userPolicy1Key, userPolicy1TypesKey, userPolicy1Type1Key));
        }
        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 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);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            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 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);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            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 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);

            container.AddExtension(new ExceptionHandlingBlockExtension());

            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()));
        }
예제 #11
0
        protected override Type GetConfigurationType(string policyName)
        {
            ExceptionHandlingConfigurationView exceptionHandlingConfigurationView = new ExceptionHandlingConfigurationView(ConfigurationContext);
            ExceptionPolicyData policy = exceptionHandlingConfigurationView.GetExceptionPolicyData(policyName);

            return(GetType(policy.TypeName));
        }
        public void CanApplyMachinePolicyOverridesToExistingPolicyType()
        {
            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");
            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType1);

            MockRegistryKey machinePoliciesKey = new MockRegistryKey(false);
            machineKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, machinePoliciesKey);
            MockRegistryKey machinePolicy1Key = new MockRegistryKey(false);
            machinePoliciesKey.AddSubKey("policy1", machinePolicy1Key);
            MockRegistryKey machinePolicy1TypesKey = new MockRegistryKey(false);
            machinePolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName,
                                        machinePolicy1TypesKey);
            MockRegistryKey machinePolicy1Type1Key = new MockRegistryKey(false);
            machinePolicy1TypesKey.AddSubKey("type1", machinePolicy1Type1Key);
            machinePolicy1Type1Key.AddStringValue(
                ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName,
                PostHandlingAction.NotifyRethrow.ToString());

            provider.OverrideWithGroupPolicies(section, true, machineKey, null);

            Assert.AreEqual(PostHandlingAction.NotifyRethrow, exceptionType1.PostHandlingAction);

            Assert.IsTrue(
                MockRegistryKey.CheckAllClosed(machinePoliciesKey, machinePolicy1Key, machinePolicy1TypesKey, machinePolicy1Type1Key));
        }
예제 #13
0
        public void ManageabilityProviderGeneratesProperAdmContentWithRegisteredProviders()
        {
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();

            configurationSource.Add(ExceptionHandlingSettings.SectionName, section);

            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");

            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType11 = new ExceptionTypeData("type11", typeof(Exception), PostHandlingAction.None);

            policy1.ExceptionTypes.Add(exceptionType11);
            ExceptionHandlerData handler11 = new ExceptionHandlerData("handler11", typeof(object));

            exceptionType11.ExceptionHandlers.Add(handler11);

            MockConfigurationElementManageabilityProvider subProvider = new MockConfigurationElementManageabilityProvider(false, true);
            Dictionary <Type, ConfigurationElementManageabilityProvider> subProviders
                = new Dictionary <Type, ConfigurationElementManageabilityProvider>();

            subProviders.Add(typeof(ExceptionHandlerData), subProvider);
            provider = new ConfigurationSectionManageabilityProviderWrapper(new ExceptionHandlingSettingsManageabilityProvider(subProviders));

            MockAdmContentBuilder contentBuilder = new MockAdmContentBuilder();

            provider.InvokeAddAdministrativeTemplateDirectives(contentBuilder, section, configurationSource, "TestApp");

            MockAdmContent            content = contentBuilder.GetMockContent();
            IEnumerator <AdmCategory> categoriesEnumerator = content.Categories.GetEnumerator();

            Assert.IsTrue(categoriesEnumerator.MoveNext());
            Assert.AreEqual(Resources.SectionCategoryName, categoriesEnumerator.Current.Name);
            IEnumerator <AdmCategory> subCategoriesEnumerator = categoriesEnumerator.Current.Categories.GetEnumerator();

            Assert.IsTrue(subCategoriesEnumerator.MoveNext());
            Assert.AreEqual(policy1.Name, subCategoriesEnumerator.Current.Name);
            IEnumerator <AdmPolicy> policyPoliciesEnumerator = subCategoriesEnumerator.Current.Policies.GetEnumerator();

            Assert.IsTrue(policyPoliciesEnumerator.MoveNext());
            Assert.AreEqual(String.Format(Resources.ExceptionTypePolicyNameTemplate, exceptionType11.Name), policyPoliciesEnumerator.Current.Name);
            IEnumerator <AdmPart> partsEnumerator = policyPoliciesEnumerator.Current.Parts.GetEnumerator();

            Assert.IsTrue(partsEnumerator.MoveNext());
            Assert.AreSame(typeof(AdmDropDownListPart), partsEnumerator.Current.GetType());
            Assert.IsNull(partsEnumerator.Current.KeyName);
            Assert.AreEqual(ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName,
                            partsEnumerator.Current.ValueName);
            Assert.IsTrue(partsEnumerator.MoveNext());
            Assert.AreSame(typeof(AdmTextPart), partsEnumerator.Current.GetType());
            Assert.AreEqual(Resources.ExceptionTypeHandlersPartName, partsEnumerator.Current.PartName);
            Assert.IsTrue(partsEnumerator.MoveNext());
            Assert.AreSame(typeof(AdmTextPart), partsEnumerator.Current.GetType());
            Assert.AreEqual(MockConfigurationElementManageabilityProvider.Part, partsEnumerator.Current.PartName);

            Assert.IsFalse(partsEnumerator.MoveNext());
            Assert.IsFalse(policyPoliciesEnumerator.MoveNext());

            Assert.IsFalse(subCategoriesEnumerator.MoveNext());
            Assert.IsFalse(categoriesEnumerator.MoveNext());
        }
예제 #14
0
        private void Initialize(ConfigurationContext context, string policyName)
        {
            ExceptionHandlingConfigurationView exceptionHandlingConfigurationView = new ExceptionHandlingConfigurationView(context);
            ExceptionPolicyData policyData = exceptionHandlingConfigurationView.GetExceptionPolicyData(policyName);

            this.AddPolicyEntriesToCache(policyData, exceptionHandlingConfigurationView.ConfigurationContext);
        }
예제 #15
0
        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 RegisteredHandlerDataProviderIsCalledWithNoOverrides()
        {
            MockConfigurationElementManageabilityProvider registeredProvider
                = new MockConfigurationElementManageabilityProvider();
            Dictionary <Type, ConfigurationElementManageabilityProvider> subProviders
                = new Dictionary <Type, ConfigurationElementManageabilityProvider>();

            subProviders.Add(typeof(ReplaceHandlerData), registeredProvider);
            provider = new ExceptionHandlingSettingsManageabilityProvider(subProviders);

            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");

            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);

            policy1.ExceptionTypes.Add(exceptionType1);
            ExceptionHandlerData handlerData1 =
                new ReplaceHandlerData("handler1", "msg", typeof(ArgumentException).AssemblyQualifiedName);

            exceptionType1.ExceptionHandlers.Add(handlerData1);

            provider.OverrideWithGroupPoliciesAndGenerateWmiObjects(section, true, machineKey, userKey, false, wmiSettings);

            Assert.IsTrue(registeredProvider.called);
            Assert.AreSame(handlerData1, registeredProvider.LastConfigurationObject);
            Assert.AreEqual(null, registeredProvider.machineKey);
            Assert.AreEqual(null, registeredProvider.userKey);
        }
예제 #17
0
        [Ignore]    // TODO the configurator will not work as currently designed
        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 WmiSettingsForExceptionHandlerAreGeneratedWithCorrectPolicyAndTypeIfWmiIsEnabled()
        {
            Dictionary <Type, ConfigurationElementManageabilityProvider> subProviders
                = new Dictionary <Type, ConfigurationElementManageabilityProvider>();

            subProviders.Add(typeof(ReplaceHandlerData), new ReplaceHandlerDataManageabilityProvider());
            provider = new ExceptionHandlingSettingsManageabilityProvider(subProviders);

            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");

            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);

            policy1.ExceptionTypes.Add(exceptionType1);
            ReplaceHandlerData handler1 =
                new ReplaceHandlerData("handler1", "message", typeof(ArgumentNullException).AssemblyQualifiedName);

            exceptionType1.ExceptionHandlers.Add(handler1);

            provider.OverrideWithGroupPoliciesAndGenerateWmiObjects(section, false, machineKey, userKey, true, wmiSettings);

            Assert.AreEqual(3, wmiSettings.Count);
            IDictionary <String, ExceptionHandlerSetting> handlerSettings =
                SelectWmiSettings <ExceptionHandlerSetting>(wmiSettings);

            Assert.AreEqual(1, handlerSettings.Count);
            Assert.IsTrue(handlerSettings.ContainsKey("handler1"));
            Assert.AreEqual("message", ((ReplaceHandlerSetting)handlerSettings["handler1"]).ExceptionMessage);
            Assert.AreEqual(typeof(ArgumentNullException).AssemblyQualifiedName,
                            ((ReplaceHandlerSetting)handlerSettings["handler1"]).ReplaceExceptionType);
            Assert.AreEqual("policy1", handlerSettings["handler1"].Policy);
            Assert.AreEqual("type1", handlerSettings["handler1"].ExceptionType);
            Assert.AreEqual(0, handlerSettings["handler1"].Order);
        }
예제 #19
0
        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 RuntimeTest()
        {
            ExceptionPolicyData policyData = new ExceptionPolicyData();

            policyData.Name = "Default Policy";

            ExceptionTypeData typeData = new ExceptionTypeData();

            typeData.Name = "ApplicationException";

            CustomHandlerData handlerData = new CustomHandlerData();

            handlerData.Name = "MockExceptionHandler";

            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();

            settings.ExceptionPolicies.Add(policyData);
            //settings.ExceptionPolicies[policyData.Name].ExceptionTypes = new ExceptionTypeDataCollection();
            settings.ExceptionPolicies[policyData.Name].ExceptionTypes.Add(typeData);
            //settings.ExceptionPolicies[policyData.Name].ExceptionTypes[typeData.Name].ExceptionHandlers = new ExceptionHandlerDataCollection();
            settings.ExceptionPolicies[policyData.Name].ExceptionTypes[typeData.Name].ExceptionHandlers.Add(handlerData);

            ExceptionHandlingSettingsNode settingsNode = new ExceptionHandlingSettingsNode(settings);

            HierarchyService.SelectedHierarchy.RootNode.Nodes.Add(settingsNode);
            Assert.AreEqual(policyData.Name, settingsNode.Nodes[0].Name);
            Assert.AreEqual(typeData.Name, settingsNode.Nodes[0].Nodes[0].Name);
            Assert.AreEqual(handlerData.Name, settingsNode.Nodes[0].Nodes[0].Nodes[0].Name);
        }
예제 #21
0
        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);
            container.AddExtension(new ExceptionHandlingBlockExtension());
            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);
            }
        }
예제 #22
0
 /// <summary>
 /// Constructs a new instance of the <see cref="ExceptionPolicyNode"/> object
 /// with the corresponding runtime configuration data.
 /// </summary>
 /// <param name="exceptionPolicyData">The corresponding runtime configuration data.</param>
 public ExceptionPolicyNode(ExceptionPolicyData exceptionPolicyData) : base()
 {
     if (exceptionPolicyData == null)
     {
         throw new ArgumentNullException("exceptionPolicyData");
     }
     this.exceptionPolicyData = exceptionPolicyData;
 }
예제 #23
0
        public void Setup()
        {
            exceptionPolicyData = new ExceptionPolicyData("policy");

            registrations = exceptionPolicyData.GetRegistration(new DictionaryConfigurationSource()).ToList();

            registration = registrations.Where(r => r.ServiceType == typeof(ExceptionPolicyImpl)).ElementAt(0);
        }
예제 #24
0
        public void HandleExceptionWithNullExceptionThrows()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyDefinition policyIml             = new ExceptionPolicyDefinition(policyData.Name, entries);

            policyIml.HandleException(null);
        }
예제 #25
0
        public void GetPolicyByNameFailTest()
        {
            TestConfigurationContext  context    = new TestConfigurationContext();
            ExceptionHandlingSettings settings   = (ExceptionHandlingSettings)context.GetConfiguration(ExceptionHandlingSettings.SectionName);
            ExceptionPolicyData       testPolicy = settings.ExceptionPolicies[BadString];

            Assert.IsNull(testPolicy);
        }
예제 #26
0
        public void HandlerInChainThrowsExceptionWhenProduceError()
        {
            ExceptionPolicyData policyData = PolicyData;
            Dictionary <Type, ExceptionPolicyEntry> entries = GetEntries(policyData);
            ExceptionPolicyDefinition policyIml             = new ExceptionPolicyDefinition(policyData.Name, entries);

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

            Assert.IsTrue(handled);
        }
		private void BuildExceptionPolicyNode(ExceptionHandlingSettingsNode node, ExceptionPolicyData policyData)
		{
			ExceptionPolicyNode policyNode = new ExceptionPolicyNode(policyData);
			node.AddNode(policyNode);
			foreach (ExceptionTypeData exceptionTypeData in policyData.ExceptionTypes)
			{
				BuildExceptionTypeNode(policyNode, exceptionTypeData);
			}			
		}
예제 #29
0
        public void Setup()
        {
            exceptionPolicyData = new ExceptionPolicyData("policy");
            exceptionPolicyData.ExceptionTypes.Add(new ExceptionTypeData("type", typeof(ArgumentException), PostHandlingAction.None));

            registration = exceptionPolicyData.GetRegistration(new DictionaryConfigurationSource())
                           .Where(r => r.ServiceType == typeof(ExceptionPolicyImpl))
                           .ElementAt(0);
        }
예제 #30
0
 /// <summary>
 /// Constructs a new instance of the <see cref="ExceptionPolicyNode"/> object
 /// with the corresponding runtime configuration data.
 /// </summary>
 /// <param name="exceptionPolicyData">The corresponding runtime configuration data.</param>
 public ExceptionPolicyNode(ExceptionPolicyData exceptionPolicyData)
     : base()
 {
     if (exceptionPolicyData == null)
     {
         throw new ArgumentNullException("exceptionPolicyData");
     }
     this.exceptionPolicyData = exceptionPolicyData;
 }
예제 #31
0
        public void CanGetPolicyDataFromConfiguration()
        {
            const string policyName = "Wrap Policy";
            ExceptionHandlingConfigurationView view = new ExceptionHandlingConfigurationView(new SystemConfigurationSource());
            ExceptionPolicyData data = view.GetExceptionPolicyData(policyName);

            Assert.IsNotNull(data);
            Assert.AreEqual(policyName, data.Name);
        }
예제 #32
0
        public void CanDeserializeSerializedConfiguration()
        {
            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();

            ExceptionTypeData typeData11 = new ExceptionTypeData(typeName11, typeof(ArgumentNullException), PostHandlingAction.None);

            typeData11.ExceptionHandlers.Add(new ReplaceHandlerData(handlerName111, handlerMessage111, typeof(ApplicationException)));
            typeData11.ExceptionHandlers.Add(new WrapHandlerData(handlerName112, handlerMessage112, typeof(ApplicationException)));

            ExceptionTypeData typeData12 = new ExceptionTypeData(typeName12, typeof(ArgumentException), PostHandlingAction.NotifyRethrow);

            typeData12.ExceptionHandlers.Add(new CustomHandlerData(handlerName121, typeof(MockExceptionHandler)));

            ExceptionPolicyData policyData1 = new ExceptionPolicyData(policyName1);

            policyData1.ExceptionTypes.Add(typeData11);
            policyData1.ExceptionTypes.Add(typeData12);

            ExceptionPolicyData policyData2 = new ExceptionPolicyData(policyName2);

            settings.ExceptionPolicies.Add(policyData1);
            settings.ExceptionPolicies.Add(policyData2);

            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(2, roSettigs.ExceptionPolicies.Count);

            Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1));
            Assert.AreEqual(2, 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(2, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Count);
            Assert.AreSame(typeof(ReplaceHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111).GetType());
            Assert.AreEqual(handlerMessage111, ((ReplaceHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).ExceptionMessage);
            Assert.AreEqual(typeof(ApplicationException), ((ReplaceHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).ReplaceExceptionType);
            Assert.AreSame(typeof(WrapHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName112).GetType());
            Assert.AreEqual(handlerMessage112, ((WrapHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName112)).ExceptionMessage);
            Assert.AreEqual(typeof(ApplicationException), ((WrapHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName112)).WrapExceptionType);

            Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12));
            Assert.AreSame(typeof(ArgumentException), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).Type);
            Assert.AreEqual(1, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).ExceptionHandlers.Count);
            Assert.AreSame(typeof(CustomHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).ExceptionHandlers.Get(handlerName121).GetType());
            Assert.AreEqual(typeof(MockExceptionHandler), ((CustomHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).ExceptionHandlers.Get(handlerName121)).Type);

            Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName2));
            Assert.AreEqual(0, roSettigs.ExceptionPolicies.Get(policyName2).ExceptionTypes.Count);
        }
 private ExceptionPolicyData CreateExceptionPolicyData(ExceptionPolicyNode policyNode)
 {
     ExceptionPolicyData policyData = new ExceptionPolicyData(policyNode.Name);
     IList<ConfigurationNode> exceptionTypes = hierarchy.FindNodesByType(policyNode, typeof(ExceptionTypeNode));
     foreach (ConfigurationNode exceptionTypeNode in exceptionTypes)
     {
         policyData.ExceptionTypes.Add(CreateExceptionTypeData((ExceptionTypeNode)exceptionTypeNode));
     }
     return policyData;
 }
예제 #34
0
        private void BuildExceptionPolicyNode(ExceptionHandlingSettingsNode node, ExceptionPolicyData policyData)
        {
            ExceptionPolicyNode policyNode = new ExceptionPolicyNode(policyData);

            node.AddNode(policyNode);
            foreach (ExceptionTypeData exceptionTypeData in policyData.ExceptionTypes)
            {
                BuildExceptionTypeNode(policyNode, exceptionTypeData);
            }
        }
        public void CanDeserializeSerializedConfiguration()
        {
            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();

            ExceptionTypeData typeData11 = new ExceptionTypeData(typeName11, typeof(ArgumentNullException), PostHandlingAction.None);
            typeData11.ExceptionHandlers.Add(new ReplaceHandlerData(handlerName111, handlerMessage111, typeof(ApplicationException)));
            typeData11.ExceptionHandlers.Add(new WrapHandlerData(handlerName112, handlerMessage112, typeof(ApplicationException)));

            ExceptionTypeData typeData12 = new ExceptionTypeData(typeName12, typeof(ArgumentException), PostHandlingAction.NotifyRethrow);
            typeData12.ExceptionHandlers.Add(new CustomHandlerData(handlerName121, typeof(MockExceptionHandler)));

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

            ExceptionPolicyData policyData2 = new ExceptionPolicyData(policyName2);

            settings.ExceptionPolicies.Add(policyData1);
            settings.ExceptionPolicies.Add(policyData2);

            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(2, roSettigs.ExceptionPolicies.Count);

            Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1));
            Assert.AreEqual(2, 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(2, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Count);
            Assert.AreSame(typeof(ReplaceHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111).GetType());
            Assert.AreEqual(handlerMessage111, ((ReplaceHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).ExceptionMessage);
            Assert.AreEqual(typeof(ApplicationException), ((ReplaceHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).ReplaceExceptionType);
            Assert.AreSame(typeof(WrapHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName112).GetType());
            Assert.AreEqual(handlerMessage112, ((WrapHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName112)).ExceptionMessage);
            Assert.AreEqual(typeof(ApplicationException), ((WrapHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName112)).WrapExceptionType);

            Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12));
            Assert.AreSame(typeof(ArgumentException), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).Type);
            Assert.AreEqual(1, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).ExceptionHandlers.Count);
            Assert.AreSame(typeof(CustomHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).ExceptionHandlers.Get(handlerName121).GetType());
            Assert.AreEqual(typeof(MockExceptionHandler), ((CustomHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName12).ExceptionHandlers.Get(handlerName121)).Type);

            Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName2));
            Assert.AreEqual(0, roSettigs.ExceptionPolicies.Get(policyName2).ExceptionTypes.Count);
        }
        public void Setup()
        {
            settings = new ExceptionHandlingSettings();
            var exceptionPolicyData = new ExceptionPolicyData("aPolicy");
            var exceptionType = new ExceptionTypeData("ExceptionType", typeof(ArgumentNullException),
                                                      PostHandlingAction.ThrowNewException);
            exceptionType.ExceptionHandlers.Add(
                new WrapHandlerData("aWrapHandler", "exception", typeof(Exception).AssemblyQualifiedName)
                );

            exceptionPolicyData.ExceptionTypes.Add(exceptionType);
            settings.ExceptionPolicies.Add(exceptionPolicyData);
        }
        public void RuntimeTest()
        {
            ExceptionPolicyData policyData = new ExceptionPolicyData();
            policyData.Name = "Default Policy";

            ExceptionTypeData typeData = new ExceptionTypeData();
            typeData.Name = "ApplicationException";

            CustomHandlerData handlerData = new CustomHandlerData();
            handlerData.Name = "MockExceptionHandler";

            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
            settings.ExceptionPolicies.Add(policyData);
            //settings.ExceptionPolicies[policyData.Name].ExceptionTypes = new ExceptionTypeDataCollection();
            settings.ExceptionPolicies[policyData.Name].ExceptionTypes.Add(typeData);
            //settings.ExceptionPolicies[policyData.Name].ExceptionTypes[typeData.Name].ExceptionHandlers = new ExceptionHandlerDataCollection();
            settings.ExceptionPolicies[policyData.Name].ExceptionTypes[typeData.Name].ExceptionHandlers.Add(handlerData);

            ExceptionHandlingSettingsNode settingsNode = new ExceptionHandlingSettingsNode(settings);
            HierarchyService.SelectedHierarchy.RootNode.Nodes.Add(settingsNode);
            Assert.AreEqual(policyData.Name, settingsNode.Nodes[0].Name);
            Assert.AreEqual(typeData.Name, settingsNode.Nodes[0].Nodes[0].Name);
            Assert.AreEqual(handlerData.Name, settingsNode.Nodes[0].Nodes[0].Nodes[0].Name);
        }
        public void UserOverridesForMissingPolicyCausesNoProblems()
        {
            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");
            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType1);

            MockRegistryKey userPoliciesKey = new MockRegistryKey(false);
            userKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, userPoliciesKey);
            MockRegistryKey userPolicy1Key = new MockRegistryKey(false);
            userPoliciesKey.AddSubKey("policy2", userPolicy1Key);
            MockRegistryKey userPolicy1TypesKey = new MockRegistryKey(false);
            userPolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName, userPolicy1TypesKey);
            MockRegistryKey userPolicy1Type1Key = new MockRegistryKey(false);
            userPolicy1TypesKey.AddSubKey("type1", userPolicy1Type1Key);
            userPolicy1Type1Key.AddStringValue(
                ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName,
                PostHandlingAction.NotifyRethrow.ToString());

            provider.OverrideWithGroupPolicies(section, true, null, userKey);

            Assert.AreEqual(PostHandlingAction.None, exceptionType1.PostHandlingAction);

            Assert.IsTrue(
                MockRegistryKey.CheckAllClosed(userPoliciesKey, userPolicy1Key, userPolicy1TypesKey, userPolicy1Type1Key));
        }
        public void Setup()
        {
            settings = new ExceptionHandlingSettings();

            var exceptionPolicyData = new ExceptionPolicyData("aPolicy");
            var exceptionType = new ExceptionTypeData("ExceptionType", typeof(ArgumentNullException),
                                                      PostHandlingAction.None);
            exceptionPolicyData.ExceptionTypes.Add(exceptionType);
            settings.ExceptionPolicies.Add(exceptionPolicyData);

            var exceptionPolicyData2 = new ExceptionPolicyData("anotherPolicy");
            var exceptionType2 = new ExceptionTypeData("ExceptionType", typeof(ArgumentNullException),
                                                       PostHandlingAction.None);
            exceptionPolicyData2.ExceptionTypes.Add(exceptionType2);
            settings.ExceptionPolicies.Add(exceptionPolicyData2);
        }
        public void Setup()
        {
            exceptionPolicyData = new ExceptionPolicyData("policy");
            exceptionPolicyData.ExceptionTypes.Add(new ExceptionTypeData("type", typeof(ArgumentException), PostHandlingAction.None));

            registration = exceptionPolicyData.GetRegistration(new DictionaryConfigurationSource())
                .Where(r => r.ServiceType == typeof(ExceptionPolicyImpl))
                .ElementAt(0);
        }
        public void Setup()
        {
            exceptionPolicyData = new ExceptionPolicyData("policy");

            registrations = exceptionPolicyData.GetRegistration(new DictionaryConfigurationSource()).ToList();

            registration = registrations.Where(r => r.ServiceType == typeof(ExceptionPolicyImpl)).ElementAt(0);
        }
        public void Setup()
        {
            settings = new ExceptionHandlingSettings();

            var exceptionPolicyData1 = new ExceptionPolicyData("policy1");
            settings.ExceptionPolicies.Add(exceptionPolicyData1);
            var exceptionType11 = new ExceptionTypeData("ArgumentNullException", typeof(ArgumentNullException),
                                                        PostHandlingAction.NotifyRethrow);
            exceptionPolicyData1.ExceptionTypes.Add(exceptionType11);
            exceptionType11.ExceptionHandlers.Add(
                new WrapHandlerData("handler1", "message", typeof(Exception).AssemblyQualifiedName));
            var exceptionType12 = new ExceptionTypeData("ArgumentException", typeof(ArgumentException),
                                                        PostHandlingAction.NotifyRethrow);
            exceptionPolicyData1.ExceptionTypes.Add(exceptionType12);
            exceptionType12.ExceptionHandlers.Add(
                new WrapHandlerData("handler1", "message", typeof(Exception).AssemblyQualifiedName));
            exceptionType12.ExceptionHandlers.Add(
                new WrapHandlerData("handler2", "message", typeof(Exception).AssemblyQualifiedName));


            var exceptionPolicyData2 = new ExceptionPolicyData("policy2");
            settings.ExceptionPolicies.Add(exceptionPolicyData2);
            var exceptionType21 = new ExceptionTypeData("ArgumentNullException", typeof(ArgumentNullException),
                                                        PostHandlingAction.NotifyRethrow);
            exceptionPolicyData2.ExceptionTypes.Add(exceptionType21);
            exceptionType21.ExceptionHandlers.Add(
                new WrapHandlerData("handler1", "message", typeof(Exception).AssemblyQualifiedName));
            exceptionType21.ExceptionHandlers.Add(
                new WrapHandlerData("handler3", "message", typeof(Exception).AssemblyQualifiedName));

            this.manager = (ExceptionManager)this.settings.BuildExceptionManager();
        }
        public void Setup()
        {
            settings = new ExceptionHandlingSettings();

            var exceptionPolicyData1 = new ExceptionPolicyData("policy1");
            settings.ExceptionPolicies.Add(exceptionPolicyData1);
            var exceptionType11 = new ExceptionTypeData("ExceptionType1", typeof(ArgumentNullException),
                                                        PostHandlingAction.NotifyRethrow);
            exceptionPolicyData1.ExceptionTypes.Add(exceptionType11);
            exceptionType11.ExceptionHandlers.Add(
                new WrapHandlerData("handler1", "message", typeof(Exception).AssemblyQualifiedName));
            var exceptionType12 = new ExceptionTypeData("ExceptionType2", typeof(ArgumentNullException),
                                                        PostHandlingAction.NotifyRethrow);
            exceptionPolicyData1.ExceptionTypes.Add(exceptionType12);
            exceptionType12.ExceptionHandlers.Add(
                new WrapHandlerData("handler1", "message", typeof(Exception).AssemblyQualifiedName));
            exceptionType12.ExceptionHandlers.Add(
                new WrapHandlerData("handler2", "message", typeof(Exception).AssemblyQualifiedName));


            var exceptionPolicyData2 = new ExceptionPolicyData("policy2");
            settings.ExceptionPolicies.Add(exceptionPolicyData2);
            var exceptionType21 = new ExceptionTypeData("ExceptionType1", typeof(ArgumentNullException),
                                                        PostHandlingAction.NotifyRethrow);
            exceptionPolicyData2.ExceptionTypes.Add(exceptionType21);
            exceptionType21.ExceptionHandlers.Add(
                new WrapHandlerData("handler1", "message", typeof(Exception).AssemblyQualifiedName));
            exceptionType21.ExceptionHandlers.Add(
                new WrapHandlerData("handler3", "message", typeof(Exception).AssemblyQualifiedName));

            registrations = settings.GetRegistrations(null);
        }
        public void ManageabilityProviderGeneratesProperAdmContentWithRegisteredProviders()
        {
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
            configurationSource.Add(ExceptionHandlingSettings.SectionName, section);

            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");
            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType11 = new ExceptionTypeData("type11", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType11);
            ExceptionHandlerData handler11 = new ExceptionHandlerData("handler11", typeof(object));
            exceptionType11.ExceptionHandlers.Add(handler11);

            MockConfigurationElementManageabilityProvider subProvider =
                new MockConfigurationElementManageabilityProvider(false, true);
            Dictionary<Type, ConfigurationElementManageabilityProvider> subProviders
                = new Dictionary<Type, ConfigurationElementManageabilityProvider>();
            subProviders.Add(typeof(ExceptionHandlerData), subProvider);
            provider = new ExceptionHandlingSettingsManageabilityProvider(subProviders);

            MockAdmContentBuilder contentBuilder = new MockAdmContentBuilder();

            provider.AddAdministrativeTemplateDirectives(contentBuilder, section, configurationSource, "TestApp");

            MockAdmContent content = contentBuilder.GetMockContent();
            IEnumerator<AdmCategory> categoriesEnumerator = content.Categories.GetEnumerator();
            Assert.IsTrue(categoriesEnumerator.MoveNext());
            IEnumerator<AdmCategory> subCategoriesEnumerator = categoriesEnumerator.Current.Categories.GetEnumerator();

            Assert.IsTrue(subCategoriesEnumerator.MoveNext());
            Assert.AreEqual(policy1.Name, subCategoriesEnumerator.Current.Name);
            IEnumerator<AdmPolicy> policyPoliciesEnumerator = subCategoriesEnumerator.Current.Policies.GetEnumerator();
            Assert.IsTrue(policyPoliciesEnumerator.MoveNext());
            IEnumerator<AdmPart> partsEnumerator = policyPoliciesEnumerator.Current.Parts.GetEnumerator();
            Assert.IsTrue(partsEnumerator.MoveNext());
            Assert.AreSame(typeof(AdmDropDownListPart), partsEnumerator.Current.GetType());
            Assert.IsNull(partsEnumerator.Current.KeyName);
            Assert.AreEqual(ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName,
                            partsEnumerator.Current.ValueName);
            Assert.IsTrue(partsEnumerator.MoveNext());
            Assert.AreSame(typeof(AdmTextPart), partsEnumerator.Current.GetType());
            Assert.IsTrue(partsEnumerator.MoveNext());
            Assert.AreSame(typeof(AdmTextPart), partsEnumerator.Current.GetType());
            Assert.AreEqual(MockConfigurationElementManageabilityProvider.Part, partsEnumerator.Current.PartName);

            Assert.IsFalse(partsEnumerator.MoveNext());
            Assert.IsFalse(policyPoliciesEnumerator.MoveNext());

            Assert.IsFalse(subCategoriesEnumerator.MoveNext());
            Assert.IsFalse(categoriesEnumerator.MoveNext());
        }
        public void RegisteredHandlerDataProviderIsCalledWithCorrectOverrides()
        {
            MockConfigurationElementManageabilityProvider registeredProvider
                = new MockConfigurationElementManageabilityProvider();
            Dictionary<Type, ConfigurationElementManageabilityProvider> subProviders
                = new Dictionary<Type, ConfigurationElementManageabilityProvider>();
            subProviders.Add(typeof(ReplaceHandlerData), registeredProvider);
            provider = new ExceptionHandlingSettingsManageabilityProvider(subProviders);

            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");
            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType1);
            ExceptionHandlerData handlerData1 =
                new ReplaceHandlerData("handler1", "msg", typeof(ArgumentException).AssemblyQualifiedName);
            exceptionType1.ExceptionHandlers.Add(handlerData1);

            MockRegistryKey machinePoliciesKey = new MockRegistryKey(false);
            machineKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, machinePoliciesKey);
            MockRegistryKey machinePolicy1Key = new MockRegistryKey(false);
            machinePoliciesKey.AddSubKey("policy1", machinePolicy1Key);
            MockRegistryKey machinePolicy1TypesKey = new MockRegistryKey(false);
            machinePolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName,
                                        machinePolicy1TypesKey);
            MockRegistryKey machinePolicy1Type1Key = new MockRegistryKey(false);
            machinePolicy1TypesKey.AddSubKey("type1", machinePolicy1Type1Key);
            MockRegistryKey machineHandlersKey = new MockRegistryKey(false);
            machinePolicy1Type1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypeHandlersPropertyName,
                                             machineHandlersKey);
            machinePolicy1Type1Key.AddEnumValue(
                ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName, PostHandlingAction.None);
            MockRegistryKey machineHandlerKey = new MockRegistryKey(false);
            machineHandlersKey.AddSubKey("handler1", machineHandlerKey);

            MockRegistryKey userPoliciesKey = new MockRegistryKey(false);
            userKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, userPoliciesKey);
            MockRegistryKey userPolicy1Key = new MockRegistryKey(false);
            userPoliciesKey.AddSubKey("policy1", userPolicy1Key);
            MockRegistryKey userPolicy1TypesKey = new MockRegistryKey(false);
            userPolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName, userPolicy1TypesKey);
            MockRegistryKey userPolicy1Type1Key = new MockRegistryKey(false);
            userPolicy1TypesKey.AddSubKey("type1", userPolicy1Type1Key);
            MockRegistryKey userHandlersKey = new MockRegistryKey(false);
            userPolicy1Type1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypeHandlersPropertyName,
                                          userHandlersKey);
            userPolicy1Type1Key.AddEnumValue(
                ExceptionHandlingSettingsManageabilityProvider.PolicyTypePostHandlingActionPropertyName, PostHandlingAction.None);
            MockRegistryKey userHandlerKey = new MockRegistryKey(false);
            userHandlersKey.AddSubKey("handler1", userHandlerKey);

            provider.OverrideWithGroupPolicies(section, true, machineKey, userKey);

            Assert.IsTrue(registeredProvider.called);
            Assert.AreSame(handlerData1, registeredProvider.LastConfigurationObject);
            Assert.AreSame(machineHandlerKey, registeredProvider.machineKey);
            Assert.AreSame(userHandlerKey, registeredProvider.userKey);

            Assert.IsTrue(MockRegistryKey.CheckAllClosed(machinePoliciesKey,
                                                         machinePolicy1Key, machinePolicy1TypesKey, machinePolicy1Type1Key,
                                                         machineHandlersKey, machineHandlerKey,
                                                         userPolicy1Key, userPolicy1TypesKey, userPolicy1Type1Key,
                                                         userHandlersKey, userHandlerKey));
        }
        public void RegisteredHandlerDataProviderIsCalledWithNoOverrides()
        {
            MockConfigurationElementManageabilityProvider registeredProvider
                = new MockConfigurationElementManageabilityProvider();
            Dictionary<Type, ConfigurationElementManageabilityProvider> subProviders
                = new Dictionary<Type, ConfigurationElementManageabilityProvider>();
            subProviders.Add(typeof(ReplaceHandlerData), registeredProvider);
            provider = new ExceptionHandlingSettingsManageabilityProvider(subProviders);

            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");
            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType1 = new ExceptionTypeData("type1", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType1);
            ExceptionHandlerData handlerData1 =
                new ReplaceHandlerData("handler1", "msg", typeof(ArgumentException).AssemblyQualifiedName);
            exceptionType1.ExceptionHandlers.Add(handlerData1);

            provider.OverrideWithGroupPolicies(section, true, machineKey, userKey);

            Assert.IsTrue(registeredProvider.called);
            Assert.AreSame(handlerData1, registeredProvider.LastConfigurationObject);
            Assert.AreEqual(null, registeredProvider.machineKey);
            Assert.AreEqual(null, registeredProvider.userKey);
        }
예제 #47
0
 /// <summary>
 /// Initialize a new instance of the <see cref="ExceptionPolicyNode"/> class with a <see cref="ExceptionPolicyData"/> instance.
 /// </summary>
 /// <param name="exceptionPolicyData">
 /// A <see cref="ExceptionPolicyData"/> instance.
 /// </param>
 public ExceptionPolicyNode(ExceptionPolicyData exceptionPolicyData)
     : base(exceptionPolicyData == null ? Resources.DefaultExceptionPolicyNodeName : exceptionPolicyData.Name)
 {
 }
        public void ExceptionTypeWithDisabledPolicyIsNotRemovedIfGroupPoliciesAreDisabled()
        {
            ExceptionPolicyData policy1 = new ExceptionPolicyData("policy1");
            section.ExceptionPolicies.Add(policy1);
            ExceptionTypeData exceptionType11 = new ExceptionTypeData("type11", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType11);
            ExceptionTypeData exceptionType12 = new ExceptionTypeData("type12", typeof(Exception), PostHandlingAction.None);
            policy1.ExceptionTypes.Add(exceptionType12);
            ExceptionPolicyData policy2 = new ExceptionPolicyData("policy2");
            section.ExceptionPolicies.Add(policy2);
            ExceptionTypeData exceptionType21 = new ExceptionTypeData("type21", typeof(Exception), PostHandlingAction.None);
            policy2.ExceptionTypes.Add(exceptionType21);

            MockRegistryKey userPoliciesKey = new MockRegistryKey(false);
            userKey.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PoliciesKeyName, userPoliciesKey);
            MockRegistryKey userPolicy1Key = new MockRegistryKey(false);
            userPoliciesKey.AddSubKey("policy1", userPolicy1Key);
            MockRegistryKey userPolicy1TypesKey = new MockRegistryKey(false);
            userPolicy1Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName, userPolicy1TypesKey);
            MockRegistryKey userPolicy1Type11Key = new MockRegistryKey(false);
            userPolicy1TypesKey.AddSubKey("type11", userPolicy1Type11Key);
            userPolicy1Type11Key.AddBooleanValue(ExceptionHandlingSettingsManageabilityProvider.PolicyValueName, false);
            MockRegistryKey userPolicy1Type12Key = new MockRegistryKey(false);
            userPolicy1TypesKey.AddSubKey("type12", userPolicy1Type12Key);
            userPolicy1Type12Key.AddBooleanValue(ExceptionHandlingSettingsManageabilityProvider.PolicyValueName, true);
            MockRegistryKey userPolicy2Key = new MockRegistryKey(false);
            userPoliciesKey.AddSubKey("policy2", userPolicy2Key);
            MockRegistryKey userPolicy2TypesKey = new MockRegistryKey(false);
            userPolicy2Key.AddSubKey(ExceptionHandlingSettingsManageabilityProvider.PolicyTypesPropertyName, userPolicy2TypesKey);
            MockRegistryKey userPolicy2Type21Key = new MockRegistryKey(false);
            userPolicy2TypesKey.AddSubKey("type21", userPolicy2Type21Key);
            userPolicy2Type21Key.AddBooleanValue(ExceptionHandlingSettingsManageabilityProvider.PolicyValueName, false);

            provider.OverrideWithGroupPolicies(section, false, machineKey, userKey);

            Assert.AreEqual(2, section.ExceptionPolicies.Count);
            Assert.IsNotNull(section.ExceptionPolicies.Get("policy1"));
            Assert.AreEqual(2, section.ExceptionPolicies.Get("policy1").ExceptionTypes.Count);
            Assert.IsNotNull(section.ExceptionPolicies.Get("policy1").ExceptionTypes.Get("type11"));
            Assert.IsNotNull(section.ExceptionPolicies.Get("policy1").ExceptionTypes.Get("type12"));
            Assert.IsNotNull(section.ExceptionPolicies.Get("policy2"));
            Assert.AreEqual(1, section.ExceptionPolicies.Get("policy2").ExceptionTypes.Count);
            Assert.IsNotNull(section.ExceptionPolicies.Get("policy2").ExceptionTypes.Get("type21"));

            Assert.IsTrue(MockRegistryKey.CheckAllClosed(userPoliciesKey,
                                                         userPolicy1Key, userPolicy1TypesKey, userPolicy1Type11Key,
                                                         userPolicy1Type12Key,
                                                         userPolicy2Key, userPolicy2TypesKey, userPolicy2Type21Key));
        }