Servlet/Portlet ApplicationContexts use a specific id based on servlet/portlet name; DefaultListableBeanFactory references are serializable now when initialized with an id; scoped proxies are serializable now, for web scopes as well as for singleton beans; injected request/session references are serializable proxies for the current request now
This commit is contained in:
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.aop.scope;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
@@ -31,6 +31,7 @@ import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
@@ -82,6 +83,7 @@ public class ScopedProxyTests {
|
||||
@Test
|
||||
public void testJdkScopedProxy() throws Exception {
|
||||
XmlBeanFactory bf = new XmlBeanFactory(TESTBEAN_CONTEXT);
|
||||
bf.setSerializationId("X");
|
||||
SimpleMapScope scope = new SimpleMapScope();
|
||||
bf.registerScope("request", scope);
|
||||
|
||||
@@ -91,14 +93,26 @@ public class ScopedProxyTests {
|
||||
assertTrue(bean instanceof ScopedObject);
|
||||
ScopedObject scoped = (ScopedObject) bean;
|
||||
assertEquals(TestBean.class, scoped.getTargetObject().getClass());
|
||||
bean.setAge(101);
|
||||
|
||||
assertTrue(scope.getMap().containsKey("testBeanTarget"));
|
||||
assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass());
|
||||
|
||||
ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean);
|
||||
assertNotNull(deserialized);
|
||||
assertTrue(AopUtils.isJdkDynamicProxy(deserialized));
|
||||
assertEquals(101, bean.getAge());
|
||||
assertTrue(deserialized instanceof ScopedObject);
|
||||
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
|
||||
assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass());
|
||||
|
||||
bf.setSerializationId(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCglibScopedProxy() {
|
||||
public void testCglibScopedProxy() throws Exception {
|
||||
XmlBeanFactory bf = new XmlBeanFactory(LIST_CONTEXT);
|
||||
bf.setSerializationId("Y");
|
||||
SimpleMapScope scope = new SimpleMapScope();
|
||||
bf.registerScope("request", scope);
|
||||
|
||||
@@ -107,9 +121,20 @@ public class ScopedProxyTests {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,16 +16,16 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import example.scannable.FooService;
|
||||
import example.scannable.ScopedProxyTestBean;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
import org.springframework.beans.factory.config.SimpleMapScope;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import example.scannable.FooService;
|
||||
import example.scannable.ScopedProxyTestBean;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -54,7 +54,7 @@ public class ComponentScanParserScopedProxyTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfacesScopedProxy() {
|
||||
public void testInterfacesScopedProxy() throws Exception {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"org/springframework/context/annotation/scopedProxyInterfacesTests.xml");
|
||||
context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
|
||||
@@ -62,16 +62,26 @@ public class ComponentScanParserScopedProxyTests {
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTargetClassScopedProxy() {
|
||||
public void testTargetClassScopedProxy() throws Exception {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"org/springframework/context/annotation/scopedProxyTargetClassTests.xml");
|
||||
context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
|
||||
ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
|
||||
// should be a class-based proxy
|
||||
assertTrue(AopUtils.isCglibProxy(bean));
|
||||
// test serializability
|
||||
assertEquals("bar", bean.foo(1));
|
||||
ScopedProxyTestBean deserialized = (ScopedProxyTestBean) SerializationTestUtils.serializeAndDeserialize(bean);
|
||||
assertNotNull(deserialized);
|
||||
assertEquals("bar", deserialized.foo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
/*
|
||||
* The Spring Framework is published under the terms
|
||||
* of the Apache Software License.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -13,20 +23,14 @@ import java.io.NotSerializableException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
/**
|
||||
* Utilities for testing serializability of objects.
|
||||
* Exposes static methods for use in other test cases.
|
||||
* Extends TestCase only to test itself.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class SerializationTestUtils extends TestCase {
|
||||
public class SerializationTestUtils {
|
||||
|
||||
public static void testSerialization(Object o) throws IOException {
|
||||
OutputStream baos = new ByteArrayOutputStream();
|
||||
@@ -55,43 +59,7 @@ public class SerializationTestUtils extends TestCase {
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
Object o2 = ois.readObject();
|
||||
|
||||
return o2;
|
||||
}
|
||||
|
||||
public SerializationTestUtils(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public void testWithNonSerializableObject() throws IOException {
|
||||
TestBean o = new TestBean();
|
||||
assertFalse(o instanceof Serializable);
|
||||
|
||||
assertFalse(isSerializable(o));
|
||||
|
||||
try {
|
||||
testSerialization(o);
|
||||
fail();
|
||||
}
|
||||
catch (NotSerializableException ex) {
|
||||
// Ok
|
||||
}
|
||||
}
|
||||
|
||||
public void testWithSerializableObject() throws Exception {
|
||||
int x = 5;
|
||||
int y = 10;
|
||||
Point p = new Point(x, y);
|
||||
assertTrue(p instanceof Serializable);
|
||||
|
||||
testSerialization(p);
|
||||
|
||||
assertTrue(isSerializable(p));
|
||||
|
||||
Point p2 = (Point) serializeAndDeserialize(p);
|
||||
assertNotSame(p, p2);
|
||||
assertEquals(x, (int) p2.getX());
|
||||
assertEquals(y, (int) p2.getY());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user