Search in sources :

Example 21 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)

Example 22 with ITestBean

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

the class TestBeanAdvisor method testPerThisAspect.

@Test
public void testPerThisAspect() {
    ClassPathXmlApplicationContext bf = newContext("perthis.xml");
    ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian1));
    assertEquals(0, adrian1.getAge());
    assertEquals(1, adrian1.getAge());
    ITestBean adrian2 = (ITestBean) bf.getBean("adrian");
    assertNotSame(adrian1, adrian2);
    assertTrue(AopUtils.isAopProxy(adrian1));
    assertEquals(0, adrian2.getAge());
    assertEquals(1, adrian2.getAge());
    assertEquals(2, adrian2.getAge());
    assertEquals(3, adrian2.getAge());
    assertEquals(2, adrian1.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 23 with ITestBean

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

the class TestBeanAdvisor method doTestTwoAdviceAspectWith.

private void doTestTwoAdviceAspectWith(String location) {
    ClassPathXmlApplicationContext bf = newContext(location);
    boolean aspectSingleton = bf.isSingleton("aspect");
    ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
    testPrototype(adrian1, 0);
    ITestBean adrian2 = (ITestBean) bf.getBean("adrian");
    assertNotSame(adrian1, adrian2);
    testPrototype(adrian2, aspectSingleton ? 2 : 0);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext)

Example 24 with ITestBean

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

the class TraceBeforeAdvice method testAfterReturningAdviceWithoutJoinPoint.

private long testAfterReturningAdviceWithoutJoinPoint(String file, int howmany, String technology) {
    ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
    StopWatch sw = new StopWatch();
    sw.start(howmany + " repeated after returning advice invocations with " + technology);
    ITestBean adrian = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian));
    Advised a = (Advised) adrian;
    assertTrue(a.getAdvisors().length >= 3);
    // Hits joinpoint
    adrian.setAge(25);
    for (int i = 0; i < howmany; i++) {
        adrian.setAge(i);
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
    return sw.getLastTaskTimeMillis();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) StopWatch(org.springframework.util.StopWatch)

Example 25 with ITestBean

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

the class TraceBeforeAdvice method testMix.

private long testMix(String file, int howmany, String technology) {
    ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
    StopWatch sw = new StopWatch();
    sw.start(howmany + " repeated mixed invocations with " + technology);
    ITestBean adrian = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian));
    Advised a = (Advised) adrian;
    assertTrue(a.getAdvisors().length >= 3);
    for (int i = 0; i < howmany; i++) {
        // Hit all 3 joinpoints
        adrian.getAge();
        adrian.getName();
        adrian.setAge(i);
        // Invoke three non-advised methods
        adrian.getDoctor();
        adrian.getLawyer();
        adrian.getSpouse();
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
    return sw.getLastTaskTimeMillis();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) StopWatch(org.springframework.util.StopWatch)

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