/** * Create a new ProxyFactory. * <p>Will proxy all interfaces that the given target implements. * @param target the target object to be proxied */ public ProxyFactory(Object target) { setTarget(target); setInterfaces(ClassUtils.getAllInterfaces(target)); }
private Foo jdkProxy(Foo foo) { ProxyFactory pf = new ProxyFactory(); pf.setTarget(foo); pf.addInterface(Foo.class); Foo proxy = (Foo) pf.getProxy(); assertTrue("Proxy is a JDK dynamic proxy", AopUtils.isJdkDynamicProxy(proxy)); assertThat(proxy, instanceOf(Foo.class)); return proxy; }
private Foo cglibProxy(Foo foo) { ProxyFactory pf = new ProxyFactory(); pf.setTarget(foo); pf.setProxyTargetClass(true); Foo proxy = (Foo) pf.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); assertThat(proxy, instanceOf(FooImpl.class)); return proxy; }
@Test public void getShortNameForCglibClass() { TestBean tb = new TestBean(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(tb); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); String className = ClassUtils.getShortName(proxy.getClass()); assertEquals("Class name did not match", "TestBean", className); } }
@Test public void testAopJdkProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); Map<?, ?> target = new HashMap<>(); factory.setTarget(target); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void invokeListenerInvalidProxy() { Object target = new InvalidProxyTestBean(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(target); proxyFactory.addInterface(SimpleService.class); Object bean = proxyFactory.getProxy(getClass().getClassLoader()); Method method = ReflectionUtils.findMethod( InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class); StaticApplicationListenerMethodAdapter listener = new StaticApplicationListenerMethodAdapter(method, bean); this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage("handleIt2"); listener.onApplicationEvent(createGenericTestEvent("test")); }
private ITestBean getIntroductionAdvisorProxy(TestBean target) { ProxyFactory pf = new ProxyFactory(ITestBean.class); pf.setProxyTargetClass(true); pf.addAdvisor(new LockMixinAdvisor()); pf.setTarget(target); pf.setFrozen(true); pf.setExposeProxy(false); return (ITestBean) pf.getProxy(); }
@Test public void testProxiedUserInterfacesWithSingleInterface() { ProxyFactory pf = new ProxyFactory(); pf.setTarget(new TestBean()); pf.addInterface(ITestBean.class); Object proxy = pf.getProxy(); Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); assertEquals(1, userInterfaces.length); assertEquals(ITestBean.class, userInterfaces[0]); }
@Test public void crossClassInterfaceOnJdkProxy() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new OtherServiceImpl()); proxyFactory.addInterface(OtherService.class); proxyFactory.addAdvice(this.ti); OtherService otherService = (OtherService) proxyFactory.getProxy(); otherService.foo(); assertGetTransactionAndCommitCount(1); }
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) { TestBean target = new TestBean(); Pointcut pointcut = getPointcut(pointcutExpression); DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); advisor.setAdvice(interceptor); advisor.setPointcut(pointcut); ProxyFactory pf = new ProxyFactory(); pf.setTarget(target); pf.addAdvisor(advisor); return (TestBean) pf.getProxy(); }
@Test public void testAopJdkProxyWithMultipleInterfaces() throws Exception { ModelMap map = new ModelMap(); Map<?, ?> target = new HashMap<>(); ProxyFactory factory = new ProxyFactory(); factory.setTarget(target); factory.addInterface(Serializable.class); factory.addInterface(Cloneable.class); factory.addInterface(Comparable.class); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void testProxiedUserInterfacesWithMultipleInterfaces() { ProxyFactory pf = new ProxyFactory(); pf.setTarget(new TestBean()); pf.addInterface(ITestBean.class); pf.addInterface(Comparable.class); Object proxy = pf.getProxy(); Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); assertEquals(2, userInterfaces.length); assertEquals(ITestBean.class, userInterfaces[0]); assertEquals(Comparable.class, userInterfaces[1]); }
@Test public void testMixinWithIntroductionInfo() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); // We don't use an IntroductionAdvisor, we can just add an advice that implements IntroductionInfo pc.addAdvice(new LockMixin()); pc.setTarget(tb); testTestBeanIntroduction(pc); }
@Test public void testAopCglibProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); SomeInnerClass val = new SomeInnerClass(); factory.setTarget(val); factory.setProxyTargetClass(true); map.addAttribute(factory.getProxy()); assertTrue(map.containsKey("someInnerClass")); assertEquals(val, map.get("someInnerClass")); }
/** * Test stateful interceptor */ @Test public void testMixinWithIntroductionAdvisor() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); pc.addAdvisor(new LockMixinAdvisor()); pc.setTarget(tb); testTestBeanIntroduction(pc); }
@Test public void crossClassInterfaceMethodLevelOnJdkProxy() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new SomeServiceImpl()); proxyFactory.addInterface(SomeService.class); proxyFactory.addAdvice(this.ti); SomeService someService = (SomeService) proxyFactory.getProxy(); someService.bar(); assertGetTransactionAndCommitCount(1); someService.foo(); assertGetTransactionAndCommitCount(2); someService.fooBar(); assertGetTransactionAndCommitCount(3); }
@Test public void withMultiMethodOverride() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithMultiMethodOverride()); proxyFactory.addAdvice(this.ti); TestWithMultiMethodOverride proxy = (TestWithMultiMethodOverride) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingCompletelyElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); }
@Test public void withSingleMethodOverride() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithSingleMethodOverride()); proxyFactory.addAdvice(this.ti); TestWithSingleMethodOverride proxy = (TestWithSingleMethodOverride) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingCompletelyElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); }
@Test public void withSingleMethodOverrideInverted() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithSingleMethodOverrideInverted()); proxyFactory.addAdvice(this.ti); TestWithSingleMethodOverrideInverted proxy = (TestWithSingleMethodOverrideInverted) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingCompletelyElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); }
@Test public void classLevelOnly() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestClassLevelOnly()); proxyFactory.addAdvice(this.ti); TestClassLevelOnly proxy = (TestClassLevelOnly) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomething(); assertGetTransactionAndCommitCount(3); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(4); }