Add spring-test-mvc tests with Spring HATEOAS links

Issue: SPR-9886
This commit is contained in:
Rossen Stoyanchev
2012-11-26 12:59:03 -05:00
parent 6e3c3c58b5
commit f30d33d32e
5 changed files with 87 additions and 18 deletions

View File

@@ -39,8 +39,8 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
/**
* Tests with Java configuration.

View File

@@ -18,12 +18,24 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;
import javax.xml.bind.annotation.XmlRootElement;
import org.junit.Before;
import org.junit.Test;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
@@ -42,6 +54,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
*/
public class ContentAssertionTests {
public static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", Charset.forName("UTF-8"));
private MockMvc mockMvc;
@Before
@@ -51,7 +65,7 @@ public class ContentAssertionTests {
@Test
public void testContentType() throws Exception {
this.mockMvc.perform(get("/handle"))
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(content().contentType("text/plain"));
@@ -62,29 +76,58 @@ public class ContentAssertionTests {
@Test
public void testContentAsString() throws Exception {
this.mockMvc.perform(get("/handle")).andExpect(content().string("Hello world!"));
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
.andExpect(content().string("Hello world!"));
this.mockMvc.perform(get("/handleUtf8"))
.andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
// Hamcrest matchers...
this.mockMvc.perform(get("/handle")).andExpect(content().string(equalTo("Hello world!")));
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)).andExpect(content().string(equalTo("Hello world!")));
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")));
}
@Test
public void testContentAsBytes() throws Exception {
this.mockMvc.perform(get("/handle")).andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
.andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
this.mockMvc.perform(get("/handleUtf8"))
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
}
@Test
public void testContentStringMatcher() throws Exception {
this.mockMvc.perform(get("/handle")).andExpect(content().string(containsString("world")));
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
.andExpect(content().string(containsString("world")));
}
@Test
public void testCharacterEncoding() throws Exception {
this.mockMvc.perform(get("/handle")).andExpect(content().encoding("ISO-8859-1"));
this.mockMvc.perform(get("/handleUtf8")).andExpect(content().encoding("UTF-8"));
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
.andExpect(content().encoding("ISO-8859-1"))
.andExpect(content().string(containsString("world")));
this.mockMvc.perform(get("/handleUtf8"))
.andExpect(content().encoding("UTF-8"))
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
}
@Test
public void testSpringHateoasJsonLink() throws Exception {
this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.links[?(@.rel == 'self')].href").value("http://myhost/people"));
}
@Test
public void testSpringHateoasXmlLink() throws Exception {
Map<String, String> ns = Collections.singletonMap("ns", "http://www.w3.org/2005/Atom");
this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(xpath("/person/ns:link[@rel='self']/@href", ns).string("http://myhost/people"));
}
@@ -102,5 +145,20 @@ public class ContentAssertionTests {
public String handleWithCharset() {
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"; // "Hello world! (Japanese)
}
@RequestMapping(value="/handle", produces={"application/json", "application/xml"})
@ResponseBody
public PersonResource handleJsonOrXml() {
PersonResource resource = new PersonResource();
resource.name = "Joe";
resource.add(new Link("http://myhost/people"));
return resource;
}
}
@XmlRootElement(name="person")
static class PersonResource extends ResourceSupport {
String name;
}
}

View File

@@ -16,11 +16,11 @@
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<value>
/WEB-INF/**/tiles.xml