Example #1
0
        public void ShouldGetCorrectHandlersGivenAttributesOnInterfaceMethodsAfterAddingAttributeDrivenPolicy()
        {
            PolicySet policies = new PolicySet();

            List <ICallHandler> oneHandlers
                = new List <ICallHandler>(policies.GetHandlersFor(GetMethodImplInfo <TwoType>("One"), container));

            Assert.AreEqual(0, oneHandlers.Count);

            policies.Add(new AttributeDrivenPolicy());

            MethodImplementationInfo oneInfo = new MethodImplementationInfo(
                typeof(IOne).GetMethod("One"),
                typeof(TwoType).GetMethod("One"));

            oneHandlers
                = new List <ICallHandler>(policies.GetHandlersFor(oneInfo, container));

            Assert.AreEqual(2, oneHandlers.Count);
            Assert.IsTrue(oneHandlers[0] is MarkerCallHandler);
            Assert.IsTrue(oneHandlers[1] is MarkerCallHandler);

            Assert.AreEqual("IOneOne", ((MarkerCallHandler)oneHandlers[0]).HandlerName);
            Assert.AreEqual("MethodOneOverride", ((MarkerCallHandler)oneHandlers[1]).HandlerName);
        }
Example #2
0
        public void ShouldMatchPolicyByMethodName()
        {
            PolicySet policies = GetMultiplePolicySet();

            MethodImplementationInfo noMatchMember   = GetMethodImplInfo <MatchesByMemberName>("NoMatch");
            MethodImplementationInfo nameMatchMember = GetMethodImplInfo <MatchesByMemberName>("NameMatch");
            List <ICallHandler>      noMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(noMatchMember, container));
            List <ICallHandler> nameMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(nameMatchMember, container));

            Assert.AreEqual(0, noMatchHandlers.Count);
            Assert.AreEqual(1, nameMatchHandlers.Count);
            Assert.IsTrue(typeof(Handler2) == nameMatchHandlers[0].GetType());
        }
Example #3
0
 public void ShouldBeCreateableWithHandlers()
 {
     IUnityContainer container = GetContainer();
     PolicySet       policies  = GetPolicies(container);
     HandlerPipeline pipeline
         = new HandlerPipeline(policies.GetHandlersFor(GetTargetMemberInfo(), container));
 }
Example #4
0
        public void ShouldMatchPolicyByMethodName()
        {
            PolicySet policies = GetMultiplePolicySet();

            Assert.IsTrue(policies.AppliesTo(typeof(MatchesByMemberName)));

            MethodInfo          noMatchMember   = typeof(MatchesByMemberName).GetMethod("NoMatch");
            MethodInfo          nameMatchMember = typeof(MatchesByMemberName).GetMethod("NameMatch");
            List <ICallHandler> noMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(noMatchMember));
            List <ICallHandler> nameMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(nameMatchMember));

            Assert.AreEqual(0, noMatchHandlers.Count);
            Assert.AreEqual(1, nameMatchHandlers.Count);
            Assert.IsTrue(typeof(Handler2) == nameMatchHandlers[0].GetType());
        }
 private void ApplyPolicies(IInterceptor interceptor, IInterceptingProxy proxy, object target, PolicySet policies)
 {
     foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(target.GetType(), target.GetType()))
     {
         HandlerPipeline pipeline = new HandlerPipeline(policies.GetHandlersFor(method, container));
         proxy.SetPipeline(method.ImplementationMethodInfo, pipeline);
     }
 }
Example #6
0
        public void ShouldHaveLoggingHandlerForNothingSpecial()
        {
            PolicySet           policies = GetPolicySet();
            List <ICallHandler> handlers = new List <ICallHandler>(policies.GetHandlersFor(nothingSpecialMethod));

            Assert.AreEqual(1, handlers.Count);
            Assert.AreSame(typeof(LogCallHandler), handlers[0].GetType());
        }
Example #7
0
 private void AddHandlersForType(Type classToProxy, PolicySet policies)
 {
     foreach (MethodInfo member in classToProxy.GetMethods())
     {
         IEnumerable <ICallHandler> handlers = policies.GetHandlersFor(member);
         HandlerPipeline            pipeline = new HandlerPipeline(handlers);
         memberHandlers[member] = pipeline;
     }
 }
Example #8
0
        public void ShouldInitializeToEmpty()
        {
            PolicySet policies = new PolicySet();

            Assert.IsFalse(policies.AppliesTo(this.GetType()));
            MethodInfo          thisMember = GetType().GetMethod("ShouldInitializeToEmpty");
            List <ICallHandler> handlers   = new List <ICallHandler>(policies.GetHandlersFor(thisMember));

            Assert.AreEqual(0, handlers.Count);
        }
Example #9
0
        public void ShouldNotMatchPolicyWhenNoRulesMatch()
        {
            PolicySet policies = GetMultiplePolicySet();

            MethodImplementationInfo noMatchMember   = GetMethodImplInfo <NoMatchAnywhere>("NoMatchHere");
            List <ICallHandler>      noMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(noMatchMember, container));

            Assert.AreEqual(0, noMatchHandlers.Count);
        }
Example #10
0
        public void ShouldClearCachesAfterChangesToPolicyInPolicySet()
        {
            PolicySet policies = GetMultiplePolicySet();

            List <ICallHandler> handlersBefore =
                new List <ICallHandler>(policies.GetHandlersFor(
                                            GetNameDoesntMatchMethod()));

            Assert.AreEqual(1, handlersBefore.Count);

            ((RuleDrivenPolicy)policies[2]).RuleSet.RemoveAt(0);
            ((RuleDrivenPolicy)policies[2]).RuleSet.Add(new MemberNameMatchingRule("NameDoesntMatch"));

            List <ICallHandler> handlersAfterChangingRule =
                new List <ICallHandler>(
                    policies.GetHandlersFor(GetNameDoesntMatchMethod()));

            Assert.AreEqual(2, handlersAfterChangingRule.Count);
        }
Example #11
0
        public void ShouldHaveLoggingAndValidationForDoSomething()
        {
            PolicySet           policies = GetPolicySet();
            List <ICallHandler> handlers =
                new List <ICallHandler>(policies.GetHandlersFor(doSomethingMethod));

            Assert.AreEqual(2, handlers.Count);
            Assert.AreSame(typeof(LogCallHandler), handlers[0].GetType());
            Assert.AreSame(typeof(ValidationCallHandler), handlers[1].GetType());
        }
Example #12
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]);
        }
Example #13
0
        public void ShouldNotMatchPolicyWhenNoRulesMatch()
        {
            PolicySet policies = GetMultiplePolicySet();

            Assert.IsFalse(policies.AppliesTo(typeof(NoMatchAnywhere)));

            MethodBase          noMatchMember   = typeof(NoMatchAnywhere).GetMethod("NoMatchHere");
            List <ICallHandler> noMatchHandlers =
                new List <ICallHandler>(policies.GetHandlersFor(noMatchMember));

            Assert.AreEqual(0, noMatchHandlers.Count);
        }
Example #14
0
        private void ApplyPolicies(IInterceptor interceptor, IInterceptingProxy proxy, object target, PolicySet policies)
        {
            PipelineManager manager = new PipelineManager();

            foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(target.GetType(), target.GetType()))
            {
                HandlerPipeline pipeline = new HandlerPipeline(policies.GetHandlersFor(method, container));
                manager.SetPipeline(method.ImplementationMethodInfo, pipeline);
            }

            proxy.AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
        }
Example #15
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);
        }
Example #16
0
        public void ShouldGetCorrectHandlersGivenAttributesOnInterfaceMethods()
        {
            PolicySet policies = new PolicySet();

            List <ICallHandler> oneHandlers = new List <ICallHandler>(policies.GetHandlersFor(
                                                                          typeof(Bar).GetMethod("One")));

            Assert.AreEqual(2, oneHandlers.Count);
            Assert.IsTrue(oneHandlers[0] is MarkerCallHandler);
            Assert.IsTrue(oneHandlers[1] is MarkerCallHandler);

            Assert.AreEqual("BarOneOverride", ((MarkerCallHandler)oneHandlers[0]).HandlerName);
            Assert.AreEqual("IOneOne", ((MarkerCallHandler)oneHandlers[1]).HandlerName);
        }
Example #17
0
        SignatureTestTarget GetTarget()
        {
            TransparentProxyInterceptor interceptor = new TransparentProxyInterceptor();
            PolicySet           policySet           = GetPolicies();
            SignatureTestTarget target = new SignatureTestTarget();
            IInterceptingProxy  proxy  = interceptor.CreateProxy(typeof(SignatureTestTarget), target);

            foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(typeof(SignatureTestTarget), typeof(SignatureTestTarget)))
            {
                HandlerPipeline pipeline = new HandlerPipeline(
                    policySet.GetHandlersFor(method, container));
                proxy.SetPipeline(method.ImplementationMethodInfo, pipeline);
            }
            return((SignatureTestTarget)proxy);
        }
Example #18
0
        public void ShouldClearCachesAfterChangesToPolicySet()
        {
            PolicySet           policies       = GetMultiplePolicySet();
            List <ICallHandler> handlersBefore =
                new List <ICallHandler>(
                    policies.GetHandlersFor(GetNameDoesntMatchMethod()));

            Assert.AreEqual(1, handlersBefore.Count);

            RuleDrivenPolicy newPolicy = new RuleDrivenPolicy("MatchesAnotherName");

            newPolicy.RuleSet.Add(new MemberNameMatchingRule("NameDoesntMatch"));
            newPolicy.Handlers.Add(new Handler2());

            policies.Add(newPolicy);

            List <ICallHandler> handlersAfter =
                new List <ICallHandler>(
                    policies.GetHandlersFor(GetNameDoesntMatchMethod()));

            Assert.AreEqual(2, handlersAfter.Count);

            newPolicy.Handlers.Add(new Handler3());
        }
Example #19
0
        public void ShouldBeInvokable()
        {
            PolicySet       policies = GetPolicies();
            HandlerPipeline pipeline =
                new HandlerPipeline(policies.GetHandlersFor(GetTargetMemberInfo()));

            IMethodReturn result = pipeline.Invoke(
                MakeCallMessage(),
                delegate(IMethodInvocation message, GetNextHandlerDelegate getNext)
            {
                return(MakeReturnMessage(message));
            });

            Assert.IsNotNull(result);
            Assert.AreEqual(1, callCountHandler.CallCount);
            Assert.AreEqual(returnHandler.ValueToRewriteTo, (string)result.ReturnValue);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PolicyInjectionBehavior"/> with the given information
        /// about what's being intercepted and the current set of injection policies.
        /// </summary>
        /// <param name="interceptionRequest">Information about what will be injected.</param>
        /// <param name="policies">Current injection policies.</param>
        /// <param name="container">Unity container that can be used to resolve call handlers.</param>
        public PolicyInjectionBehavior(CurrentInterceptionRequest interceptionRequest, InjectionPolicy[] policies,
            IUnityContainer container)
        {
            var allPolicies = new PolicySet(policies);
            bool hasHandlers = false;

            var manager = new PipelineManager();

            foreach (MethodImplementationInfo method in
                interceptionRequest.Interceptor.GetInterceptableMethods(
                    interceptionRequest.TypeToIntercept, interceptionRequest.ImplementationType))
            {
                bool hasNewHandlers = manager.InitializePipeline(method,
                    allPolicies.GetHandlersFor(method, container));
                hasHandlers = hasHandlers || hasNewHandlers;
            }
            pipelineManager = hasHandlers ? manager : null;
        }
        private ISignatureTestTarget GetTarget()
        {
            var                interceptor = new InterfaceInterceptor();
            PolicySet          policySet   = GetPolicies();
            var                target      = new SignatureTestTarget();
            IInterceptingProxy proxy       = interceptor.CreateProxy(typeof(ISignatureTestTarget), target);

            PipelineManager manager = new PipelineManager();

            foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(typeof(ISignatureTestTarget), typeof(SignatureTestTarget)))
            {
                HandlerPipeline pipeline = new HandlerPipeline(
                    policySet.GetHandlersFor(method, container));
                manager.SetPipeline(method.ImplementationMethodInfo, pipeline);
            }
            proxy.AddInterceptionBehavior(new PolicyInjectionBehavior(manager));

            return((ISignatureTestTarget)proxy);
        }
Example #22
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PolicyInjectionBehavior"/> with the given information
        /// about what's being intercepted and the current set of injection policies.
        /// </summary>
        /// <param name="interceptionRequest">Information about what will be injected.</param>
        /// <param name="policies">Current injection policies.</param>
        /// <param name="container">Unity container that can be used to resolve call handlers.</param>
        public PolicyInjectionBehavior(CurrentInterceptionRequest interceptionRequest, InjectionPolicy[] policies,
                                       IUnityContainer container)
        {
            Guard.ArgumentNotNull(interceptionRequest, "interceptionRequest");

            var  allPolicies = new PolicySet(policies);
            bool hasHandlers = false;

            var manager = new PipelineManager();

            foreach (MethodImplementationInfo method in
                     interceptionRequest.Interceptor.GetInterceptableMethods(
                         interceptionRequest.TypeToIntercept, interceptionRequest.ImplementationType))
            {
                bool hasNewHandlers = manager.InitializePipeline(method,
                                                                 allPolicies.GetHandlersFor(method, container));
                hasHandlers = hasHandlers || hasNewHandlers;
            }
            _pipelineManager = hasHandlers ? manager : null;
        }
Example #23
0
        public void ShouldNotDuplicateHandlersWhenCreatingViaInterface()
        {
            container
            .RegisterInstance <ICallHandler>("Handler1", new CallCountHandler())
            .RegisterInstance <ICallHandler>("Handler2", new CallCountHandler());

            RuleDrivenPolicy policy
                = new RuleDrivenPolicy("MatchesInterfacePolicy",
                                       new IMatchingRule[] { new TypeMatchingRule("ITwo") },
                                       new string[] { "Handler1", "Handler2" });

            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(2, handlers.Count);
        }
Example #24
0
        public void ShouldBeAbleToAddOnePolicy()
        {
            PolicySet policies = new PolicySet();

            RuleDrivenPolicy p = new RuleDrivenPolicy("NameMatching");

            p.RuleSet.Add(new MemberNameMatchingRule("ShouldBeAbleToAddOnePolicy"));
            p.Handlers.Add(new Handler1());
            p.Handlers.Add(new Handler2());

            policies.Add(p);

            MethodBase          thisMember = GetType().GetMethod("ShouldBeAbleToAddOnePolicy");
            List <ICallHandler> handlers   =
                new List <ICallHandler>(policies.GetHandlersFor(thisMember));

            Assert.IsTrue(policies.AppliesTo(GetType()));
            Assert.AreEqual(2, handlers.Count);
            Assert.IsTrue(typeof(Handler1) == handlers[0].GetType());
            Assert.IsTrue(typeof(Handler2) == handlers[1].GetType());
        }
Example #25
0
        public void ShouldBeAbleToAddOnePolicy()
        {
            container
            .RegisterInstance <ICallHandler>("Handler1", new Handler1())
            .RegisterInstance <ICallHandler>("Handler2", new Handler2());

            PolicySet policies = new PolicySet();

            RuleDrivenPolicy p
                = new RuleDrivenPolicy(
                      "NameMatching",
                      new IMatchingRule[] { new MemberNameMatchingRule("ShouldBeAbleToAddOnePolicy") },
                      new string[] { "Handler1", "Handler2" });

            policies.Add(p);

            MethodImplementationInfo thisMember = GetMethodImplInfo <PolicySetFixture>("ShouldBeAbleToAddOnePolicy");
            List <ICallHandler>      handlers   =
                new List <ICallHandler>(policies.GetHandlersFor(thisMember, container));

            Assert.AreEqual(2, handlers.Count);
            Assert.IsTrue(typeof(Handler1) == handlers[0].GetType());
            Assert.IsTrue(typeof(Handler2) == handlers[1].GetType());
        }
Example #26
0
 public void ShouldBeCreateableWithHandlers()
 {
     PolicySet       policies = GetPolicies();
     HandlerPipeline pipeline =
         new HandlerPipeline(policies.GetHandlersFor(GetTargetMemberInfo()));
 }
        private void ApplyPolicies(IInterceptor interceptor, IInterceptingProxy proxy, object target, PolicySet policies)
        {
            PipelineManager manager = new PipelineManager();

            foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(target.GetType(), target.GetType()))
            {
                HandlerPipeline pipeline = new HandlerPipeline(policies.GetHandlersFor(method, container));
                manager.SetPipeline(method.ImplementationMethodInfo, pipeline);
            }

            proxy.AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
        }