private void render(int type, boolean jspx) throws Exception {
initServlet("webapp/WEB-INF/web.xml");
initFactory();
RequestContextChainingService requestContexts = (RequestContextChainingService) factory
.getBean("requestContexts");
RequestContext rc = null;
try {
// 预备request, response
rc = requestContexts.getRequestContext(servletContext, request, response);
// 设置contentType和charset,和jsp中的设置不同,以此为准
rc.getResponse().setContentType("text/plain; charset=GBK"); // JSP: text/html; charset=UTF-8
// 预备template context
TemplateContext context = new MappedTemplateContext();
context.put("hello", "中国");
String jsp = jspx ? "/test.jspx" : "/test.jsp";
// 渲染
switch (type) {
case 1:
assertEquals("", templateService.getText(jsp, context));
break;
case 2:
templateService.writeTo(jsp, context, (OutputStream) null);
break;
case 3:
templateService.writeTo(jsp, context, (Writer) null);
break;
default:
fail();
break;
}
} finally {
// 结束并提交response
requestContexts.commitRequestContext(rc);
}
WebResponse webResponse = client.getResponse(ic);
assertEquals("text/plain", webResponse.getContentType());