Search in sources :

Example 1 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ScopedProxyTests method testCglibScopedProxy.

@Test
public void testCglibScopedProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
    bf.setSerializationId("Y");
    SimpleMapScope scope = new SimpleMapScope();
    bf.registerScope("request", scope);
    TestBean tb = (TestBean) bf.getBean("testBean");
    assertTrue(AopUtils.isCglibProxy(tb.getFriends()));
    assertTrue(tb.getFriends() instanceof ScopedObject);
    ScopedObject scoped = (ScopedObject) tb.getFriends();
    assertEquals(ArrayList.class, scoped.getTargetObject().getClass());
    tb.getFriends().add("myFriend");
    assertTrue(scope.getMap().containsKey("scopedTarget.scopedList"));
    assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass());
    ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
    assertNotNull(deserialized);
    assertTrue(AopUtils.isCglibProxy(deserialized));
    assertTrue(deserialized.contains("myFriend"));
    assertTrue(deserialized instanceof ScopedObject);
    ScopedObject scopedDeserialized = (ScopedObject) deserialized;
    assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass());
    bf.setSerializationId(null);
}
Also used : SimpleMapScope(org.springframework.tests.context.SimpleMapScope) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ScopedProxyTests method testScopedOverride.

@Test
public void testScopedOverride() throws Exception {
    GenericApplicationContext ctx = new GenericApplicationContext();
    new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
    SimpleMapScope scope = new SimpleMapScope();
    ctx.getBeanFactory().registerScope("request", scope);
    ctx.refresh();
    ITestBean bean = (ITestBean) ctx.getBean("testBean");
    assertEquals("male", bean.getName());
    assertEquals(99, bean.getAge());
    assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
    assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Test(org.junit.Test)

Example 3 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ComponentScanParserScopedProxyTests method testInterfacesScopedProxy.

@Test
public void testInterfacesScopedProxy() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/context/annotation/scopedProxyInterfacesTests.xml");
    context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
    // should cast to the interface
    FooService bean = (FooService) context.getBean("scopedProxyTestBean");
    // should be dynamic proxy
    assertTrue(AopUtils.isJdkDynamicProxy(bean));
    // test serializability
    assertEquals("bar", bean.foo(1));
    FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
    assertNotNull(deserialized);
    assertEquals("bar", deserialized.foo(1));
    context.close();
}
Also used : FooService(example.scannable.FooService) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 4 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ComponentScanWithBasePackagesAndValueAlias method withScopedProxy.

@Test
public void withScopedProxy() throws IOException, ClassNotFoundException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ComponentScanWithScopedProxy.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), is(true));
    // test serializability
    assertThat(bean.foo(1), equalTo("bar"));
    FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean);
    assertThat(deserialized, notNullValue());
    assertThat(deserialized.foo(1), equalTo("bar"));
}
Also used : FooService(example.scannable.FooService) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) Test(org.junit.Test)

Example 5 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testPublicExtendedPersistenceContextSetterWithSerialization.

@Test
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
    DummyInvocationHandler ih = new DummyInvocationHandler();
    Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { EntityManager.class }, ih);
    given(mockEmf.createEntityManager()).willReturn((EntityManager) mockEm);
    GenericApplicationContext gac = new GenericApplicationContext();
    SimpleMapScope myScope = new SimpleMapScope();
    gac.getDefaultListableBeanFactory().registerScope("myScope", myScope);
    gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
    gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
    RootBeanDefinition bd = new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class);
    bd.setScope("myScope");
    gac.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), bd);
    gac.refresh();
    DefaultPublicPersistenceContextSetter bean = (DefaultPublicPersistenceContextSetter) gac.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertNotNull(bean.em);
    assertNotNull(SerializationTestUtils.serializeAndDeserialize(bean.em));
    SimpleMapScope serialized = (SimpleMapScope) SerializationTestUtils.serializeAndDeserialize(myScope);
    serialized.close();
    assertTrue(DummyInvocationHandler.closed);
    DummyInvocationHandler.closed = false;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 SimpleMapScope (org.springframework.tests.context.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 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 ITestBean (org.springframework.tests.sample.beans.ITestBean)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 ArrayList (java.util.ArrayList)1 ScopedObject (org.springframework.aop.scope.ScopedObject)1 FactoryMethodComponent (org.springframework.context.annotation4.FactoryMethodComponent)1