Support classes AND locations in @ContextConfiguration

Prior to this commit, the Spring TestContext Framework did not support
the declaration of both 'locations' and 'classes' within
@ContextConfiguration at the same time.

This commit addresses this in the following manner:

 - ContextConfigurationAttributes no longer throws an
   IllegalArgumentException if both 'locations' and 'classes' are
   supplied to its constructor.

 - Concrete SmartContextLoader implementations now validate the
   supplied MergedContextConfiguration before attempting to load the
   ApplicationContext. See validateMergedContextConfiguration().

 - Introduced tests for hybrid context loaders like the one used in
   Spring Boot. See HybridContextLoaderTests.

 - Updated the Testing chapter of the reference manual so that it no
   longer states that locations and classes cannot be used
   simultaneously, mentioning Spring Boot as well.

 - The Javadoc for @ContextConfiguration has been updated accordingly.

 - Added hasLocations(), hasClasses(), and hasResources() convenience
   methods to MergedContextConfiguration.

Issue: SPR-11634
This commit is contained in:
Sam Brannen
2014-04-01 18:07:52 +02:00
parent 8edbdf4ddb
commit 1f017c4acb
22 changed files with 650 additions and 61 deletions

View File

@@ -143,9 +143,26 @@ public class ContextLoaderUtilsConfigurationAttributesTests extends AbstractCont
assertClassesFooAttributes(attributesList.get(1));
}
/**
* Verifies change requested in <a href="https://jira.spring.io/browse/SPR-11634">SPR-11634</a>.
* @since 4.0.4
*/
@Test
public void resolveConfigAttributesWithLocationsAndClasses() {
List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsAndClasses.class);
assertNotNull(attributesList);
assertEquals(1, attributesList.size());
}
// -------------------------------------------------------------------------
@ContextConfiguration(value = "x", locations = "y")
private static class ConflictingLocations {
}
@ContextConfiguration(locations = "x", classes = Object.class)
private static class LocationsAndClasses {
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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.junit4.hybrid;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.test.context.support.AbstractGenericContextLoader;
import org.springframework.util.Assert;
import static org.springframework.test.context.support.AnnotationConfigContextLoaderUtils.*;
/**
* Hybrid {@link SmartContextLoader} that supports path-based and class-based
* resources simultaneously.
* <p>This test loader is inspired by Spring Boot.
* <p>Detects defaults for XML configuration and annotated classes.
* <p>Beans from XML configuration always override those from annotated classes.
*
* @author Sam Brannen
* @since 4.0.4
*/
public class HybridContextLoader extends AbstractGenericContextLoader {
@Override
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
Assert.isTrue(mergedConfig.hasClasses() || mergedConfig.hasLocations(), getClass().getSimpleName()
+ " requires either classes or locations");
}
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
// Detect default XML configuration files:
super.processContextConfiguration(configAttributes);
// Detect default configuration classes:
if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
}
}
@Override
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
// Order doesn't matter: <bean> always wins over @Bean.
new XmlBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
new AnnotatedBeanDefinitionReader(context).register(mergedConfig.getClasses());
}
@Override
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
throw new UnsupportedOperationException(getClass().getSimpleName() + " doesn't support this");
}
@Override
protected String getResourceSuffix() {
return "-context.xml";
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="fooFromXml" class="java.lang.String" c:_="XML" />
<bean id="enigma" class="java.lang.String" c:_="enigma from XML" />
</beans>

View File

@@ -0,0 +1,80 @@
/*
* 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.junit4.hybrid;
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.SmartContextLoader;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* Integration tests for hybrid {@link SmartContextLoader} implementations that
* support path-based and class-based resources simultaneously, as is done in
* Spring Boot.
*
* @author Sam Brannen
* @since 4.0.4
* @see HybridContextLoader
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = HybridContextLoader.class)
public class HybridContextLoaderTests {
@Configuration
static class Config {
@Bean
public String fooFromJava() {
return "Java";
}
@Bean
public String enigma() {
return "enigma from Java";
}
}
@Autowired
private String fooFromXml;
@Autowired
private String fooFromJava;
@Autowired
private String enigma;
@Test
public void verifyContentsOfHybridApplicationContext() {
assertEquals("XML", fooFromXml);
assertEquals("Java", fooFromJava);
// Note: the XML bean definition for "enigma" always wins since
// ConfigurationClassBeanDefinitionReader.isOverriddenByExistingDefinition()
// lets XML bean definitions override those "discovered" later via an
// @Bean method.
assertEquals("enigma from XML", enigma);
}
}

View File

@@ -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,10 +16,14 @@
package org.springframework.test.context.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.MergedContextConfiguration;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link AnnotationConfigContextLoader}.
@@ -31,6 +35,25 @@ public class AnnotationConfigContextLoaderTests {
private final AnnotationConfigContextLoader contextLoader = new AnnotationConfigContextLoader();
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
/**
* @since 4.0.4
*/
@Test
public void configMustNotContainLocations() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support resource locations"));
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, contextLoader);
contextLoader.loadContext(mergedConfig);
}
@Test
public void detectDefaultConfigurationClassesForAnnotatedInnerClass() {

View File

@@ -0,0 +1,51 @@
/*
* 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.support;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.MergedContextConfiguration;
import static org.hamcrest.CoreMatchers.*;
/**
* Unit tests for {@link GenericPropertiesContextLoader}.
*
* @author Sam Brannen
* @since 4.0.4
*/
public class GenericPropertiesContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support annotated classes"));
GenericPropertiesContextLoader loader = new GenericPropertiesContextLoader();
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
}
}

View File

@@ -0,0 +1,52 @@
/*
* 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.support;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.MergedContextConfiguration;
import static org.hamcrest.CoreMatchers.*;
/**
* Unit tests for {@link GenericXmlContextLoader}.
*
* @author Sam Brannen
* @since 4.0.4
* @see GenericXmlContextLoaderResourceLocationsTests
*/
public class GenericXmlContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support annotated classes"));
GenericXmlContextLoader loader = new GenericXmlContextLoader();
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
}
}

View File

@@ -0,0 +1,52 @@
/*
* 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.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.*;
/**
* Unit tests for {@link AnnotationConfigWebContextLoader}.
*
* @author Sam Brannen
* @since 4.0.4
*/
public class AnnotationConfigWebContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainLocations() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support resource locations"));
AnnotationConfigWebContextLoader loader = new AnnotationConfigWebContextLoader();
WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(),
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, "resource/path", loader, null,
null);
loader.loadContext(mergedConfig);
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.*;
/**
* Unit tests for {@link GenericXmlWebContextLoader}.
*
* @author Sam Brannen
* @since 4.0.4
*/
public class GenericXmlWebContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support annotated classes"));
GenericXmlWebContextLoader loader = new GenericXmlWebContextLoader();
WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, null, EMPTY_STRING_ARRAY, "resource/path", loader, null, null);
loader.loadContext(mergedConfig);
}
}