Search in sources :

Example 6 with TestBean

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

the class ControlFlowPointcutTests method testMatches.

@Test
public void testMatches() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));
    // Not advised, not under One
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(0, nop.getCount());
    // Will be advised
    assertEquals(target.getAge(), new One().getAge(proxied));
    assertEquals(1, nop.getCount());
    // Won't be advised
    assertEquals(target.getAge(), new One().nomatch(proxied));
    assertEquals(1, nop.getCount());
    assertEquals(3, cflow.getEvaluations());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 7 with TestBean

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

the class ControlFlowPointcutTests method testSelectiveApplication.

/**
	 * Check that we can use a cflow pointcut only in conjunction with
	 * a static pointcut: e.g. all setter methods that are invoked under
	 * a particular class. This greatly reduces the number of calls
	 * to the cflow pointcut, meaning that it's not so prohibitively
	 * expensive.
	 */
@Test
public void testSelectiveApplication() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
    Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
    // Not advised, not under One
    target.setAge(16);
    assertEquals(0, nop.getCount());
    // Not advised; under One but not a setter
    assertEquals(16, new One().getAge(proxied));
    assertEquals(0, nop.getCount());
    // Won't be advised
    new One().set(proxied);
    assertEquals(1, nop.getCount());
    // We saved most evaluations
    assertEquals(1, cflow.getEvaluations());
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 8 with TestBean

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

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.

@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
    final long t = 1001L;
    class Tester implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertTrue(ts.getTimeStamp() == t);
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 9 with TestBean

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithSuperInterface.

@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);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 10 with TestBean

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithInterfaceHierarchy.

@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);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27