Search in sources :

Example 6 with TimeStamped

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

the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.

/**
	 * Try adding and removing interfaces and interceptors on prototype.
	 * Changes will only affect future references obtained from the factory.
	 * Each instance will be independent.
	 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
    assertThat("Shouldn't implement TimeStamped before manipulation", factory.getBean("test2"), not(instanceOf(TimeStamped.class)));
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to head 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) factory.getBean("test2");
    assertEquals(time, ts.getTimeStamp());
    // Can remove
    config.removeAdvice(ti);
    assertTrue(config.getAdvisors().length == oldCount);
    // Check no change on existing object reference
    assertTrue(ts.getTimeStamp() == time);
    assertThat("Should no longer implement TimeStamped", factory.getBean("test2"), not(instanceOf(TimeStamped.class)));
    // 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();
    // Won't affect existing reference
    assertTrue(debugInterceptor.getCount() == 0);
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertEquals(1, debugInterceptor.getCount());
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // Still invoked wiht old reference
    assertEquals(2, debugInterceptor.getCount());
    // not invoked with new object
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertEquals(2, debugInterceptor.getCount());
    // Our own timestamped reference should still work
    assertEquals(time, ts.getTimeStamp());
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 7 with TimeStamped

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

the class CreatesTestBean method testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean.

@Test
public void testJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
    ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals("NOP should not have done any work yet", 0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertTrue("Introduction was made", tb instanceof TimeStamped);
    assertEquals(0, ((TimeStamped) tb).getTimeStamp());
    assertEquals(3, nop.getCount());
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    tb.setAge(65);
    assertEquals(65, tb.getAge());
    lockable1.lock();
    assertTrue(lockable1.locked());
    // Shouldn't affect second
    assertFalse(lockable2.locked());
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    try {
        tb.setAge(6);
        fail("Mixin should have locked this object");
    } catch (LockedException ex) {
    // Ok
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TimeStamped(org.springframework.tests.TimeStamped) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable) Test(org.junit.Test)

Example 8 with TimeStamped

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

the class CreatesTestBean method testJdkIntroduction.

@Test
public void testJdkIntroduction() {
    ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertEquals(0, nop.getCount());
    assertTrue(AopUtils.isJdkDynamicProxy(tb));
    int age = 5;
    tb.setAge(age);
    assertEquals(age, tb.getAge());
    assertTrue("Introduction was made", tb instanceof TimeStamped);
    assertEquals(0, ((TimeStamped) tb).getTimeStamp());
    assertEquals(3, nop.getCount());
    assertEquals("introductionUsingJdk", tb.getName());
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertFalse(lockable1.locked());
    assertFalse(lockable2.locked());
    tb.setAge(65);
    assertEquals(65, tb.getAge());
    lockable1.lock();
    assertTrue(lockable1.locked());
    // Shouldn't affect second
    assertFalse(lockable2.locked());
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    try {
        tb.setAge(6);
        fail("Mixin should have locked this object");
    } catch (LockedException ex) {
    // Ok
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TimeStamped(org.springframework.tests.TimeStamped) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable) Test(org.junit.Test)

Example 9 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 10 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)

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