NIHVIVO-3542 Enhance the Stub classes so I can write unit tests for the IndividualController. Adjust AuthenticateTest to use the enhanced HttpServletRequestStub.

This commit is contained in:
j2blake 2012-01-24 17:22:34 +00:00
parent 191d579065
commit ca46511f40
12 changed files with 1007 additions and 191 deletions

View file

@ -27,9 +27,17 @@ public class ServletContextStub implements ServletContext {
// Stub infrastructure
// ----------------------------------------------------------------------
private String contextPath = ""; // root context returns ""
private final Map<String, Object> attributes = new HashMap<String, Object>();
private final Map<String, String> mockResources = new HashMap<String, String>();
private final Map<String, String> realPaths = new HashMap<String, String>();
public void setContextPath(String contextPath) {
if (contextPath == null) {
throw new NullPointerException("contextPath may not be null.");
}
}
public void setMockResource(String path, String contents) {
if (path == null) {
throw new NullPointerException("path may not be null.");
@ -40,11 +48,27 @@ public class ServletContextStub implements ServletContext {
mockResources.put(path, contents);
}
}
public void setRealPath(String path, String filepath) {
if (path == null) {
throw new NullPointerException("path may not be null.");
}
if (filepath == null) {
realPaths.remove(path);
} else {
realPaths.put(path, filepath);
}
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public String getContextPath() {
return contextPath;
}
@Override
public Object getAttribute(String name) {
return attributes.get(name);
@ -78,6 +102,11 @@ public class ServletContextStub implements ServletContext {
}
}
@Override
public String getRealPath(String path) {
return realPaths.get(path);
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@ -88,12 +117,6 @@ public class ServletContextStub implements ServletContext {
"ServletContextStub.getContext() not implemented.");
}
@Override
public String getContextPath() {
throw new RuntimeException(
"ServletContextStub.getContextPath() not implemented.");
}
@Override
public String getInitParameter(String arg0) {
throw new RuntimeException(
@ -131,12 +154,6 @@ public class ServletContextStub implements ServletContext {
"ServletContextStub.getNamedDispatcher() not implemented.");
}
@Override
public String getRealPath(String arg0) {
throw new RuntimeException(
"ServletContextStub.getRealPath() not implemented.");
}
@Override
public RequestDispatcher getRequestDispatcher(String arg0) {
throw new RuntimeException(