@Test public void testUseAsHashKey() { TestBean target1 = new TestBean(); ProxyFactory pf1 = new ProxyFactory(target1); pf1.addAdvice(new NopInterceptor()); ITestBean proxy1 = (ITestBean) createProxy(pf1); TestBean target2 = new TestBean(); ProxyFactory pf2 = new ProxyFactory(target2); pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor())); ITestBean proxy2 = (ITestBean) createProxy(pf2); HashMap<ITestBean, Object> h = new HashMap<>(); Object value1 = "foo"; Object value2 = "bar"; assertNull(h.get(proxy1)); h.put(proxy1, value1); h.put(proxy2, value2); assertEquals(h.get(proxy1), value1); assertEquals(h.get(proxy2), value2); }
@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(); }
/** * Check that the string is informative. */ @Test public void testProxyConfigString() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); MethodBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba); pc.addAdvisor(advisor); ITestBean proxied = (ITestBean) createProxy(pc); String proxyConfigString = ((Advised) proxied).toProxyConfigString(); assertTrue(proxyConfigString.contains(advisor.toString())); assertTrue(proxyConfigString.contains("1 interface")); }
/** * Test that the proxy returns itself when the * target returns {@code this} */ @Test public void testTargetReturnsThis() throws Throwable { // Test return value TestBean raw = new OwnSpouse(); ProxyCreatorSupport pc = new ProxyCreatorSupport(); pc.setInterfaces(ITestBean.class); pc.setTarget(raw); ITestBean tb = (ITestBean) createProxy(pc); assertSame("this return is wrapped in proxy", tb, tb.getSpouse()); }
/** * Should only be able to introduce interfaces, not classes. */ @Test public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); try { pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class)); fail("Shouldn't be able to add introduction advice that introduces a class, rather than an interface"); } catch (IllegalArgumentException ex) { assertTrue(ex.getMessage().contains("interface")); } // Check it still works: proxy factory state shouldn't have been corrupted ITestBean proxied = (ITestBean) createProxy(pc); assertEquals(target.getAge(), proxied.getAge()); }
@Test public void testCannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); try { pc.addAdvice(new DummyIntroductionAdviceImpl()); fail("Shouldn't be able to add introduction interceptor except via introduction advice"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("ntroduction")); } // Check it still works: proxy factory state shouldn't have been corrupted ITestBean proxied = (ITestBean) createProxy(pc); assertEquals(target.getAge(), proxied.getAge()); }
@Test public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class)); try { // TODO May fail on either call: may want to tighten up definition ITestBean proxied = (ITestBean) createProxy(pc); proxied.getName(); fail("Bogus introduction"); } catch (Exception ex) { // TODO used to catch UnknownAdviceTypeException, but // with CGLIB some errors are in proxy creation and are wrapped // in aspect exception. Error message is still fine. //assertTrue(ex.getMessage().indexOf("ntroduction") > -1); } }
@Test public void testAdviceImplementsIntroductionInfo() throws Throwable { TestBean tb = new TestBean(); String name = "tony"; tb.setName(name); ProxyFactory pc = new ProxyFactory(tb); NopInterceptor di = new NopInterceptor(); pc.addAdvice(di); final long ts = 37; pc.addAdvice(new DelegatingIntroductionInterceptor(new TimeStamped() { @Override public long getTimeStamp() { return ts; } })); ITestBean proxied = (ITestBean) createProxy(pc); assertEquals(name, proxied.getName()); TimeStamped intro = (TimeStamped) proxied; assertEquals(ts, intro.getTimeStamp()); }
/** * Check that the introduction advice isn't allowed to introduce interfaces * that are unsupported by the IntroductionInterceptor. */ @Test public void testCannotAddIntroductionAdviceWithUnimplementedInterface() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); try { pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), ITestBean.class)); fail("Shouldn't be able to add introduction advice introducing an unimplemented interface"); } catch (IllegalArgumentException ex) { //assertTrue(ex.getMessage().indexOf("ntroduction") > -1); } // Check it still works: proxy factory state shouldn't have been corrupted ITestBean proxied = (ITestBean) createProxy(pc); assertEquals(target.getAge(), proxied.getAge()); }
@Test public void testCannotAddInterceptorWhenFrozen() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); try { pc.addAdvice(0, new NopInterceptor()); fail("Shouldn't be able to add interceptor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, ((Advised) proxied).getAdvisors().length); }
@Test public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge"); pc.addAdvisor(dp); pc.setTarget(tb); ITestBean it = (ITestBean) createProxy(pc); assertEquals(0, dp.count); it.getAge(); assertEquals(1, dp.count); it.setAge(11); assertEquals(11, it.getAge()); assertEquals(2, dp.count); }
@Test public void testStaticMethodPointcut() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); NopInterceptor di = new NopInterceptor(); TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge"); pc.addAdvisor(sp); pc.setTarget(tb); ITestBean it = (ITestBean) createProxy(pc); assertEquals(di.getCount(), 0); it.getAge(); assertEquals(di.getCount(), 1); it.setAge(11); assertEquals(it.getAge(), 11); assertEquals(di.getCount(), 2); }
@Test public void testAfterReturningAdvisorIsNotInvokedOnException() { CountingAfterReturningAdvice car = new CountingAfterReturningAdvice(); TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(new NopInterceptor()); pf.addAdvice(car); assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice()); ITestBean proxied = (ITestBean) createProxy(pf); assertEquals(0, car.getCalls()); int age = 10; proxied.setAge(age); assertEquals(age, proxied.getAge()); assertEquals(2, car.getCalls()); Exception exc = new Exception(); // On exception it won't be invoked try { proxied.exceptional(exc); fail(); } catch (Throwable t) { assertSame(exc, t); } assertEquals(2, car.getCalls()); }
@Test public void testReentrance() { int age1 = 33; TestBean target1 = new TestBean(); ProxyFactory pf1 = new ProxyFactory(target1); NopInterceptor di1 = new NopInterceptor(); pf1.addAdvice(0, di1); ITestBean advised1 = (ITestBean) createProxy(pf1); advised1.setAge(age1); // = 1 invocation advised1.setSpouse(advised1); // = 2 invocations assertEquals("one was invoked correct number of times", 2, di1.getCount()); assertEquals("Advised one has correct age", age1, advised1.getAge()); // = 3 invocations assertEquals("one was invoked correct number of times", 3, di1.getCount()); // = 5 invocations, as reentrant call to spouse is advised also assertEquals("Advised spouse has correct age", age1, advised1.getSpouse().getAge()); assertEquals("one was invoked correct number of times", 5, di1.getCount()); }
/** * Check that casting to Advised can't get around advice freeze. */ @Test public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); Advised advised = (Advised) proxied; assertTrue(pc.isFrozen()); try { advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); fail("Shouldn't be able to add Advisor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, advised.getAdvisors().length); }
@Test public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); // Could apply dynamically to getAge/setAge but not to getName TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age"); pc.addAdvisor(dp); this.mockTargetSource.setTarget(tb); pc.setTargetSource(mockTargetSource); ITestBean it = (ITestBean) createProxy(pc); assertEquals(0, dp.count); it.getAge(); // Statically vetoed assertEquals(0, dp.count); it.setAge(11); assertEquals(11, it.getAge()); assertEquals(1, dp.count); // Applies statically but not dynamically it.setName("joe"); assertEquals(1, dp.count); }
private void testTestBeanIntroduction(ProxyFactory pc) { int newAge = 65; ITestBean itb = (ITestBean) createProxy(pc); itb.setAge(newAge); assertEquals(newAge, itb.getAge()); Lockable lockable = (Lockable) itb; assertFalse(lockable.locked()); lockable.lock(); assertEquals(newAge, itb.getAge()); try { itb.setAge(1); fail("Setters should fail when locked"); } catch (LockedException ex) { // ok } assertEquals(newAge, itb.getAge()); // Unlock assertTrue(lockable.locked()); lockable.unlock(); itb.setAge(1); assertEquals(1, itb.getAge()); }
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
@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()); }
@Test public void testBeforeAdvisorIsInvoked() { CountingBeforeAdvice cba = new CountingBeforeAdvice(); @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) { @Override public boolean matches(Method m, @Nullable Class<?> targetClass) { return m.getParameterCount() == 0; } }; TestBean target = new TestBean(); target.setAge(80); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(new NopInterceptor()); pf.addAdvisor(matchesNoArgs); assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]); ITestBean proxied = (ITestBean) createProxy(pf); assertEquals(0, cba.getCalls()); assertEquals(0, cba.getCalls("getAge")); assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(1, cba.getCalls("getAge")); assertEquals(0, cba.getCalls("setAge")); // Won't be advised proxied.setAge(26); assertEquals(1, cba.getCalls()); assertEquals(26, proxied.getAge()); }