use of org.springframework.beans.testfixture.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(factory.getBean("test2")).as("Shouldn't implement TimeStamped before manipulation").isNotInstanceOf(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));
assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
TimeStamped ts = (TimeStamped) factory.getBean("test2");
assertThat(ts.getTimeStamp()).isEqualTo(time);
// Can remove
config.removeAdvice(ti);
assertThat(config.getAdvisors().length == oldCount).isTrue();
// Check no change on existing object reference
assertThat(ts.getTimeStamp() == time).isTrue();
assertThat(factory.getBean("test2")).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertThat(config.getAdvisors().length == oldCount).isTrue();
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
config.addAdvice(0, debugInterceptor);
it.getSpouse();
// Won't affect existing reference
assertThat(debugInterceptor.getCount() == 0).isTrue();
it = (ITestBean) factory.getBean("test2");
it.getSpouse();
assertThat(debugInterceptor.getCount()).isEqualTo(1);
config.removeAdvice(debugInterceptor);
it.getSpouse();
// Still invoked with old reference
assertThat(debugInterceptor.getCount()).isEqualTo(2);
// not invoked with new object
it = (ITestBean) factory.getBean("test2");
it.getSpouse();
assertThat(debugInterceptor.getCount()).isEqualTo(2);
// Our own timestamped reference should still work
assertThat(ts.getTimeStamp()).isEqualTo(time);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testDetectsInterfaces.
@Test
public void testDetectsInterfaces() {
ProxyFactoryBean fb = new ProxyFactoryBean();
fb.setTarget(new TestBean());
fb.addAdvice(new DebugInterceptor());
fb.setBeanFactory(new DefaultListableBeanFactory());
ITestBean proxy = (ITestBean) fb.getObject();
assertThat(AopUtils.isJdkDynamicProxy(proxy)).isTrue();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class TestNamespaceHandler method testProxyingDecorator.
@Test
public void testProxyingDecorator() throws Exception {
ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean");
assertTestBean(bean);
assertThat(AopUtils.isAopProxy(bean)).isTrue();
Advisor[] advisors = ((Advised) bean).getAdvisors();
assertThat(advisors.length).as("Incorrect number of advisors").isEqualTo(1);
assertThat(advisors[0].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(DebugInterceptor.class);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class OverloadLookup method testAutoProxiedLookup.
@Test
public void testAutoProxiedLookup() {
OverloadLookup olup = (OverloadLookup) applicationContext.getBean("autoProxiedOverload");
ITestBean jenny = olup.newTestBean();
assertThat(jenny.getName()).isEqualTo("Jenny");
assertThat(olup.testMethod()).isEqualTo("foo");
assertInterceptorCount(2);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ConfigurationClassProcessingTests method configurationWithPostProcessor.
@Test
public void configurationWithPostProcessor() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithPostProcessor.class);
@SuppressWarnings("deprecation") RootBeanDefinition placeholderConfigurer = new RootBeanDefinition(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.class);
placeholderConfigurer.getPropertyValues().add("properties", "myProp=myValue");
ctx.registerBeanDefinition("placeholderConfigurer", placeholderConfigurer);
ctx.refresh();
TestBean foo = ctx.getBean("foo", TestBean.class);
ITestBean bar = ctx.getBean("bar", ITestBean.class);
ITestBean baz = ctx.getBean("baz", ITestBean.class);
assertThat(foo.getName()).isEqualTo("foo-processed-myValue");
assertThat(bar.getName()).isEqualTo("bar-processed-myValue");
assertThat(baz.getName()).isEqualTo("baz-processed-myValue");
SpousyTestBean listener = ctx.getBean("listenerTestBean", SpousyTestBean.class);
assertThat(listener.refreshed).isTrue();
ctx.close();
}
Aggregations