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 ReplaceAdvisor() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba1 = new CountingBeforeAdvice(); CountingBeforeAdvice cba2 = new CountingBeforeAdvice(); IAdvisor advisor1 = new DefaultPointcutAdvisor(cba1); IAdvisor advisor2 = new DefaultPointcutAdvisor(cba2); pf.AddAdvisor(advisor1); pf.AddAdvice(nop); ITestObject proxied = (ITestObject)pf.GetProxy(); // Use the type cast feature // Replace etc methods on advised should be same as on ProxyFactory IAdvised advised = (IAdvised)proxied; proxied.Age = 5; Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(0, cba2.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(advised.ReplaceAdvisor(null, null)); Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2)); Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null)); Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2)); Assert.AreEqual(advisor2, pf.Advisors[0]); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(1, cba2.GetCalls()); Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); }
public void AddAndRemoveEventHandlerThroughInterceptor() { TestObject target = new TestObject(); NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); target.Name = "SOME-NAME"; ProxyFactory pf = new ProxyFactory(target); pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = proxy as ITestObject; // add event handler through proxy to.Click += new EventHandler(OnClick); OnClickWasCalled = false; to.OnClick(); Assert.IsTrue(OnClickWasCalled); Assert.AreEqual(2, countingBeforeAdvice.GetCalls()); // remove event handler through proxy to.Click -= new EventHandler(OnClick); OnClickWasCalled = false; to.OnClick(); Assert.IsFalse(OnClickWasCalled); Assert.AreEqual(4, countingBeforeAdvice.GetCalls()); }
public void RemoveAdvisorByIndex() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); IAdvisor advisor = new DefaultPointcutAdvisor(cba); pf.AddAdvice(nop); pf.AddAdvisor(advisor); NopInterceptor nop2 = new NopInterceptor(2); // make instance unique (see SPRNET-847) pf.AddAdvice(nop2); ITestObject proxied = (ITestObject)pf.GetProxy(); proxied.Age = 5; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.AreEqual(1, nop2.Count); // Removes counting before advisor pf.RemoveAdvisor(1); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(2, nop2.Count); // Removes Nop1 pf.RemoveAdvisor(0); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(3, nop2.Count); // Check out of bounds try { pf.RemoveAdvisor(-1); Assert.Fail("Supposed to throw exception"); } catch (AopConfigException) { // Ok } try { pf.RemoveAdvisor(2); Assert.Fail("Supposed to throw exception"); } catch (AopConfigException) { // Ok } Assert.AreEqual(5, proxied.Age); Assert.AreEqual(4, nop2.Count); }
public void CanAddThrowsAdviceWithoutAdvisor() { IObjectFactory f = new XmlObjectFactory(new ReadOnlyXmlTestResource("throwsAdvice.xml", GetType())); ThrowsAdviceInterceptorTests.MyThrowsHandler th = (ThrowsAdviceInterceptorTests.MyThrowsHandler)f.GetObject("throwsAdvice"); CountingBeforeAdvice cba = (CountingBeforeAdvice)f.GetObject("countingBeforeAdvice"); Assert.AreEqual(0, cba.GetCalls()); Assert.AreEqual(0, th.GetCalls()); ThrowsAdviceInterceptorTests.IEcho echo = (ThrowsAdviceInterceptorTests.IEcho)f.GetObject("throwsAdvised"); echo.A = 12; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(0, th.GetCalls()); Exception expected = new Exception(); try { echo.EchoException(1, expected); Assert.Fail(); } catch (Exception ex) { Assert.AreEqual(expected, ex); } // No throws handler method: count should still be 0 Assert.AreEqual(0, th.GetCalls()); // Handler knows how to handle this exception expected = new HttpException(); try { echo.EchoException(1, expected); Assert.Fail(); } catch (HttpException ex) { Assert.AreEqual(expected, ex); } // One match Assert.AreEqual(1, th.GetCalls("HttpException")); }
public void GetObjectTypeWithDirectTarget() { IObjectFactory bf = new XmlObjectFactory( new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml", GetType())); // We have a counting before advice here CountingBeforeAdvice cba = (CountingBeforeAdvice)bf.GetObject("countingBeforeAdvice"); Assert.AreEqual(0, cba.GetCalls()); ITestObject tb = (ITestObject)bf.GetObject("directTarget"); Assert.IsTrue(tb.Name.Equals("Adam")); Assert.AreEqual(1, cba.GetCalls()); ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&directTarget"); Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type"); }
public void RemoveAdvisorByReference() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); IAdvisor advisor = new DefaultPointcutAdvisor(cba); pf.AddAdvice(nop); pf.AddAdvisor(advisor); ITestObject proxied = (ITestObject)pf.GetProxy(); proxied.Age = 5; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(pf.RemoveAdvisor(null)); Assert.IsTrue(pf.RemoveAdvisor(advisor)); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.IsFalse(pf.RemoveAdvisor(new DefaultPointcutAdvisor(null))); }
public void CreateProxyFactoryWithoutTargetThenSetTarget() { TestObject target = new TestObject(); target.Name = "Adam"; NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.Target = target; pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = (ITestObject)proxy; Assert.AreEqual("Adam", to.Name); Assert.AreEqual(1, countingBeforeAdvice.GetCalls()); }
public void ReplaceAdvisor() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba1 = new CountingBeforeAdvice(); CountingBeforeAdvice cba2 = new CountingBeforeAdvice(); IAdvisor advisor1 = new DefaultPointcutAdvisor(cba1); IAdvisor advisor2 = new DefaultPointcutAdvisor(cba2); pf.AddAdvisor(advisor1); pf.AddAdvice(nop); ITestObject proxied = (ITestObject)pf.GetProxy(); // Use the type cast feature // Replace etc methods on advised should be same as on ProxyFactory IAdvised advised = (IAdvised)proxied; proxied.Age = 5; Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(0, cba2.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(advised.ReplaceAdvisor(null, null)); Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2)); Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null)); Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2)); Assert.AreEqual(advisor2, pf.Advisors[0]); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba1.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(1, cba2.GetCalls()); Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); }
public void RemoveAdvisorByReference() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); IAdvisor advisor = new DefaultPointcutAdvisor(cba); pf.AddAdvice(nop); pf.AddAdvisor(advisor); ITestObject proxied = (ITestObject)pf.GetProxy(); proxied.Age = 5; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.IsFalse(pf.RemoveAdvisor(null)); Assert.IsTrue(pf.RemoveAdvisor(advisor)); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.IsFalse(pf.RemoveAdvisor(new DefaultPointcutAdvisor(null))); }
public void RemoveAdvisorByIndex() { TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); IAdvisor advisor = new DefaultPointcutAdvisor(cba); pf.AddAdvice(nop); pf.AddAdvisor(advisor); NopInterceptor nop2 = new NopInterceptor(2); // make instance unique (see SPRNET-847) pf.AddAdvice(nop2); ITestObject proxied = (ITestObject)pf.GetProxy(); proxied.Age = 5; Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(1, nop.Count); Assert.AreEqual(1, nop2.Count); // Removes counting before advisor pf.RemoveAdvisor(1); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(2, nop2.Count); // Removes Nop1 pf.RemoveAdvisor(0); Assert.AreEqual(5, proxied.Age); Assert.AreEqual(1, cba.GetCalls()); Assert.AreEqual(2, nop.Count); Assert.AreEqual(3, nop2.Count); // Check out of bounds try { pf.RemoveAdvisor(-1); Assert.Fail("Supposed to throw exception"); } catch (AopConfigException) { // Ok } try { pf.RemoveAdvisor(2); Assert.Fail("Supposed to throw exception"); } catch (AopConfigException) { // Ok } Assert.AreEqual(5, proxied.Age); Assert.AreEqual(4, nop2.Count); }
public void CreateProxyFactoryWithoutTargetThenSetTarget() { TestObject target = new TestObject(); target.Name = "Adam"; NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.Target = target; pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = (ITestObject)proxy; Assert.AreEqual("Adam", to.Name); Assert.AreEqual(1, countingBeforeAdvice.GetCalls()); }
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 AddAndRemoveEventHandlerThroughInterceptor() { TestObject target = new TestObject(); NopInterceptor nopInterceptor = new NopInterceptor(); CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice(); target.Name = "SOME-NAME"; ProxyFactory pf = new ProxyFactory(target); pf.AddAdvice(nopInterceptor); pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice)); object proxy = pf.GetProxy(); ITestObject to = proxy as ITestObject; // add event handler through proxy to.Click += new EventHandler(OnClick); OnClickWasCalled = false; to.OnClick(); Assert.IsTrue(OnClickWasCalled); Assert.AreEqual(2, countingBeforeAdvice.GetCalls()); // remove event handler through proxy to.Click -= new EventHandler(OnClick); OnClickWasCalled = false; to.OnClick(); Assert.IsFalse(OnClickWasCalled); Assert.AreEqual(4, countingBeforeAdvice.GetCalls()); }