From 899084a1fb8df348b9a36af2c7a41def5c37b87a Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 3 Nov 2008 11:14:16 +0000 Subject: [PATCH] Moved tests from testsuite to web git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@265 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../util/SerializationTestUtils.java | 97 +++++++++++++++++++ .../ServletRequestAttributesTests.java | 48 ++++----- .../request/ServletWebRequestTests.java | 0 .../context/request/SessionScopeTests.java | 0 .../web/context/request/sessionScopeTests.xml | 10 ++ 5 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 org.springframework.web/src/test/java/org/springframework/util/SerializationTestUtils.java rename {org.springframework.testsuite => org.springframework.web}/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java (81%) rename {org.springframework.testsuite => org.springframework.web}/src/test/java/org/springframework/web/context/request/ServletWebRequestTests.java (100%) rename {org.springframework.testsuite => org.springframework.web}/src/test/java/org/springframework/web/context/request/SessionScopeTests.java (100%) create mode 100644 org.springframework.web/src/test/resources/org/springframework/web/context/request/sessionScopeTests.xml diff --git a/org.springframework.web/src/test/java/org/springframework/util/SerializationTestUtils.java b/org.springframework.web/src/test/java/org/springframework/util/SerializationTestUtils.java new file mode 100644 index 00000000000..dbe6421093e --- /dev/null +++ b/org.springframework.web/src/test/java/org/springframework/util/SerializationTestUtils.java @@ -0,0 +1,97 @@ +/* + * The Spring Framework is published under the terms + * of the Apache Software License. + */ + +package org.springframework.util; + +import java.awt.Point; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +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 static void testSerialization(Object o) throws IOException { + OutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(o); + } + + public static boolean isSerializable(Object o) throws IOException { + try { + testSerialization(o); + return true; + } + catch (NotSerializableException ex) { + return false; + } + } + + public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(o); + oos.flush(); + baos.flush(); + byte[] bytes = baos.toByteArray(); + + 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()); + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java b/org.springframework.web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java similarity index 81% rename from org.springframework.testsuite/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java rename to org.springframework.web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java index d6c83b12387..10936c4fd13 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java @@ -17,38 +17,33 @@ package org.springframework.web.context.request; import java.io.Serializable; - import javax.servlet.http.HttpServletRequest; -import junit.framework.TestCase; import org.easymock.MockControl; +import static org.junit.Assert.*; +import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpSession; -import org.springframework.test.AssertThrows; /** * @author Rick Evans * @author Juergen Hoeller */ -public class ServletRequestAttributesTests extends TestCase { +public class ServletRequestAttributesTests { private static final String KEY = "ThatThingThatThing"; - private static final Serializable VALUE = new Serializable() { }; - - public void testCtorRejectsNullArg() throws Exception { - new AssertThrows(IllegalArgumentException.class) { - public void test() throws Exception { - new ServletRequestAttributes(null); - } - }.runTest(); + @Test(expected = IllegalArgumentException.class) + public void ctorRejectsNullArg() throws Exception { + new ServletRequestAttributes(null); } - public void testUpdateAccessedAttributes() throws Exception { + @Test + public void updateAccessedAttributes() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -59,7 +54,8 @@ public class ServletRequestAttributesTests extends TestCase { attrs.requestCompleted(); } - public void testSetRequestScopedAttribute() throws Exception { + @Test + public void setRequestScopedAttribute() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes attrs = new ServletRequestAttributes(request); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST); @@ -67,7 +63,8 @@ public class ServletRequestAttributesTests extends TestCase { assertSame(VALUE, value); } - public void testSetRequestScopedAttributeAfterCompletion() throws Exception { + @Test + public void setRequestScopedAttributeAfterCompletion() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes attrs = new ServletRequestAttributes(request); request.close(); @@ -80,7 +77,8 @@ public class ServletRequestAttributesTests extends TestCase { } } - public void testSetSessionScopedAttribute() throws Exception { + @Test + public void setSessionScopedAttribute() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -91,7 +89,8 @@ public class ServletRequestAttributesTests extends TestCase { assertSame(VALUE, value); } - public void testSetSessionScopedAttributeAfterCompletion() throws Exception { + @Test + public void setSessionScopedAttributeAfterCompletion() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -104,7 +103,8 @@ public class ServletRequestAttributesTests extends TestCase { assertSame(VALUE, value); } - public void testSetGlobalSessionScopedAttribute() throws Exception { + @Test + public void setGlobalSessionScopedAttribute() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -115,7 +115,8 @@ public class ServletRequestAttributesTests extends TestCase { assertSame(VALUE, value); } - public void testSetGlobalSessionScopedAttributeAfterCompletion() throws Exception { + @Test + public void setGlobalSessionScopedAttributeAfterCompletion() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -128,7 +129,8 @@ public class ServletRequestAttributesTests extends TestCase { assertSame(VALUE, value); } - public void testGetSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception { + @Test + public void getSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception { MockControl mockRequest = MockControl.createControl(HttpServletRequest.class); HttpServletRequest request = (HttpServletRequest) mockRequest.getMock(); request.getSession(false); @@ -142,7 +144,8 @@ public class ServletRequestAttributesTests extends TestCase { mockRequest.verify(); } - public void testRemoveSessionScopedAttribute() throws Exception { + @Test + public void removeSessionScopedAttribute() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -153,7 +156,8 @@ public class ServletRequestAttributesTests extends TestCase { assertNull(value); } - public void testRemoveSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception { + @Test + public void removeSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception { MockControl mockRequest = MockControl.createControl(HttpServletRequest.class); HttpServletRequest request = (HttpServletRequest) mockRequest.getMock(); request.getSession(false); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/context/request/ServletWebRequestTests.java b/org.springframework.web/src/test/java/org/springframework/web/context/request/ServletWebRequestTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/context/request/ServletWebRequestTests.java rename to org.springframework.web/src/test/java/org/springframework/web/context/request/ServletWebRequestTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/context/request/SessionScopeTests.java b/org.springframework.web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/context/request/SessionScopeTests.java rename to org.springframework.web/src/test/java/org/springframework/web/context/request/SessionScopeTests.java diff --git a/org.springframework.web/src/test/resources/org/springframework/web/context/request/sessionScopeTests.xml b/org.springframework.web/src/test/resources/org/springframework/web/context/request/sessionScopeTests.xml new file mode 100644 index 00000000000..e4f05b9abe5 --- /dev/null +++ b/org.springframework.web/src/test/resources/org/springframework/web/context/request/sessionScopeTests.xml @@ -0,0 +1,10 @@ + + + + + + + + + +