Add some unit tests to JsonServlet
This commit is contained in:
parent
80b24fcfc2
commit
588fbcf0e7
4 changed files with 579 additions and 12 deletions
|
@ -10,6 +10,8 @@ import java.io.StringWriter;
|
|||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
|
@ -29,6 +31,7 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
private String errorMessage;
|
||||
private Map<String, String> headers = new HashMap<String, String>();
|
||||
private String contentType;
|
||||
private String charset = "";
|
||||
|
||||
private ByteArrayOutputStream outputStream;
|
||||
private StringWriter outputWriter;
|
||||
|
@ -129,9 +132,19 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
return headers.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling setContentType("this/type;charset=UTF-8") is the same as calling
|
||||
* setContentType("this/type;charset=UTF-8"); setCharacterEncoding("UTF-8")
|
||||
*/
|
||||
@Override
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
|
||||
Pattern p = Pattern.compile(";\\scharset=([^;]+)");
|
||||
Matcher m = p.matcher(contentType);
|
||||
if (m.find()) {
|
||||
this.charset = m.group(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -139,6 +152,16 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
return contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCharacterEncoding(String charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCharacterEncoding() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Un-implemented methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -155,12 +178,6 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
"HttpServletResponseStub.getBufferSize() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCharacterEncoding() {
|
||||
throw new RuntimeException(
|
||||
"HttpServletResponseStub.getCharacterEncoding() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getLocale() {
|
||||
throw new RuntimeException(
|
||||
|
@ -191,12 +208,6 @@ public class HttpServletResponseStub implements HttpServletResponse {
|
|||
"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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue