Search in sources :

Example 1 with TimeStamped

use of org.springframework.tests.TimeStamped in project spring-framework by spring-projects.

the class ProxyFactoryTests method testAddRepeatedInterface.

@Test
public void testAddRepeatedInterface() {
    TimeStamped tst = new TimeStamped() {

        @Override
        public long getTimeStamp() {
            throw new UnsupportedOperationException("getTimeStamp");
        }
    };
    ProxyFactory pf = new ProxyFactory(tst);
    // We've already implicitly added this interface.
    // This call should be ignored without error
    pf.addInterface(TimeStamped.class);
    // All cool
    assertThat(pf.getProxy(), instanceOf(TimeStamped.class));
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) Test(org.junit.Test)

Example 2 with TimeStamped

use of org.springframework.tests.TimeStamped in project spring-framework by spring-projects.

the class ProxyFactoryTests method testGetsAllInterfaces.

@Test
public void testGetsAllInterfaces() throws Exception {
    // Extend to get new interface
    class TestBeanSubclass extends TestBean implements Comparable<Object> {

        @Override
        public int compareTo(Object arg0) {
            throw new UnsupportedOperationException("compareTo");
        }
    }
    TestBeanSubclass raw = new TestBeanSubclass();
    ProxyFactory factory = new ProxyFactory(raw);
    //System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
    assertEquals("Found correct number of interfaces", 5, factory.getProxiedInterfaces().length);
    ITestBean tb = (ITestBean) factory.getProxy();
    assertThat("Picked up secondary interface", tb, instanceOf(IOther.class));
    raw.setAge(25);
    assertTrue(tb.getAge() == raw.getAge());
    long t = 555555L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
    Class<?>[] oldProxiedInterfaces = factory.getProxiedInterfaces();
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
    assertEquals("Advisor proxies one more interface after introduction", oldProxiedInterfaces.length + 1, newProxiedInterfaces.length);
    TimeStamped ts = (TimeStamped) factory.getProxy();
    assertTrue(ts.getTimeStamp() == t);
    // Shouldn't fail;
    ((IOther) ts).absquatulate();
}
Also used : IOther(org.springframework.tests.sample.beans.IOther) ITestBean(org.springframework.tests.sample.beans.ITestBean) TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.Test)

Example 3 with TimeStamped

use of org.springframework.tests.TimeStamped 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 4 with TimeStamped

use of org.springframework.tests.TimeStamped 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 5 with TimeStamped

use of org.springframework.tests.TimeStamped 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

Test (org.junit.Test)16 TimeStamped (org.springframework.tests.TimeStamped)16 ITestBean (org.springframework.tests.sample.beans.ITestBean)14 TestBean (org.springframework.tests.sample.beans.TestBean)11 ProxyFactory (org.springframework.aop.framework.ProxyFactory)8 INestedTestBean (org.springframework.tests.sample.beans.INestedTestBean)7 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)7 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)4 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)3 DebugInterceptor (org.springframework.aop.interceptor.DebugInterceptor)2 DelegatingIntroductionInterceptor (org.springframework.aop.support.DelegatingIntroductionInterceptor)2 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)2 Lockable (test.mixin.Lockable)2 LockedException (test.mixin.LockedException)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 IntroductionAdvisor (org.springframework.aop.IntroductionAdvisor)1 IntroductionInterceptor (org.springframework.aop.IntroductionInterceptor)1 TimestampIntroductionInterceptor (org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor)1 IOther (org.springframework.tests.sample.beans.IOther)1 Person (org.springframework.tests.sample.beans.Person)1