public void AddAndRemoveEventHandlerThroughIntroduction() { TestObject target = new TestObject(); DoubleClickableIntroduction dci = new DoubleClickableIntroduction(); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(dci); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); target.Name = "SOME-NAME"; ProxyFactory pf = new ProxyFactory(target); pf.AddIntroduction(advisor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = proxy as ITestObject; Assert.IsNotNull(to); Assert.AreEqual("SOME-NAME", to.Name); IDoubleClickable doubleClickable = proxy as IDoubleClickable; // add event handler through introduction doubleClickable.DoubleClick += new EventHandler(OnClick); OnClickWasCalled = false; doubleClickable.FireDoubleClickEvent(); Assert.IsTrue(OnClickWasCalled); Assert.AreEqual(3, countingBeforeAdvice.GetCalls()); // remove event handler through introduction doubleClickable.DoubleClick -= new EventHandler(OnClick); OnClickWasCalled = false; doubleClickable.FireDoubleClickEvent(); Assert.IsFalse(OnClickWasCalled); Assert.AreEqual(5, countingBeforeAdvice.GetCalls()); }
public void DoesNotCacheWithDifferentInterfaces() { ProxyFactory advisedSupport = new ProxyFactory(new TestObject()); CreateAopProxy(advisedSupport); advisedSupport = new ProxyFactory(new TestObject()); advisedSupport.AddInterface(typeof(IPerson)); CreateAopProxy(advisedSupport); AssertAopProxyTypeCacheCount(2); // Same with Introductions advisedSupport = new ProxyFactory(new TestObject()); TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.TimeStamp = new DateTime(666L); IIntroductionAdvisor introduction = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)); advisedSupport.AddIntroduction(introduction); CreateAopProxy(advisedSupport); AssertAopProxyTypeCacheCount(3); }
public void CanAddAndRemoveAspectInterfacesOnPrototype() { try { ITimeStamped ts = (ITimeStamped)factory.GetObject("test2"); Assert.Fail("Shouldn't implement ITimeStamped before manipulation"); } catch (InvalidCastException) { } IAdvised config = (IAdvised)factory.GetObject("&test2"); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.TimeStamp = new DateTime(time); IIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)); // add to front of introduction chain int oldCount = config.Introductions.Count; config.AddIntroduction(0, advisor); Assert.IsTrue(config.Introductions.Count == oldCount + 1); ITimeStamped ts2 = (ITimeStamped)factory.GetObject("test2"); Assert.IsTrue(ts2.TimeStamp == new DateTime(time)); // Can remove config.RemoveIntroduction(advisor); Assert.IsTrue(config.Introductions.Count == oldCount); // Existing reference will still work object o = ts2.TimeStamp; // But new proxies should not implement ITimeStamped try { ts2 = (ITimeStamped)factory.GetObject("test2"); Assert.Fail("Should no longer implement ITimeStamped"); } catch (InvalidCastException) { } // Now check non-effect of removing interceptor that isn't there ITestObject it = (ITestObject)factory.GetObject("test2"); config = (IAdvised)it; oldCount = config.Advisors.Count; config.RemoveAdvice(new DebugAdvice()); Assert.IsTrue(config.Advisors.Count == oldCount); DebugAdvice debugInterceptor = new DebugAdvice(); config.AddAdvice(0, debugInterceptor); object foo = it.Spouse; Assert.AreEqual(1, debugInterceptor.Count); config.RemoveAdvice(debugInterceptor); foo = it.Spouse; // not invoked again Assert.IsTrue(debugInterceptor.Count == 1); }