@Test public void testTargetCanGetProxy() { NopInterceptor di = new NopInterceptor(); INeedsToSeeProxy target = new TargetChecker(); ProxyFactory proxyFactory = new ProxyFactory(target); proxyFactory.setExposeProxy(true); assertTrue(proxyFactory.isExposeProxy()); proxyFactory.addAdvice(0, di); INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(proxyFactory); assertEquals(0, di.getCount()); assertEquals(0, target.getCount()); proxied.incrementViaThis(); assertEquals("Increment happened", 1, target.getCount()); assertEquals("Only one invocation via AOP as use of this wasn't proxied", 1, di.getCount()); // 1 invocation assertEquals("Increment happened", 1, proxied.getCount()); proxied.incrementViaProxy(); // 2 invocations assertEquals("Increment happened", 2, target.getCount()); assertEquals("3 more invocations via AOP as the first call was reentrant through the proxy", 4, di.getCount()); }
/** * Check that although a method is eligible for advice chain optimization and * direct reflective invocation, it doesn't happen if we've asked to see the proxy, * so as to guarantee a consistent programming model. * @throws Throwable */ @Test public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable { NeedsToSeeProxy target = new NeedsToSeeProxy(); AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class); pc.setTarget(target); pc.setExposeProxy(true); // Now let's try it with the special target AopProxy aop = createAopProxy(pc); INeedsToSeeProxy proxied = (INeedsToSeeProxy) aop.getProxy(); // It will complain if it can't get the proxy proxied.incrementViaProxy(); }
@Test(expected = IllegalStateException.class) // Should fail to get proxy as exposeProxy wasn't set to true public void testTargetCantGetProxyByDefault() { NeedsToSeeProxy et = new NeedsToSeeProxy(); ProxyFactory pf1 = new ProxyFactory(et); assertFalse(pf1.isExposeProxy()); INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(pf1); proxied.incrementViaProxy(); }