use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ScopingTests method testScopedProxyConfiguration.
@Test
public void testScopedProxyConfiguration() throws Exception {
TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedInterfaceDep");
ITestBean spouse = singleton.getSpouse();
boolean condition = spouse instanceof ScopedObject;
assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
String beanName = "scopedProxyInterface";
String scopedBeanName = "scopedTarget." + beanName;
// get hidden bean
assertThat(spouse.getName()).isEqualTo(flag);
ITestBean spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
// the scope proxy has kicked in
assertThat(spouseFromBF).isNotSameAs(spouse);
// create a new bean
customScope.createNewScope = true;
// get the bean again from the BF
spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
// make sure the name has been updated
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
assertThat(spouseFromBF).isNotSameAs(spouse);
// get the bean again
spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ScopingTests method testScopedProxyConfigurationWithClasses.
@Test
public void testScopedProxyConfigurationWithClasses() throws Exception {
TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedClassDep");
ITestBean spouse = singleton.getSpouse();
boolean condition = spouse instanceof ScopedObject;
assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
String beanName = "scopedProxyClass";
String scopedBeanName = "scopedTarget." + beanName;
// get hidden bean
assertThat(spouse.getName()).isEqualTo(flag);
TestBean spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
// the scope proxy has kicked in
assertThat(spouseFromBF).isNotSameAs(spouse);
// create a new bean
customScope.createNewScope = true;
flag = "boo";
// get the bean again from the BF
spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
// make sure the name has been updated
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
assertThat(spouseFromBF).isNotSameAs(spouse);
// get the bean again
spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class LazyInitTargetSourceTests method testLazyInitSingletonTargetSource.
@Test
public void testLazyInitSingletonTargetSource() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SINGLETON_CONTEXT);
bf.preInstantiateSingletons();
ITestBean tb = (ITestBean) bf.getBean("proxy");
assertThat(bf.containsSingleton("target")).isFalse();
assertThat(tb.getAge()).isEqualTo(10);
assertThat(bf.containsSingleton("target")).isTrue();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.
@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
final long t = 1001L;
class Tester implements TimeStamped, ITester {
@Override
public void foo() throws Exception {
}
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
// assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
TimeStamped ts = (TimeStamped) pf.getProxy();
assertThat(ts.getTimeStamp() == t).isTrue();
((ITester) ts).foo();
((ITestBean) ts).getAge();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ThreadLocalTargetSourceTests method testUseDifferentManagedInstancesInSameThread.
/**
* Check we can use two different ThreadLocalTargetSources
* managing objects of different types without them interfering
* with one another.
*/
@Test
public void testUseDifferentManagedInstancesInSameThread() {
SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
assertThat(apartment.getCount()).isEqualTo(INITIAL_COUNT);
apartment.doWork();
assertThat(apartment.getCount()).isEqualTo((INITIAL_COUNT + 1));
ITestBean test = (ITestBean) beanFactory.getBean("threadLocal2");
assertThat(test.getName()).isEqualTo("Rod");
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
}
Aggregations