consistent copies of mocks

This commit is contained in:
Juergen Hoeller 2009-01-20 18:01:40 +00:00
parent 76a6527c4c
commit f0415306d5
13 changed files with 2290 additions and 2293 deletions

View File

@ -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.
@ -18,7 +18,6 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletInputStream;
import org.springframework.util.Assert;
@ -26,12 +25,12 @@ import org.springframework.util.Assert;
/**
* Delegating implementation of {@link javax.servlet.ServletInputStream}.
*
* <p>Used by {@link org.springframework.mock.web.MockHttpServletRequest}; typically not directly
* <p>Used by {@link MockHttpServletRequest}; typically not directly
* used for testing application controllers.
*
* @author Juergen Hoeller
* @since 1.0.2
* @see org.springframework.mock.web.MockHttpServletRequest
* @see MockHttpServletRequest
*/
public class DelegatingServletInputStream extends ServletInputStream {

View File

@ -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.
@ -18,7 +18,6 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import org.springframework.util.Assert;
@ -26,12 +25,12 @@ import org.springframework.util.Assert;
/**
* Delegating implementation of {@link javax.servlet.ServletOutputStream}.
*
* <p>Used by {@link org.springframework.mock.web.MockHttpServletResponse}; typically not directly
* <p>Used by {@link MockHttpServletResponse}; typically not directly
* used for testing application controllers.
*
* @author Juergen Hoeller
* @since 1.0.2
* @see org.springframework.mock.web.MockHttpServletResponse
* @see MockHttpServletResponse
*/
public class DelegatingServletOutputStream extends ServletOutputStream {

View File

@ -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,9 +16,9 @@
package org.springframework.mock.web;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -35,7 +35,7 @@ import org.springframework.util.CollectionUtils;
*/
class HeaderValueHolder {
private final List values = new LinkedList();
private final List<Object> values = new LinkedList<Object>();
public void setValue(Object value) {
@ -47,7 +47,7 @@ class HeaderValueHolder {
this.values.add(value);
}
public void addValues(Collection values) {
public void addValues(Collection<?> values) {
this.values.addAll(values);
}
@ -55,10 +55,18 @@ class HeaderValueHolder {
CollectionUtils.mergeArrayIntoCollection(values, this.values);
}
public List getValues() {
public List<Object> getValues() {
return Collections.unmodifiableList(this.values);
}
public List<String> getStringValues() {
List<String> stringList = new ArrayList<String>(this.values.size());
for (Object value : this.values) {
stringList.add(value.toString());
}
return Collections.unmodifiableList(stringList);
}
public Object getValue() {
return (!this.values.isEmpty() ? this.values.get(0) : null);
}
@ -71,12 +79,11 @@ class HeaderValueHolder {
* @return the corresponding HeaderValueHolder,
* or <code>null</code> if none found
*/
public static HeaderValueHolder getByName(Map headers, String name) {
public static HeaderValueHolder getByName(Map<String, HeaderValueHolder> headers, String name) {
Assert.notNull(name, "Header name must not be null");
for (Iterator it = headers.keySet().iterator(); it.hasNext();) {
String headerName = (String) it.next();
for (String headerName : headers.keySet()) {
if (headerName.equalsIgnoreCase(name)) {
return (HeaderValueHolder) headers.get(headerName);
return headers.get(headerName);
}
}
return null;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 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,12 +16,10 @@
package org.springframework.mock.web;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Collections;
import java.util.Map;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 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.
@ -28,14 +28,12 @@ import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
@ -47,7 +45,7 @@ import org.springframework.util.Assert;
/**
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest}
* interface. Supports the Servlet 2.4 API level.
* interface. Supports the Servlet 2.5 API level.
*
* <p>Used for testing the web framework; also useful for testing
* application controllers.
@ -98,7 +96,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
// ServletRequest properties
//---------------------------------------------------------------------
private final Hashtable attributes = new Hashtable();
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
private String characterEncoding;
@ -121,7 +119,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
private String remoteHost = DEFAULT_REMOTE_HOST;
/** List of locales in descending order */
private final Vector locales = new Vector();
private final List<Locale> locales = new LinkedList<Locale>();
private boolean secure = false;
@ -147,7 +145,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
/**
* The key is the lowercase header name; the value is a {@link HeaderValueHolder} object.
*/
private final Hashtable headers = new Hashtable();
private final Map<String, HeaderValueHolder> headers = new LinkedHashMap<String, HeaderValueHolder>();
private String method;
@ -159,7 +157,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
private String remoteUser;
private final Set userRoles = new HashSet();
private final Set<String> userRoles = new HashSet<String>();
private Principal userPrincipal;
@ -284,9 +282,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
return this.attributes.get(name);
}
public Enumeration getAttributeNames() {
public Enumeration<String> getAttributeNames() {
checkActive();
return this.attributes.keys();
return Collections.enumeration(this.attributes.keySet());
}
public String getCharacterEncoding() {
@ -439,7 +437,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
return (arr != null && arr.length > 0 ? arr[0] : null);
}
public Enumeration getParameterNames() {
public Enumeration<String> getParameterNames() {
return Collections.enumeration(this.parameters.keySet());
}
@ -448,7 +446,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
return this.parameters.get(name);
}
public Map getParameterMap() {
public Map<String, String[]> getParameterMap() {
return Collections.unmodifiableMap(this.parameters);
}
@ -545,11 +543,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public Locale getLocale() {
return (Locale) this.locales.get(0);
return this.locales.get(0);
}
public Enumeration getLocales() {
return this.locales.elements();
public Enumeration<Locale> getLocales() {
return Collections.enumeration(this.locales);
}
public void setSecure(boolean secure) {
@ -613,7 +611,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
return this.authType;
}
public void setCookies(Cookie[] cookies) {
public void setCookies(Cookie... cookies) {
this.cookies = cookies;
}
@ -679,13 +677,13 @@ public class MockHttpServletRequest implements HttpServletRequest {
return (header != null ? header.getValue().toString() : null);
}
public Enumeration getHeaders(String name) {
public Enumeration<String> getHeaders(String name) {
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
return Collections.enumeration(header != null ? header.getValues() : Collections.EMPTY_LIST);
return Collections.enumeration(header != null ? header.getStringValues() : new LinkedList<String>());
}
public Enumeration getHeaderNames() {
return this.headers.keys();
public Enumeration<String> getHeaderNames() {
return Collections.enumeration(this.headers.keySet());
}
public int getIntHeader(String name) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 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.
@ -26,12 +26,10 @@ import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@ -41,7 +39,7 @@ import org.springframework.web.util.WebUtils;
/**
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse}
* interface. Supports the Servlet 2.4 API level.
* interface. Supports the Servlet 2.5 API level.
*
* <p>Used for testing the web framework; also useful for testing
* application controllers.
@ -88,12 +86,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
// HttpServletResponse properties
//---------------------------------------------------------------------
private final List cookies = new ArrayList();
private final List<Cookie> cookies = new ArrayList<Cookie>();
/**
* The key is the lowercase header name; the value is a {@link HeaderValueHolder} object.
*/
private final Map headers = new HashMap();
private final Map<String, HeaderValueHolder> headers = new HashMap<String, HeaderValueHolder>();
private int status = HttpServletResponse.SC_OK;
@ -266,13 +264,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
public Cookie[] getCookies() {
return (Cookie[]) this.cookies.toArray(new Cookie[this.cookies.size()]);
return this.cookies.toArray(new Cookie[this.cookies.size()]);
}
public Cookie getCookie(String name) {
Assert.notNull(name, "Cookie name must not be null");
for (Iterator it = this.cookies.iterator(); it.hasNext();) {
Cookie cookie = (Cookie) it.next();
for (Cookie cookie : this.cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
@ -288,7 +285,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
* Return the names of all specified headers as a Set of Strings.
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
*/
public Set getHeaderNames() {
public Set<String> getHeaderNames() {
return this.headers.keySet();
}
@ -308,9 +305,9 @@ public class MockHttpServletResponse implements HttpServletResponse {
* @param name the name of the header
* @return the associated header values, or an empty List if none
*/
public List getHeaders(String name) {
public List<Object> getHeaders(String name) {
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
return (header != null ? header.getValues() : Collections.EMPTY_LIST);
return (header != null ? header.getValues() : Collections.emptyList());
}
/**
@ -372,11 +369,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
public void setDateHeader(String name, long value) {
setHeaderValue(name, new Long(value));
setHeaderValue(name, value);
}
public void addDateHeader(String name, long value) {
addHeaderValue(name, new Long(value));
addHeaderValue(name, value);
}
public void setHeader(String name, String value) {
@ -388,11 +385,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
public void setIntHeader(String name, int value) {
setHeaderValue(name, new Integer(value));
setHeaderValue(name, value);
}
public void addIntHeader(String name, int value) {
addHeaderValue(name, new Integer(value));
addHeaderValue(name, value);
}
private void setHeaderValue(String name, Object value) {

View File

@ -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.
@ -17,12 +17,12 @@
package org.springframework.mock.web;
import java.io.Serializable;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
@ -60,7 +60,7 @@ public class MockHttpSession implements HttpSession {
private final ServletContext servletContext;
private final Hashtable attributes = new Hashtable();
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
private boolean invalid = false;
@ -68,8 +68,8 @@ public class MockHttpSession implements HttpSession {
/**
* Create a new MockHttpSession with a default {@link org.springframework.mock.web.MockServletContext}.
* @see org.springframework.mock.web.MockServletContext
* Create a new MockHttpSession with a default {@link MockServletContext}.
* @see MockServletContext
*/
public MockHttpSession() {
this(null);
@ -136,12 +136,12 @@ public class MockHttpSession implements HttpSession {
return getAttribute(name);
}
public Enumeration getAttributeNames() {
return this.attributes.keys();
public Enumeration<String> getAttributeNames() {
return Collections.enumeration(this.attributes.keySet());
}
public String[] getValueNames() {
return (String[]) this.attributes.keySet().toArray(new String[this.attributes.size()]);
return this.attributes.keySet().toArray(new String[this.attributes.size()]);
}
public void setAttribute(String name, Object value) {
@ -177,9 +177,9 @@ public class MockHttpSession implements HttpSession {
* Clear all of this session's attributes.
*/
public void clearAttributes() {
for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String name = (String) entry.getKey();
for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Object> entry = it.next();
String name = entry.getKey();
Object value = entry.getValue();
it.remove();
if (value instanceof HttpSessionBindingListener) {
@ -212,14 +212,14 @@ public class MockHttpSession implements HttpSession {
* @return a representation of this session's serialized state
*/
public Serializable serializeState() {
HashMap state = new HashMap();
for (Iterator it = this.attributes.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String name = (String) entry.getKey();
HashMap<String, Serializable> state = new HashMap<String, Serializable>();
for (Iterator<Map.Entry<String, Object>> it = this.attributes.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Object> entry = it.next();
String name = entry.getKey();
Object value = entry.getValue();
it.remove();
if (value instanceof Serializable) {
state.put(name, value);
state.put(name, (Serializable) value);
}
else {
// Not serializable... Servlet containers usually automatically
@ -237,9 +237,10 @@ public class MockHttpSession implements HttpSession {
* created by {@link #serializeState()}.
* @param state a representation of this session's serialized state
*/
@SuppressWarnings("unchecked")
public void deserializeState(Serializable state) {
Assert.isTrue(state instanceof Map, "Serialized state needs to be of type [java.util.Map]");
this.attributes.putAll((Map) state);
this.attributes.putAll((Map<String, Object>) state);
}
}

View File

@ -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.
@ -19,7 +19,6 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

View File

@ -19,7 +19,7 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.el.ELContext;
import javax.servlet.Servlet;
@ -61,7 +61,7 @@ public class MockPageContext extends PageContext {
private final ServletConfig servletConfig;
private final Map<String, Object> attributes = new HashMap<String, Object>();
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
private JspWriter out;

View File

@ -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.
@ -76,7 +76,7 @@ public class MockRequestDispatcher implements RequestDispatcher {
/**
* Obtain the underlying MockHttpServletResponse,
* unwrapping {@link javax.servlet.http.HttpServletResponseWrapper} decorators if necessary.
* unwrapping {@link HttpServletResponseWrapper} decorators if necessary.
*/
protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) {
if (response instanceof MockHttpServletResponse) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 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.

View File

@ -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.
@ -24,12 +24,11 @@ import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.activation.FileTypeMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
@ -90,11 +89,11 @@ public class MockServletContext implements ServletContext {
private String contextPath = "";
private final Map contexts = new HashMap();
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
private final Properties initParameters = new Properties();
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
private final Hashtable attributes = new Hashtable();
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
private String servletContextName = "MockServletContext";
@ -174,7 +173,7 @@ public class MockServletContext implements ServletContext {
if (this.contextPath.equals(contextPath)) {
return this;
}
return (ServletContext) this.contexts.get(contextPath);
return this.contexts.get(contextPath);
}
public int getMajorVersion() {
@ -189,7 +188,7 @@ public class MockServletContext implements ServletContext {
return MimeTypeResolver.getMimeType(filePath);
}
public Set getResourcePaths(String path) {
public Set<String> getResourcePaths(String path) {
String actualPath = (path.endsWith("/") ? path : path + "/");
Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
try {
@ -198,10 +197,10 @@ public class MockServletContext implements ServletContext {
if (ObjectUtils.isEmpty(fileList)) {
return null;
}
Set resourcePaths = new LinkedHashSet(fileList.length);
for (int i = 0; i < fileList.length; i++) {
String resultPath = actualPath + fileList[i];
if (resource.createRelative(fileList[i]).getFile().isDirectory()) {
Set<String> resourcePaths = new LinkedHashSet<String>(fileList.length);
for (String fileEntry : fileList) {
String resultPath = actualPath + fileEntry;
if (resource.createRelative(fileEntry).getFile().isDirectory()) {
resultPath += "/";
}
resourcePaths.add(resultPath);
@ -260,12 +259,12 @@ public class MockServletContext implements ServletContext {
return null;
}
public Enumeration getServlets() {
return Collections.enumeration(Collections.EMPTY_SET);
public Enumeration<Servlet> getServlets() {
return Collections.enumeration(new HashSet<Servlet>());
}
public Enumeration getServletNames() {
return Collections.enumeration(Collections.EMPTY_SET);
public Enumeration<String> getServletNames() {
return Collections.enumeration(new HashSet<String>());
}
public void log(String message) {
@ -297,16 +296,16 @@ public class MockServletContext implements ServletContext {
public String getInitParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.initParameters.getProperty(name);
return this.initParameters.get(name);
}
public void addInitParameter(String name, String value) {
Assert.notNull(name, "Parameter name must not be null");
this.initParameters.setProperty(name, value);
this.initParameters.put(name, value);
}
public Enumeration getInitParameterNames() {
return this.initParameters.keys();
public Enumeration<String> getInitParameterNames() {
return Collections.enumeration(this.initParameters.keySet());
}
public Object getAttribute(String name) {
@ -314,8 +313,8 @@ public class MockServletContext implements ServletContext {
return this.attributes.get(name);
}
public Enumeration getAttributeNames() {
return this.attributes.keys();
public Enumeration<String> getAttributeNames() {
return Collections.enumeration(this.attributes.keySet());
}
public void setAttribute(String name, Object value) {