📄 commandcontrollertestsuite.java
字号:
return 0;
}
}
LastModifiedTestController mc = new LastModifiedTestController();
mc.setCacheSeconds(10);
HttpServletRequest request = new MockHttpServletRequest(null, "GET", "/ok.html");
MockHttpServletResponse response = new MockHttpServletResponse();
mc.handleRequest(request, response);
assertTrue("Correct expires header", response.getHeader("Expires") != null);
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10, must-revalidate"));
}
public void testCachingWithCustomCacheForSecondsCall() throws Exception {
TestController mc = new TestController() {
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
cacheForSeconds(response, 5);
return super.handle(request, response, command, errors);
}
};
HttpServletRequest request = new MockHttpServletRequest(null, "GET", "/ok.html");
MockHttpServletResponse response = new MockHttpServletResponse();
mc.handleRequest(request, response);
assertTrue("Correct expires header", response.getHeader("Expires") != null);
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=5"));
}
public void testCachingWithCustomApplyCacheSecondsCall1() throws Exception {
TestController mc = new TestController() {
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
applyCacheSeconds(response, 5);
return super.handle(request, response, command, errors);
}
};
HttpServletRequest request = new MockHttpServletRequest(null, "GET", "/ok.html");
MockHttpServletResponse response = new MockHttpServletResponse();
mc.handleRequest(request, response);
assertTrue("Correct expires header", response.getHeader("Expires") != null);
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=5"));
}
public void testCachingWithCustomApplyCacheSecondsCall2() throws Exception {
TestController mc = new TestController() {
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
applyCacheSeconds(response, 0);
return super.handle(request, response, command, errors);
}
};
HttpServletRequest request = new MockHttpServletRequest(null, "GET", "/ok.html");
MockHttpServletResponse response = new MockHttpServletResponse();
mc.handleRequest(request, response);
assertTrue("Correct expires header", response.getHeader("Expires").equals("" + 1L));
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("no-cache"));
}
public void testCachingWithCustomApplyCacheSecondsCall3() throws Exception {
TestController mc = new TestController() {
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
applyCacheSeconds(response, -1);
return super.handle(request, response, command, errors);
}
};
HttpServletRequest request = new MockHttpServletRequest(null, "GET", "/ok.html");
MockHttpServletResponse response = new MockHttpServletResponse();
mc.handleRequest(request, response);
assertTrue("No expires header", response.getHeader("Expires") == null);
assertTrue("No cache control", response.getHeader("Cache-Control") == null);
}
public void testCustomDateEditorWithAllowEmpty() throws Exception {
final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
TestController mc = new TestController() {
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
}
};
MockHttpServletRequest request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("date", "1.5.2003");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
TestBean tb = (TestBean) mv.getModel().get("command");
Errors errors = (Errors) mv.getModel().get("errors");
assertTrue("No field error", !errors.hasFieldErrors("date"));
assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate()));
assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date")));
request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("date", "");
response = new MockHttpServletResponse();
mv = mc.handleRequest(request, response);
tb = (TestBean) mv.getModel().get("command");
errors = (Errors) mv.getModel().get("errors");
assertTrue("No field error", !errors.hasFieldErrors("date"));
assertTrue("Correct date property", tb.getDate() == null);
assertTrue("Correct date value", errors.getFieldValue("date") == null);
}
public void testCustomDateEditorWithoutAllowEmpty() throws Exception {
final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
TestController mc = new TestController() {
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
}
};
MockHttpServletRequest request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("date", "1.5.2003");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
TestBean tb = (TestBean) mv.getModel().get("command");
Errors errors = (Errors) mv.getModel().get("errors");
assertTrue("No field error", !errors.hasFieldErrors("date"));
assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate()));
assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date")));
request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("date", "");
response = new MockHttpServletResponse();
mv = mc.handleRequest(request, response);
tb = (TestBean) mv.getModel().get("command");
errors = (Errors) mv.getModel().get("errors");
assertTrue("Has field error", errors.hasFieldErrors("date"));
assertTrue("Correct date property", tb.getDate() != null);
assertTrue("Correct date value", errors.getFieldValue("date") != null);
}
public void testCustomNumberEditorWithAllowEmpty() throws Exception {
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
TestController mc = new TestController() {
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
}
};
MockHttpServletRequest request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("myFloat", "5,1");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
TestBean tb = (TestBean) mv.getModel().get("command");
Errors errors = (Errors) mv.getModel().get("errors");
assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
assertTrue("Correct float property", (new Float(5.1)).equals(tb.getMyFloat()));
assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat")));
request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("myFloat", "");
response = new MockHttpServletResponse();
mv = mc.handleRequest(request, response);
tb = (TestBean) mv.getModel().get("command");
errors = (Errors) mv.getModel().get("errors");
assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
assertTrue("Correct float property", tb.getMyFloat() == null);
assertTrue("Correct float value", errors.getFieldValue("myFloat") == null);
}
public void testCustomNumberEditorWithoutAllowEmpty() throws Exception {
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
TestController mc = new TestController() {
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, false));
}
};
MockHttpServletRequest request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("myFloat", "5,1");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
TestBean tb = (TestBean) mv.getModel().get("command");
Errors errors = (Errors) mv.getModel().get("errors");
assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
assertTrue("Correct float property", (new Float(5.1)).equals(tb.getMyFloat()));
assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat")));
request = new MockHttpServletRequest(null, "GET", "/welcome.html");
request.addParameter("myFloat", "");
response = new MockHttpServletResponse();
mv = mc.handleRequest(request, response);
tb = (TestBean) mv.getModel().get("command");
errors = (Errors) mv.getModel().get("errors");
assertTrue("Has field error", errors.hasFieldErrors("myFloat"));
assertTrue("Correct float property", tb.getMyFloat() != null);
assertTrue("Correct float value", errors.getFieldValue("myFloat") != null);
}
private static class TestController extends AbstractCommandController {
private TestController() {
super(TestBean.class, "person");
}
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
Map m = new HashMap();
assertTrue("Command not null", command != null);
assertTrue("errors not null", errors != null);
m.put("errors", errors);
m.put("command", command);
return new ModelAndView(request.getServletPath(), m);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -