public void CanInterceptOnFormsClass() { CallCountHandler countHandler = new CallCountHandler(); RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPolicySet(countHandler)); IMyForm magicForm = factory.Create<MyForm, IMyForm>(); }
public void CanInterceptOnFormsClass() { CallCountHandler countHandler = new CallCountHandler(); RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPolicySet(countHandler)); IMyForm magicForm = factory.Create <MyForm, IMyForm>(); }
public void ParametersPassProperlyToTarget() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); MyDal target = new MyDal(); CallCountHandler depositHandler = new CallCountHandler(); CallCountHandler withdrawHandler = new CallCountHandler(); PipelineManager manager = new PipelineManager(); manager.SetPipeline(typeof(IDal).GetMethod("Deposit"), new HandlerPipeline(new ICallHandler[] { depositHandler })); manager.SetPipeline(typeof(IDal).GetMethod("Withdraw"), new HandlerPipeline(new ICallHandler[] { withdrawHandler })); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IDal), target); proxy.AddInterceptionBehavior(new PolicyInjectionBehavior(manager)); IDal intercepted = (IDal)proxy; intercepted.Deposit(100.0); intercepted.Deposit(25.95); intercepted.Deposit(19.95); intercepted.Withdraw(15.00); intercepted.Withdraw(6.25); Assert.AreEqual(3, depositHandler.CallCount); Assert.AreEqual(2, withdrawHandler.CallCount); Assert.AreEqual(100.0 + 25.95 + 19.95 - 15.00 - 6.25, target.Balance); }
public void CanSetUpAPolicyWithGivenRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); IMatchingRule rule1 = new AlwaysMatchingRule(); ICallHandler handler1 = new CallCountHandler(); container .Configure <Interception>() .AddPolicy("policy1") .AddMatchingRule(rule1) .AddCallHandler(handler1); container .Configure <Interception>() .SetInterceptorFor <Wrappable>("wrappable", new VirtualMethodInterceptor()); Wrappable wrappable1 = container.Resolve <Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, ((CallCountHandler)handler1).CallCount); }
public void CanSetUpAPolicyWithLifetimeManagedNamedInjectedRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container .Configure<Interception>() .AddPolicy("foo") .AddMatchingRule<AlwaysMatchingRule>( "rule1", new ContainerControlledLifetimeManager()) .AddCallHandler<CallCountHandler>( "handler1", (LifetimeManager)null) .AddCallHandler<CallCountHandler>( "handler2", new ContainerControlledLifetimeManager(), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); CallCountHandler handler1 = (CallCountHandler)container.Resolve<ICallHandler>("handler1"); CallCountHandler handler2 = (CallCountHandler)container.Resolve<ICallHandler>("handler2"); Assert.AreEqual(0, handler1.CallCount); // not lifetime maanaged Assert.AreEqual(1, handler2.CallCount); // lifetime managed }
public void CanInterceptOpenGenericInterfaces() { IUnityContainer container = new UnityContainer() .AddNewExtension <Interception>() .RegisterType(typeof(InterfaceInterceptorFixture.IGenericOne <>), typeof(InterfaceInterceptorFixture.GenericImplementationOne <>)) .Configure <Interception>() .SetInterceptorFor(typeof(InterfaceInterceptorFixture.IGenericOne <>), new InterfaceInterceptor()) .AddPolicy("AlwaysMatches") .AddMatchingRule <AlwaysMatchingRule>() .AddCallHandler <CallCountHandler>("callCount", new ContainerControlledLifetimeManager()) .Interception .Container; InterfaceInterceptorFixture.IGenericOne <decimal> target = container.Resolve <InterfaceInterceptorFixture.IGenericOne <decimal> >(); decimal result = target.DoSomething(52m); Assert.AreEqual(52m, result); target.DoSomething(17m); target.DoSomething(84.2m); CallCountHandler handler = (CallCountHandler)(container.Resolve <ICallHandler>("callCount")); Assert.AreEqual(3, handler.CallCount); }
public void CanInterceptInstancesViaTheContainer() { IUnityContainer container = new UnityContainer() .AddNewExtension <Interception>() .RegisterType <IDal, MockDal>() .Configure <Interception>() .SetInterceptorFor <IDal>(new InterfaceInterceptor()) .AddPolicy("AlwaysMatches") .AddMatchingRule <AlwaysMatchingRule>() .AddCallHandler <CallCountHandler>("callCount", new ContainerControlledLifetimeManager()) .Interception .Container; IDal dal = container.Resolve <IDal>(); Assert.IsTrue(dal is IInterceptingProxy); dal.Deposit(50.0); dal.Deposit(65.0); dal.Withdraw(15.0); CallCountHandler handler = (CallCountHandler)(container.Resolve <ICallHandler>("callCount")); Assert.AreEqual(3, handler.CallCount); }
public void ThrowingFromInterceptedMethodStillRunsAllHandlers() { MethodInfo thrower = typeof(ClassWithDefaultCtor).GetMethod("NotImplemented"); ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>(); IInterceptingProxy pm = (IInterceptingProxy)instance; CallCountHandler handler = new CallCountHandler(); PostCallCountHandler postHandler = new PostCallCountHandler(); HandlerPipeline pipeline = new HandlerPipeline(new ICallHandler[] { postHandler, handler }); PipelineManager manager = new PipelineManager(); manager.SetPipeline(thrower, pipeline); pm.AddInterceptionBehavior(new PolicyInjectionBehavior(manager)); try { instance.NotImplemented(); Assert.Fail("Should have thrown before getting here"); } catch (NotImplementedException) { // We're expecting this one } Assert.AreEqual(1, handler.CallCount); Assert.AreEqual(1, postHandler.CallsCompleted); }
public void ImplicitlyImplementedMethodsAreInterceptedIfVirtual() { CallCountHandler handler = new CallCountHandler(); Interesting instance = WireupHelper.GetInterceptedInstance <Interesting>("DoSomethingInteresting", handler); instance.DoSomethingInteresting(); Assert.IsTrue(instance.SomethingWasCalled); Assert.AreEqual(1, handler.CallCount); }
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); }
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]); }
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); }
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); }
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)); }
IUnityContainer GetContainer() { callCountHandler = new CallCountHandler(); returnHandler = new StringReturnRewriteHandler("REWRITE"); IUnityContainer container = new UnityContainer() .RegisterInstance <ICallHandler>("call count", callCountHandler) .RegisterInstance <ICallHandler>("rewrite", returnHandler); return(container); }
public void CanAddHandlersToPipeline() { MethodInfo methodOne = typeof(ClassWithDefaultCtor).GetMethod("MethodOne"); ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>(); IInterceptingProxy pm = (IInterceptingProxy)instance; CallCountHandler handler = new CallCountHandler(); HandlerPipeline pipeline = new HandlerPipeline(new CallCountHandler[] { handler }); pm.SetPipeline(methodOne, pipeline); }
public void ConfigureContainerReturnEmptyIfNoInterception() { ICallHandler myHandler1 = new CallCountHandler(); IMatchingRule myMatchingRule1 = new AlwaysMatchingRule(); IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container.Configure <Interception>(). AddPolicy("myRDP"). AddCallHandler(myHandler1). AddMatchingRule(myMatchingRule1); Assert.AreEqual(0, ((CallCountHandler)myHandler1).CallCount); }
public void InterceptedClassGetsReturned() { CallCountHandler h1 = new CallCountHandler(); CallCountHandler h2 = new CallCountHandler(); IUnityContainer container = GetConfiguredContainer(h1, h2); AddPoliciesToContainer(container); ConfigureInterceptionWithRegisterType(container); Interceptee target = container.Resolve <Interceptee>(); Assert.AreNotSame(typeof(Interceptee), target.GetType()); }
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 Setup() { this.handler = new CallCountHandler(); var container = new UnityContainer(); container.AddNewExtension <Interception>(); container.AddNewExtension <TransientPolicyBuildUpExtension>(); container.Configure <Interception>() .AddPolicy("policy") .AddMatchingRule(new AlwaysMatchingRule()) .AddCallHandler(this.handler); this.policyInjector = new PolicyInjector(new UnityServiceLocator(container)); }
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 CanInterceptMethods() { CallCountHandler h1 = new CallCountHandler(); VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor(); Type proxyType = interceptor.CreateProxyType(typeof(TwoOverrideableMethods)); TwoOverrideableMethods instance = (TwoOverrideableMethods)Activator.CreateInstance(proxyType); SetPipeline(instance, "DoSomething", h1); instance.DoSomething(); Assert.IsTrue(instance.DidSomething); Assert.AreEqual(1, h1.CallCount); }
public void CallingMethodInvokesHandlers() { MethodInfo methodOne = typeof(ClassWithDefaultCtor).GetMethod("MethodOne"); ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>(); IInterceptingProxy pm = (IInterceptingProxy)instance; CallCountHandler handler = new CallCountHandler(); PostCallCountHandler postHandler = new PostCallCountHandler(); HandlerPipeline pipeline = new HandlerPipeline(new ICallHandler[] { postHandler, handler }); pm.SetPipeline(methodOne, pipeline); instance.MethodOne(); Assert.AreEqual(1, handler.CallCount); Assert.AreEqual(1, postHandler.CallsCompleted); }
public void ConfigureContainerbyAddingPolicyHanlderRule() { ICallHandler myHandler1 = new CallCountHandler(); IMatchingRule myMatchingRule1 = new AlwaysMatchingRule(); IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.Configure<Interception>(). AddPolicy("myRDP"). AddCallHandler(myHandler1). AddMatchingRule(myMatchingRule1); container.Configure<Interception>(). SetInterceptorFor<InterceptionClass>("interceptionclass", new TransparentProxyInterceptor()); InterceptionClass ic = container.Resolve<InterceptionClass>("interceptionclass"); ic.MethodA(); Assert.AreEqual(1, ((CallCountHandler)myHandler1).CallCount); }
public void CanInterceptMethods() { CallCountHandler h1 = new CallCountHandler(); VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor(); Type proxyType = interceptor.CreateProxyType(typeof(TwoOverrideableMethods)); TwoOverrideableMethods instance = (TwoOverrideableMethods)Activator.CreateInstance(proxyType); PipelineManager manager = new PipelineManager(); ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager)); SetPipeline(manager, instance, "DoSomething", h1); instance.DoSomething(); Assert.IsTrue(instance.DidSomething); Assert.AreEqual(1, h1.CallCount); }
public void CanInterceptTypeWithNonDefaultCtor() { CallCountHandler h1 = new CallCountHandler(); VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor(); Type proxyType = interceptor.CreateProxyType(typeof(ClassWithNonDefaultCtor)); ClassWithNonDefaultCtor instance = (ClassWithNonDefaultCtor)Activator.CreateInstance(proxyType, "arg-value"); PipelineManager manager = new PipelineManager(); ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager)); SetPipeline(manager, instance, "GetArg", h1); Assert.AreEqual("arg-value", instance.GetArg()); Assert.AreEqual(1, h1.CallCount); }
public void CanInterceptMethodsThroughProxy() { MethodInfo doSomething = typeof(MBROWithOneMethod).GetMethod("DoSomething"); CallCountHandler handler = new CallCountHandler(); MBROWithOneMethod original = new MBROWithOneMethod(); MBROWithOneMethod intercepted = new InterceptingRealProxy(original, typeof(MBROWithOneMethod)) .GetTransparentProxy() as MBROWithOneMethod; IInterceptingProxy proxy = (IInterceptingProxy)intercepted; proxy.SetPipeline(doSomething, new HandlerPipeline(Seq.Collect <ICallHandler>(handler))); int result = intercepted.DoSomething(5); Assert.AreEqual(5 * 3, result); Assert.AreEqual(1, handler.CallCount); }
public void CanGenerateProxyForClosedGeneric() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); GenericImplementationOne <DateTime> target = new GenericImplementationOne <DateTime>(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IGenericOne <DateTime>), target); CallCountHandler handler = new CallCountHandler(); proxy.SetPipeline(typeof(IGenericOne <DateTime>).GetMethod("DoSomething"), new HandlerPipeline(new ICallHandler[] { handler })); IGenericOne <DateTime> intercepted = (IGenericOne <DateTime>)proxy; DateTime now = DateTime.Now; DateTime result = intercepted.DoSomething(now); Assert.AreEqual(now, result); Assert.IsTrue(target.DidSomething); }
public void GeneratedProxyCallsHandlers() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); ImplementsInterfaceOne target = new ImplementsInterfaceOne(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target); CallCountHandler handler = new CallCountHandler(); proxy.SetPipeline(typeof(IInterfaceOne).GetMethod("TargetMethod"), new HandlerPipeline(new ICallHandler[] { handler })); IInterfaceOne intercepted = (IInterfaceOne)proxy; intercepted.TargetMethod(); intercepted.TargetMethod(); intercepted.TargetMethod(); Assert.AreEqual(3, handler.CallCount); }
public void ProxyMapsInterfaceMethodsToTheirImplementations() { MethodInfo something = typeof(InterfaceOne).GetMethod("Something"); MethodInfo somethingImpl = typeof(MBROWithInterface).GetMethod("Something"); CallCountHandler handler = new CallCountHandler(); MBROWithInterface original = new MBROWithInterface(); MBROWithInterface intercepted = new InterceptingRealProxy(original, typeof(MBROWithOneMethod)) .GetTransparentProxy() as MBROWithInterface; HandlerPipeline pipeline = new HandlerPipeline(Seq.Collect <ICallHandler>(handler)); IInterceptingProxy proxy = (IInterceptingProxy)intercepted; proxy.SetPipeline(something, pipeline); HandlerPipeline implPipeline = proxy.GetPipeline(somethingImpl); Assert.AreSame(pipeline, implPipeline); }
public void ConfigureContainerbyAddingPolicyHanlderRule() { ICallHandler myHandler1 = new CallCountHandler(); IMatchingRule myMatchingRule1 = new AlwaysMatchingRule(); IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container.Configure <Interception>(). AddPolicy("myRDP"). AddCallHandler(myHandler1). AddMatchingRule(myMatchingRule1); container.Configure <Interception>(). SetInterceptorFor <InterceptionClass>("interceptionclass", new TransparentProxyInterceptor()); InterceptionClass ic = container.Resolve <InterceptionClass>("interceptionclass"); ic.MethodA(); Assert.AreEqual(1, ((CallCountHandler)myHandler1).CallCount); }
public void ShouldCallHandlerOnImplementationMethodWhenCalledViaInterface() { // Set up policy to match underlying type and method name RuleDrivenPolicy policy = new RuleDrivenPolicy("MockDal.Deposit policy"); policy.RuleSet.Add(new TypeMatchingRule(typeof(MockDal))); policy.RuleSet.Add(new MemberNameMatchingRule("Deposit")); countHandler = new CallCountHandler(); policy.Handlers.Add(countHandler); RemotingPolicyInjector factory = new RemotingPolicyInjector(new PolicySet(policy)); IDal dal = factory.Create <MockDal, IDal>(); dal.Deposit(10); dal.Deposit(54); dal.Deposit(72.5); Assert.AreEqual(3, countHandler.CallCount); }
public void ConfigureContainerReturnEmptyIfNoInterception() { ICallHandler myHandler1 = new CallCountHandler(); IMatchingRule myMatchingRule1 = new AlwaysMatchingRule(); IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.Configure<Interception>(). AddPolicy("myRDP"). AddCallHandler(myHandler1). AddMatchingRule(myMatchingRule1); Assert.AreEqual(0, ((CallCountHandler)myHandler1).CallCount); }