1 16 17 package org.springframework.web.servlet.handler; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.mock.web.MockHttpServletRequest; 22 import org.springframework.mock.web.MockServletContext; 23 import org.springframework.web.context.ConfigurableWebApplicationContext; 24 import org.springframework.web.context.support.XmlWebApplicationContext; 25 import org.springframework.web.servlet.HandlerExecutionChain; 26 import org.springframework.web.servlet.HandlerMapping; 27 28 31 public class PathMatchingUrlHandlerMappingTests extends TestCase { 32 33 public static final String CONF = "/org/springframework/web/servlet/handler/map3.xml"; 34 35 private HandlerMapping hm; 36 37 private ConfigurableWebApplicationContext wac; 38 39 public void setUp() throws Exception { 40 MockServletContext sc = new MockServletContext(""); 41 wac = new XmlWebApplicationContext(); 42 wac.setServletContext(sc); 43 wac.setConfigLocations(new String [] {CONF}); 44 wac.refresh(); 45 hm = (HandlerMapping) wac.getBean("urlMapping"); 46 } 47 48 public void testRequestsWithHandlers() throws Exception { 49 Object bean = wac.getBean("mainController"); 50 51 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html"); 52 HandlerExecutionChain hec = hm.getHandler(req); 53 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 54 55 req = new MockHttpServletRequest("GET", "/show.html"); 56 hec = hm.getHandler(req); 57 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 58 59 req = new MockHttpServletRequest("GET", "/bookseats.html"); 60 hec = hm.getHandler(req); 61 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 62 } 63 64 public void testActualPathMatching() throws Exception { 65 68 Object bean = wac.getBean("mainController"); 69 Object defaultBean = wac.getBean("starController"); 70 71 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/pathmatchingTest.html"); 73 HandlerExecutionChain hec = hm.getHandler(req); 74 assertTrue("Handler is null", hec != null); 75 assertTrue("Handler is correct bean", hec.getHandler() == bean); 76 77 req = new MockHttpServletRequest("GET", "welcome.html"); 79 hec = hm.getHandler(req); 80 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 81 82 83 req = new MockHttpServletRequest("GET", "/pathmatchingAA.html"); 85 hec = hm.getHandler(req); 86 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 87 88 req = new MockHttpServletRequest("GET", "/pathmatchingA.html"); 90 hec = hm.getHandler(req); 91 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 92 93 req = new MockHttpServletRequest("GET", "/administrator/pathmatching.html"); 95 hec = hm.getHandler(req); 96 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 97 98 req = new MockHttpServletRequest("GET", "/administrator/test/pathmatching.html"); 100 hec = hm.getHandler(req); 101 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 102 103 req = new MockHttpServletRequest("GET", "/administratort/pathmatching.html"); 105 hec = hm.getHandler(req); 106 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 107 108 req = new MockHttpServletRequest("GET", "/bla.jsp"); 110 hec = hm.getHandler(req); 111 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 112 113 req = new MockHttpServletRequest("GET", "/testing/bla.jsp"); 115 hec = hm.getHandler(req); 116 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 117 118 req = new MockHttpServletRequest("GET", "/administrator/another/bla.xml"); 120 hec = hm.getHandler(req); 121 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 122 123 req = new MockHttpServletRequest("GET", "/administrator/another/bla.gif"); 125 hec = hm.getHandler(req); 126 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 127 128 req = new MockHttpServletRequest("GET", "/administrator/test/testlastbit"); 130 hec = hm.getHandler(req); 131 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 132 133 req = new MockHttpServletRequest("GET", "/administrator/test/testla"); 135 hec = hm.getHandler(req); 136 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 137 138 req = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla"); 139 hec = hm.getHandler(req); 140 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 141 142 req = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp"); 143 hec = hm.getHandler(req); 144 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 145 146 req = new MockHttpServletRequest("GET", "/administrator/testing/longer2/notmatching/notmatching"); 147 hec = hm.getHandler(req); 148 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 149 150 req = new MockHttpServletRequest("GET", "/shortpattern/testing/toolong"); 151 hec = hm.getHandler(req); 152 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 153 154 req = new MockHttpServletRequest("GET", "/XXpathXXmatching.html"); 155 hec = hm.getHandler(req); 156 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 157 158 req = new MockHttpServletRequest("GET", "/pathXXmatching.html"); 159 hec = hm.getHandler(req); 160 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 161 162 req = new MockHttpServletRequest("GET", "/XpathXXmatching.html"); 163 hec = hm.getHandler(req); 164 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 165 166 req = new MockHttpServletRequest("GET", "/XXpathmatching.html"); 167 hec = hm.getHandler(req); 168 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 169 170 req = new MockHttpServletRequest("GET", "/show12.html"); 171 hec = hm.getHandler(req); 172 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 173 174 req = new MockHttpServletRequest("GET", "/show123.html"); 175 hec = hm.getHandler(req); 176 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 177 178 req = new MockHttpServletRequest("GET", "/show1.html"); 179 hec = hm.getHandler(req); 180 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 181 182 req = new MockHttpServletRequest("GET", "/reallyGood-test-is-this.jpeg"); 183 hec = hm.getHandler(req); 184 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 185 186 req = new MockHttpServletRequest("GET", "/reallyGood-tst-is-this.jpeg"); 187 hec = hm.getHandler(req); 188 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 189 190 req = new MockHttpServletRequest("GET", "/testing/test.jpeg"); 191 hec = hm.getHandler(req); 192 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 193 194 req = new MockHttpServletRequest("GET", "/testing/test.jpg"); 195 hec = hm.getHandler(req); 196 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 197 198 req = new MockHttpServletRequest("GET", "/anotherTest"); 199 hec = hm.getHandler(req); 200 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 201 202 req = new MockHttpServletRequest("GET", "/stillAnotherTest"); 203 hec = hm.getHandler(req); 204 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 205 206 req = new MockHttpServletRequest("GET", "/outofpattern*ye"); 208 hec = hm.getHandler(req); 209 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 210 211 req = new MockHttpServletRequest("GET", "/test't est/path'm atching.html"); 212 hec = hm.getHandler(req); 213 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 214 215 req = new MockHttpServletRequest("GET", "/test%26t%20est/path%26m%20atching.html"); 216 hec = hm.getHandler(req); 217 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean); 218 } 219 220 public void testDefaultMapping() throws Exception { 221 Object bean = wac.getBean("starController"); 222 MockHttpServletRequest req = new MockHttpServletRequest("GET", "/goggog.html"); 223 HandlerExecutionChain hec = hm.getHandler(req); 224 assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); 225 } 226 227 } 228 | Popular Tags |