Beispiel #1
0
        private static InjectionPolicy CreatePolicy(IUnityContainer container, IMatchingRule[] rules)
        {
            InjectionPolicy p
                = new RuleDrivenPolicy(rules, new string[] { "handler1", "handler2", "handler3" });

            return(p);
        }
Beispiel #2
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);
        }
        public void AssembledProperlyPerfCounterHandler()
        {
            PolicyInjectionSettings settings = new PolicyInjectionSettings();

            PolicyData policyData = new PolicyData("policy");
            PerformanceCounterCallHandlerData data = new PerformanceCounterCallHandlerData("FooCallHandler", 2);

            policyData.MatchingRules.Add(new CustomMatchingRuleData("match everything", typeof(AlwaysMatchingRule)));
            policyData.Handlers.Add(data);
            settings.Policies.Add(policyData);

            DictionaryConfigurationSource dictConfigurationSource = new DictionaryConfigurationSource();

            dictConfigurationSource.Add(PolicyInjectionSettings.SectionName, settings);

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

            settings.ConfigureContainer(container);

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

            ICallHandler handler
                = (policy.GetHandlersFor(GetMethodImpl(MethodBase.GetCurrentMethod()), container)).ElementAt(0);

            Assert.IsNotNull(handler);
            Assert.AreEqual(handler.Order, data.Order);
        }
Beispiel #4
0
        public void TestInitialize()
        {
            IConfigurationSource authorizationConfiguration = new FileConfigurationSource("Authorization.config");

            RuleDrivenPolicy allowFredPolicy = new RuleDrivenPolicy("allowFred");

            allowFredPolicy.Handlers.Add(new AuthorizationCallHandler("RuleProvider", "OnlyFredHasAccess", authorizationConfiguration));
            allowFredPolicy.RuleSet.Add(new AlwaysMatchingRule());
            AllowFredPolicySet = new PolicySet(allowFredPolicy);


            RuleDrivenPolicy allowJackPolicy = new RuleDrivenPolicy("allowJack");

            allowJackPolicy.Handlers.Add(new AuthorizationCallHandler(string.Empty, "OnlyJackHasAccess", authorizationConfiguration));
            allowJackPolicy.RuleSet.Add(new AlwaysMatchingRule());
            AllowJackPolicySet = new PolicySet(allowJackPolicy);

            RuleDrivenPolicy tokenBasedPolicy = new RuleDrivenPolicy("tokens");

            tokenBasedPolicy.RuleSet.Add(new AlwaysMatchingRule());
            tokenBasedPolicy.Handlers.Add(
                new AuthorizationCallHandler(string.Empty, "{type}-{method}",
                                             authorizationConfiguration));
            AllowBasedOnTokensPolicySet = new PolicySet(tokenBasedPolicy);
        }
Beispiel #5
0
        public void ShouldCreateCorrectMatchingRule()
        {
            PolicyData policyData = new PolicyData("Validate Parameters");

            policyData.Handlers.Add(new ValidationCallHandlerData());
            ParameterTypeMatchingRuleData matchingRuleData = GetParameterTypeMatchingRuleData();

            policyData.MatchingRules.Add(matchingRuleData);

            PolicyInjectionSettings settings = new PolicyInjectionSettings();

            settings.Policies.Add(policyData);

            DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();

            configSource.Add(PolicyInjectionSettings.SectionName, settings);

            PolicySetFactory factory  = new PolicySetFactory(configSource);
            PolicySet        policies = factory.Create();

            Policy policy = policies[1];

            Assert.IsTrue(policy is RuleDrivenPolicy);
            RuleDrivenPolicy validateRule = (RuleDrivenPolicy)policy;

            Assert.IsTrue(validateRule.RuleSet[0] is ParameterTypeMatchingRule);
            ParameterTypeMatchingRule rule = (ParameterTypeMatchingRule)(validateRule.RuleSet[0]);

            Assert.AreEqual(3, rule.ParameterMatches.Count);
            for (int i = 0; i < matchingRuleData.Matches.Count; ++i)
            {
                AssertMatchDataEqual(matchingRuleData.Matches[i], rule.ParameterMatches[i], "Mismatch at element {0}", i);
            }
        }
 PolicySet GetPolicySet(ICallHandler handler)
 {
     RuleDrivenPolicy magicPolicy = new RuleDrivenPolicy();
     magicPolicy.RuleSet.Add(new TagAttributeMatchingRule("Magic"));
     magicPolicy.Handlers.Add(handler);
     return new PolicySet(magicPolicy);
 }
Beispiel #7
0
        public void ShouldInitializeToEmpty()
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("Empty");

            Assert.AreEqual("Empty", p.Name);
            Assert.AreEqual(0, p.RuleSet.Count);
            Assert.AreEqual(0, p.Handlers.Count);
        }
 public static List <IMatchingRule> GetRules(RuleDrivenPolicy policy)
 {
     return((List <IMatchingRule>) typeof(Microsoft.Practices.Unity.InterceptionExtension.RuleDrivenPolicy)
            .GetField(
                "ruleSet",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
            .GetValue(policy));
 }
Beispiel #9
0
        private PolicySet GetPolicySet(ICallHandler handler)
        {
            RuleDrivenPolicy magicPolicy = new RuleDrivenPolicy();

            magicPolicy.RuleSet.Add(new TagAttributeMatchingRule("Magic"));
            magicPolicy.Handlers.Add(handler);
            return(new PolicySet(magicPolicy));
        }
Beispiel #10
0
        private void AddNoopPolicy(PolicyInjector factory, params IMatchingRule[] rules)
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("Noop");

            p.RuleSet.AddRange(rules);
            p.Handlers.Add(new NoopCallHandler());

            factory.Policies.Add(p);
        }
Beispiel #11
0
        private PolicySet GetCachingPolicies()
        {
            RuleDrivenPolicy cachePolicy = new RuleDrivenPolicy("Caching");

            cachePolicy.RuleSet.Add(new TypeMatchingRule(typeof(CachingTarget)));
            cachePolicy.Handlers.Add(new CachingCallHandler());

            return(new PolicySet(cachePolicy));
        }
Beispiel #12
0
        private void AddExceptionPolicy(PolicyInjector factory, string exceptionPolicyName, params IMatchingRule[] rules)
        {
            RuleDrivenPolicy exceptionPolicy = new RuleDrivenPolicy();

            exceptionPolicy.RuleSet.AddRange(rules);
            exceptionPolicy.Handlers.Add(new ExceptionCallHandler(exceptionPolicyName));

            factory.Policies.Add(exceptionPolicy);
        }
Beispiel #13
0
        private PolicySet GetPerfMonPolicies()
        {
            RuleDrivenPolicy policy = new RuleDrivenPolicy("Monitor all methods");

            policy.RuleSet.Add(new TypeMatchingRule(typeof(MonitorTarget)));
            callHandler = new PerformanceCounterCallHandler(TestCategoryName, TestInstanceName);
            policy.Handlers.Add(callHandler);
            return(new PolicySet(policy));
        }
Beispiel #14
0
        PolicySet GetPolicies(IUnityContainer container)
        {
            RuleDrivenPolicy p
                = new RuleDrivenPolicy(
                      "PipelineTestPolicy",
                      new IMatchingRule[] { new AlwaysMatchingRule() },
                      new string[] { "call count", "rewrite" });

            return(new PolicySet(p));
        }
Beispiel #15
0
        private RuleDrivenPolicy GetPolicyThatMatchesInterface()
        {
            RuleDrivenPolicy           policy = new RuleDrivenPolicy("Matches IDal");
            TypeMatchingAssignmentRule rule   = new TypeMatchingAssignmentRule(typeof(IDal));

            countHandler = new CallCountHandler();
            policy.RuleSet.Add(rule);
            policy.Handlers.Add(countHandler);
            return(policy);
        }
        PolicySet GetPolicies(IUnityContainer container)
        {
            RuleDrivenPolicy p
                = new RuleDrivenPolicy(
                    "PipelineTestPolicy",
                    new IMatchingRule[] { new AlwaysMatchingRule() },
                    new string[] { "call count", "rewrite" });

            return new PolicySet(p);
        }
Beispiel #17
0
        private PolicySet GetPolicies()
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("PipelineTestPolicy");

            p.RuleSet.Add(new AlwaysMatchingRule());
            callCountHandler = new CallCountHandler();
            returnHandler    = new StringReturnRewriteHandler("REWRITE");
            p.Handlers.Add(callCountHandler);
            p.Handlers.Add(returnHandler);
            return(new PolicySet(p));
        }
Beispiel #18
0
        private void AddOverloadsPolicy(PolicyInjector factory)
        {
            RuleDrivenPolicy         policy  = new RuleDrivenPolicy("NullStringPolicy");
            TagAttributeMatchingRule tagRule = new TagAttributeMatchingRule("NullString");

            policy.RuleSet.Add(tagRule);

            policy.Handlers.Add(new MakeReturnNullHandler());

            factory.Policies.Add(policy);
        }
Beispiel #19
0
        private RuleDrivenPolicy GetShortcutPolicy( )
        {
            RuleDrivenPolicy           typeMatchPolicy = new RuleDrivenPolicy("ShortcutPolicy");
            TypeMatchingAssignmentRule typeRule        = new TypeMatchingAssignmentRule(typeof(MockDal));

            typeMatchPolicy.RuleSet.Add(typeRule);
            ShortcuttingHandler handler = new ShortcuttingHandler("shortcut");

            typeMatchPolicy.Handlers.Add(handler);
            return(typeMatchPolicy);
        }
Beispiel #20
0
        private RuleDrivenPolicy GetCallCountingPolicy()
        {
            RuleDrivenPolicy      typeMatchPolicy = new RuleDrivenPolicy("DALPolicy");
            NamespaceMatchingRule nsMatchRule     = new NamespaceMatchingRule(
                "Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Tests.ObjectsUnderTest");

            typeMatchPolicy.RuleSet.Add(nsMatchRule);
            countHandler = new CallCountHandler();
            typeMatchPolicy.Handlers.Add(countHandler);
            return(typeMatchPolicy);
        }
Beispiel #21
0
        private void AddPropertiesPolicy(PolicyInjector factory)
        {
            RuleDrivenPolicy       policy = new RuleDrivenPolicy("Intercept balance policy");
            MemberNameMatchingRule rule   = new MemberNameMatchingRule("get_Balance");

            policy.RuleSet.Add(rule);

            countHandler = new CallCountHandler();
            policy.Handlers.Add(countHandler);
            factory.Policies.Add(policy);
        }
Beispiel #22
0
        public void HandlersOrderedProperlyUsingRelativeAndAbsoluteOrder()
        {
            RuleDrivenPolicy policy
                = new RuleDrivenPolicy("MatchesInterfacePolicy",
                                       new IMatchingRule[] { new TypeMatchingRule("ITwo") },
                                       new string[] { "Handler1", "Handler2", "Handler3", "Handler4", "Handler5", "Handler6" });

            ICallHandler handler1 = new CallCountHandler();

            handler1.Order = 0;

            ICallHandler handler2 = new CallCountHandler();

            handler2.Order = 3;

            ICallHandler handler3 = new CallCountHandler();

            handler3.Order = 3;

            ICallHandler handler4 = new CallCountHandler();

            handler4.Order = 2;

            ICallHandler handler5 = new CallCountHandler();

            handler5.Order = 4;

            ICallHandler handler6 = new CallCountHandler();

            handler6.Order = 1;

            container
            .RegisterInstance <ICallHandler>("Handler1", handler1)
            .RegisterInstance <ICallHandler>("Handler2", handler2)
            .RegisterInstance <ICallHandler>("Handler3", handler3)
            .RegisterInstance <ICallHandler>("Handler4", handler4)
            .RegisterInstance <ICallHandler>("Handler5", handler5)
            .RegisterInstance <ICallHandler>("Handler6", handler6);

            PolicySet policies = new PolicySet(policy);

            MethodImplementationInfo twoInfo = new MethodImplementationInfo(
                typeof(ITwo).GetMethod("Two"), typeof(TwoType).GetMethod("Two"));

            List <ICallHandler> handlers
                = new List <ICallHandler>(policies.GetHandlersFor(twoInfo, container));

            Assert.AreEqual(handler6, handlers[0]);
            Assert.AreEqual(handler4, handlers[1]);
            Assert.AreEqual(handler2, handlers[2]);
            Assert.AreEqual(handler3, handlers[3]);
            Assert.AreEqual(handler5, handlers[4]);
            Assert.AreEqual(handler1, handlers[5]);
        }
        public void CanCreateValidationCallHandlerThroughFactory()
        {
            ValidationCallHandlerData validationCallHandler = new ValidationCallHandlerData("validationHandler");
            IUnityContainer           container             = new UnityContainer().AddNewExtension <Interception>();

            RuleDrivenPolicy policy = CreatePolicySetContainingCallHandler(validationCallHandler, container);

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

            Assert.IsNotNull(runtimeHandler);
        }
Beispiel #24
0
        public void ShouldMatchForMethodWhenTagIsOnInterfaceViaPolicy()
        {
            RuleDrivenPolicy policy = new RuleDrivenPolicy("Count tagged calls");

            policy.RuleSet.Add(new TagAttributeMatchingRule("Tag on interface", true));
            policy.Handlers.Add(new CallCountHandler());

            MethodInfo          createMethod = typeof(DaoImpl).GetMethod("Create");
            List <ICallHandler> handlers     = new List <ICallHandler>(policy.GetHandlersFor(createMethod));

            Assert.AreEqual(1, handlers.Count);
            Assert.IsTrue(handlers[0] is CallCountHandler);
        }
Beispiel #25
0
        public void ShouldHaveNoHandlersWhenPolicyDoesntMatch()
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("NoRules");

            ICallHandler[] handlers = { new Handler1(), new Handler2(), new Handler3() };
            Array.ForEach(handlers, delegate(ICallHandler handler) { p.Handlers.Add(handler); });

            MethodBase thisMember =
                this.GetType().GetMethod("ShouldHaveNoHandlersWhenPolicyDoesntMatch");
            List <ICallHandler> memberHandlers = new List <ICallHandler>(p.GetHandlersFor(thisMember));

            Assert.AreEqual(0, memberHandlers.Count);
        }
Beispiel #26
0
        public void ShouldNotDuplicateHandlersWhenCreatingViaInterface()
        {
            DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
            RuleDrivenPolicy policy = new RuleDrivenPolicy("MatchesInterfacePolicy");

            policy.RuleSet.Add(new TypeMatchingRule("ITwo"));
            policy.Handlers.Add(new ValidationCallHandler(string.Empty, SpecificationSource.Both));
            policy.Handlers.Add(new CallCountHandler());

            PolicySet           policies = new PolicySet(policy);
            List <ICallHandler> handlers = new List <ICallHandler>(policies.GetHandlersFor(typeof(Bar).GetMethod("Two")));

            Assert.AreEqual(2, handlers.Count);
        }
Beispiel #27
0
        private PolicySet GetMultiplePolicySet()
        {
            RuleDrivenPolicy typeMatchPolicy = new RuleDrivenPolicy("MatchesType");

            typeMatchPolicy.RuleSet.Add(new TypeMatchingRule(typeof(MatchesByType)));
            typeMatchPolicy.Handlers.Add(new Handler1());

            RuleDrivenPolicy nameMatchPolicy = new RuleDrivenPolicy("MatchesName");

            nameMatchPolicy.RuleSet.Add(new MemberNameMatchingRule("NameMatch"));
            nameMatchPolicy.Handlers.Add(new Handler2());

            return(new PolicySet(typeMatchPolicy, nameMatchPolicy));
        }
Beispiel #28
0
        public void ShouldOnlyGetHandlersOnceIfPolicyMatchesBothClassAndInterface()
        {
            RuleDrivenPolicy       p           = new RuleDrivenPolicy();
            ICallHandler           callHandler = new CallCountHandler();
            MemberNameMatchingRule nameRule    = new MemberNameMatchingRule("MyMethod");

            p.RuleSet.Add(nameRule);
            p.Handlers.Add(callHandler);

            MethodInfo          myMethod = typeof(MyFooClass).GetMethod("MyMethod");
            List <ICallHandler> handlers = new List <ICallHandler>(p.GetHandlersFor(myMethod));

            Assert.AreEqual(1, handlers.Count);
            Assert.AreSame(callHandler, handlers[0]);
        }
Beispiel #29
0
        private PolicySet GetLoggingPolicies()
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("Logging");

            p.RuleSet.Add(new TypeMatchingRule("LoggingTarget"));

            callHandler = new LogCallHandler(log);
            callHandler.LogBeforeCall = callHandler.LogAfterCall = true;
            callHandler.BeforeMessage = beforeMessage;
            callHandler.AfterMessage  = afterMessage;
            callHandler.Categories.Add("General");
            callHandler.Categories.Add("PIAB");
            p.Handlers.Add(callHandler);

            return(new PolicySet(p));
        }
Beispiel #30
0
        public void ShouldBeAbleToMatchPropertyGet()
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("Property get");

            p.RuleSet.Add(new MemberNameMatchingRule("get_Balance"));
            ICallHandler callHandler = new CallCountHandler();

            p.Handlers.Add(callHandler);

            PropertyInfo        balanceProperty = typeof(MockDal).GetProperty("Balance");
            MethodBase          getMethod       = balanceProperty.GetGetMethod();
            List <ICallHandler> handlers        = new List <ICallHandler>(p.GetHandlersFor(getMethod));

            Assert.AreEqual(1, handlers.Count);
            Assert.AreSame(callHandler, handlers[0]);
        }
Beispiel #31
0
        public void ShouldPreserveHandlerOrder()
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("OrderedHandlers");

            ICallHandler h1 = new Handler1();
            ICallHandler h2 = new Handler2();
            ICallHandler h3 = new Handler3();

            p.Handlers.Add(h2);
            p.Handlers.Add(h1);
            p.Handlers.Add(h3);

            Assert.AreEqual(3, p.Handlers.Count);
            Assert.AreSame(h2, p.Handlers[0]);
            Assert.AreSame(h1, p.Handlers[1]);
            Assert.AreSame(h3, p.Handlers[2]);
        }
Beispiel #32
0
        private PolicySet GetMultiplePolicySet()
        {
            container
            .RegisterInstance <ICallHandler>("Handler1", new Handler1())
            .RegisterInstance <ICallHandler>("Handler2", new Handler2());

            RuleDrivenPolicy typeMatchPolicy
                = new RuleDrivenPolicy("MatchesType",
                                       new IMatchingRule[] { new TypeMatchingRule(typeof(MatchesByType)) },
                                       new string[] { "Handler1" });

            RuleDrivenPolicy nameMatchPolicy
                = new RuleDrivenPolicy("MatchesName",
                                       new IMatchingRule[] { new MemberNameMatchingRule("NameMatch") },
                                       new string[] { "Handler2" });

            return(new PolicySet(typeMatchPolicy, nameMatchPolicy));
        }
        PolicySet GetPolicies()
        {
            PolicySet policies = new PolicySet();

            RuleDrivenPolicy noParamsPolicy
                = new RuleDrivenPolicy(
                    "noParamsPolicy",
                    new IMatchingRule[] { new MatchByNameRule("MethodWithNoParameters") },
                    new string[] { "Handler1" });
            container.RegisterInstance<ICallHandler>("Handler1", new SignatureCheckingHandler(new Type[] { }));
            policies.Add(noParamsPolicy);

            RuleDrivenPolicy simpleInputsPolicy
                = new RuleDrivenPolicy(
                    "simpleInputsPolicy",
                    new IMatchingRule[] { new MatchByNameRule("MethodWithSimpleInputs") },
                    new string[] { "Handler2" });
            container.RegisterInstance<ICallHandler>(
                "Handler2",
                new SignatureCheckingHandler(new Type[] { typeof(int), typeof(string) }));
            policies.Add(simpleInputsPolicy);

            RuleDrivenPolicy outParamsPolicy
                = new RuleDrivenPolicy(
                    "outParamsPolicy",
                    new IMatchingRule[] { new MatchByNameRule("MethodWithOutParams") },
                    new string[] { "Handler3" });
            container.RegisterInstance<ICallHandler>(
                "Handler3",
                new SignatureCheckingHandler(new Type[] { typeof(int).MakeByRefType(), typeof(string).MakeByRefType() }));
            policies.Add(outParamsPolicy);

            RuleDrivenPolicy mixedParamsPolicy
                = new RuleDrivenPolicy(
                    "mixedParamsPolicy",
                    new IMatchingRule[] { new MatchByNameRule("MethodWithInOutByrefParams") },
                    new string[] { "Handler4" });
            container.RegisterInstance<ICallHandler>(
                "Handler4",
                new SignatureCheckingHandler(
                   new Type[]
                           {
                               typeof(int),
                               typeof(string).MakeByRefType(),
                               typeof(float).MakeByRefType(),
                               typeof(decimal)
                           }));
            policies.Add(mixedParamsPolicy);

            RuleDrivenPolicy varargsParamsPolicy
                = new RuleDrivenPolicy(
                    "varargsParamsPolicy",
                    new IMatchingRule[] { new MatchByNameRule("MethodWithVarArgs") },
                    new string[] { "Handler5" });
            container.RegisterInstance<ICallHandler>(
                "Handler5",
                new SignatureCheckingHandler(new Type[] { typeof(int), typeof(string).MakeArrayType() }));
            policies.Add(varargsParamsPolicy);

            return policies;
        }
        PolicySet GetPolicies()
        {
            RuleDrivenPolicy doubleInputPolicy
                = new RuleDrivenPolicy(
                    "Double your inputs",
                    new IMatchingRule[] { new MemberNameMatchingRule("DoSomething") },
                    new string[] { "Handler1" });
            container.RegisterInstance<ICallHandler>("Handler1", new DoubleInputHandler());

            RuleDrivenPolicy tripleOutputPolicy
                = new RuleDrivenPolicy(
                    "Triple an output parameter",
                    new IMatchingRule[] { new MemberNameMatchingRule(new[] { "DoSomethingElse", "DoSomethingElseWithRef" }) },
                    new string[] { "Handler2" });
            container.RegisterInstance<ICallHandler>("Handler2", new TripleOutputHandler());

            return new PolicySet(doubleInputPolicy, tripleOutputPolicy);
        }