1 16 17 package org.springframework.web.servlet.handler; 18 19 import javax.servlet.ServletException ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.context.ApplicationContextException; 24 import org.springframework.mock.web.MockHttpServletRequest; 25 import org.springframework.mock.web.MockServletContext; 26 import org.springframework.web.context.ConfigurableWebApplicationContext; 27 import org.springframework.web.context.support.XmlWebApplicationContext; 28 import org.springframework.web.servlet.HandlerExecutionChain; 29 import org.springframework.web.servlet.HandlerMapping; 30 31 35 public class BeanNameUrlHandlerMappingTests extends TestCase { 36 37 public static final String CONF = "/org/springframework/web/servlet/handler/map1.xml"; 38 39 private ConfigurableWebApplicationContext wac; 40 41 public void setUp() throws Exception { 42 MockServletContext sc = new MockServletContext(""); 43 wac = new XmlWebApplicationContext(); 44 wac.setServletContext(sc); 45 wac.setConfigLocations(new String [] {CONF}); 46 wac.refresh(); 47 } 48 49 public void testRequestsWithoutHandlers() throws Exception { 50 HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); 51 52 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html"); 53 req.setContextPath("/myapp"); 54 Object h = hm.getHandler(req); 55 assertTrue("Handler is null", h == null); 56 57 req = new MockHttpServletRequest("GET", "/foo/bar/baz.html"); 58 h = hm.getHandler(req); 59 assertTrue("Handler is null", h == null); 60 } 61 62 public void testRequestsWithSubPaths() throws Exception { 63 HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); 64 Object bean = wac.getBean("godCtrl"); 65 66 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html"); 67 HandlerExecutionChain hec = hm.getHandler(req); 68 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 69 70 req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html"); 71 req.setContextPath("/myapp"); 72 hec = hm.getHandler(req); 73 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 74 75 req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html"); 76 req.setContextPath("/myapp"); 77 req.setServletPath("/mypath/welcome.html"); 78 hec = hm.getHandler(req); 79 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 80 81 req = new MockHttpServletRequest("GET", "/myapp/myservlet/mypath/welcome.html"); 82 req.setContextPath("/myapp"); 83 req.setServletPath("/myservlet"); 84 hec = hm.getHandler(req); 85 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 86 87 req = new MockHttpServletRequest("GET", "/myapp/myapp/mypath/welcome.html"); 88 req.setContextPath("/myapp"); 89 req.setServletPath("/myapp"); 90 hec = hm.getHandler(req); 91 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 92 93 req = new MockHttpServletRequest("GET", "/mypath/show.html"); 94 hec = hm.getHandler(req); 95 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 96 97 req = new MockHttpServletRequest("GET", "/mypath/bookseats.html"); 98 hec = hm.getHandler(req); 99 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 100 } 101 102 public void testRequestsWithFullPaths() throws Exception { 103 BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping(); 104 hm.setAlwaysUseFullPath(true); 105 hm.setApplicationContext(wac); 106 Object bean = wac.getBean("godCtrl"); 107 108 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html"); 109 HandlerExecutionChain hec = hm.getHandler(req); 110 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 111 112 req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html"); 113 req.setContextPath("/myapp"); 114 hec = hm.getHandler(req); 115 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 116 117 req = new MockHttpServletRequest("GET", "/mypath/welcome.html"); 118 req.setContextPath(""); 119 req.setServletPath("/mypath"); 120 hec = hm.getHandler(req); 121 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 122 123 req = new MockHttpServletRequest("GET", "/Myapp/mypath/welcome.html"); 124 req.setContextPath("/myapp"); 125 req.setServletPath("/mypath"); 126 hec = hm.getHandler(req); 127 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 128 } 129 130 public void testAsteriskMatches() throws Exception { 131 HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); 132 Object bean = wac.getBean("godCtrl"); 133 134 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html"); 135 HandlerExecutionChain hec = hm.getHandler(req); 136 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 137 138 req = new MockHttpServletRequest("GET", "/mypath/testarossa"); 139 hec = hm.getHandler(req); 140 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 141 142 req = new MockHttpServletRequest("GET", "/mypath/tes"); 143 hec = hm.getHandler(req); 144 assertTrue("Handler is correct bean", hec == null); 145 } 146 147 public void testOverlappingMappings() throws Exception { 148 BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping"); 149 Object anotherHandler = new Object (); 150 hm.registerHandler("/mypath/testaross*", anotherHandler); 151 Object bean = wac.getBean("godCtrl"); 152 153 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html"); 154 HandlerExecutionChain hec = hm.getHandler(req); 155 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 156 157 req = new MockHttpServletRequest("GET", "/mypath/testarossa"); 158 hec = hm.getHandler(req); 159 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == anotherHandler); 160 161 req = new MockHttpServletRequest("GET", "/mypath/tes"); 162 hec = hm.getHandler(req); 163 assertTrue("Handler is correct bean", hec == null); 164 } 165 166 public void testDoubleMappings() throws ServletException { 167 BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping"); 168 try { 169 hm.registerHandler("/mypath/welcome.html", new Object ()); 170 fail("Should have thrown ApplicationContextException"); 171 } 172 catch (ApplicationContextException ex) { 173 } 175 } 176 177 } 178 | Popular Tags |