Skip to content

Commit f30d33d

Browse files
committed
Add spring-test-mvc tests with Spring HATEOAS links
Issue: SPR-9886
1 parent 6e3c3c5 commit f30d33d

File tree

5 files changed

+87
-18
lines changed

5 files changed

+87
-18
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,6 @@ project('spring-test-mvc') {
595595
exclude group: 'com.sun.jmx', module: 'jmxri'
596596
}
597597
testCompile "javax.servlet:jstl:1.2"
598-
testCompile "org.apache.tiles:tiles-jsp:2.2.2"
599598
testCompile "org.hibernate:hibernate-validator:4.2.0.Final"
600599
testCompile "org.codehaus.jackson:jackson-mapper-asl:1.4.2"
601600
testCompile project(":spring-oxm")
@@ -613,6 +612,7 @@ project('spring-test-mvc') {
613612
testCompile("org.springframework.security:spring-security-config:3.1.2.RELEASE") {
614613
exclude group: 'org.springframework'
615614
}
615+
testCompile("org.springframework.hateoas:spring-hateoas:0.3.0.RELEASE")
616616
}
617617
}
618618

spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.test.util;
1818

1919
import static org.springframework.test.util.AssertionErrors.assertEquals;
20-
import static org.springframework.test.util.AssertionErrors.assertTrue;
20+
import static org.springframework.test.util.AssertionErrors.*;
2121
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
2222

2323
import java.text.ParseException;
@@ -85,6 +85,17 @@ private Object evaluateJsonPath(String content) throws ParseException {
8585
*/
8686
public void assertValue(String responseContent, Object expectedValue) throws ParseException {
8787
Object actualValue = evaluateJsonPath(responseContent);
88+
if ((actualValue instanceof List) && !(expectedValue instanceof List)) {
89+
@SuppressWarnings("rawtypes")
90+
List actualValueList = (List) actualValue;
91+
if (actualValueList.size() == 0) {
92+
fail("No matching value for JSON path \"" + this.expression + "\"");
93+
}
94+
if (actualValueList.size() != 1) {
95+
fail("Got a list of values " + actualValue + " instead of the value " + expectedValue);
96+
}
97+
actualValue = actualValueList.get(0);
98+
}
8899
assertEquals("JSON path" + this.expression, expectedValue, actualValue);
89100
}
90101

@@ -93,7 +104,7 @@ public void assertValue(String responseContent, Object expectedValue) throws Par
93104
*/
94105
public void assertValueIsArray(String responseContent) throws ParseException {
95106
Object actualValue = evaluateJsonPath(responseContent);
96-
assertTrue("No value for JSON path " + this.expression, actualValue != null);
107+
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
97108
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
98109
assertTrue(reason, actualValue instanceof List);
99110
}

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
4040
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
4141
import org.springframework.web.servlet.view.UrlBasedViewResolver;
42-
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
43-
import org.springframework.web.servlet.view.tiles2.TilesView;
42+
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
43+
import org.springframework.web.servlet.view.tiles3.TilesView;
4444

4545
/**
4646
* Tests with Java configuration.

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@
1818

1919
import static org.hamcrest.Matchers.containsString;
2020
import static org.hamcrest.Matchers.equalTo;
21-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
22-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
2327
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
2428

29+
import java.nio.charset.Charset;
30+
import java.util.Collections;
31+
import java.util.Map;
32+
33+
import javax.xml.bind.annotation.XmlRootElement;
34+
2535
import org.junit.Before;
2636
import org.junit.Test;
37+
import org.springframework.hateoas.Link;
38+
import org.springframework.hateoas.ResourceSupport;
2739
import org.springframework.http.MediaType;
2840
import org.springframework.stereotype.Controller;
2941
import org.springframework.test.web.servlet.MockMvc;
@@ -42,6 +54,8 @@
4254
*/
4355
public class ContentAssertionTests {
4456

57+
public static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", Charset.forName("UTF-8"));
58+
4559
private MockMvc mockMvc;
4660

4761
@Before
@@ -51,7 +65,7 @@ public void setup() {
5165

5266
@Test
5367
public void testContentType() throws Exception {
54-
this.mockMvc.perform(get("/handle"))
68+
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
5569
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
5670
.andExpect(content().contentType("text/plain"));
5771

@@ -62,29 +76,58 @@ public void testContentType() throws Exception {
6276

6377
@Test
6478
public void testContentAsString() throws Exception {
65-
this.mockMvc.perform(get("/handle")).andExpect(content().string("Hello world!"));
66-
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
79+
80+
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
81+
.andExpect(content().string("Hello world!"));
82+
83+
this.mockMvc.perform(get("/handleUtf8"))
84+
.andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
6785

6886
// Hamcrest matchers...
69-
this.mockMvc.perform(get("/handle")).andExpect(content().string(equalTo("Hello world!")));
87+
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)).andExpect(content().string(equalTo("Hello world!")));
7088
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")));
7189
}
7290

7391
@Test
7492
public void testContentAsBytes() throws Exception {
75-
this.mockMvc.perform(get("/handle")).andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
76-
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
93+
94+
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
95+
.andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
96+
97+
this.mockMvc.perform(get("/handleUtf8"))
98+
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
7799
}
78100

79101
@Test
80102
public void testContentStringMatcher() throws Exception {
81-
this.mockMvc.perform(get("/handle")).andExpect(content().string(containsString("world")));
103+
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
104+
.andExpect(content().string(containsString("world")));
82105
}
83106

84107
@Test
85108
public void testCharacterEncoding() throws Exception {
86-
this.mockMvc.perform(get("/handle")).andExpect(content().encoding("ISO-8859-1"));
87-
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().encoding("UTF-8"));
109+
110+
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
111+
.andExpect(content().encoding("ISO-8859-1"))
112+
.andExpect(content().string(containsString("world")));
113+
114+
this.mockMvc.perform(get("/handleUtf8"))
115+
.andExpect(content().encoding("UTF-8"))
116+
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
117+
}
118+
119+
@Test
120+
public void testSpringHateoasJsonLink() throws Exception {
121+
this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_JSON))
122+
.andExpect(jsonPath("$.links[?(@.rel == 'self')].href").value("http://myhost/people"));
123+
}
124+
125+
@Test
126+
public void testSpringHateoasXmlLink() throws Exception {
127+
Map<String, String> ns = Collections.singletonMap("ns", "http://www.w3.org/2005/Atom");
128+
this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_XML))
129+
.andDo(print())
130+
.andExpect(xpath("/person/ns:link[@rel='self']/@href", ns).string("http://myhost/people"));
88131
}
89132

90133

@@ -102,5 +145,20 @@ public String handle() {
102145
public String handleWithCharset() {
103146
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"; // "Hello world! (Japanese)
104147
}
148+
149+
@RequestMapping(value="/handle", produces={"application/json", "application/xml"})
150+
@ResponseBody
151+
public PersonResource handleJsonOrXml() {
152+
PersonResource resource = new PersonResource();
153+
resource.name = "Joe";
154+
resource.add(new Link("http://myhost/people"));
155+
return resource;
156+
}
105157
}
158+
159+
@XmlRootElement(name="person")
160+
static class PersonResource extends ResourceSupport {
161+
String name;
162+
}
163+
106164
}

spring-test-mvc/src/test/resources/org/springframework/test/web/servlet/samples/context/servlet-context.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
<bean id="viewResolver"
1717
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
1818
<property name="viewClass"
19-
value="org.springframework.web.servlet.view.tiles2.TilesView" />
19+
value="org.springframework.web.servlet.view.tiles3.TilesView" />
2020
</bean>
2121

2222
<bean id="tilesConfigurer"
23-
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
23+
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
2424
<property name="definitions">
2525
<value>
2626
/WEB-INF/**/tiles.xml

0 commit comments

Comments
 (0)