NIHVIVO-1207 enhance stub classes for unit tests.

This commit is contained in:
jeb228 2010-11-04 14:50:38 +00:00
parent 78df1b181f
commit 631870cad2
4 changed files with 447 additions and 105 deletions

View file

@ -0,0 +1,56 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.javax.servlet;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
/**
* A simple stub for testing servlets.
*/
public class ServletConfigStub implements ServletConfig {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public ServletContext getServletContext() {
return servletContext;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public String getInitParameter(String arg0) {
throw new RuntimeException(
"ServletConfigStub.getInitParameter() not implemented.");
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getInitParameterNames() {
throw new RuntimeException(
"ServletConfigStub.getInitParameterNames() not implemented.");
}
@Override
public String getServletName() {
throw new RuntimeException(
"ServletConfigStub.getServletName() not implemented.");
}
}

View file

@ -5,8 +5,10 @@ package stubs.javax.servlet.http;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -20,18 +22,26 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
/** /**
* * A simple stub for HttpServletRequest
* @author jeb228
*/ */
@SuppressWarnings("deprecation")
public class HttpServletRequestStub implements HttpServletRequest { public class HttpServletRequestStub implements HttpServletRequest {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Stub infrastructure // Stub infrastructure
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private URL requestUrl;
private String httpMethodType = "GET";
private String remoteAddr = "127.0.0.1";
private HttpSession session;
private final Map<String, List<String>> parameters; private final Map<String, List<String>> parameters;
private final Map<String, Object> attributes; private final Map<String, Object> attributes;
public HttpServletRequestStub() {
parameters = new HashMap<String, List<String>>();
attributes = new HashMap<String, Object>();
}
public HttpServletRequestStub(Map<String, List<String>> parameters, public HttpServletRequestStub(Map<String, List<String>> parameters,
Map<String, Object> attributes) { Map<String, Object> attributes) {
this(); this();
@ -39,9 +49,17 @@ public class HttpServletRequestStub implements HttpServletRequest {
this.attributes.putAll(attributes); this.attributes.putAll(attributes);
} }
public HttpServletRequestStub() { public void setRequestUrl(URL url) {
parameters = new HashMap<String, List<String>>(); this.requestUrl = url;
attributes = new HashMap<String, Object>(); }
/** Set to "GET" or "POST", etc. */
public void setMethod(String method) {
this.httpMethodType = method;
}
public void setRemoteAddr(String remoteAddr) {
this.remoteAddr = remoteAddr;
} }
public void addParameter(String name, String value) { public void addParameter(String name, String value) {
@ -56,17 +74,52 @@ public class HttpServletRequestStub implements HttpServletRequest {
parameters.remove(name); parameters.remove(name);
} }
// ---------------------------------------------------------------------- public void setSession(HttpSession session) {
// Stub methods this.session = session;
// ----------------------------------------------------------------------
public String getParameter(String name) {
if (!parameters.containsKey(name)) {
return null;
}
return parameters.get(name).get(0);
} }
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
public HttpSession getSession() {
return getSession(true);
}
public HttpSession getSession(boolean create) {
if (create && (session == null)) {
session = new HttpSessionStub();
}
return session;
}
public String getContextPath() {
String path = requestUrl.getPath();
if (path.isEmpty()) {
return "";
}
int secondSlash = path.indexOf("/", 1);
if (secondSlash == -1) {
return "";
} else {
return path.substring(0, secondSlash);
}
}
public String getMethod() {
return httpMethodType;
}
public String getRemoteAddr() {
return remoteAddr;
}
@SuppressWarnings("rawtypes")
public Enumeration getParameterNames() {
return Collections.enumeration(parameters.keySet());
}
@SuppressWarnings("rawtypes")
public Map getParameterMap() { public Map getParameterMap() {
Map<String, String[]> map = new HashMap<String, String[]>(); Map<String, String[]> map = new HashMap<String, String[]>();
for (String key : parameters.keySet()) { for (String key : parameters.keySet()) {
@ -75,20 +128,50 @@ public class HttpServletRequestStub implements HttpServletRequest {
return map; return map;
} }
// ---------------------------------------------------------------------- public String getParameter(String name) {
// Un-implemented methods if (!parameters.containsKey(name)) {
// ---------------------------------------------------------------------- return null;
}
return parameters.get(name).get(0);
}
public String[] getParameterValues(String name) {
if (!parameters.containsKey(name)) {
return null;
}
List<String> list = parameters.get(name);
return list.toArray(new String[list.size()]);
}
public Object getAttribute(String name) {
return attributes.get(name);
}
@SuppressWarnings("rawtypes")
public Enumeration getAttributeNames() {
return Collections.enumeration(attributes.keySet());
}
public void removeAttribute(String name) {
attributes.remove(name);
}
public void setAttribute(String name, Object value) {
if (value == null) {
removeAttribute(name);
}
attributes.put(name, value);
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
public String getAuthType() { public String getAuthType() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getAuthType() not implemented."); "HttpServletRequestStub.getAuthType() not implemented.");
} }
public String getContextPath() {
throw new RuntimeException(
"HttpServletRequestStub.getContextPath() not implemented.");
}
public Cookie[] getCookies() { public Cookie[] getCookies() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getCookies() not implemented."); "HttpServletRequestStub.getCookies() not implemented.");
@ -104,11 +187,13 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.getHeader() not implemented."); "HttpServletRequestStub.getHeader() not implemented.");
} }
@SuppressWarnings("rawtypes")
public Enumeration getHeaderNames() { public Enumeration getHeaderNames() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getHeaderNames() not implemented."); "HttpServletRequestStub.getHeaderNames() not implemented.");
} }
@SuppressWarnings("rawtypes")
public Enumeration getHeaders(String arg0) { public Enumeration getHeaders(String arg0) {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getHeaders() not implemented."); "HttpServletRequestStub.getHeaders() not implemented.");
@ -119,11 +204,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.getIntHeader() not implemented."); "HttpServletRequestStub.getIntHeader() not implemented.");
} }
public String getMethod() {
throw new RuntimeException(
"HttpServletRequestStub.getMethod() not implemented.");
}
public String getPathInfo() { public String getPathInfo() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getPathInfo() not implemented."); "HttpServletRequestStub.getPathInfo() not implemented.");
@ -164,16 +244,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.getServletPath() not implemented."); "HttpServletRequestStub.getServletPath() not implemented.");
} }
public HttpSession getSession() {
throw new RuntimeException(
"HttpServletRequestStub.getSession() not implemented.");
}
public HttpSession getSession(boolean arg0) {
throw new RuntimeException(
"HttpServletRequestStub.getSession() not implemented.");
}
public Principal getUserPrincipal() { public Principal getUserPrincipal() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getUserPrincipal() not implemented."); "HttpServletRequestStub.getUserPrincipal() not implemented.");
@ -204,16 +274,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.isUserInRole() not implemented."); "HttpServletRequestStub.isUserInRole() not implemented.");
} }
public Object getAttribute(String arg0) {
throw new RuntimeException(
"HttpServletRequestStub.getAttribute() not implemented.");
}
public Enumeration getAttributeNames() {
throw new RuntimeException(
"HttpServletRequestStub.getAttributeNames() not implemented.");
}
public String getCharacterEncoding() { public String getCharacterEncoding() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getCharacterEncoding() not implemented."); "HttpServletRequestStub.getCharacterEncoding() not implemented.");
@ -254,21 +314,12 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.getLocale() not implemented."); "HttpServletRequestStub.getLocale() not implemented.");
} }
@SuppressWarnings("rawtypes")
public Enumeration getLocales() { public Enumeration getLocales() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getLocales() not implemented."); "HttpServletRequestStub.getLocales() not implemented.");
} }
public Enumeration getParameterNames() {
throw new RuntimeException(
"HttpServletRequestStub.getParameterNames() not implemented.");
}
public String[] getParameterValues(String arg0) {
throw new RuntimeException(
"HttpServletRequestStub.getParameterValues() not implemented.");
}
public String getProtocol() { public String getProtocol() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getProtocol() not implemented."); "HttpServletRequestStub.getProtocol() not implemented.");
@ -284,11 +335,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.getRealPath() not implemented."); "HttpServletRequestStub.getRealPath() not implemented.");
} }
public String getRemoteAddr() {
throw new RuntimeException(
"HttpServletRequestStub.getRemoteAddr() not implemented.");
}
public String getRemoteHost() { public String getRemoteHost() {
throw new RuntimeException( throw new RuntimeException(
"HttpServletRequestStub.getRemoteHost() not implemented."); "HttpServletRequestStub.getRemoteHost() not implemented.");
@ -324,16 +370,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
"HttpServletRequestStub.isSecure() not implemented."); "HttpServletRequestStub.isSecure() not implemented.");
} }
public void removeAttribute(String arg0) {
throw new RuntimeException(
"HttpServletRequestStub.removeAttribute() not implemented.");
}
public void setAttribute(String arg0, Object arg1) {
throw new RuntimeException(
"HttpServletRequestStub.setAttribute() not implemented.");
}
public void setCharacterEncoding(String arg0) public void setCharacterEncoding(String arg0)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
throw new RuntimeException( throw new RuntimeException(

View file

@ -0,0 +1,226 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.javax.servlet.http;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
/**
* A simple stub for HttpServletResponse
*/
public class HttpServletResponseStub implements HttpServletResponse {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private String redirectLocation;
public String getRedirectLocation() {
return redirectLocation;
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public void sendRedirect(String location) throws IOException {
this.redirectLocation = location;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public void flushBuffer() throws IOException {
throw new RuntimeException(
"HttpServletResponseStub.flushBuffer() not implemented.");
}
@Override
public int getBufferSize() {
throw new RuntimeException(
"HttpServletResponseStub.getBufferSize() not implemented.");
}
@Override
public String getCharacterEncoding() {
throw new RuntimeException(
"HttpServletResponseStub.getCharacterEncoding() not implemented.");
}
@Override
public String getContentType() {
throw new RuntimeException(
"HttpServletResponseStub.getContentType() not implemented.");
}
@Override
public Locale getLocale() {
throw new RuntimeException(
"HttpServletResponseStub.getLocale() not implemented.");
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
throw new RuntimeException(
"HttpServletResponseStub.getOutputStream() not implemented.");
}
@Override
public PrintWriter getWriter() throws IOException {
throw new RuntimeException(
"HttpServletResponseStub.getWriter() not implemented.");
}
@Override
public boolean isCommitted() {
throw new RuntimeException(
"HttpServletResponseStub.isCommitted() not implemented.");
}
@Override
public void reset() {
throw new RuntimeException(
"HttpServletResponseStub.reset() not implemented.");
}
@Override
public void resetBuffer() {
throw new RuntimeException(
"HttpServletResponseStub.resetBuffer() not implemented.");
}
@Override
public void setBufferSize(int arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setBufferSize() not implemented.");
}
@Override
public void setCharacterEncoding(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setCharacterEncoding() not implemented.");
}
@Override
public void setContentLength(int arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setContentLength() not implemented.");
}
@Override
public void setContentType(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setContentType() not implemented.");
}
@Override
public void setLocale(Locale arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setLocale() not implemented.");
}
@Override
public void addCookie(Cookie arg0) {
throw new RuntimeException(
"HttpServletResponseStub.addCookie() not implemented.");
}
@Override
public void addDateHeader(String arg0, long arg1) {
throw new RuntimeException(
"HttpServletResponseStub.addDateHeader() not implemented.");
}
@Override
public void addHeader(String arg0, String arg1) {
throw new RuntimeException(
"HttpServletResponseStub.addHeader() not implemented.");
}
@Override
public void addIntHeader(String arg0, int arg1) {
throw new RuntimeException(
"HttpServletResponseStub.addIntHeader() not implemented.");
}
@Override
public boolean containsHeader(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.containsHeader() not implemented.");
}
@Override
public String encodeRedirectURL(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.encodeRedirectURL() not implemented.");
}
@Override
public String encodeRedirectUrl(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.encodeRedirectUrl() not implemented.");
}
@Override
public String encodeURL(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.encodeURL() not implemented.");
}
@Override
public String encodeUrl(String arg0) {
throw new RuntimeException(
"HttpServletResponseStub.encodeUrl() not implemented.");
}
@Override
public void sendError(int arg0) throws IOException {
throw new RuntimeException(
"HttpServletResponseStub.sendError() not implemented.");
}
@Override
public void sendError(int arg0, String arg1) throws IOException {
throw new RuntimeException(
"HttpServletResponseStub.sendError() not implemented.");
}
@Override
public void setDateHeader(String arg0, long arg1) {
throw new RuntimeException(
"HttpServletResponseStub.setDateHeader() not implemented.");
}
@Override
public void setHeader(String arg0, String arg1) {
throw new RuntimeException(
"HttpServletResponseStub.setHeader() not implemented.");
}
@Override
public void setIntHeader(String arg0, int arg1) {
throw new RuntimeException(
"HttpServletResponseStub.setIntHeader() not implemented.");
}
@Override
public void setStatus(int arg0) {
throw new RuntimeException(
"HttpServletResponseStub.setStatus() not implemented.");
}
@Override
public void setStatus(int arg0, String arg1) {
throw new RuntimeException(
"HttpServletResponseStub.setStatus() not implemented.");
}
}

View file

@ -3,20 +3,36 @@
package stubs.javax.servlet.http; package stubs.javax.servlet.http;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** /**
* A simple stand-in for the HttpSession, for use in unit tests. * A simple stand-in for the HttpSession, for use in unit tests.
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class HttpSessionStub implements HttpSession { public class HttpSessionStub implements HttpSession {
private static final Log log = LogFactory.getLog(HttpSessionStub.class);
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Stub infrastructure // Stub infrastructure
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private String id = "arbitraryId";
private ServletContext context; private ServletContext context;
private final Map<String, Object> attributes = new HashMap<String, Object>();
@SuppressWarnings("unused")
private int maxInactiveInterval;
public void setId(String id) {
this.id = id;
}
public void setServletContext(ServletContext context) { public void setServletContext(ServletContext context) {
this.context = context; this.context = context;
@ -26,21 +42,52 @@ public class HttpSessionStub implements HttpSession {
// Stub methods // Stub methods
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@Override
public String getId() {
return this.id;
}
@Override @Override
public ServletContext getServletContext() { public ServletContext getServletContext() {
return this.context; return this.context;
} }
@Override
public void setAttribute(String name, Object value) {
if (name == null) {
throw new NullPointerException("name may not be null.");
}
if (value == null) {
removeAttribute(name);
}
attributes.put(name, value);
log.debug("setAttribute: " + name + "=" + value);
}
@Override
public void removeAttribute(String name) {
attributes.remove(name);
log.debug("removeAttribute: " + name);
}
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
/**
* So far, we don't do anything with this, or even confirm that it has been
* set. We just don't throw an exception if someone wants to set it.
*/
@Override
public void setMaxInactiveInterval(int seconds) {
this.maxInactiveInterval = seconds;
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Un-implemented methods // Un-implemented methods
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@Override
public Object getAttribute(String arg0) {
throw new RuntimeException(
"HttpSessionStub.getAttribute() not implemented.");
}
@Override @Override
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public Enumeration getAttributeNames() { public Enumeration getAttributeNames() {
@ -54,11 +101,6 @@ public class HttpSessionStub implements HttpSession {
"HttpSessionStub.getCreationTime() not implemented."); "HttpSessionStub.getCreationTime() not implemented.");
} }
@Override
public String getId() {
throw new RuntimeException("HttpSessionStub.getId() not implemented.");
}
@Override @Override
public long getLastAccessedTime() { public long getLastAccessedTime() {
throw new RuntimeException( throw new RuntimeException(
@ -106,27 +148,9 @@ public class HttpSessionStub implements HttpSession {
"HttpSessionStub.putValue() not implemented."); "HttpSessionStub.putValue() not implemented.");
} }
@Override
public void removeAttribute(String arg0) {
throw new RuntimeException(
"HttpSessionStub.removeAttribute() not implemented.");
}
public void removeValue(String arg0) { public void removeValue(String arg0) {
throw new RuntimeException( throw new RuntimeException(
"HttpSessionStub.removeValue() not implemented."); "HttpSessionStub.removeValue() not implemented.");
} }
@Override
public void setAttribute(String arg0, Object arg1) {
throw new RuntimeException(
"HttpSessionStub.setAttribute() not implemented.");
}
@Override
public void setMaxInactiveInterval(int arg0) {
throw new RuntimeException(
"HttpSessionStub.setMaxInactiveInterval() not implemented.");
}
} }