Example #1
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 ShouldLogOnlyToCategoriesGivenInConfig()
        {
            using (var configSource = new FileConfigurationSource("LogCallHandler.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        LoggingTarget target = injector.Create <LoggingTarget>();
                        target.DoSomething(1, "two", 3.0);
                    }

                    Assert.AreEqual(1, eventLog.NewEntries().Select(le => le.Category == "Default Category").Count());
                }
            }
        }
        public void ShouldLogOnlyToCategoriesGivenInConfig()
        {
            using (var configSource = new FileConfigurationSource("LogCallHandler.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        LoggingTarget target = injector.Create<LoggingTarget>();
                        target.DoSomething(1, "two", 3.0);
                    }

                    Assert.AreEqual(1, eventLog.NewEntries().Select(le => le.Category == "Default Category").Count());
                }
            }
        }
        public void ShouldCombineWithPoliciesDefinedInConfiguration()
        {
            using (var configSource = new FileConfigurationSource("CombinesWithConfig.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        var typeWhichUndergoesLoggingOnMethodCall =
                            injector.Create <TypeWhichUndergoesAttributeBasedLogging>();

                        typeWhichUndergoesLoggingOnMethodCall.TestMethod();

                        typeWhichUndergoesLoggingOnMethodCall.MyProperty = "hello";
                    }

                    Assert.AreEqual(2,
                                    eventLog.NewEntries().Select(le => le.Message.Contains("This is before the call")).
                                    Count());
                }
            }
        }
        public void ShouldCombineWithPoliciesDefinedInConfiguration()
        {
            using (var configSource = new FileConfigurationSource("CombinesWithConfig.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        var typeWhichUndergoesLoggingOnMethodCall =
                            injector.Create<TypeWhichUndergoesAttributeBasedLogging>();

                        typeWhichUndergoesLoggingOnMethodCall.TestMethod();

                        typeWhichUndergoesLoggingOnMethodCall.MyProperty = "hello";
                    }

                    Assert.AreEqual(2,
                                    eventLog.NewEntries().Select(le => le.Message.Contains("This is before the call")).
                                        Count());
                }
            }
        }
        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();
            }
        }