Search in sources :

Example 91 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 92 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 93 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 94 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)

Example 95 with ITestBean

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

the class CounterAspect method testProgrammaticProxyCreation.

@Test
public void testProgrammaticProxyCreation() {
    ITestBean testBean = new TestBean();
    AspectJProxyFactory factory = new AspectJProxyFactory();
    factory.setTarget(testBean);
    CounterAspect myCounterAspect = new CounterAspect();
    factory.addAspect(myCounterAspect);
    ITestBean proxyTestBean = factory.getProxy();
    assertTrue("Expected a proxy", proxyTestBean instanceof Advised);
    proxyTestBean.setAge(20);
    assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) AspectJProxyFactory(org.springframework.aop.aspectj.annotation.AspectJProxyFactory) Advised(org.springframework.aop.framework.Advised) Test(org.junit.Test)

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