use of org.springframework.context.testfixture.SimpleMapScope in project spring-framework by spring-projects.
the class ScopedProxyTests method testJdkScopedProxy.
@Test
public void testJdkScopedProxy() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT);
bf.setSerializationId("X");
SimpleMapScope scope = new SimpleMapScope();
bf.registerScope("request", scope);
ITestBean bean = (ITestBean) bf.getBean("testBean");
assertThat(bean).isNotNull();
assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue();
boolean condition1 = bean instanceof ScopedObject;
assertThat(condition1).isTrue();
ScopedObject scoped = (ScopedObject) bean;
assertThat(scoped.getTargetObject().getClass()).isEqualTo(TestBean.class);
bean.setAge(101);
assertThat(scope.getMap().containsKey("testBeanTarget")).isTrue();
assertThat(scope.getMap().get("testBeanTarget").getClass()).isEqualTo(TestBean.class);
ITestBean deserialized = SerializationTestUtils.serializeAndDeserialize(bean);
assertThat(deserialized).isNotNull();
assertThat(AopUtils.isJdkDynamicProxy(deserialized)).isTrue();
assertThat(bean.getAge()).isEqualTo(101);
boolean condition = deserialized instanceof ScopedObject;
assertThat(condition).isTrue();
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
assertThat(scopedDeserialized.getTargetObject().getClass()).isEqualTo(TestBean.class);
bf.setSerializationId(null);
}
use of org.springframework.context.testfixture.SimpleMapScope in project spring-framework by spring-projects.
the class ClassPathFactoryBeanDefinitionScannerTests method testSingletonScopedFactoryMethod.
@Test
public void testSingletonScopedFactoryMethod() {
GenericApplicationContext context = new GenericApplicationContext();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
context.getBeanFactory().registerScope("request", new SimpleMapScope());
scanner.scan(BASE_PACKAGE);
context.registerBeanDefinition("clientBean", new RootBeanDefinition(QualifiedClientBean.class));
context.refresh();
FactoryMethodComponent fmc = context.getBean("factoryMethodComponent", FactoryMethodComponent.class);
assertThat(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)).isFalse();
// 2
TestBean tb = (TestBean) context.getBean("publicInstance");
assertThat(tb.getName()).isEqualTo("publicInstance");
// 2
TestBean tb2 = (TestBean) context.getBean("publicInstance");
assertThat(tb2.getName()).isEqualTo("publicInstance");
assertThat(tb).isSameAs(tb2);
// 3
tb = (TestBean) context.getBean("protectedInstance");
assertThat(tb.getName()).isEqualTo("protectedInstance");
assertThat(context.getBean("protectedInstance")).isSameAs(tb);
assertThat(tb.getCountry()).isEqualTo("0");
// 3
tb2 = context.getBean("protectedInstance", TestBean.class);
assertThat(tb2.getName()).isEqualTo("protectedInstance");
assertThat(tb).isSameAs(tb2);
// 4
tb = context.getBean("privateInstance", TestBean.class);
assertThat(tb.getName()).isEqualTo("privateInstance");
assertThat(tb.getAge()).isEqualTo(1);
// 4
tb2 = context.getBean("privateInstance", TestBean.class);
assertThat(tb2.getAge()).isEqualTo(2);
assertThat(tb).isNotSameAs(tb2);
// 5
Object bean = context.getBean("requestScopedInstance");
assertThat(AopUtils.isCglibProxy(bean)).isTrue();
boolean condition = bean instanceof ScopedObject;
assertThat(condition).isTrue();
QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
assertThat(clientBean.testBean).isSameAs(context.getBean("publicInstance"));
assertThat(clientBean.dependencyBean).isSameAs(context.getBean("dependencyBean"));
assertThat(clientBean.applicationContext).isSameAs(context);
}
use of org.springframework.context.testfixture.SimpleMapScope in project spring-framework by spring-projects.
the class ComponentScanWithBasePackagesAndValueAlias method withScopedProxyThroughRegex.
@Test
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
ctx.refresh();
// should cast to the interface
FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
// should be dynamic proxy
assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue();
}
use of org.springframework.context.testfixture.SimpleMapScope in project spring-framework by spring-projects.
the class ComponentScanWithBasePackagesAndValueAlias method withScopedProxyThroughAspectJPattern.
@Test
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
ctx.refresh();
// should cast to the interface
FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
// should be dynamic proxy
assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue();
}
use of org.springframework.context.testfixture.SimpleMapScope in project spring-framework by spring-projects.
the class ComponentScanParserScopedProxyTests method testDefaultScopedProxy.
@Test
public void testDefaultScopedProxy() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/context/annotation/scopedProxyDefaultTests.xml");
context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
// should not be a proxy
assertThat(AopUtils.isAopProxy(bean)).isFalse();
context.close();
}
Aggregations