Moved tests from testsuite to web
This commit is contained in:
@@ -1,169 +0,0 @@
|
||||
/*
|
||||
* 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 junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
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 {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public void testUpdateAccessedAttributes() 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();
|
||||
}
|
||||
|
||||
public void testSetRequestScopedAttribute() 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);
|
||||
}
|
||||
|
||||
public void testSetRequestScopedAttributeAfterCompletion() 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
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetSessionScopedAttribute() 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);
|
||||
}
|
||||
|
||||
public void testSetSessionScopedAttributeAfterCompletion() 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);
|
||||
}
|
||||
|
||||
public void testSetGlobalSessionScopedAttribute() 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);
|
||||
}
|
||||
|
||||
public void testSetGlobalSessionScopedAttributeAfterCompletion() 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);
|
||||
}
|
||||
|
||||
public void testGetSessionScopedAttributeDoesNotForceCreationOfSession() 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();
|
||||
}
|
||||
|
||||
public void testRemoveSessionScopedAttribute() 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);
|
||||
}
|
||||
|
||||
public void testRemoveSessionScopedAttributeDoesNotForceCreationOfSession() 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user