protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) { pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class)); }
public DynamicAsyncInterfaceBean() { ProxyFactory pf = new ProxyFactory(new HashMap<>()); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); if (Future.class.equals(invocation.getMethod().getReturnType())) { return new AsyncResult<>(invocation.getArguments()[0].toString()); } return null; } }); advisor.addInterface(AsyncInterface.class); pf.addAdvisor(advisor); this.proxy = (AsyncInterface) pf.getProxy(); }
public DynamicAsyncMethodsInterfaceBean() { ProxyFactory pf = new ProxyFactory(new HashMap<>()); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); if (Future.class.equals(invocation.getMethod().getReturnType())) { return new AsyncResult<>(invocation.getArguments()[0].toString()); } return null; } }); advisor.addInterface(AsyncMethodsInterface.class); pf.addAdvisor(advisor); this.proxy = (AsyncMethodsInterface) pf.getProxy(); }
@Test public void testNoIntroduction() { String beanName = "foo"; TestBean target = new RequiresBeanNameBoundTestBean(beanName); ProxyFactory pf = new ProxyFactory(target); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName)); ITestBean proxy = (ITestBean) pf.getProxy(); assertFalse("No introduction", proxy instanceof NamedBean); // Requires binding proxy.getAge(); }
@Test public void testWithIntroduction() { String beanName = "foo"; TestBean target = new RequiresBeanNameBoundTestBean(beanName); ProxyFactory pf = new ProxyFactory(target); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorIntroducingNamedBean(beanName)); ITestBean proxy = (ITestBean) pf.getProxy(); assertTrue("Introduction was made", proxy instanceof NamedBean); // Requires binding proxy.getAge(); NamedBean nb = (NamedBean) proxy; assertEquals("Name returned correctly", beanName, nb.getBeanName()); }
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 testMethodValidationInterceptor() { MyValidBean bean = new MyValidBean(); ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.addAdvice(new MethodValidationInterceptor()); proxyFactory.addAdvisor(new AsyncAnnotationAdvisor()); doTestProxyValidation((MyValidInterface) proxyFactory.getProxy()); }
@Test public void testMethodValidationInterceptor() { MyValidBean bean = new MyValidBean(); ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.addAdvice(new MethodValidationInterceptor()); proxyFactory.addAdvisor(new AsyncAnnotationAdvisor()); doTestProxyValidation((MyValidInterface) proxyFactory.getProxy()); }
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(); }
/** * Create an empty pointcut, populating instance variables. */ @Before public void setup() { ProxyFactory pf = new ProxyFactory(new SerializablePerson()); nop = new SerializableNopInterceptor(); pc = new NameMatchMethodPointcut(); pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop)); proxied = (Person) pf.getProxy(); }
@SuppressWarnings("serial") @Test public void testIntroductionInterceptorDoesntReplaceToString() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = new SerializableTimeStamped(0); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts) { @Override public String toString() { throw new UnsupportedOperationException("Shouldn't be invoked"); } })); TimeStamped tsp = (TimeStamped) factory.getProxy(); assertEquals(0, tsp.getTimeStamp()); assertEquals(raw.toString(), tsp.toString()); }
/** * 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 testIntroductionInterceptorWithDelegation() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(TimeStamped.class); long timestamp = 111L; given(ts.getTimeStamp()).willReturn(timestamp); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts))); TimeStamped tsp = (TimeStamped) factory.getProxy(); assertTrue(tsp.getTimeStamp() == timestamp); }
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, boolean proxyTargetClass) throws Exception { logAdvice.reset(); ProxyFactory factory = new ProxyFactory(target); factory.setProxyTargetClass(proxyTargetClass); factory.addAdvisor(advisor); TestService bean = (TestService) factory.getProxy(); assertEquals(0, logAdvice.getCountThrows()); try { bean.sayHello(); fail("Expected exception"); } catch (TestException ex) { assertEquals(message, ex.getMessage()); } assertEquals(1, logAdvice.getCountThrows()); }
@Test public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof SubTimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(SubTimeStamped.class); long timestamp = 111L; given(ts.getTimeStamp()).willReturn(timestamp); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class)); SubTimeStamped tsp = (SubTimeStamped) factory.getProxy(); assertTrue(tsp.getTimeStamp() == timestamp); }
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, boolean proxyTargetClass) throws Exception { logAdvice.reset(); ProxyFactory factory = new ProxyFactory(target); factory.setProxyTargetClass(proxyTargetClass); factory.addAdvisor(advisor); TestService bean = (TestService) factory.getProxy(); assertEquals(0, logAdvice.getCountThrows()); try { bean.sayHello(); fail("Expected exception"); } catch (TestException ex) { assertEquals(message, ex.getMessage()); } assertEquals(1, logAdvice.getCountThrows()); }
@Test public void testIntroductionInterceptorWithSuperInterface() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(SubTimeStamped.class); long timestamp = 111L; given(ts.getTimeStamp()).willReturn(timestamp); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class)); TimeStamped tsp = (TimeStamped) factory.getProxy(); assertTrue(!(tsp instanceof SubTimeStamped)); assertTrue(tsp.getTimeStamp() == timestamp); }
@Test public void testIndexOfMethods() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice()); Advised advised = (Advised) pf.getProxy(); // Can use advised and ProxyFactory interchangeably advised.addAdvice(nop); pf.addAdvisor(advisor); assertEquals(-1, pf.indexOf(new NopInterceptor())); assertEquals(0, pf.indexOf(nop)); assertEquals(1, pf.indexOf(advisor)); assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null))); }
@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); }