[SPR-8395][SPR-8386] AnnotationConfigContextLoader now generates a list of default configuration classes by finding all non-private, non-final, static, inner classes of the test class that are annotated with @Configuration; updated JavaDoc in AbstractGenericContextLoader and AnnotationConfigContextLoader to reflect changes resulting from the SmartContextLoader integration.

This commit is contained in:
Sam Brannen
2011-06-19 17:36:25 +00:00
parent c070a4b0a4
commit a77cf0f652
9 changed files with 410 additions and 148 deletions

View File

@@ -32,31 +32,50 @@ public class AnnotationConfigContextLoaderTests {
private final AnnotationConfigContextLoader contextLoader = new AnnotationConfigContextLoader();
// Developer's note: AnnotationConfigContextLoader currently generates a
// default config class named exactly ContextConfiguration, which is a
// static inner class of the test class itself.
@Test
public void generateDefaultConfigurationClassesForAnnotatedInnerClassNamedContextConfiguration() {
Class<?>[] defaultLocations = contextLoader.generateDefaultConfigurationClasses(ContextConfigurationInnerClassTestCase.class);
assertNotNull(defaultLocations);
assertEquals("ContextConfigurationInnerClassTestCase.ContextConfiguration should be found", 1,
defaultLocations.length);
public void generateDefaultConfigurationClassesForAnnotatedInnerClass() {
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(ContextConfigurationInnerClassTestCase.class);
assertNotNull(configClasses);
assertEquals("annotated static ContextConfiguration should be considered.", 1, configClasses.length);
configClasses = contextLoader.generateDefaultConfigurationClasses(AnnotatedFooConfigInnerClassTestCase.class);
assertNotNull(configClasses);
assertEquals("annotated static FooConfig should be considered.", 1, configClasses.length);
}
@Test
public void generateDefaultConfigurationClassesForAnnotatedInnerClass() {
Class<?>[] defaultLocations = contextLoader.generateDefaultConfigurationClasses(AnnotatedFooConfigInnerClassTestCase.class);
assertNotNull(defaultLocations);
assertEquals("AnnotatedFooConfigInnerClassTestCase.FooConfig should NOT be found", 0, defaultLocations.length);
public void generateDefaultConfigurationClassesForMultipleAnnotatedInnerClasses() {
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(MultipleStaticConfigurationClassesTestCase.class);
assertNotNull(configClasses);
assertEquals("multiple annotated static classes should be considered.", 2, configClasses.length);
}
@Test
public void generateDefaultConfigurationClassesForNonAnnotatedInnerClass() {
Class<?>[] defaultLocations = contextLoader.generateDefaultConfigurationClasses(PlainVanillaFooConfigInnerClassTestCase.class);
assertNotNull(defaultLocations);
assertEquals("PlainVanillaFooConfigInnerClassTestCase.FooConfig should NOT be found", 0,
defaultLocations.length);
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(PlainVanillaFooConfigInnerClassTestCase.class);
assertNotNull(configClasses);
assertEquals("non-annotated static FooConfig should NOT be considered.", 0, configClasses.length);
}
@Test
public void generateDefaultConfigurationClassesForFinalAnnotatedInnerClass() {
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(FinalConfigInnerClassTestCase.class);
assertNotNull(configClasses);
assertEquals("final annotated static Config should NOT be considered.", 0, configClasses.length);
}
@Test
public void generateDefaultConfigurationClassesForPrivateAnnotatedInnerClass() {
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(PrivateConfigInnerClassTestCase.class);
assertNotNull(configClasses);
assertEquals("private annotated inner classes should NOT be considered.", 0, configClasses.length);
}
@Test
public void generateDefaultConfigurationClassesForNonStaticAnnotatedInnerClass() {
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(NonStaticConfigInnerClassesTestCase.class);
assertNotNull(configClasses);
assertEquals("non-static annotated inner classes should NOT be considered.", 0, configClasses.length);
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.test.context.support;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
@@ -30,11 +29,6 @@ public class ContextConfigurationInnerClassTestCase {
@Configuration
static class ContextConfiguration {
@Bean
public String foo() {
return "foo";
}
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.
* 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.springframework.context.annotation.Configuration;
/**
* Not an actual <em>test case</em>.
*
* @author Sam Brannen
* @since 3.1
* @see AnnotationConfigContextLoaderTests
*/
public class FinalConfigInnerClassTestCase {
// Intentionally FINAL.
@Configuration
static final class Config {
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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.
* 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.springframework.context.annotation.Configuration;
/**
* Not an actual <em>test case</em>.
*
* @author Sam Brannen
* @since 3.1
* @see AnnotationConfigContextLoaderTests
*/
public class MultipleStaticConfigurationClassesTestCase {
@Configuration
static class ConfigA {
}
@Configuration
static class ConfigB {
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.
* 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.springframework.context.annotation.Configuration;
/**
* Not an actual <em>test case</em>.
*
* @author Sam Brannen
* @since 3.1
* @see AnnotationConfigContextLoaderTests
*/
public class NonStaticConfigInnerClassesTestCase {
// Intentionally not static
@Configuration
class FooConfig {
}
// Intentionally not static
@Configuration
class BarConfig {
}
}

View File

@@ -16,8 +16,6 @@
package org.springframework.test.context.support;
import org.springframework.context.annotation.Bean;
/**
* Not an actual <em>test case</em>.
*
@@ -29,11 +27,6 @@ public class PlainVanillaFooConfigInnerClassTestCase {
// Intentionally NOT annotated with @Configuration
static class FooConfig {
@Bean
public String foo() {
return "foo";
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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.
* 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.springframework.context.annotation.Configuration;
/**
* Not an actual <em>test case</em>.
*
* @author Sam Brannen
* @since 3.1
* @see AnnotationConfigContextLoaderTests
*/
public class PrivateConfigInnerClassTestCase {
// Intentionally private
@SuppressWarnings("unused")
@Configuration
private static class PrivateConfig {
}
}