Search in sources :

Example 6 with SimpleMapScope

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);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 7 with SimpleMapScope

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);
}
Also used : ScopedObject(org.springframework.aop.scope.ScopedObject) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) TestBean(org.springframework.beans.testfixture.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ScopedObject(org.springframework.aop.scope.ScopedObject) FactoryMethodComponent(org.springframework.context.annotation4.FactoryMethodComponent) Test(org.junit.jupiter.api.Test)

Example 8 with SimpleMapScope

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();
}
Also used : FooService(example.scannable.FooService) SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) Test(org.junit.jupiter.api.Test)

Example 9 with SimpleMapScope

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();
}
Also used : FooService(example.scannable.FooService) SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) Test(org.junit.jupiter.api.Test)

Example 10 with SimpleMapScope

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();
}
Also used : SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ScopedProxyTestBean(example.scannable.ScopedProxyTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)12 SimpleMapScope (org.springframework.context.testfixture.SimpleMapScope)12 FooService (example.scannable.FooService)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ScopedProxyTestBean (example.scannable.ScopedProxyTestBean)3 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)3 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 ArrayList (java.util.ArrayList)1 ScopedObject (org.springframework.aop.scope.ScopedObject)1 FactoryMethodComponent (org.springframework.context.annotation4.FactoryMethodComponent)1