From d01bc41e1e9acb0b76795ad6f7f4c4e160a096e2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 28 Sep 2015 19:34:09 -0700 Subject: [PATCH] Detect XML config files and Groovy test scripts Update SpringApplicationContextLoader to detect xml and groovy configuration based on convention. Fixes gh-2516 --- .../test/SpringApplicationContextLoader.java | 8 +++- ...ionGroovyConventionConfigurationTests.java | 44 +++++++++++++++++++ ...rationXmlConventionConfigurationTests.java | 44 +++++++++++++++++++ ...ConventionConfigurationTestsContext.groovy | 3 ++ ...mlConventionConfigurationTests-context.xml | 13 ++++++ 5 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTests.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests.java create mode 100644 spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTestsContext.groovy create mode 100644 spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests-context.xml diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index 94d226e4cf..a360291dd8 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -211,6 +211,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { @Override public void processContextConfiguration( ContextConfigurationAttributes configAttributes) { + super.processContextConfiguration(configAttributes); if (!configAttributes.hasLocations() && !configAttributes.hasClasses()) { Class[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes .getDeclaringClass()); @@ -238,9 +239,14 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { + "does not support the loadContext(String...) method"); } + @Override + protected String[] getResourceSuffixes() { + return new String[] { "-context.xml", "Context.groovy" }; + } + @Override protected String getResourceSuffix() { - return "-context.xml"; + throw new IllegalStateException(); } /** diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTests.java new file mode 100644 index 0000000000..85780edd8b --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2015 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.boot.test; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link SpringApplicationContextLoader} finding groovy config. + * + * @author Phillip Webb + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration +public class SpringApplicationConfigurationGroovyConventionConfigurationTests { + + @Autowired + private String foo; + + @Test + public void groovyConfigLoaded() { + assertThat(this.foo, equalTo("World")); + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests.java new file mode 100644 index 0000000000..d81fd3b708 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2015 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.boot.test; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link SpringApplicationContextLoader} finding XML config. + * + * @author Phillip Webb + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration +public class SpringApplicationConfigurationXmlConventionConfigurationTests { + + @Autowired + private String foo; + + @Test + public void groovyConfigLoaded() { + assertThat(this.foo, equalTo("World")); + } + +} diff --git a/spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTestsContext.groovy b/spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTestsContext.groovy new file mode 100644 index 0000000000..a0c755956c --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationGroovyConventionConfigurationTestsContext.groovy @@ -0,0 +1,3 @@ +beans { + foo String, "World" +} diff --git a/spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests-context.xml b/spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests-context.xml new file mode 100644 index 0000000000..a25ca5b996 --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/test/SpringApplicationConfigurationXmlConventionConfigurationTests-context.xml @@ -0,0 +1,13 @@ + + + + + + World + + + +