Search in sources :

Example 16 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testUndeclaredCheckedException.

/**
	 * An interceptor throws a checked exception not on the method signature.
	 * For efficiency, we don't bother unifying java.lang.reflect and
	 * org.springframework.cglib UndeclaredThrowableException
	 */
@Test
public void testUndeclaredCheckedException() throws Throwable {
    final Exception unexpectedException = new Exception();
    // 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 (UndeclaredThrowableException thrown) {
        assertEquals("exception matches", unexpectedException, thrown.getUndeclaredThrowable());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Didn't expect exception: " + ex);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) MethodInvocation(org.aopalliance.intercept.MethodInvocation) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 17 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testCloneInvocationToProceedThreeTimes.

/**
	 * There are times when we want to call proceed() twice.
	 * We can do this if we clone the invocation.
	 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    pc.addInterface(ITestBean.class);
    MethodInterceptor twoBirthdayInterceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation mi) throws Throwable {
            // Clone the invocation to proceed three times
            // "The Moor's Last Sigh": this technology can cause premature aging
            MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
            MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
            clone1.proceed();
            clone2.proceed();
            return mi.proceed();
        }
    };
    @SuppressWarnings("serial") StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return "haveBirthday".equals(m.getName());
        }
    };
    pc.addAdvisor(advisor);
    ITestBean it = (ITestBean) createProxy(pc);
    final int age = 20;
    it.setAge(age);
    assertEquals(age, it.getAge());
    // Should return the age before the third, AOP-induced birthday
    assertEquals(age + 2, it.haveBirthday());
    // Return the final age produced by 3 birthdays
    assertEquals(age + 3, it.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 18 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testBeforeAdviceThrowsException.

@Test
public void testBeforeAdviceThrowsException() {
    final RuntimeException rex = new RuntimeException();
    @SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {

        @Override
        public void before(Method m, Object[] args, Object target) throws Throwable {
            super.before(m, args, target);
            if (m.getName().startsWith("set"))
                throw rex;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    NopInterceptor nop1 = new NopInterceptor();
    NopInterceptor nop2 = new NopInterceptor();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(nop1);
    pf.addAdvice(ba);
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) createProxy(pf);
    // Won't throw an exception
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, ba.getCalls());
    assertEquals(1, ba.getCalls("getAge"));
    assertEquals(1, nop1.getCount());
    assertEquals(1, nop2.getCount());
    // Will fail, after invoking Nop1
    try {
        proxied.setAge(26);
        fail("before advice should have ended chain");
    } catch (RuntimeException ex) {
        assertEquals(rex, ex);
    }
    assertEquals(2, ba.getCalls());
    assertEquals(2, nop1.getCount());
    // Nop2 didn't get invoked when the exception was thrown
    assertEquals(1, nop2.getCount());
    // Shouldn't have changed value in joinpoint
    assertEquals(target.getAge(), proxied.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 19 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class AfterReturningAdviceBindingTestAspect method testReturningBeanArray.

@Test
public void testReturningBeanArray() {
    this.testBeanTarget.setSpouse(new TestBean());
    ITestBean[] spouses = this.testBeanTarget.getSpouses();
    testBeanProxy.getSpouses();
    verify(mockCollaborator).testBeanArrayArg(spouses);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 20 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class AroundAdviceBindingTestAspect method onSetUp.

@Before
public void onSetUp() throws Exception {
    ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
    AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect"));
    ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean");
    assertTrue(AopUtils.isAopProxy(injectedTestBean));
    this.testBeanProxy = injectedTestBean;
    // we need the real target too, not just the proxy...
    this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
    mockCollaborator = mock(AroundAdviceBindingCollaborator.class);
    aroundAdviceAspect.setCollaborator(mockCollaborator);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AroundAdviceBindingCollaborator(org.springframework.aop.aspectj.AroundAdviceBindingTestAspect.AroundAdviceBindingCollaborator) Before(org.junit.Before)

Aggregations

ITestBean (org.springframework.tests.sample.beans.ITestBean)221 Test (org.junit.Test)205 TestBean (org.springframework.tests.sample.beans.TestBean)127 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)37 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)24 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 IOException (java.io.IOException)15 Advisor (org.springframework.aop.Advisor)15 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)15 Method (java.lang.reflect.Method)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)14 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)13 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)13 MethodInvocation (org.aopalliance.intercept.MethodInvocation)12 Advised (org.springframework.aop.framework.Advised)12 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)11 LockedException (test.mixin.LockedException)11 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)10