From 7b4bc08b14ed7e98c275b939549444e892c49ed3 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 8 Oct 2012 12:41:09 -0400 Subject: [PATCH] Update context-based tests in spring-test-mvc Added WebAppResourceTests Removed unused config file TestContextTests-context.xml Moved servlet-context.xml in package that matches the test classes --- .../samples/context/JavaConfigTests.java | 109 ++++++++++++++++++ .../samples/context/SpringSecurityTests.java | 2 +- ...extTests.java => WebAppResourceTests.java} | 46 ++++++-- .../servlet/samples/context/WebConfig.java | 62 ---------- ...tContextTests.java => XmlConfigTests.java} | 4 +- .../context/TestContextTests-context.xml | 14 --- .../samples/{ => context}/servlet-context.xml | 0 7 files changed, 146 insertions(+), 91 deletions(-) create mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaConfigTests.java rename spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/{JavaTestContextTests.java => WebAppResourceTests.java} (51%) delete mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebConfig.java rename spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/{XmlTestContextTests.java => XmlConfigTests.java} (95%) delete mode 100644 spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/context/TestContextTests-context.xml rename spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/{ => context}/servlet-context.xml (100%) diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaConfigTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaConfigTests.java new file mode 100644 index 0000000000..363d199910 --- /dev/null +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaConfigTests.java @@ -0,0 +1,109 @@ +/* + * 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.test.web.mock.servlet.samples.context; + +import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.mock.servlet.MockMvc; +import org.springframework.test.web.mock.servlet.samples.context.JavaConfigTests.WebConfig; +import org.springframework.test.web.mock.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +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; + +/** + * Tests with Java configuration. + * + * @author Rossen Stoyanchev + * @author Sam Brannen + */ +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration("src/test/resources/META-INF/web-resources") +@ContextConfiguration(classes = WebConfig.class) +public class JavaConfigTests { + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void tilesDefinitions() throws Exception { + this.mockMvc.perform(get("/"))// + .andExpect(status().isOk())// + .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); + } + + + @Configuration + @EnableWebMvc + static class WebConfig extends WebMvcConfigurerAdapter { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("home"); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + @Bean + public UrlBasedViewResolver urlBasedViewResolver() { + UrlBasedViewResolver resolver = new UrlBasedViewResolver(); + resolver.setViewClass(TilesView.class); + return resolver; + } + + @Bean + public TilesConfigurer tilesConfigurer() { + TilesConfigurer configurer = new TilesConfigurer(); + configurer.setDefinitions(new String[] {"/WEB-INF/**/tiles.xml"}); + return configurer; + } + } + +} diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/SpringSecurityTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/SpringSecurityTests.java index b1da11b99f..ead229289c 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/SpringSecurityTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/SpringSecurityTests.java @@ -61,7 +61,7 @@ import org.springframework.web.context.WebApplicationContext; */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration("src/test/resources/META-INF/web-resources") -@ContextConfiguration({ "security.xml", "../servlet-context.xml" }) +@ContextConfiguration({ "security.xml", "servlet-context.xml" }) public class SpringSecurityTests { private static final String SEC_CONTEXT_ATTR = HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY; diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaTestContextTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebAppResourceTests.java similarity index 51% rename from spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaTestContextTests.java rename to spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebAppResourceTests.java index dbb5ff73ae..f05ed9d668 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/JavaTestContextTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebAppResourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2011 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. @@ -16,8 +16,12 @@ package org.springframework.test.web.mock.servlet.samples.context; -import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.*; +import static org.hamcrest.Matchers.containsString; +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.forwardedUrl; +import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.handler; +import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status; import org.junit.Before; import org.junit.Test; @@ -29,34 +33,52 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.mock.servlet.MockMvc; import org.springframework.test.web.mock.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler; /** - * Tests with Java configuration. + * Tests dependent on access to resources under the web application root directory. * * @author Rossen Stoyanchev - * @author Sam Brannen */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration("src/test/resources/META-INF/web-resources") -@ContextConfiguration(classes = WebConfig.class) -public class JavaTestContextTests { +@ContextConfiguration("servlet-context.xml") +public class WebAppResourceTests { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; - @Before public void setup() { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build(); } + // TilesConfigurer: resources under "/WEB-INF/**/tiles.xml" + @Test public void tilesDefinitions() throws Exception { - this.mockMvc.perform(get("/"))// - .andExpect(status().isOk())// - .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); + this.mockMvc.perform(get("/")) + .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); + } + + // Resources served via + + @Test + public void resourceRequest() throws Exception { + this.mockMvc.perform(get("/resources/Spring.js")) + .andExpect(content().mimeType("text/javascript")) + .andExpect(content().string(containsString("Spring={};"))); + } + + // Forwarded to the "default" servlet via + + @Test + public void resourcesViaDefaultServlet() throws Exception { + this.mockMvc.perform(get("/unknown/resource")) + .andExpect(handler().handlerType(DefaultServletHttpRequestHandler.class)) + .andExpect(forwardedUrl("default")); } } diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebConfig.java b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebConfig.java deleted file mode 100644 index 133e69fea6..0000000000 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/WebConfig.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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.test.web.mock.servlet.samples.context; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -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; - -@Configuration -@EnableWebMvc -class WebConfig extends WebMvcConfigurerAdapter { - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); - } - - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addViewController("/").setViewName("home"); - } - - @Override - public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { - configurer.enable(); - } - - @Bean - public UrlBasedViewResolver urlBasedViewResolver() { - UrlBasedViewResolver resolver = new UrlBasedViewResolver(); - resolver.setViewClass(TilesView.class); - return resolver; - } - - @Bean - public TilesConfigurer tilesConfigurer() { - TilesConfigurer configurer = new TilesConfigurer(); - configurer.setDefinitions(new String[] {"/WEB-INF/**/tiles.xml"}); - return configurer; - } -} \ No newline at end of file diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/XmlTestContextTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/XmlConfigTests.java similarity index 95% rename from spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/XmlTestContextTests.java rename to spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/XmlConfigTests.java index 3274c6137d..24563c0d51 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/XmlTestContextTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/mock/servlet/samples/context/XmlConfigTests.java @@ -38,8 +38,8 @@ import org.springframework.web.context.WebApplicationContext; */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration("src/test/resources/META-INF/web-resources") -@ContextConfiguration("../servlet-context.xml") -public class XmlTestContextTests { +@ContextConfiguration("servlet-context.xml") +public class XmlConfigTests { @Autowired private WebApplicationContext wac; diff --git a/spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/context/TestContextTests-context.xml b/spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/context/TestContextTests-context.xml deleted file mode 100644 index 069aff1387..0000000000 --- a/spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/context/TestContextTests-context.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/servlet-context.xml b/spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/context/servlet-context.xml similarity index 100% rename from spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/servlet-context.xml rename to spring-test-mvc/src/test/resources/org/springframework/test/web/mock/servlet/samples/context/servlet-context.xml