use of org.springframework.context.testfixture.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());
assertThat(bean.em).isNotNull();
assertThat(SerializationTestUtils.serializeAndDeserialize(bean.em)).isNotNull();
SimpleMapScope serialized = SerializationTestUtils.serializeAndDeserialize(myScope);
serialized.close();
assertThat(DummyInvocationHandler.closed).isTrue();
DummyInvocationHandler.closed = false;
}
use of org.springframework.context.testfixture.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
assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue();
// test serializability
assertThat(bean.foo(1)).isEqualTo("bar");
FooService deserialized = SerializationTestUtils.serializeAndDeserialize(bean);
assertThat(deserialized).isNotNull();
assertThat(deserialized.foo(1)).isEqualTo("bar");
context.close();
}
use of org.springframework.context.testfixture.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)).isTrue();
// test serializability
assertThat(bean.foo(1)).isEqualTo("bar");
FooService deserialized = SerializationTestUtils.serializeAndDeserialize(bean);
assertThat(deserialized).isNotNull();
assertThat(deserialized.foo(1)).isEqualTo("bar");
}
use of org.springframework.context.testfixture.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");
assertThat(AopUtils.isCglibProxy(tb.getFriends())).isTrue();
boolean condition1 = tb.getFriends() instanceof ScopedObject;
assertThat(condition1).isTrue();
ScopedObject scoped = (ScopedObject) tb.getFriends();
assertThat(scoped.getTargetObject().getClass()).isEqualTo(ArrayList.class);
tb.getFriends().add("myFriend");
assertThat(scope.getMap().containsKey("scopedTarget.scopedList")).isTrue();
assertThat(scope.getMap().get("scopedTarget.scopedList").getClass()).isEqualTo(ArrayList.class);
ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
assertThat(deserialized).isNotNull();
assertThat(AopUtils.isCglibProxy(deserialized)).isTrue();
assertThat(deserialized.contains("myFriend")).isTrue();
boolean condition = deserialized instanceof ScopedObject;
assertThat(condition).isTrue();
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
assertThat(scopedDeserialized.getTargetObject().getClass()).isEqualTo(ArrayList.class);
bf.setSerializationId(null);
}
use of org.springframework.context.testfixture.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");
assertThat(bean.getName()).isEqualTo("male");
assertThat(bean.getAge()).isEqualTo(99);
assertThat(scope.getMap().containsKey("scopedTarget.testBean")).isTrue();
assertThat(scope.getMap().get("scopedTarget.testBean").getClass()).isEqualTo(TestBean.class);
}
Aggregations