Skip to content

Commit

Permalink
Use spring.factories to declare each test slice's auto-config imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed May 27, 2016
1 parent 1b0bbd8 commit 73b01cf
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.springframework.context.annotation.Import;

/**
* Import and apply the selected auto-configuration classes. Applies the same ordering
* Import and apply the specified auto-configuration classes. Applies the same ordering
* rules as {@code @EnableAutoConfiguration} but restricts the auto-configuration classes
* to the specified set, rather than consulting {@code spring.factories}.
* <p>
Expand All @@ -35,6 +35,7 @@
* and especially when writing tests.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.3.0
*/
@Target(ElementType.TYPE)
Expand All @@ -46,9 +47,11 @@
public @interface ImportAutoConfiguration {

/**
* The auto-configuration classes that should be imported.
* The auto-configuration classes that should be imported. When empty, the classes are
* specified using an entry in {@code META-INF/spring.factories} where the key is the
* fully-qualified name of the annotated class.
* @return the classes to import
*/
Class<?>[] value();
Class<?>[] value() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;

Expand Down Expand Up @@ -77,19 +78,25 @@ private void collectCandidateConfigurations(Class<?> source, Set<String> candida
if (source != null && seen.add(source)) {
for (Annotation annotation : source.getDeclaredAnnotations()) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
collectCandidateConfigurations(annotation, candidates, seen);
collectCandidateConfigurations(source, annotation, candidates, seen);
}
}
collectCandidateConfigurations(source.getSuperclass(), candidates, seen);
}
}

private void collectCandidateConfigurations(Annotation annotation,
private void collectCandidateConfigurations(Class<?> source, Annotation annotation,
Set<String> candidates, Set<Class<?>> seen) {
if (ANNOTATION_NAMES.contains(annotation.annotationType().getName())) {
String[] value = (String[]) AnnotationUtils
.getAnnotationAttributes(annotation, true).get("value");
candidates.addAll(Arrays.asList(value));
if (value.length > 0) {
candidates.addAll(Arrays.asList(value));
}
else {
candidates.addAll(SpringFactoriesLoader.loadFactoryNames(source,
getClass().getClassLoader()));
}
}
collectCandidateConfigurations(annotation.annotationType(), candidates, seen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.CacheType;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.cache.support.NoOpCacheManager;
Expand All @@ -39,7 +38,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ImportAutoConfiguration(CacheAutoConfiguration.class)
@ImportAutoConfiguration
public @interface AutoConfigureCache {

@PropertyMapping("spring.cache.type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;

/**
* {@link ImportAutoConfiguration Auto-configuration imports} for typical JSON tests. Most
Expand All @@ -38,7 +36,7 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ImportAutoConfiguration({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class })
@ImportAutoConfiguration
public @interface AutoConfigureJson {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;

/**
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Data JPA tests.
Expand All @@ -44,11 +37,7 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ImportAutoConfiguration({ HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class, TransactionAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
DataSourceAutoConfiguration.class, FlywayAutoConfiguration.class,
LiquibaseAutoConfiguration.class })
@ImportAutoConfiguration
public @interface AutoConfigureDataJpa {

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ImportAutoConfiguration(TestDatabaseAutoConfiguration.class)
@ImportAutoConfiguration
@PropertyMapping("spring.test.database")
public @interface AutoConfigureTestDatabase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ImportAutoConfiguration(TestEntityManagerAutoConfiguration.class)
@ImportAutoConfiguration
public @interface AutoConfigureTestEntityManager {

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ImportAutoConfiguration(RestDocsAutoConfiguration.class)
@ImportAutoConfiguration
@Import(RestDocumentationContextProviderRegistrar.class)
@PropertyMapping("spring.test.restdocs")
public @interface AutoConfigureRestDocs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ImportAutoConfiguration({ MockMvcAutoConfiguration.class,
MockMvcWebClientAutoConfiguration.class, MockMvcWebDriverAutoConfiguration.class,
MockMvcSecurityAutoConfiguration.class })
@ImportAutoConfiguration
@PropertyMapping("spring.test.mockmvc")
public @interface AutoConfigureMockMvc {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;

/**
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Spring MVC
Expand All @@ -46,11 +37,7 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ImportAutoConfiguration({ WebMvcAutoConfiguration.class, GsonAutoConfiguration.class,
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
FreeMarkerAutoConfiguration.class, GroovyTemplateAutoConfiguration.class,
MustacheAutoConfiguration.class, ThymeleafAutoConfiguration.class,
HypermediaAutoConfiguration.class })
@ImportAutoConfiguration
public @interface AutoConfigureWebMvc {

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
# AutoConfigureCache auto-configuration imports
org.springframework.boot.test.autoconfigure.core.AutoConfigureCache=\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration

# AutoConfigureDataJpa auto-configuration imports
org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa=\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureJson auto-configuration imports
org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration

# AutoConfigureMockMvc auto-configuration imports
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration

# AutoConfigureRestDocs auto-configuration imports
org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs=\
org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration

# AutoConfigureTestDatabase auto-configuration imports
org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase=\
org.springframework.boot.test.autoconfigure.orm.jpa.TestDataBaseAutoConfiguration

# AutoConfigureTestEntityManager auto-configuration imports
org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager=\
org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration

# AutoConfigureWebMvc auto-configuration imports
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc=\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

# Spring Test ContextCustomizerFactories
org.springframework.test.context.ContextCustomizerFactory=\
org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory,\
Expand Down

0 comments on commit 73b01cf

Please sign in to comment.