Search in sources :

Example 11 with TimeStamped

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

the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.

/**
	 * Should see effect immediately on behavior.
	 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
    ProxyFactory config = new ProxyFactory(new TestBean());
    assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped);
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to front of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertTrue(config.getAdvisors().length == oldCount + 1);
    TimeStamped ts = (TimeStamped) config.getProxy();
    assertTrue(ts.getTimeStamp() == time);
    // Can remove
    config.removeAdvice(ti);
    assertTrue(config.getAdvisors().length == oldCount);
    try {
        // Existing reference will fail
        ts.getTimeStamp();
        fail("Existing object won't implement this interface any more");
    } catch (RuntimeException ex) {
    }
    assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped);
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertTrue(config.getAdvisors().length == oldCount);
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    assertEquals(1, debugInterceptor.getCount());
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // not invoked again
    assertTrue(debugInterceptor.getCount() == 1);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 12 with TimeStamped

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithDelegation.

@Test
public void testIntroductionInterceptorWithDelegation() throws Exception {
    TestBean raw = new TestBean();
    assertTrue(!(raw instanceof TimeStamped));
    ProxyFactory factory = new ProxyFactory(raw);
    TimeStamped ts = mock(TimeStamped.class);
    long timestamp = 111L;
    given(ts.getTimeStamp()).willReturn(timestamp);
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
    TimeStamped tsp = (TimeStamped) 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)

Example 13 with TimeStamped

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorDoesntReplaceToString.

@SuppressWarnings("serial")
@Test
public void testIntroductionInterceptorDoesntReplaceToString() throws Exception {
    TestBean raw = new TestBean();
    assertTrue(!(raw instanceof TimeStamped));
    ProxyFactory factory = new ProxyFactory(raw);
    TimeStamped ts = new SerializableTimeStamped(0);
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts) {

        @Override
        public String toString() {
            throw new UnsupportedOperationException("Shouldn't be invoked");
        }
    }));
    TimeStamped tsp = (TimeStamped) factory.getProxy();
    assertEquals(0, tsp.getTimeStamp());
    assertEquals(raw.toString(), tsp.toString());
}
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 14 with TimeStamped

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

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInSubclass.

@Test
public void testAutomaticInterfaceRecognitionInSubclass() throws Exception {
    final long t = 1001L;
    @SuppressWarnings("serial")
    class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new TestII();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
    assertTrue(ia.isPerInstance());
    pf.addAdvisor(0, ia);
    //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertThat(ts, instanceOf(TimeStamped.class));
    // Shoulnd't proxy framework interfaces
    assertTrue(!(ts instanceof MethodInterceptor));
    assertTrue(!(ts instanceof IntroductionInterceptor));
    assertTrue(ts.getTimeStamp() == t);
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
    // Test removal
    ii.suppressInterface(TimeStamped.class);
    // Note that we need to construct a new proxy factory,
    // or suppress the interface on the proxy factory
    pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    Object o = pf.getProxy();
    assertTrue(!(o instanceof TimeStamped));
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IntroductionInterceptor(org.springframework.aop.IntroductionInterceptor) TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) 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) Test(org.junit.Test)

Example 15 with TimeStamped

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

the class DelegatingIntroductionInterceptorTests method testSerializableDelegatingIntroductionInterceptorSerializable.

@Test
public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception {
    SerializablePerson serializableTarget = new SerializablePerson();
    String name = "Tony";
    serializableTarget.setName("Tony");
    ProxyFactory factory = new ProxyFactory(serializableTarget);
    factory.addInterface(Person.class);
    long time = 1000;
    TimeStamped ts = new SerializableTimeStamped(time);
    factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
    factory.addAdvice(new SerializableNopInterceptor());
    Person p = (Person) factory.getProxy();
    assertEquals(name, p.getName());
    assertEquals(time, ((TimeStamped) p).getTimeStamp());
    Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(name, p1.getName());
    assertEquals(time, ((TimeStamped) p1).getTimeStamp());
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) 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