Moved tests from testsuite to web
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.web.context.request;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ServletRequestAttributesTests {
|
||||
|
||||
private static final String KEY = "ThatThingThatThing";
|
||||
|
||||
private static final Serializable VALUE = new Serializable() {
|
||||
};
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void ctorRejectsNullArg() throws Exception {
|
||||
new ServletRequestAttributes(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAccessedAttributes() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION);
|
||||
assertSame(VALUE, value);
|
||||
attrs.requestCompleted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setRequestScopedAttribute() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
|
||||
Object value = request.getAttribute(KEY);
|
||||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setRequestScopedAttributeAfterCompletion() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
request.close();
|
||||
try {
|
||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSessionScopedAttribute() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
|
||||
Object value = session.getAttribute(KEY);
|
||||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSessionScopedAttributeAfterCompletion() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.requestCompleted();
|
||||
request.close();
|
||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
|
||||
Object value = session.getAttribute(KEY);
|
||||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setGlobalSessionScopedAttribute() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_GLOBAL_SESSION);
|
||||
Object value = session.getAttribute(KEY);
|
||||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setGlobalSessionScopedAttributeAfterCompletion() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.requestCompleted();
|
||||
request.close();
|
||||
attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_GLOBAL_SESSION);
|
||||
Object value = session.getAttribute(KEY);
|
||||
assertSame(VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
||||
MockControl mockRequest = MockControl.createControl(HttpServletRequest.class);
|
||||
HttpServletRequest request = (HttpServletRequest) mockRequest.getMock();
|
||||
request.getSession(false);
|
||||
mockRequest.setReturnValue(null, 1);
|
||||
mockRequest.replay();
|
||||
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
Object value = attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION);
|
||||
assertNull(value);
|
||||
|
||||
mockRequest.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeSessionScopedAttribute() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(KEY, VALUE);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
|
||||
Object value = session.getAttribute(KEY);
|
||||
assertNull(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception {
|
||||
MockControl mockRequest = MockControl.createControl(HttpServletRequest.class);
|
||||
HttpServletRequest request = (HttpServletRequest) mockRequest.getMock();
|
||||
request.getSession(false);
|
||||
mockRequest.setReturnValue(null, 1);
|
||||
mockRequest.replay();
|
||||
|
||||
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
|
||||
attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
|
||||
|
||||
mockRequest.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2002-2006 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.web.context.request;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 26.07.2006
|
||||
*/
|
||||
public class ServletWebRequestTests extends TestCase {
|
||||
|
||||
public void testParameters() {
|
||||
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
|
||||
servletRequest.addParameter("param1", "value1");
|
||||
servletRequest.addParameter("param2", "value2");
|
||||
servletRequest.addParameter("param2", "value2a");
|
||||
|
||||
ServletWebRequest request = new ServletWebRequest(servletRequest);
|
||||
assertEquals("value1", request.getParameter("param1"));
|
||||
assertEquals(1, request.getParameterValues("param1").length);
|
||||
assertEquals("value1", request.getParameterValues("param1")[0]);
|
||||
assertEquals("value2", request.getParameter("param2"));
|
||||
assertEquals(2, request.getParameterValues("param2").length);
|
||||
assertEquals("value2", request.getParameterValues("param2")[0]);
|
||||
assertEquals("value2a", request.getParameterValues("param2")[1]);
|
||||
|
||||
Map paramMap = request.getParameterMap();
|
||||
assertEquals(2, paramMap.size());
|
||||
assertEquals(1, ((String[]) paramMap.get("param1")).length);
|
||||
assertEquals("value1", ((String[]) paramMap.get("param1"))[0]);
|
||||
assertEquals(2, ((String[]) paramMap.get("param2")).length);
|
||||
assertEquals("value2", ((String[]) paramMap.get("param2"))[0]);
|
||||
assertEquals("value2a", ((String[]) paramMap.get("param2"))[1]);
|
||||
}
|
||||
|
||||
public void testLocale() {
|
||||
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
|
||||
servletRequest.addPreferredLocale(Locale.UK);
|
||||
|
||||
ServletWebRequest request = new ServletWebRequest(servletRequest);
|
||||
assertEquals(Locale.UK, request.getLocale());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.web.context.request;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.DerivedTestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class SessionScopeTests extends TestCase {
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
this.beanFactory = new DefaultListableBeanFactory();
|
||||
this.beanFactory.registerScope("session", new SessionScope());
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("sessionScopeTests.xml", getClass()));
|
||||
}
|
||||
|
||||
public void testGetFromScope() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
try {
|
||||
String name = "sessionScopedObject";
|
||||
assertNull(session.getAttribute(name));
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean(name);
|
||||
assertEquals(session.getAttribute(name), bean);
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testDestructionAtSessionTermination() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
try {
|
||||
String name = "sessionScopedDisposableObject";
|
||||
assertNull(session.getAttribute(name));
|
||||
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
|
||||
assertEquals(session.getAttribute(name), bean);
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
|
||||
requestAttributes.requestCompleted();
|
||||
session.invalidate();
|
||||
assertTrue(bean.wasDestroyed());
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testDestructionWithSessionSerialization() throws Exception {
|
||||
doTestDestructionWithSessionSerialization(false);
|
||||
}
|
||||
|
||||
public void testDestructionWithSessionSerializationAndBeanPostProcessor() throws Exception {
|
||||
this.beanFactory.addBeanPostProcessor(new CustomDestructionAwareBeanPostProcessor());
|
||||
doTestDestructionWithSessionSerialization(false);
|
||||
}
|
||||
|
||||
public void testDestructionWithSessionSerializationAndSerializableBeanPostProcessor() throws Exception {
|
||||
this.beanFactory.addBeanPostProcessor(new CustomSerializableDestructionAwareBeanPostProcessor());
|
||||
doTestDestructionWithSessionSerialization(true);
|
||||
}
|
||||
|
||||
private void doTestDestructionWithSessionSerialization(boolean beanNameReset) throws Exception {
|
||||
Serializable serializedState = null;
|
||||
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
try {
|
||||
String name = "sessionScopedDisposableObject";
|
||||
assertNull(session.getAttribute(name));
|
||||
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
|
||||
assertEquals(session.getAttribute(name), bean);
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
|
||||
requestAttributes.requestCompleted();
|
||||
serializedState = session.serializeState();
|
||||
assertFalse(bean.wasDestroyed());
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
|
||||
serializedState = (Serializable) SerializationTestUtils.serializeAndDeserialize(serializedState);
|
||||
|
||||
session = new MockHttpSession();
|
||||
session.deserializeState(serializedState);
|
||||
request = new MockHttpServletRequest();
|
||||
request.setSession(session);
|
||||
requestAttributes = new ServletRequestAttributes(request);
|
||||
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
try {
|
||||
String name = "sessionScopedDisposableObject";
|
||||
assertNotNull(session.getAttribute(name));
|
||||
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
|
||||
assertEquals(session.getAttribute(name), bean);
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
|
||||
requestAttributes.requestCompleted();
|
||||
session.invalidate();
|
||||
assertTrue(bean.wasDestroyed());
|
||||
|
||||
if (beanNameReset) {
|
||||
assertNull(bean.getBeanName());
|
||||
}
|
||||
else {
|
||||
assertNotNull(bean.getBeanName());
|
||||
}
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class CustomDestructionAwareBeanPostProcessor implements DestructionAwareBeanPostProcessor {
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class CustomSerializableDestructionAwareBeanPostProcessor
|
||||
implements DestructionAwareBeanPostProcessor, Serializable {
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof BeanNameAware) {
|
||||
((BeanNameAware) bean).setBeanName(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user