1 16 17 package org.springframework.web.servlet.handler; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.beans.FatalBeanException; 22 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 23 import org.springframework.mock.web.MockHttpServletRequest; 24 import org.springframework.mock.web.MockServletContext; 25 import org.springframework.web.context.support.XmlWebApplicationContext; 26 import org.springframework.web.servlet.HandlerExecutionChain; 27 import org.springframework.web.servlet.HandlerMapping; 28 import org.springframework.web.util.UrlPathHelper; 29 30 34 public class SimpleUrlHandlerMappingTests extends TestCase { 35 36 public void testHandlerBeanNotFound() throws Exception { 37 MockServletContext sc = new MockServletContext(""); 38 XmlWebApplicationContext root = new XmlWebApplicationContext(); 39 root.setServletContext(sc); 40 root.setConfigLocations(new String [] {"/org/springframework/web/servlet/handler/map1.xml"}); 41 root.refresh(); 42 XmlWebApplicationContext wac = new XmlWebApplicationContext(); 43 wac.setParent(root); 44 wac.setServletContext(sc); 45 wac.setNamespace("map2err"); 46 wac.setConfigLocations(new String [] {"/org/springframework/web/servlet/handler/map2err.xml"}); 47 try { 48 wac.refresh(); 49 fail("Should have thrown NoSuchBeanDefinitionException"); 50 } 51 catch (FatalBeanException ex) { 52 NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); 53 assertEquals("mainControlle", nestedEx.getBeanName()); 54 } 55 } 56 57 public void testUrlMappingWithUrlMap() throws Exception { 58 checkMappings("urlMapping"); 59 } 60 61 public void testUrlMappingWithProps() throws Exception { 62 checkMappings("urlMappingWithProps"); 63 } 64 65 private void checkMappings(String beanName) throws Exception { 66 MockServletContext sc = new MockServletContext(""); 67 XmlWebApplicationContext wac = new XmlWebApplicationContext(); 68 wac.setServletContext(sc); 69 wac.setConfigLocations(new String [] {"/org/springframework/web/servlet/handler/map2.xml"}); 70 wac.refresh(); 71 Object bean = wac.getBean("mainController"); 72 Object otherBean = wac.getBean("otherController"); 73 HandlerMapping hm = (HandlerMapping) wac.getBean(beanName); 74 75 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html"); 76 HandlerExecutionChain hec = hm.getHandler(req); 77 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 78 79 req = new MockHttpServletRequest("GET", "/welcome.x"); 80 hec = hm.getHandler(req); 81 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean); 82 83 req = new MockHttpServletRequest("GET", "/"); 84 req.setServletPath("/welcome.html"); 85 hec = hm.getHandler(req); 86 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 87 88 req = new MockHttpServletRequest("GET", "/welcome.html"); 89 req.setContextPath("/app"); 90 hec = hm.getHandler(req); 91 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 92 93 req = new MockHttpServletRequest("GET", "/show.html"); 94 hec = hm.getHandler(req); 95 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 96 97 req = new MockHttpServletRequest("GET", "/bookseats.html"); 98 hec = hm.getHandler(req); 99 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 100 101 req = new MockHttpServletRequest("GET", "/original-welcome.html"); 102 req.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/welcome.html"); 103 hec = hm.getHandler(req); 104 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 105 106 req = new MockHttpServletRequest("GET", "/original-show.html"); 107 req.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/show.html"); 108 hec = hm.getHandler(req); 109 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 110 111 req = new MockHttpServletRequest("GET", "/original-bookseats.html"); 112 req.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/bookseats.html"); 113 hec = hm.getHandler(req); 114 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 115 } 116 117 } 118 | Popular Tags |