Search in sources :

Example 31 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean 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 32 with ITestBean

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

the class ProxyFactoryBeanTests method testPrototypeInstancesAreNotEqual.

@Test
public void testPrototypeInstancesAreNotEqual() {
    assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("prototype")));
    ITestBean test2 = (ITestBean) factory.getBean("prototype");
    ITestBean test2_1 = (ITestBean) factory.getBean("prototype");
    assertTrue("Prototype instances !=", test2 != test2_1);
    assertTrue("Prototype instances equal", test2.equals(test2_1));
    assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("prototype")));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Test(org.junit.Test)

Example 33 with ITestBean

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

the class ProxyFactoryBeanTests method testCanAddAndRemoveAdvicesOnSingleton.

/**
	 * Note that we can't add or remove interfaces without reconfiguring the
	 * singleton.
	 */
@Test
public void testCanAddAndRemoveAdvicesOnSingleton() {
    ITestBean it = (ITestBean) factory.getBean("test1");
    Advised pc = (Advised) it;
    it.getAge();
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    assertEquals(0, di.getCount());
    it.setAge(25);
    assertEquals(25, it.getAge());
    assertEquals(2, di.getCount());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Test(org.junit.Test)

Example 34 with ITestBean

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

the class ProxyFactoryBeanTests method testDetectsInterfaces.

@Test
public void testDetectsInterfaces() throws Exception {
    ProxyFactoryBean fb = new ProxyFactoryBean();
    fb.setTarget(new TestBean());
    fb.addAdvice(new DebugInterceptor());
    fb.setBeanFactory(new DefaultListableBeanFactory());
    ITestBean proxy = (ITestBean) fb.getObject();
    assertTrue(AopUtils.isJdkDynamicProxy(proxy));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 35 with ITestBean

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

the class ProxyFactoryBeanTests method testSingletonInstancesAreEqual.

/**
	 * The instances are equal, but do not have object identity.
	 * Interceptors and interfaces and the target are the same.
	 */
@Test
public void testSingletonInstancesAreEqual() {
    ITestBean test1 = (ITestBean) factory.getBean("test1");
    ITestBean test1_1 = (ITestBean) factory.getBean("test1");
    //assertTrue("Singleton instances ==", test1 == test1_1);
    assertEquals("Singleton instances ==", test1, test1_1);
    test1.setAge(25);
    assertEquals(test1.getAge(), test1_1.getAge());
    test1.setAge(250);
    assertEquals(test1.getAge(), test1_1.getAge());
    Advised pc1 = (Advised) test1;
    Advised pc2 = (Advised) test1_1;
    assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors());
    int oldLength = pc1.getAdvisors().length;
    NopInterceptor di = new NopInterceptor();
    pc1.addAdvice(1, di);
    assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors());
    assertEquals("Now have one more advisor", oldLength + 1, pc2.getAdvisors().length);
    assertEquals(di.getCount(), 0);
    test1.setAge(5);
    assertEquals(test1_1.getAge(), test1.getAge());
    assertEquals(di.getCount(), 3);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Test(org.junit.Test)

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