Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
Upgrade to Spring 3.2 and Servlet API 3 and add tests
Browse files Browse the repository at this point in the history
Tests take advantage of the new Spring MVC Test framework in 3.2.
  • Loading branch information
rstoyanchev committed Oct 15, 2012
1 parent 3d0e9f9 commit b03226a
Show file tree
Hide file tree
Showing 22 changed files with 1,183 additions and 41 deletions.
24 changes: 19 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.0.RELEASE</org.springframework-version>
<org.springframework-version>3.2.0.BUILD-SNAPSHOT</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.1</org.slf4j-version>
</properties>
Expand Down Expand Up @@ -73,10 +73,9 @@

<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.30</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
Expand Down Expand Up @@ -141,12 +140,27 @@
</dependency>

<!-- Test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>

</dependencies>
<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ConvertController {
public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {
return "Converted date " + value;
}

@RequestMapping("collection")
public @ResponseBody String collection(@RequestParam Collection<Integer> values) {
return "Converted collection " + values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
@RequestMapping("messageconverters/*")
public class MessageConvertersController {

// StringHttpMessageConverter
// StringHttpMessageConverter

@RequestMapping(value="/string", method=RequestMethod.POST)
public @ResponseBody String readString(@RequestBody String string) {
return "Read string '" + string + "'";
}

@RequestMapping(value="/string", method=RequestMethod.GET)
public @ResponseBody String writeString() {
return "Wrote a string";
}

// Form encoded data (application/x-www-form-urlencoded)

@RequestMapping(value="/form", method=RequestMethod.POST)
public @ResponseBody String readForm(@ModelAttribute JavaBean bean) {
return "Read x-www-form-urlencoded: " + bean;
}

@RequestMapping(value="/form", method=RequestMethod.GET)
public @ResponseBody MultiValueMap<String, String> writeForm() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
Expand All @@ -46,36 +46,36 @@ public class MessageConvertersController {
}

// Jaxb2RootElementHttpMessageConverter (requires JAXB2 on the classpath - useful for serving clients that expect to work with XML)

@RequestMapping(value="/xml", method=RequestMethod.POST)
public @ResponseBody String readXml(@RequestBody JavaBean bean) {
return "Read from XML: " + bean;
}

@RequestMapping(value="/xml", method=RequestMethod.GET)
public @ResponseBody JavaBean writeXml() {
return new JavaBean("bar", "fruit");
return new JavaBean("bar", "apple");
}

// MappingJacksonHttpMessageConverter (requires Jackson on the classpath - particularly useful for serving JavaScript clients that expect to work with JSON)

@RequestMapping(value="/json", method=RequestMethod.POST)
public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) {
return "Read from JSON: " + bean;
}

@RequestMapping(value="/json", method=RequestMethod.GET)
public @ResponseBody JavaBean writeJson() {
return new JavaBean("bar", "fruit");
return new JavaBean("bar", "apple");
}

// AtomFeedHttpMessageConverter (requires Rome on the classpath - useful for serving Atom feeds)

@RequestMapping(value="/atom", method=RequestMethod.POST)
public @ResponseBody String readFeed(@RequestBody Feed feed) {
return "Read " + feed.getTitle();
}

@RequestMapping(value="/atom", method=RequestMethod.GET)
public @ResponseBody Feed writeFeed() {
Feed feed = new Feed();
Expand All @@ -85,12 +85,12 @@ public class MessageConvertersController {
}

// RssChannelHttpMessageConverter (requires Rome on the classpath - useful for serving RSS feeds)

@RequestMapping(value="/rss", method=RequestMethod.POST)
public @ResponseBody String readChannel(@RequestBody Channel channel) {
return "Read " + channel.getTitle();
}

@RequestMapping(value="/rss", method=RequestMethod.GET)
public @ResponseBody Channel writeChannel() {
Channel channel = new Channel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class ResponseController {

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

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

@RequestMapping(value="/response/entity/status", method=RequestMethod.GET)
Expand Down
9 changes: 5 additions & 4 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

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

<servlet-mapping>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;

@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")
public class AbstractContextControllerTests {

@Autowired
protected WebApplicationContext wac;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.mvc.convert;

import static org.hamcrest.Matchers.startsWith;
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup;

import org.junit.Before;
import org.junit.Test;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.test.web.mock.servlet.MockMvc;

public class ConvertControllerTests {

private MockMvc mockMvc;

@Before
public void setup() throws Exception {
FormattingConversionService cs = new DefaultFormattingConversionService();
cs.addFormatterForFieldAnnotation(new MaskFormatAnnotationFormatterFactory());

this.mockMvc = standaloneSetup(new ConvertController())
.setConversionService(cs)
.alwaysExpect(status().isOk())
.build();
}

@Test
public void primitive() throws Exception {
this.mockMvc.perform(get("/convert/primitive").param("value", "3"))
.andExpect(content().string("Converted primitive 3"));
}

@Test
public void date() throws Exception {
this.mockMvc.perform(get("/convert/date/2010-07-04"))
.andExpect(content().string("Converted date Sun Jul 04 00:00:00 EDT 2010"));
}

@Test
public void collection() throws Exception {
this.mockMvc.perform(get("/convert/collection?values=1&values=2&values=3&values=4&values=5"))
.andExpect(content().string("Converted collection [1, 2, 3, 4, 5]"));
}

@Test
public void collection2() throws Exception {
this.mockMvc.perform(get("/convert/collection?values=1,2,3,4,5"))
.andExpect(content().string("Converted collection [1, 2, 3, 4, 5]"));
}

@Test
public void formattedCollection() throws Exception {
this.mockMvc.perform(get("/convert/formattedCollection?values=2010-07-04,2011-07-04"))
.andExpect(content().string("Converted formatted collection [Sun Jul 04 00:00:00 EDT 2010, Mon Jul 04 00:00:00 EDT 2011]"));
}

@Test
public void valueOf() throws Exception {
this.mockMvc.perform(get("/convert/value?value=123456789"))
.andExpect(content().string(startsWith(
"Converted value object org.springframework.samples.mvc.convert.SocialSecurityNumber")));
}

@Test
public void custom() throws Exception {
this.mockMvc.perform(get("/convert/custom?value=123-45-6789"))
.andExpect(content().string("Converted '123456789' with a custom converter"));
}

@Test
public void beanPrimitive() throws Exception {
this.mockMvc.perform(get("/convert/bean?primitive=3"))
.andExpect(content().string("Converted JavaBean primitive=3"));
}

@Test
public void beanDate() throws Exception {
this.mockMvc.perform(get("/convert/bean?date=2010-07-04"))
.andExpect(content().string("Converted JavaBean date=Sun Jul 04 00:00:00 EDT 2010"));
}

@Test
public void beanMasked() throws Exception {
this.mockMvc.perform(get("/convert/bean?masked=(205) 333-3333"))
.andExpect(content().string("Converted JavaBean masked=2053333333"));
}

@Test
public void beanCollection() throws Exception {
this.mockMvc.perform(get("/convert/bean?list[0]=1&list[1]=2&list[2]=3"))
.andExpect(content().string("Converted JavaBean list=[1, 2, 3]"));
}

@Test
public void beanFormattedCollection() throws Exception {
this.mockMvc.perform(get("/convert/bean?formattedList[0]=2010-07-04&formattedList[1]=2011-07-04"))
.andExpect(content().string(
"Converted JavaBean formattedList=[Sun Jul 04 00:00:00 EDT 2010, Mon Jul 04 00:00:00 EDT 2011]"));
}

@Test
public void beanMap() throws Exception {
this.mockMvc.perform(get("/convert/bean?map[0]=apple&map[1]=pear"))
.andExpect(content().string("Converted JavaBean map={0=apple, 1=pear}"));
}

@Test
public void beanNested() throws Exception {
this.mockMvc.perform(get("/convert/bean?nested.foo=bar&nested.list[0].foo=baz&nested.map[key].list[0].foo=bip"))
.andExpect(content().string(
"Converted JavaBean nested=NestedBean foo=bar list=[NestedBean foo=baz] map={key=NestedBean list=[NestedBean foo=bip]}"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.mvc.data;

import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup;

import org.junit.Before;
import org.junit.Test;
import org.springframework.samples.mvc.data.custom.CustomArgumentController;
import org.springframework.samples.mvc.data.custom.CustomArgumentResolver;
import org.springframework.test.web.mock.servlet.MockMvc;

public class CustomArgumentControllerTests {
private MockMvc mockMvc;

@Before
public void setup() throws Exception {
this.mockMvc = standaloneSetup(new CustomArgumentController())
.setCustomArgumentResolvers(new CustomArgumentResolver()).build();
}

@Test
public void param() throws Exception {
this.mockMvc.perform(get("/data/custom"))
.andExpect(content().string("Got 'foo' request attribute value 'bar'"));
}

}
Loading

0 comments on commit b03226a

Please sign in to comment.