Moved tests from testsuite to web
This commit is contained in:
parent
e7f5ebf992
commit
f0e0d9b494
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -17,38 +17,33 @@
|
||||||
package org.springframework.web.context.request;
|
package org.springframework.web.context.request;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import org.easymock.MockControl;
|
import org.easymock.MockControl;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockHttpSession;
|
import org.springframework.mock.web.MockHttpSession;
|
||||||
import org.springframework.test.AssertThrows;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rick Evans
|
* @author Rick Evans
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
*/
|
*/
|
||||||
public class ServletRequestAttributesTests extends TestCase {
|
public class ServletRequestAttributesTests {
|
||||||
|
|
||||||
private static final String KEY = "ThatThingThatThing";
|
private static final String KEY = "ThatThingThatThing";
|
||||||
|
|
||||||
|
|
||||||
private static final Serializable VALUE = new Serializable() {
|
private static final Serializable VALUE = new Serializable() {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testCtorRejectsNullArg() throws Exception {
|
public void ctorRejectsNullArg() throws Exception {
|
||||||
new AssertThrows(IllegalArgumentException.class) {
|
|
||||||
public void test() throws Exception {
|
|
||||||
new ServletRequestAttributes(null);
|
new ServletRequestAttributes(null);
|
||||||
}
|
}
|
||||||
}.runTest();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testUpdateAccessedAttributes() throws Exception {
|
@Test
|
||||||
|
public void updateAccessedAttributes() throws Exception {
|
||||||
MockHttpSession session = new MockHttpSession();
|
MockHttpSession session = new MockHttpSession();
|
||||||
session.setAttribute(KEY, VALUE);
|
session.setAttribute(KEY, VALUE);
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
@ -59,7 +54,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
attrs.requestCompleted();
|
attrs.requestCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetRequestScopedAttribute() throws Exception {
|
@Test
|
||||||
|
public void setRequestScopedAttribute() throws Exception {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
|
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
|
||||||
|
|
@ -67,7 +63,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
assertSame(VALUE, value);
|
assertSame(VALUE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetRequestScopedAttributeAfterCompletion() throws Exception {
|
@Test
|
||||||
|
public void setRequestScopedAttributeAfterCompletion() throws Exception {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||||
request.close();
|
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();
|
MockHttpSession session = new MockHttpSession();
|
||||||
session.setAttribute(KEY, VALUE);
|
session.setAttribute(KEY, VALUE);
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
@ -91,7 +89,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
assertSame(VALUE, value);
|
assertSame(VALUE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetSessionScopedAttributeAfterCompletion() throws Exception {
|
@Test
|
||||||
|
public void setSessionScopedAttributeAfterCompletion() throws Exception {
|
||||||
MockHttpSession session = new MockHttpSession();
|
MockHttpSession session = new MockHttpSession();
|
||||||
session.setAttribute(KEY, VALUE);
|
session.setAttribute(KEY, VALUE);
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
@ -104,7 +103,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
assertSame(VALUE, value);
|
assertSame(VALUE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetGlobalSessionScopedAttribute() throws Exception {
|
@Test
|
||||||
|
public void setGlobalSessionScopedAttribute() throws Exception {
|
||||||
MockHttpSession session = new MockHttpSession();
|
MockHttpSession session = new MockHttpSession();
|
||||||
session.setAttribute(KEY, VALUE);
|
session.setAttribute(KEY, VALUE);
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
@ -115,7 +115,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
assertSame(VALUE, value);
|
assertSame(VALUE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetGlobalSessionScopedAttributeAfterCompletion() throws Exception {
|
@Test
|
||||||
|
public void setGlobalSessionScopedAttributeAfterCompletion() throws Exception {
|
||||||
MockHttpSession session = new MockHttpSession();
|
MockHttpSession session = new MockHttpSession();
|
||||||
session.setAttribute(KEY, VALUE);
|
session.setAttribute(KEY, VALUE);
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
@ -128,7 +129,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
assertSame(VALUE, value);
|
assertSame(VALUE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
@Test
|
||||||
|
public void getSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
||||||
MockControl mockRequest = MockControl.createControl(HttpServletRequest.class);
|
MockControl mockRequest = MockControl.createControl(HttpServletRequest.class);
|
||||||
HttpServletRequest request = (HttpServletRequest) mockRequest.getMock();
|
HttpServletRequest request = (HttpServletRequest) mockRequest.getMock();
|
||||||
request.getSession(false);
|
request.getSession(false);
|
||||||
|
|
@ -142,7 +144,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
mockRequest.verify();
|
mockRequest.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRemoveSessionScopedAttribute() throws Exception {
|
@Test
|
||||||
|
public void removeSessionScopedAttribute() throws Exception {
|
||||||
MockHttpSession session = new MockHttpSession();
|
MockHttpSession session = new MockHttpSession();
|
||||||
session.setAttribute(KEY, VALUE);
|
session.setAttribute(KEY, VALUE);
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
@ -153,7 +156,8 @@ public class ServletRequestAttributesTests extends TestCase {
|
||||||
assertNull(value);
|
assertNull(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRemoveSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
@Test
|
||||||
|
public void removeSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
||||||
MockControl mockRequest = MockControl.createControl(HttpServletRequest.class);
|
MockControl mockRequest = MockControl.createControl(HttpServletRequest.class);
|
||||||
HttpServletRequest request = (HttpServletRequest) mockRequest.getMock();
|
HttpServletRequest request = (HttpServletRequest) mockRequest.getMock();
|
||||||
request.getSession(false);
|
request.getSession(false);
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||||
|
|
||||||
|
<beans>
|
||||||
|
|
||||||
|
<bean id="sessionScopedObject" class="org.springframework.beans.TestBean" scope="session"/>
|
||||||
|
|
||||||
|
<bean id="sessionScopedDisposableObject" class="org.springframework.beans.DerivedTestBean" scope="session"/>
|
||||||
|
|
||||||
|
</beans>
|
||||||
Loading…
Reference in New Issue