From a521ef5ceeef5f4eae76b2667fede1aa4464a392 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 1 Feb 2014 02:08:21 +0100 Subject: [PATCH] Verify ServletCtxAware beans are processed in WAC tests SPR-11145 claims that ServletContextAware beans declared in an ApplicationContext loaded for an integration test by the TestContext framework (TCF) do not have their setServletContext() methods invoked if the tests are executed manually using JUnit 4.11. This commit verifies that such ServletContextAware beans are processed properly regardless of how the test was launched. Specifically: - A ServletContextAwareBean has been introduced. - BasicAnnotationConfigWacTests has been retrofitted with a ServletContextAwareBean in its context. - ServletContextAwareBeanWacTests has been introduced to execute BasicAnnotationConfigWacTests manually via JUnitCore. Issue: SPR-11145 --- .../web/BasicAnnotationConfigWacTests.java | 20 ++++++-- .../context/web/ServletContextAwareBean.java | 38 ++++++++++++++ .../web/ServletContextAwareBeanWacTests.java | 49 +++++++++++++++++++ 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBean.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBeanWacTests.java diff --git a/spring-test/src/test/java/org/springframework/test/context/web/BasicAnnotationConfigWacTests.java b/spring-test/src/test/java/org/springframework/test/context/web/BasicAnnotationConfigWacTests.java index de4ef83cfd5..53684d54d51 100644 --- a/spring-test/src/test/java/org/springframework/test/context/web/BasicAnnotationConfigWacTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/web/BasicAnnotationConfigWacTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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,13 +16,14 @@ package org.springframework.test.context.web; -import static org.junit.Assert.*; - import org.junit.Test; +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 static org.junit.Assert.*; + /** * @author Sam Brannen * @since 3.2 @@ -37,12 +38,25 @@ public class BasicAnnotationConfigWacTests extends AbstractBasicWacTests { public String foo() { return "enigma"; } + + @Bean + public ServletContextAwareBean servletContextAwareBean() { + return new ServletContextAwareBean(); + } } + @Autowired + protected ServletContextAwareBean servletContextAwareBean; @Test public void fooEnigmaAutowired() { assertEquals("enigma", foo); } + @Test + public void servletContextAwareBeanProcessed() { + assertNotNull(servletContextAwareBean); + assertNotNull(servletContextAwareBean.servletContext); + } + } diff --git a/spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBean.java b/spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBean.java new file mode 100644 index 00000000000..9b0be486b3c --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBean.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2014 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.context.web; + +import javax.servlet.ServletContext; + +import org.springframework.web.context.ServletContextAware; + +/** + * Introduced to investigate claims in SPR-11145. + * + * @author Sam Brannen + * @since 4.0.2 + */ +public class ServletContextAwareBean implements ServletContextAware { + + protected ServletContext servletContext; + + @Override + public void setServletContext(ServletContext servletContext) { + this.servletContext = servletContext; + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBeanWacTests.java b/spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBeanWacTests.java new file mode 100644 index 00000000000..1c9275d9fab --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBeanWacTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2014 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.context.web; + +import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.springframework.test.context.junit4.TrackingRunListener; + +import static org.junit.Assert.*; + +/** + * Introduced to investigate claims in SPR-11145. + * + *

+ * Yes, this test class does in fact use JUnit to run JUnit. ;) + * + * @author Sam Brannen + * @since 4.0.2 + */ +public class ServletContextAwareBeanWacTests { + + @Test + public void ensureServletContextAwareBeanIsProcessedProperlyWhenExecutingJUnitManually() { + TrackingRunListener listener = new TrackingRunListener(); + JUnitCore junit = new JUnitCore(); + junit.addListener(listener); + + junit.run(BasicAnnotationConfigWacTests.class); + + assertEquals(3, listener.getTestStartedCount()); + assertEquals(3, listener.getTestFinishedCount()); + assertEquals(0, listener.getTestFailureCount()); + } + +}