📄 dispatcherservlettestsuite.java
字号:
request.addPreferredLocale(Locale.CANADA);
request.addRole("role1");
request.addParameter("servlet", "yes");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("forwarded to failed", "failed3.jsp", response.getForwardedUrl());
assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException);
}
public void testSimpleMappingExceptionResolverWithAllHandlers1() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/loc.do");
request.addPreferredLocale(Locale.CANADA);
request.addRole("role1");
request.addParameter("access", "yes");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl());
assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException);
}
public void testSimpleMappingExceptionResolverWithAllHandlers2() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/loc.do");
request.addPreferredLocale(Locale.CANADA);
request.addRole("role1");
request.addParameter("servlet", "yes");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl());
assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException);
}
public void testSimpleMappingExceptionResolverWithDefaultErrorView() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/locale.do");
request.addPreferredLocale(Locale.CANADA);
request.addRole("role1");
request.addParameter("exception", "yes");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(RuntimeException.class));
}
public void testLocaleChangeInterceptor1() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/locale.do");
request.addPreferredLocale(Locale.GERMAN);
request.addRole("role2");
request.addParameter("locale", "en");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException.class));
}
public void testLocaleChangeInterceptor2() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/locale.do");
request.addPreferredLocale(Locale.GERMAN);
request.addRole("role2");
request.addParameter("locale", "en");
request.addParameter("locale2", "en_CA");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertTrue("Not forwarded", response.getForwardedUrl() == null);
}
public void testThemeChangeInterceptor1() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/locale.do");
request.addPreferredLocale(Locale.CANADA);
request.addRole("role1");
request.addParameter("theme", "mytheme");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException.class));
}
public void testThemeChangeInterceptor2() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/locale.do");
request.addPreferredLocale(Locale.CANADA);
request.addRole("role1");
request.addParameter("theme", "mytheme");
request.addParameter("theme2", "theme");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
complexDispatcherServlet.service(request, response);
assertTrue("Not forwarded", response.getForwardedUrl() == null);
}
catch (ServletException ex) {
fail("Should not have thrown ServletException: " + ex.getMessage());
}
}
public void testNotAuthorized() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/locale.do");
request.addPreferredLocale(Locale.CANADA);
MockHttpServletResponse response = new MockHttpServletResponse();
try {
complexDispatcherServlet.service(request, response);
assertTrue("Correct response", response.getStatus() == HttpServletResponse.SC_FORBIDDEN);
}
catch (ServletException ex) {
fail("Should not have thrown ServletException: " + ex.getMessage());
}
}
public void testNotDetectAllHandlerMappings() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setDetectAllHandlerMappings(false);
complexDispatcherServlet.init(new MockServletConfig(servletConfig.getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/unknown.do");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertTrue(response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
public void testHandlerNotMappedWithAutodetect() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
// No parent
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.init(new MockServletConfig(servletConfig.getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET",
URL_KNOWN_ONLY_PARENT);
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
}
public void testDetectHandlerMappingFromParent() throws ServletException, IOException {
// Create a parent context that includes a mapping
StaticWebApplicationContext parent = new StaticWebApplicationContext();
parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
parent.refresh();
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
// Will have parent
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
ServletConfig config = new MockServletConfig(servletConfig.getServletContext(), "complex");
config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
complexDispatcherServlet.init(config);
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET",
URL_KNOWN_ONLY_PARENT);
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(),
response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
public static class ControllerFromParent implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView(ControllerFromParent.class.getName());
}
}
public void testNotDetectAllHandlerExceptionResolvers() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setDetectAllHandlerExceptionResolvers(false);
complexDispatcherServlet.init(new MockServletConfig(servletConfig.getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/unknown.do");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
complexDispatcherServlet.service(request, response);
fail("Should have thrown ServletException");
}
catch (ServletException ex) {
// expected
assertTrue(ex.getMessage().indexOf("No adapter for handler") != -1);
}
}
public void testNotDetectAllViewResolvers() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setDetectAllViewResolvers(false);
complexDispatcherServlet.init(new MockServletConfig(servletConfig.getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/unknown.do");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
complexDispatcherServlet.service(request, response);
fail("Should have thrown ServletException");
}
catch (ServletException ex) {
// expected
assertTrue(ex.getMessage().indexOf("failed0") != -1);
}
}
public void testThrowawayController() throws Exception {
SimpleWebApplicationContext.TestThrowawayController.counter = 0;
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/throwaway.do");
request.addParameter("myInt", "5");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
simpleDispatcherServlet.service(request, response);
assertTrue("Correct response", "view5".equals(response.getForwardedUrl()));
assertEquals(1, SimpleWebApplicationContext.TestThrowawayController.counter);
}
catch (ServletException ex) {
fail("Should not have thrown ServletException: " + ex.getMessage());
}
}
public void testThrowawayControllerWithBindingFailure() throws Exception {
SimpleWebApplicationContext.TestThrowawayController.counter = 0;
MockHttpServletRequest request = new MockHttpServletRequest(servletConfig.getServletContext(), "GET", "/throwaway.do");
request.addParameter("myInt", "5x");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
simpleDispatcherServlet.service(request, response);
fail("Should have thrown ServletException");
}
catch (ServletException ex) {
// expected
assertTrue(ex.getRootCause() instanceof BindException);
assertEquals(1, SimpleWebApplicationContext.TestThrowawayController.counter);
}
}
public void testWebApplicationContextLookup() {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/invalid.do");
try {
RequestContextUtils.getWebApplicationContext(request);
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
try {
RequestContextUtils.getWebApplicationContext(request, servletContext);
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, new StaticWebApplicationContext());
try {
RequestContextUtils.getWebApplicationContext(request, servletContext);
}
catch (IllegalStateException ex) {
fail("Should not have thrown IllegalStateException: " + ex.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -