Skip to content

Commit b03226a

Browse files
committed
Upgrade to Spring 3.2 and Servlet API 3 and add tests
Tests take advantage of the new Spring MVC Test framework in 3.2.
1 parent 3d0e9f9 commit b03226a

22 files changed

+1183
-41
lines changed

pom.xml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>1.0.0-BUILD-SNAPSHOT</version>
1010
<properties>
1111
<java-version>1.6</java-version>
12-
<org.springframework-version>3.1.0.RELEASE</org.springframework-version>
12+
<org.springframework-version>3.2.0.BUILD-SNAPSHOT</org.springframework-version>
1313
<org.aspectj-version>1.6.10</org.aspectj-version>
1414
<org.slf4j-version>1.6.1</org.slf4j-version>
1515
</properties>
@@ -73,10 +73,9 @@
7373

7474
<!-- Servlet -->
7575
<dependency>
76-
<groupId>javax.servlet</groupId>
77-
<artifactId>servlet-api</artifactId>
78-
<version>2.5</version>
79-
<scope>provided</scope>
76+
<groupId>org.apache.tomcat</groupId>
77+
<artifactId>tomcat-servlet-api</artifactId>
78+
<version>7.0.30</version>
8079
</dependency>
8180
<dependency>
8281
<groupId>javax.servlet.jsp</groupId>
@@ -141,12 +140,27 @@
141140
</dependency>
142141

143142
<!-- Test -->
143+
<dependency>
144+
<groupId>org.springframework</groupId>
145+
<artifactId>spring-test</artifactId>
146+
<version>${org.springframework-version}</version>
147+
</dependency>
144148
<dependency>
145149
<groupId>junit</groupId>
146150
<artifactId>junit</artifactId>
147151
<version>4.8.2</version>
148152
<scope>test</scope>
149153
</dependency>
154+
<dependency>
155+
<groupId>xmlunit</groupId>
156+
<artifactId>xmlunit</artifactId>
157+
<version>1.2</version>
158+
</dependency>
159+
<dependency>
160+
<groupId>com.jayway.jsonpath</groupId>
161+
<artifactId>json-path</artifactId>
162+
<version>0.8.1</version>
163+
</dependency>
150164

151165
</dependencies>
152166
<repositories>

src/main/java/org/springframework/samples/mvc/convert/ConvertController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ConvertController {
2525
public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {
2626
return "Converted date " + value;
2727
}
28-
28+
2929
@RequestMapping("collection")
3030
public @ResponseBody String collection(@RequestParam Collection<Integer> values) {
3131
return "Converted collection " + values;

src/main/java/org/springframework/samples/mvc/messageconverters/MessageConvertersController.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
@RequestMapping("messageconverters/*")
1919
public class MessageConvertersController {
2020

21-
// StringHttpMessageConverter
21+
// StringHttpMessageConverter
2222

2323
@RequestMapping(value="/string", method=RequestMethod.POST)
2424
public @ResponseBody String readString(@RequestBody String string) {
2525
return "Read string '" + string + "'";
2626
}
27-
27+
2828
@RequestMapping(value="/string", method=RequestMethod.GET)
2929
public @ResponseBody String writeString() {
3030
return "Wrote a string";
3131
}
3232

3333
// Form encoded data (application/x-www-form-urlencoded)
34-
34+
3535
@RequestMapping(value="/form", method=RequestMethod.POST)
3636
public @ResponseBody String readForm(@ModelAttribute JavaBean bean) {
3737
return "Read x-www-form-urlencoded: " + bean;
3838
}
39-
39+
4040
@RequestMapping(value="/form", method=RequestMethod.GET)
4141
public @ResponseBody MultiValueMap<String, String> writeForm() {
4242
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
@@ -46,36 +46,36 @@ public class MessageConvertersController {
4646
}
4747

4848
// Jaxb2RootElementHttpMessageConverter (requires JAXB2 on the classpath - useful for serving clients that expect to work with XML)
49-
49+
5050
@RequestMapping(value="/xml", method=RequestMethod.POST)
5151
public @ResponseBody String readXml(@RequestBody JavaBean bean) {
5252
return "Read from XML: " + bean;
5353
}
54-
54+
5555
@RequestMapping(value="/xml", method=RequestMethod.GET)
5656
public @ResponseBody JavaBean writeXml() {
57-
return new JavaBean("bar", "fruit");
57+
return new JavaBean("bar", "apple");
5858
}
5959

6060
// MappingJacksonHttpMessageConverter (requires Jackson on the classpath - particularly useful for serving JavaScript clients that expect to work with JSON)
61-
61+
6262
@RequestMapping(value="/json", method=RequestMethod.POST)
6363
public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) {
6464
return "Read from JSON: " + bean;
6565
}
66-
66+
6767
@RequestMapping(value="/json", method=RequestMethod.GET)
6868
public @ResponseBody JavaBean writeJson() {
69-
return new JavaBean("bar", "fruit");
69+
return new JavaBean("bar", "apple");
7070
}
7171

7272
// AtomFeedHttpMessageConverter (requires Rome on the classpath - useful for serving Atom feeds)
73-
73+
7474
@RequestMapping(value="/atom", method=RequestMethod.POST)
7575
public @ResponseBody String readFeed(@RequestBody Feed feed) {
7676
return "Read " + feed.getTitle();
7777
}
78-
78+
7979
@RequestMapping(value="/atom", method=RequestMethod.GET)
8080
public @ResponseBody Feed writeFeed() {
8181
Feed feed = new Feed();
@@ -85,12 +85,12 @@ public class MessageConvertersController {
8585
}
8686

8787
// RssChannelHttpMessageConverter (requires Rome on the classpath - useful for serving RSS feeds)
88-
88+
8989
@RequestMapping(value="/rss", method=RequestMethod.POST)
9090
public @ResponseBody String readChannel(@RequestBody Channel channel) {
9191
return "Read " + channel.getTitle();
9292
}
93-
93+
9494
@RequestMapping(value="/rss", method=RequestMethod.GET)
9595
public @ResponseBody Channel writeChannel() {
9696
Channel channel = new Channel();

src/main/java/org/springframework/samples/mvc/response/ResponseController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public class ResponseController {
1919

2020
@RequestMapping(value="/response/charset/accept", method=RequestMethod.GET)
2121
public @ResponseBody String responseAcceptHeaderCharset() {
22-
return "こんにちは世界! (\"Hello world!\" in Japanese)";
22+
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
2323
}
2424

2525
@RequestMapping(value="/response/charset/produce", method=RequestMethod.GET, produces="text/plain;charset=UTF-8")
2626
public @ResponseBody String responseProducesConditionCharset() {
27-
return "こんにちは世界! (\"Hello world!\" in Japanese)";
27+
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
2828
}
2929

3030
@RequestMapping(value="/response/entity/status", method=RequestMethod.GET)

src/main/webapp/WEB-INF/web.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
1+
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
4+
version="3.0">
55

66
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
77
<context-param>
@@ -23,6 +23,7 @@
2323
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
2424
</init-param>
2525
<load-on-startup>1</load-on-startup>
26+
<async-supported>true</async-supported>
2627
</servlet>
2728

2829
<servlet-mapping>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.samples.mvc;
17+
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.test.context.ContextConfiguration;
20+
import org.springframework.test.context.web.WebAppConfiguration;
21+
import org.springframework.web.context.WebApplicationContext;
22+
23+
@WebAppConfiguration
24+
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")
25+
public class AbstractContextControllerTests {
26+
27+
@Autowired
28+
protected WebApplicationContext wac;
29+
30+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.samples.mvc.convert;
17+
18+
import static org.hamcrest.Matchers.startsWith;
19+
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.content;
21+
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status;
22+
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.springframework.format.support.DefaultFormattingConversionService;
27+
import org.springframework.format.support.FormattingConversionService;
28+
import org.springframework.test.web.mock.servlet.MockMvc;
29+
30+
public class ConvertControllerTests {
31+
32+
private MockMvc mockMvc;
33+
34+
@Before
35+
public void setup() throws Exception {
36+
FormattingConversionService cs = new DefaultFormattingConversionService();
37+
cs.addFormatterForFieldAnnotation(new MaskFormatAnnotationFormatterFactory());
38+
39+
this.mockMvc = standaloneSetup(new ConvertController())
40+
.setConversionService(cs)
41+
.alwaysExpect(status().isOk())
42+
.build();
43+
}
44+
45+
@Test
46+
public void primitive() throws Exception {
47+
this.mockMvc.perform(get("/convert/primitive").param("value", "3"))
48+
.andExpect(content().string("Converted primitive 3"));
49+
}
50+
51+
@Test
52+
public void date() throws Exception {
53+
this.mockMvc.perform(get("/convert/date/2010-07-04"))
54+
.andExpect(content().string("Converted date Sun Jul 04 00:00:00 EDT 2010"));
55+
}
56+
57+
@Test
58+
public void collection() throws Exception {
59+
this.mockMvc.perform(get("/convert/collection?values=1&values=2&values=3&values=4&values=5"))
60+
.andExpect(content().string("Converted collection [1, 2, 3, 4, 5]"));
61+
}
62+
63+
@Test
64+
public void collection2() throws Exception {
65+
this.mockMvc.perform(get("/convert/collection?values=1,2,3,4,5"))
66+
.andExpect(content().string("Converted collection [1, 2, 3, 4, 5]"));
67+
}
68+
69+
@Test
70+
public void formattedCollection() throws Exception {
71+
this.mockMvc.perform(get("/convert/formattedCollection?values=2010-07-04,2011-07-04"))
72+
.andExpect(content().string("Converted formatted collection [Sun Jul 04 00:00:00 EDT 2010, Mon Jul 04 00:00:00 EDT 2011]"));
73+
}
74+
75+
@Test
76+
public void valueOf() throws Exception {
77+
this.mockMvc.perform(get("/convert/value?value=123456789"))
78+
.andExpect(content().string(startsWith(
79+
"Converted value object org.springframework.samples.mvc.convert.SocialSecurityNumber")));
80+
}
81+
82+
@Test
83+
public void custom() throws Exception {
84+
this.mockMvc.perform(get("/convert/custom?value=123-45-6789"))
85+
.andExpect(content().string("Converted '123456789' with a custom converter"));
86+
}
87+
88+
@Test
89+
public void beanPrimitive() throws Exception {
90+
this.mockMvc.perform(get("/convert/bean?primitive=3"))
91+
.andExpect(content().string("Converted JavaBean primitive=3"));
92+
}
93+
94+
@Test
95+
public void beanDate() throws Exception {
96+
this.mockMvc.perform(get("/convert/bean?date=2010-07-04"))
97+
.andExpect(content().string("Converted JavaBean date=Sun Jul 04 00:00:00 EDT 2010"));
98+
}
99+
100+
@Test
101+
public void beanMasked() throws Exception {
102+
this.mockMvc.perform(get("/convert/bean?masked=(205) 333-3333"))
103+
.andExpect(content().string("Converted JavaBean masked=2053333333"));
104+
}
105+
106+
@Test
107+
public void beanCollection() throws Exception {
108+
this.mockMvc.perform(get("/convert/bean?list[0]=1&list[1]=2&list[2]=3"))
109+
.andExpect(content().string("Converted JavaBean list=[1, 2, 3]"));
110+
}
111+
112+
@Test
113+
public void beanFormattedCollection() throws Exception {
114+
this.mockMvc.perform(get("/convert/bean?formattedList[0]=2010-07-04&formattedList[1]=2011-07-04"))
115+
.andExpect(content().string(
116+
"Converted JavaBean formattedList=[Sun Jul 04 00:00:00 EDT 2010, Mon Jul 04 00:00:00 EDT 2011]"));
117+
}
118+
119+
@Test
120+
public void beanMap() throws Exception {
121+
this.mockMvc.perform(get("/convert/bean?map[0]=apple&map[1]=pear"))
122+
.andExpect(content().string("Converted JavaBean map={0=apple, 1=pear}"));
123+
}
124+
125+
@Test
126+
public void beanNested() throws Exception {
127+
this.mockMvc.perform(get("/convert/bean?nested.foo=bar&nested.list[0].foo=baz&nested.map[key].list[0].foo=bip"))
128+
.andExpect(content().string(
129+
"Converted JavaBean nested=NestedBean foo=bar list=[NestedBean foo=baz] map={key=NestedBean list=[NestedBean foo=bip]}"));
130+
}
131+
132+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.samples.mvc.data;
17+
18+
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
19+
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.content;
20+
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.springframework.samples.mvc.data.custom.CustomArgumentController;
25+
import org.springframework.samples.mvc.data.custom.CustomArgumentResolver;
26+
import org.springframework.test.web.mock.servlet.MockMvc;
27+
28+
public class CustomArgumentControllerTests {
29+
private MockMvc mockMvc;
30+
31+
@Before
32+
public void setup() throws Exception {
33+
this.mockMvc = standaloneSetup(new CustomArgumentController())
34+
.setCustomArgumentResolvers(new CustomArgumentResolver()).build();
35+
}
36+
37+
@Test
38+
public void param() throws Exception {
39+
this.mockMvc.perform(get("/data/custom"))
40+
.andExpect(content().string("Got 'foo' request attribute value 'bar'"));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)