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); }
public void ConfiguresCustomCallHandlerAsSingleton() { PolicyInjectionSettings settings = new PolicyInjectionSettings(); settings.Policies.Add(new PolicyData("Policy")); settings.Policies.Get(0).MatchingRules.Add(new CustomMatchingRuleData("alwaysTrue", typeof(AlwaysMatchingRule))); settings.Policies.Get(0).Handlers.Add(new CustomCallHandlerData("callCountHandler", typeof(CallCountHandler))); DictionaryConfigurationSource dictSource = new DictionaryConfigurationSource(); dictSource.Add(PolicyInjectionSettings.SectionName, settings); settings.ConfigureContainer(container); RuleDrivenPolicy policy = container.Resolve <RuleDrivenPolicy>("Policy"); List <ICallHandler> handlers1 = new List <ICallHandler>(policy.GetHandlersFor(MakeMethodImplementationInfo(MethodBase.GetCurrentMethod()), container)); List <ICallHandler> handlers2 = new List <ICallHandler>(policy.GetHandlersFor(MakeMethodImplementationInfo(MethodBase.GetCurrentMethod()), container)); CollectionAssert.AreEquivalent(handlers1, handlers2); }
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); }
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); }
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); }
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]); }
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]); }
public void ShouldGetHandlersInOrderWithGetHandlersFor() { RuleDrivenPolicy p = new RuleDrivenPolicy("OrderedHandlers"); p.RuleSet.Add(new MemberNameMatchingRule("ShouldGetHandlersInOrderWithGetHandlersFor")); ICallHandler[] handlers = { new Handler1(), new Handler2(), new Handler3() }; Array.ForEach(handlers, delegate(ICallHandler handler) { p.Handlers.Add(handler); }); int i = 0; MethodBase member = this.GetType().GetMethod("ShouldGetHandlersInOrderWithGetHandlersFor"); foreach (ICallHandler h in p.GetHandlersFor(member)) { Assert.AreSame(handlers[i], h); ++i; } Assert.AreEqual(3, i); // Make sure we actually got handlers }
public void CanCreatePolicyWithCustomCallHandler() { PolicyInjectionSettings settings = new PolicyInjectionSettings(); settings.Policies.Add(new PolicyData("Policy")); settings.Policies.Get(0).MatchingRules.Add(new CustomMatchingRuleData("alwaysTrue", typeof(AlwaysMatchingRule))); settings.Policies.Get(0).Handlers.Add(new CustomCallHandlerData("callCountHandler", typeof(CallCountHandler))); DictionaryConfigurationSource dictSource = new DictionaryConfigurationSource(); dictSource.Add(PolicyInjectionSettings.SectionName, settings); settings.ConfigureContainer(container); RuleDrivenPolicy policy = container.Resolve <RuleDrivenPolicy>("Policy"); List <ICallHandler> handlers = new List <ICallHandler>(policy.GetHandlersFor(MakeMethodImplementationInfo(MethodBase.GetCurrentMethod()), container)); Assert.IsNotNull(policy); Assert.AreEqual("Policy", policy.Name); Assert.AreEqual(1, handlers.Count); Assert.AreEqual(typeof(CallCountHandler), handlers[0].GetType()); }