Polishing
This commit is contained in:
parent
8815788004
commit
0e45f4cec4
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.aop.scope;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -33,6 +34,7 @@ import org.springframework.core.io.ClassPathResource;
|
|||
import org.springframework.core.testfixture.io.SerializationTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.InstanceOfAssertFactories.type;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
|
|
@ -51,95 +53,91 @@ class ScopedProxyTests {
|
|||
|
||||
|
||||
@Test // SPR-2108
|
||||
void testProxyAssignable() {
|
||||
void proxyAssignable() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(MAP_CONTEXT);
|
||||
Object baseMap = bf.getBean("singletonMap");
|
||||
assertThat(baseMap instanceof Map).isTrue();
|
||||
assertThat(baseMap).isInstanceOf(Map.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSimpleProxy() {
|
||||
void simpleProxy() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(MAP_CONTEXT);
|
||||
Object simpleMap = bf.getBean("simpleMap");
|
||||
assertThat(simpleMap instanceof Map).isTrue();
|
||||
assertThat(simpleMap instanceof HashMap).isTrue();
|
||||
assertThat(simpleMap).isInstanceOf(HashMap.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testScopedOverride() {
|
||||
void scopedOverride() {
|
||||
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");
|
||||
ITestBean bean = ctx.getBean("testBean", ITestBean.class);
|
||||
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);
|
||||
assertThat(scope.getMap()).containsKey("scopedTarget.testBean");
|
||||
assertThat(scope.getMap().get("scopedTarget.testBean")).isExactlyInstanceOf(TestBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testJdkScopedProxy() throws Exception {
|
||||
void jdkScopedProxy() 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");
|
||||
ITestBean bean = bf.getBean("testBean", ITestBean.class);
|
||||
assertThat(bean).isNotNull();
|
||||
assertThat(AopUtils.isJdkDynamicProxy(bean)).isTrue();
|
||||
assertThat(bean instanceof ScopedObject).isTrue();
|
||||
ScopedObject scoped = (ScopedObject) bean;
|
||||
assertThat(scoped.getTargetObject().getClass()).isEqualTo(TestBean.class);
|
||||
assertThat(bean).asInstanceOf(type(ScopedObject.class))
|
||||
.extracting(ScopedObject::getTargetObject)
|
||||
.isExactlyInstanceOf(TestBean.class);
|
||||
|
||||
assertThat(scope.getMap()).containsKey("testBeanTarget");
|
||||
assertThat(scope.getMap().get("testBeanTarget")).isExactlyInstanceOf(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);
|
||||
assertThat(deserialized instanceof ScopedObject).isTrue();
|
||||
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
|
||||
assertThat(scopedDeserialized.getTargetObject().getClass()).isEqualTo(TestBean.class);
|
||||
|
||||
bf.setSerializationId(null);
|
||||
assertThat(deserialized.getAge()).isEqualTo(101);
|
||||
assertThat(deserialized).asInstanceOf(type(ScopedObject.class))
|
||||
.extracting(ScopedObject::getTargetObject)
|
||||
.isExactlyInstanceOf(TestBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCglibScopedProxy() throws Exception {
|
||||
void cglibScopedProxy() 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();
|
||||
assertThat(tb.getFriends() instanceof ScopedObject).isTrue();
|
||||
ScopedObject scoped = (ScopedObject) tb.getFriends();
|
||||
assertThat(scoped.getTargetObject().getClass()).isEqualTo(ArrayList.class);
|
||||
tb.getFriends().add("myFriend");
|
||||
TestBean tb = bf.getBean("testBean", TestBean.class);
|
||||
Collection<Object> friends = tb.getFriends();
|
||||
assertThat(AopUtils.isCglibProxy(friends)).isTrue();
|
||||
assertThat(friends).asInstanceOf(type(ScopedObject.class))
|
||||
.extracting(ScopedObject::getTargetObject)
|
||||
.isExactlyInstanceOf(ArrayList.class);
|
||||
|
||||
assertThat(scope.getMap().containsKey("scopedTarget.scopedList")).isTrue();
|
||||
assertThat(scope.getMap().get("scopedTarget.scopedList").getClass()).isEqualTo(ArrayList.class);
|
||||
assertThat(scope.getMap()).containsKey("scopedTarget.scopedList");
|
||||
assertThat(scope.getMap().get("scopedTarget.scopedList")).isExactlyInstanceOf(ArrayList.class);
|
||||
|
||||
ArrayList<Object> deserialized = (ArrayList<Object>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
|
||||
friends.add("myFriend");
|
||||
ArrayList<Object> deserialized = (ArrayList<Object>) SerializationTestUtils.serializeAndDeserialize(friends);
|
||||
assertThat(deserialized).isNotNull();
|
||||
assertThat(AopUtils.isCglibProxy(deserialized)).isTrue();
|
||||
assertThat(deserialized).contains("myFriend");
|
||||
assertThat(deserialized instanceof ScopedObject).isTrue();
|
||||
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
|
||||
assertThat(scopedDeserialized.getTargetObject().getClass()).isEqualTo(ArrayList.class);
|
||||
|
||||
bf.setSerializationId(null);
|
||||
assertThat(deserialized).asInstanceOf(type(ScopedObject.class))
|
||||
.extracting(ScopedObject::getTargetObject)
|
||||
.isExactlyInstanceOf(ArrayList.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue