@Override public void addAdvice(Advice advice) throws AopConfigException { int pos = this.advisors.size(); addAdvice(pos, advice); }
@Test public void testTargetCanGetInvocationWithPrivateClass() { final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() { @Override protected void assertions(MethodInvocation invocation) { assertEquals(this, invocation.getThis()); assertEquals("Invocation should be on ITestBean: " + invocation.getMethod(), ITestBean.class, invocation.getMethod().getDeclaringClass()); } }; AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); TrapTargetInterceptor tii = new TrapTargetInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } }; pc.addAdvice(tii); pc.setTarget(expectedTarget); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); tb.getName(); }
@Test public void testTargetCanGetInvocation() throws Throwable { final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean(); AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); TrapTargetInterceptor tii = new TrapTargetInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } }; pc.addAdvice(tii); pc.setTarget(expectedTarget); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); tb.getName(); }
@Test public void testProtectedMethodInvocation() { ProtectedMethodTestBean bean = new ProtectedMethodTestBean(); bean.value = "foo"; mockTargetSource.setTarget(bean); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); ProtectedMethodTestBean proxy = (ProtectedMethodTestBean) aop.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader()); assertEquals("foo", proxy.getString()); }
@Test(expected = AopConfigException.class) public void testNoTarget() { AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(new NopInterceptor()); AopProxy aop = createAopProxy(pc); aop.getProxy(); }
@Test public void testPackageMethodInvocation() { PackageMethodTestBean bean = new PackageMethodTestBean(); bean.value = "foo"; mockTargetSource.setTarget(bean); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader()); assertEquals("foo", proxy.getString()); }
@Test public void testUndeclaredUncheckedException() throws Throwable { final RuntimeException unexpectedException = new RuntimeException(); // Test return value MethodInterceptor mi = new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { throw unexpectedException; } }; AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); pc.addAdvice(mi); // We don't care about the object pc.setTarget(new TestBean()); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); try { // Note: exception param below isn't used tb.getAge(); fail("Should have wrapped exception raised by interceptor"); } catch (RuntimeException thrown) { assertEquals("exception matches", unexpectedException, thrown); } }
@Test public void testInterceptorIsInvokedWithNoTarget() { // Test return value final int age = 25; MethodInterceptor mi = (invocation -> age); AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(mi); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); assertEquals("correct return value", age, tb.getAge()); }
@Test public void testDeclaredException() throws Throwable { final Exception expectedException = new Exception(); // Test return value MethodInterceptor mi = new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { throw expectedException; } }; AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); pc.addAdvice(mi); // We don't care about the object mockTargetSource.setTarget(new TestBean()); pc.setTargetSource(mockTargetSource); AopProxy aop = createAopProxy(pc); try { ITestBean tb = (ITestBean) aop.getProxy(); // Note: exception param below isn't used tb.exceptional(expectedException); fail("Should have thrown exception raised by interceptor"); } catch (Exception thrown) { assertEquals("exception matches", expectedException, thrown); } }
@Test public void testProxyAProxy() { ITestBean target = new TestBean(); mockTargetSource.setTarget(target); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); CglibAopProxy cglib = new CglibAopProxy(as); ITestBean proxy1 = (ITestBean) cglib.getProxy(); mockTargetSource.setTarget(proxy1); as = new AdvisedSupport(new Class<?>[]{}); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); cglib = new CglibAopProxy(as); assertThat(cglib.getProxy(), instanceOf(ITestBean.class)); }
@Test public void testToStringInvocation() { PrivateCglibTestBean bean = new PrivateCglibTestBean(); bean.setName("Rob Harrop"); AdvisedSupport as = new AdvisedSupport(); as.setTarget(bean); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); PrivateCglibTestBean proxy = (PrivateCglibTestBean) aop.getProxy(); assertEquals("The name property has been overwritten by the constructor", "Rob Harrop", proxy.toString()); }
@Test public void testMethodInvocationDuringConstructor() { CglibTestBean bean = new CglibTestBean(); bean.setName("Rob Harrop"); AdvisedSupport as = new AdvisedSupport(); as.setTarget(bean); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); CglibTestBean proxy = (CglibTestBean) aop.getProxy(); assertEquals("The name property has been overwritten by the constructor", "Rob Harrop", proxy.getName()); }
@Test public void testProxyAProxyWithAdditionalInterface() { ITestBean target = new TestBean(); mockTargetSource.setTarget(target); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); as.addInterface(Serializable.class); CglibAopProxy cglib = new CglibAopProxy(as); ITestBean proxy1 = (ITestBean) cglib.getProxy(); mockTargetSource.setTarget(proxy1); as = new AdvisedSupport(new Class<?>[]{}); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); cglib = new CglibAopProxy(as); ITestBean proxy2 = (ITestBean) cglib.getProxy(); assertTrue(proxy2 instanceof Serializable); }
@Test public void testExceptionHandling() { ExceptionThrower bean = new ExceptionThrower(); mockTargetSource.setTarget(bean); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); ExceptionThrower proxy = (ExceptionThrower) aop.getProxy(); try { proxy.doTest(); } catch (Exception ex) { assertTrue("Invalid exception class", ex instanceof ApplicationContextException); } assertTrue("Catch was not invoked", proxy.isCatchInvoked()); assertTrue("Finally was not invoked", proxy.isFinallyInvoked()); }
@Override public void addAdvice(Advice advice) throws AopConfigException { int pos = this.advisors.size(); addAdvice(pos, advice); }
public void addAdvice(Advice advice) throws AopConfigException { int pos = (this.advisors != null) ? this.advisors.size() : 0; addAdvice(pos, advice); }
@Override public void addAdvice(Advice advice) throws AopConfigException { int pos = this.advisors.size(); addAdvice(pos, advice); }