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());
}
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")));
}
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());
}
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));
}
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);
}
Aggregations