View Javadoc
1   /*
2    * Copyright 2002-2014 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  
17  package org.springframework.test.web.servlet.samples.standalone;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.junit.Test;
23  
24  import org.springframework.http.MediaType;
25  import org.springframework.oxm.jaxb.Jaxb2Marshaller;
26  import org.springframework.stereotype.Controller;
27  import org.springframework.test.web.Person;
28  import org.springframework.test.web.servlet.MockMvc;
29  import org.springframework.ui.Model;
30  import org.springframework.web.accept.ContentNegotiationManager;
31  import org.springframework.web.accept.FixedContentNegotiationStrategy;
32  import org.springframework.web.accept.HeaderContentNegotiationStrategy;
33  import org.springframework.web.bind.annotation.PathVariable;
34  import org.springframework.web.bind.annotation.RequestMapping;
35  import org.springframework.web.bind.annotation.RequestMethod;
36  import org.springframework.web.servlet.View;
37  import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
38  import org.springframework.web.servlet.view.InternalResourceViewResolver;
39  import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
40  import org.springframework.web.servlet.view.xml.MarshallingView;
41  
42  import static org.hamcrest.Matchers.*;
43  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
44  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
45  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
46  
47  /**
48   * Tests with view resolution.
49   *
50   * @author Rossen Stoyanchev
51   */
52  public class ViewResolutionTests {
53  
54  	@Test
55  	public void testJspOnly() throws Exception {
56  
57  		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
58  		viewResolver.setPrefix("/WEB-INF/");
59  		viewResolver.setSuffix(".jsp");
60  
61  		standaloneSetup(new PersonController()).setViewResolvers(viewResolver).build()
62  			.perform(get("/person/Corea"))
63  				.andExpect(status().isOk())
64  				.andExpect(model().size(1))
65  				.andExpect(model().attributeExists("person"))
66  				.andExpect(forwardedUrl("/WEB-INF/person/show.jsp"));
67  	}
68  
69  	@Test
70  	public void testJsonOnly() throws Exception {
71  
72  		standaloneSetup(new PersonController()).setSingleView(new MappingJackson2JsonView()).build()
73  			.perform(get("/person/Corea"))
74  				.andExpect(status().isOk())
75  				.andExpect(content().contentType(MediaType.APPLICATION_JSON))
76  				.andExpect(jsonPath("$.person.name").value("Corea"));
77  	}
78  
79  	@Test
80  	public void testXmlOnly() throws Exception {
81  
82  		Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
83  		marshaller.setClassesToBeBound(Person.class);
84  
85  		standaloneSetup(new PersonController()).setSingleView(new MarshallingView(marshaller)).build()
86  			.perform(get("/person/Corea"))
87  				.andExpect(status().isOk())
88  				.andExpect(content().contentType(MediaType.APPLICATION_XML))
89  				.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
90  	}
91  
92  	@Test
93  	public void testContentNegotiation() throws Exception {
94  
95  		Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
96  		marshaller.setClassesToBeBound(Person.class);
97  
98  		List<View> viewList = new ArrayList<View>();
99  		viewList.add(new MappingJackson2JsonView());
100 		viewList.add(new MarshallingView(marshaller));
101 
102 		ContentNegotiationManager manager = new ContentNegotiationManager(
103 				new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
104 
105 		ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
106 		cnViewResolver.setDefaultViews(viewList);
107 		cnViewResolver.setContentNegotiationManager(manager);
108 		cnViewResolver.afterPropertiesSet();
109 
110 		MockMvc mockMvc =
111 			standaloneSetup(new PersonController())
112 				.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
113 				.build();
114 
115 		mockMvc.perform(get("/person/Corea"))
116 			.andExpect(status().isOk())
117 			.andExpect(model().size(1))
118 			.andExpect(model().attributeExists("person"))
119 			.andExpect(forwardedUrl("person/show"));
120 
121 		mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
122 			.andExpect(status().isOk())
123 			.andExpect(content().contentType(MediaType.APPLICATION_JSON))
124 			.andExpect(jsonPath("$.person.name").value("Corea"));
125 
126 		mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
127 			.andExpect(status().isOk())
128 			.andExpect(content().contentType(MediaType.APPLICATION_XML))
129 			.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
130 	}
131 
132 	@Test
133 	public void defaultViewResolver() throws Exception {
134 
135 		standaloneSetup(new PersonController()).build()
136 			.perform(get("/person/Corea"))
137 				.andExpect(model().attribute("person", hasProperty("name", equalTo("Corea"))))
138 				.andExpect(status().isOk())
139 				.andExpect(forwardedUrl("person/show"));  // InternalResourceViewResolver
140 	}
141 
142 
143 	@Controller
144 	private static class PersonController {
145 
146 		@RequestMapping(value="/person/{name}", method=RequestMethod.GET)
147 		public String show(@PathVariable String name, Model model) {
148 			Person person = new Person(name);
149 			model.addAttribute(person);
150 			return "person/show";
151 		}
152 	}
153 
154 }
155