use of org.springframework.web.servlet.HandlerMapping in project spring-framework by spring-projects.
the class HandlerMappingIntrospectorTests method detectHandlerMappingsOrdered.
@Test
public void detectHandlerMappingsOrdered() throws Exception {
StaticWebApplicationContext cxt = new StaticWebApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues(Collections.singletonMap("order", "3"));
cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class, pvs);
pvs = new MutablePropertyValues(Collections.singletonMap("order", "2"));
cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class, pvs);
pvs = new MutablePropertyValues(Collections.singletonMap("order", "1"));
cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class, pvs);
cxt.refresh();
List<?> expected = Arrays.asList(cxt.getBean("hmC"), cxt.getBean("hmB"), cxt.getBean("hmA"));
List<HandlerMapping> actual = new HandlerMappingIntrospector(cxt).getHandlerMappings();
assertEquals(expected, actual);
}
use of org.springframework.web.servlet.HandlerMapping in project spring-framework by spring-projects.
the class HandlerMappingIntrospectorTests method detectHandlerMappings.
@Test
public void detectHandlerMappings() throws Exception {
StaticWebApplicationContext cxt = new StaticWebApplicationContext();
cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class);
cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class);
cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class);
cxt.refresh();
List<?> expected = Arrays.asList(cxt.getBean("hmA"), cxt.getBean("hmB"), cxt.getBean("hmC"));
List<HandlerMapping> actual = new HandlerMappingIntrospector(cxt).getHandlerMappings();
assertEquals(expected, actual);
}
use of org.springframework.web.servlet.HandlerMapping in project spring-framework by spring-projects.
the class BeanNameUrlHandlerMappingTests method requestsWithSubPaths.
@Test
public void requestsWithSubPaths() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
doTestRequestsWithSubPaths(hm);
}
use of org.springframework.web.servlet.HandlerMapping in project spring-framework by spring-projects.
the class BeanNameUrlHandlerMappingTests method requestsWithoutHandlers.
@Test
public void requestsWithoutHandlers() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html");
req.setContextPath("/myapp");
Object h = hm.getHandler(req);
assertTrue("Handler is null", h == null);
req = new MockHttpServletRequest("GET", "/foo/bar/baz.html");
h = hm.getHandler(req);
assertTrue("Handler is null", h == null);
}
use of org.springframework.web.servlet.HandlerMapping in project spring-framework by spring-projects.
the class SimpleUrlHandlerMappingTests method checkMappings.
private void checkMappings(String beanName) throws Exception {
MockServletContext sc = new MockServletContext("");
XmlWebApplicationContext wac = new XmlWebApplicationContext();
wac.setServletContext(sc);
wac.setConfigLocations(new String[] { "/org/springframework/web/servlet/handler/map2.xml" });
wac.refresh();
Object bean = wac.getBean("mainController");
Object otherBean = wac.getBean("otherController");
Object defaultBean = wac.getBean("starController");
HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
HandlerExecutionChain hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/welcome.x");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/welcome/");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
assertEquals("welcome", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/");
req.setServletPath("/welcome.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/welcome.html");
req.setContextPath("/app");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/show.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/bookseats.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/original-welcome.html");
req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/original-show.html");
req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/original-bookseats.html");
req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
req = new MockHttpServletRequest("GET", "/");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
assertEquals("/", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
req = new MockHttpServletRequest("GET", "/somePath");
hec = getHandler(hm, req);
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
assertEquals("/somePath", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Aggregations