NIHVIVO-1207 enhance stub classes for unit tests.
This commit is contained in:
parent
78df1b181f
commit
631870cad2
4 changed files with 447 additions and 105 deletions
|
@ -5,8 +5,10 @@ package stubs.javax.servlet.http;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -20,18 +22,26 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jeb228
|
||||
* A simple stub for HttpServletRequest
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
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, Object> attributes;
|
||||
|
||||
public HttpServletRequestStub() {
|
||||
parameters = new HashMap<String, List<String>>();
|
||||
attributes = new HashMap<String, Object>();
|
||||
}
|
||||
|
||||
public HttpServletRequestStub(Map<String, List<String>> parameters,
|
||||
Map<String, Object> attributes) {
|
||||
this();
|
||||
|
@ -39,9 +49,17 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
this.attributes.putAll(attributes);
|
||||
}
|
||||
|
||||
public HttpServletRequestStub() {
|
||||
parameters = new HashMap<String, List<String>>();
|
||||
attributes = new HashMap<String, Object>();
|
||||
public void setRequestUrl(URL url) {
|
||||
this.requestUrl = url;
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
|
@ -56,17 +74,52 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
parameters.remove(name);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
public String getParameter(String name) {
|
||||
if (!parameters.containsKey(name)) {
|
||||
return null;
|
||||
}
|
||||
return parameters.get(name).get(0);
|
||||
public void setSession(HttpSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// 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() {
|
||||
Map<String, String[]> map = new HashMap<String, String[]>();
|
||||
for (String key : parameters.keySet()) {
|
||||
|
@ -75,20 +128,50 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
return map;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Un-implemented methods
|
||||
// ----------------------------------------------------------------------
|
||||
public String getParameter(String name) {
|
||||
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() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getAuthType() not implemented.");
|
||||
}
|
||||
|
||||
public String getContextPath() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getContextPath() not implemented.");
|
||||
}
|
||||
|
||||
public Cookie[] getCookies() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getCookies() not implemented.");
|
||||
|
@ -104,11 +187,13 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"HttpServletRequestStub.getHeader() not implemented.");
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getHeaderNames() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getHeaderNames() not implemented.");
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getHeaders(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getHeaders() not implemented.");
|
||||
|
@ -119,11 +204,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"HttpServletRequestStub.getIntHeader() not implemented.");
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getMethod() not implemented.");
|
||||
}
|
||||
|
||||
public String getPathInfo() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getPathInfo() not implemented.");
|
||||
|
@ -164,16 +244,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"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() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getUserPrincipal() not implemented.");
|
||||
|
@ -204,16 +274,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"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() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getCharacterEncoding() not implemented.");
|
||||
|
@ -254,21 +314,12 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"HttpServletRequestStub.getLocale() not implemented.");
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getLocales() {
|
||||
throw new RuntimeException(
|
||||
"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() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getProtocol() not implemented.");
|
||||
|
@ -284,11 +335,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"HttpServletRequestStub.getRealPath() not implemented.");
|
||||
}
|
||||
|
||||
public String getRemoteAddr() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getRemoteAddr() not implemented.");
|
||||
}
|
||||
|
||||
public String getRemoteHost() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletRequestStub.getRemoteHost() not implemented.");
|
||||
|
@ -324,16 +370,6 @@ public class HttpServletRequestStub implements HttpServletRequest {
|
|||
"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)
|
||||
throws UnsupportedEncodingException {
|
||||
throw new RuntimeException(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue