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); }
private PolicySet GetPolicies() { PolicySet policies = new PolicySet(); RuleDrivenPolicy noParamsPolicy = new RuleDrivenPolicy("noParamsPolicy"); noParamsPolicy.RuleSet.Add(new MatchByNameRule("MethodWithNoParameters")); noParamsPolicy.Handlers.Add(new SignatureCheckingHandler(new Type[] { })); policies.Add(noParamsPolicy); RuleDrivenPolicy simpleInputsPolicy = new RuleDrivenPolicy("simpleInputsPolicy"); simpleInputsPolicy.RuleSet.Add(new MatchByNameRule("MethodWithSimpleInputs")); simpleInputsPolicy.Handlers.Add(new SignatureCheckingHandler(new Type[] { typeof(int), typeof(string) })); policies.Add(simpleInputsPolicy); RuleDrivenPolicy outParamsPolicy = new RuleDrivenPolicy("outParamsPolicy"); outParamsPolicy.RuleSet.Add(new MatchByNameRule("MethodWithOutParams")); outParamsPolicy.Handlers.Add(new SignatureCheckingHandler(new Type[] { typeof(int).MakeByRefType(), typeof(string).MakeByRefType() })); policies.Add(outParamsPolicy); RuleDrivenPolicy mixedParamsPolicy = new RuleDrivenPolicy("mixedParamsPolicy"); mixedParamsPolicy.RuleSet.Add(new MatchByNameRule("MethodWithInOutByrefParams")); mixedParamsPolicy.Handlers.Add(new SignatureCheckingHandler( new Type[] { typeof(int), typeof(string).MakeByRefType(), typeof(float).MakeByRefType(), typeof(decimal) })); policies.Add(mixedParamsPolicy); RuleDrivenPolicy varargsParamsPolicy = new RuleDrivenPolicy("varargsParamsPolicy"); varargsParamsPolicy.RuleSet.Add(new MatchByNameRule("MethodWithVarArgs")); varargsParamsPolicy.Handlers.Add(new SignatureCheckingHandler( new Type[] { typeof(int), typeof(string).MakeArrayType() })); policies.Add(varargsParamsPolicy); return(policies); }
public void ShouldReturnAllPoliciesInOrderThatApplyToTarget() { PolicySet policies = new PolicySet(); Policy p1 = new Policy("DALPolicy"); p1.RuleSet.Add(new TypeMatchingAssignmentRule(typeof(MockDal))); Policy p2 = new Policy("LoggingPolicy"); p2.RuleSet.Add(new TypeMatchingAssignmentRule(typeof(string))); Policy p3 = new Policy("ExceptionPolicy"); p3.RuleSet.Add(new TypeMatchingAssignmentRule(typeof(MockDal))); policies.Add(p1); policies.Add(p2); policies.Add(p3); PolicySet matchingPolicies = policies.GetPoliciesFor(typeof(MockDal)); Assert.AreEqual(2, matchingPolicies.Count); Assert.AreSame(p1, matchingPolicies[0]); Assert.AreSame(p3, matchingPolicies[1]); }
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()); }
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()); }
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()); }
private 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() { 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; }