Skip to content

Commit 65afc80

Browse files
committed
Working on SPR-5631
1 parent accf974 commit 65afc80

File tree

1 file changed

+111
-7
lines changed

1 file changed

+111
-7
lines changed

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/UriTemplateServletAnnotationControllerTests.java

+111-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import static org.junit.Assert.*;
1111
import org.junit.Test;
12+
import org.junit.Ignore;
1213

1314
import org.springframework.beans.BeansException;
1415
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -21,6 +22,7 @@
2122
import org.springframework.web.bind.annotation.InitBinder;
2223
import org.springframework.web.bind.annotation.PathVariable;
2324
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.RequestMethod;
2426
import org.springframework.web.context.WebApplicationContext;
2527
import org.springframework.web.context.support.GenericWebApplicationContext;
2628
import org.springframework.web.servlet.DispatcherServlet;
@@ -44,10 +46,10 @@ public void simple() throws Exception {
4446
public void multiple() throws Exception {
4547
initServlet(MultipleUriTemplateController.class);
4648

47-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42/bookings/21");
49+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42/bookings/21-other");
4850
MockHttpServletResponse response = new MockHttpServletResponse();
4951
servlet.service(request, response);
50-
assertEquals("test-42-21", response.getContentAsString());
52+
assertEquals("test-42-21-other", response.getContentAsString());
5153
}
5254

5355
@Test
@@ -71,13 +73,19 @@ public void ambiguous() throws Exception {
7173
}
7274

7375
@Test
76+
@Ignore("In progress")
7477
public void relative() throws Exception {
7578
initServlet(RelativePathUriTemplateController.class);
7679

7780
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42/bookings/21");
7881
MockHttpServletResponse response = new MockHttpServletResponse();
7982
servlet.service(request, response);
8083
assertEquals("test-42-21", response.getContentAsString());
84+
85+
request = new MockHttpServletRequest("GET", "/hotels/42/bookings/21.html");
86+
response = new MockHttpServletResponse();
87+
servlet.service(request, response);
88+
assertEquals("test-42-21", response.getContentAsString());
8189
}
8290

8391
@Test
@@ -101,6 +109,48 @@ public void typeConversionError() throws Exception {
101109
assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
102110
}
103111

112+
@Test
113+
public void explicitSubPath() throws Exception {
114+
initServlet(ExplicitSubPathController.class);
115+
116+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42");
117+
MockHttpServletResponse response = new MockHttpServletResponse();
118+
servlet.service(request, response);
119+
assertEquals("test-42", response.getContentAsString());
120+
}
121+
122+
@Test
123+
@Ignore("In progress")
124+
public void implicitSubPath() throws Exception {
125+
initServlet(ImplicitSubPathController.class);
126+
127+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42");
128+
MockHttpServletResponse response = new MockHttpServletResponse();
129+
servlet.service(request, response);
130+
assertEquals("test-42", response.getContentAsString());
131+
}
132+
133+
@Test
134+
@Ignore("In progress")
135+
public void crud() throws Exception {
136+
initServlet(CrudController.class);
137+
138+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
139+
MockHttpServletResponse response = new MockHttpServletResponse();
140+
servlet.service(request, response);
141+
assertEquals("getHotels", response.getContentAsString());
142+
143+
request = new MockHttpServletRequest("POST", "/hotels");
144+
response = new MockHttpServletResponse();
145+
servlet.service(request, response);
146+
assertEquals("newHotel", response.getContentAsString());
147+
148+
request = new MockHttpServletRequest("POST", "/hotels");
149+
response = new MockHttpServletResponse();
150+
servlet.service(request, response);
151+
assertEquals("newHotel", response.getContentAsString());
152+
}
153+
104154
private void initServlet(final Class<?> controllerclass) throws ServletException {
105155
servlet = new DispatcherServlet() {
106156
@Override
@@ -133,12 +183,14 @@ public void handle(@PathVariable("root") int root, Writer writer) throws IOExcep
133183
@Controller
134184
public static class MultipleUriTemplateController {
135185

136-
@RequestMapping("/hotels/{hotel}/bookings/{booking}")
137-
public void handle(@PathVariable("hotel") String hotel, @PathVariable int booking, Writer writer)
138-
throws IOException {
186+
@RequestMapping("/hotels/{hotel}/bookings/{booking}-{other}")
187+
public void handle(@PathVariable("hotel") String hotel,
188+
@PathVariable int booking,
189+
@PathVariable String other,
190+
Writer writer) throws IOException {
139191
assertEquals("Invalid path variable value", "42", hotel);
140192
assertEquals("Invalid path variable value", 21, booking);
141-
writer.write("test-" + hotel + "-" + booking);
193+
writer.write("test-" + hotel + "-" + booking + "-" + other);
142194
}
143195

144196
}
@@ -166,7 +218,7 @@ public void handle(@PathVariable("hotel") String hotel, @PathVariable Date date,
166218
}
167219

168220
@Controller
169-
@RequestMapping("/hotels/{hotel}/**")
221+
@RequestMapping("/hotels/{hotel}")
170222
public static class RelativePathUriTemplateController {
171223

172224
@RequestMapping("bookings/{booking}")
@@ -198,7 +250,59 @@ public void handleWildCard(Writer writer) throws IOException {
198250
writer.write("wildcard");
199251
}
200252

253+
}
254+
255+
@Controller
256+
@RequestMapping("/hotels/*")
257+
public static class ExplicitSubPathController {
258+
259+
@RequestMapping("{hotel}")
260+
public void handleHotel(@PathVariable String hotel, Writer writer) throws IOException {
261+
writer.write("test-" + hotel);
262+
}
201263

202264
}
203265

266+
@Controller
267+
@RequestMapping("hotels")
268+
public static class ImplicitSubPathController {
269+
270+
@RequestMapping("{hotel}")
271+
public void handleHotel(@PathVariable String hotel, Writer writer) throws IOException {
272+
writer.write("test-" + hotel);
273+
}
274+
}
275+
276+
@Controller
277+
@RequestMapping("hotels")
278+
public static class CrudController {
279+
280+
@RequestMapping(method = RequestMethod.GET)
281+
public void list(Writer writer) throws IOException {
282+
writer.write("list");
283+
}
284+
285+
@RequestMapping(method = RequestMethod.POST)
286+
public void create(Writer writer) throws IOException {
287+
writer.write("create");
288+
}
289+
290+
@RequestMapping(value = "{hotel}", method = RequestMethod.GET)
291+
public void show(@PathVariable String hotel, Writer writer) throws IOException {
292+
writer.write("show-" + hotel);
293+
}
294+
295+
@RequestMapping(value = "{hotel}", method = RequestMethod.PUT)
296+
public void createOrUpdate(@PathVariable String hotel, Writer writer) throws IOException {
297+
writer.write("createOrUpdate-" + hotel);
298+
}
299+
300+
@RequestMapping(value = "{hotel}", method = RequestMethod.DELETE)
301+
public void remove(@PathVariable String hotel, Writer writer) throws IOException {
302+
writer.write("remove-" + hotel);
303+
}
304+
305+
}
306+
307+
204308
}

0 commit comments

Comments
 (0)