public void CanImplementINotifyPropertyChangedThroughExplicitInterface() { ObjectWithOnePropertyForExplicitlyImplementedInterface target = new ObjectWithOnePropertyForExplicitlyImplementedInterface(); IInterfaceWithOneProperty proxy = new InterceptingRealProxy(target, typeof(IInterfaceWithOneProperty), typeof(INotifyPropertyChanged)) .GetTransparentProxy() as IInterfaceWithOneProperty; ((IInterceptingProxy)proxy).AddInterceptionBehavior(new NaiveINotifyPropertyChangedInterceptionBehavior()); string changeProperty; PropertyChangedEventHandler handler = (sender, args) => changeProperty = args.PropertyName; changeProperty = null; ((INotifyPropertyChanged)proxy).PropertyChanged += handler; proxy.TheProperty = 100; Assert.AreEqual(100, proxy.TheProperty); Assert.AreEqual("TheProperty", changeProperty); changeProperty = null; ((INotifyPropertyChanged)proxy).PropertyChanged -= handler; proxy.TheProperty = 200; Assert.AreEqual(200, proxy.TheProperty); Assert.AreEqual(null, changeProperty); }
public void ProxyImplementsIInterceptingProxy() { MBROWithOneMethod original = new MBROWithOneMethod(); MBROWithOneMethod proxy = new InterceptingRealProxy(original, typeof(MBROWithOneMethod)) .GetTransparentProxy() as MBROWithOneMethod; Assert.IsNotNull(proxy as IInterceptingProxy); }
public void CanCreateProxyWithAdditionalInterfaces() { MBROWithOneMethod original = new MBROWithOneMethod(); MBROWithOneMethod proxy = new InterceptingRealProxy(original, typeof(MBROWithOneMethod), typeof(InterfaceOne)) .GetTransparentProxy() as MBROWithOneMethod; Assert.IsTrue(proxy is InterfaceOne); }
public void CanSuccessfullyInvokeAnAdditionalInterfaceMethodIfAnInterceptorDoesNotForwardTheCall() { MBROWithOneMethod original = new MBROWithOneMethod(); InterfaceOne proxy = new InterceptingRealProxy(original, typeof(MBROWithOneMethod), typeof(InterfaceOne)) .GetTransparentProxy() as InterfaceOne; bool invoked = false; ((IInterceptingProxy)proxy).AddInterceptionBehavior( new DelegateInterceptionBehavior( (input, getNext) => { invoked = true; return(input.CreateMethodReturn(null)); })); proxy.Something(); Assert.IsTrue(invoked); }
public void CanInterceptMethodsThroughProxy() { CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior(); MBROWithOneMethod original = new MBROWithOneMethod(); MBROWithOneMethod intercepted = new InterceptingRealProxy(original, typeof(MBROWithOneMethod)) .GetTransparentProxy() as MBROWithOneMethod; IInterceptingProxy proxy = (IInterceptingProxy)intercepted; proxy.AddInterceptionBehavior(interceptor); int result = intercepted.DoSomething(5); Assert.AreEqual(5 * 3, result); Assert.AreEqual(1, interceptor.CallCount); }
public void CanInterceptGenericMethodOnInterface() { var interceptor = new CallCountInterceptionBehavior(); var original = new ObjectWithGenericMethod(); var intercepted = new InterceptingRealProxy(original, typeof(IInterfaceWithGenericMethod)) .GetTransparentProxy() as IInterfaceWithGenericMethod; IInterceptingProxy proxy = (IInterceptingProxy)intercepted; proxy.AddInterceptionBehavior(interceptor); var result = intercepted.GetTypeName(6); Assert.AreEqual("Int32", result); Assert.AreEqual(1, interceptor.CallCount); }
public void InvokingMethodOnAdditionalInterfaceThrowsIfNotHandledByInterceptor() { MBROWithOneMethod original = new MBROWithOneMethod(); InterfaceOne proxy = new InterceptingRealProxy(original, typeof(MBROWithOneMethod), typeof(InterfaceOne)) .GetTransparentProxy() as InterfaceOne; try { proxy.Something(); Assert.Fail("should have thrown"); } catch (InvalidOperationException) { // expected } }
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 ProxyInterceptsAddingAHandlerToAnEvent() { // arrange CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior(); MBROWithAnEvent original = new MBROWithAnEvent(); MBROWithAnEvent intercepted = new InterceptingRealProxy(original, typeof(MBROWithAnEvent)) .GetTransparentProxy() as MBROWithAnEvent; ((IInterceptingProxy)intercepted).AddInterceptionBehavior(interceptor); // act intercepted.SomeEvent += (s, a) => { }; // assert Assert.AreEqual(1, interceptor.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 ProxySendsOriginalWhenRaisingEvent() { // arrange CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior(); MBROWithAnEvent original = new MBROWithAnEvent(); MBROWithAnEvent intercepted = new InterceptingRealProxy(original, typeof(MBROWithAnEvent)) .GetTransparentProxy() as MBROWithAnEvent; ((IInterceptingProxy)intercepted).AddInterceptionBehavior(interceptor); object sender = null; intercepted.SomeEvent += (s, a) => { sender = s; }; // act intercepted.TriggerIt(); // assert Assert.AreSame(original, sender); Assert.AreEqual(2, interceptor.CallCount); // adding + calling TriggerIt }