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:
parent
191d579065
commit
ca46511f40
12 changed files with 1007 additions and 191 deletions
|
@ -2,10 +2,14 @@
|
|||
|
||||
package stubs.javax.servlet.http;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
|
@ -23,7 +27,11 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
private String redirectLocation;
|
||||
private int status = 200;
|
||||
private String errorMessage;
|
||||
private StringWriter outputWriter = new StringWriter();
|
||||
private Map<String, String> headers = new HashMap<String, String>();
|
||||
private String contentType;
|
||||
|
||||
private ByteArrayOutputStream outputStream;
|
||||
private StringWriter outputWriter;
|
||||
|
||||
public String getRedirectLocation() {
|
||||
return redirectLocation;
|
||||
|
@ -37,6 +45,20 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
return errorMessage;
|
||||
}
|
||||
|
||||
public String getOutput() {
|
||||
if (outputStream != null) {
|
||||
return outputStream.toString();
|
||||
} else if (outputWriter != null) {
|
||||
return outputWriter.toString();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public String getHeader(String name) {
|
||||
return headers.get(name);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -47,11 +69,18 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("hiding")
|
||||
public void sendError(int status) throws IOException {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("hiding")
|
||||
public void sendError(int status, String message) throws IOException {
|
||||
this.status = status;
|
||||
this.errorMessage = message;
|
||||
|
@ -59,7 +88,55 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
|
||||
@Override
|
||||
public PrintWriter getWriter() throws IOException {
|
||||
return new PrintWriter(outputWriter);
|
||||
if (outputStream != null) {
|
||||
throw new IllegalStateException(
|
||||
"Can't get a Writer after getting an OutputStream.");
|
||||
}
|
||||
|
||||
if (outputWriter == null) {
|
||||
outputWriter = new StringWriter();
|
||||
}
|
||||
|
||||
return new PrintWriter(outputWriter, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletOutputStream getOutputStream() throws IOException {
|
||||
if (outputWriter != null) {
|
||||
throw new IllegalStateException(
|
||||
"Can't get an OutputStream after getting a Writer.");
|
||||
}
|
||||
|
||||
if (outputStream == null) {
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
return new ServletOutputStream() {
|
||||
@Override
|
||||
public void write(int thisChar) throws IOException {
|
||||
outputStream.write(thisChar);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeader(String name, String value) {
|
||||
headers.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsHeader(String name) {
|
||||
return headers.containsKey(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -84,24 +161,12 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
"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 boolean isCommitted() {
|
||||
throw new RuntimeException(
|
||||
|
@ -138,12 +203,6 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
"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(
|
||||
|
@ -174,12 +233,6 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
"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(
|
||||
|
@ -210,24 +263,12 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
"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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue