Ignore @ImportAutoConfiguration exclude errors

Update `ImportAutoConfigurationImportSelector` to ignore excludes for
classes that aren't loaded. Since the import classes for tests tend to
be much more limited, the exception isn't really helpful.

Closes gh-6809
This commit is contained in:
Phillip Webb 2016-12-30 12:53:59 -08:00
parent b76978ff7e
commit b27f4e23be
3 changed files with 38 additions and 7 deletions

View File

@ -161,20 +161,31 @@ public class EnableAutoConfigurationImportSelector
private void checkExcludedClasses(List<String> configurations,
Set<String> exclusions) {
StringBuilder message = new StringBuilder();
List<String> invalidExcludes = new ArrayList<String>();
for (String exclusion : exclusions) {
if (ClassUtils.isPresent(exclusion, getClass().getClassLoader())
&& !configurations.contains(exclusion)) {
message.append("\t- ").append(exclusion).append(String.format("%n"));
invalidExcludes.add(exclusion);
}
}
if (!message.toString().isEmpty()) {
throw new IllegalStateException(String.format(
"The following classes could not be excluded because they are"
+ " not auto-configuration classes:%n%s",
message.toString()));
if (!invalidExcludes.isEmpty()) {
handleInvalidExcludes(invalidExcludes);
}
}
/**
* Handle any invalid excludes that have been specified.
* @param invalidExcludes the list of invalid excludes (will always have at least on
* element)
*/
protected void handleInvalidExcludes(List<String> invalidExcludes) {
StringBuilder message = new StringBuilder();
for (String exclude : invalidExcludes) {
message.append("\t- ").append(exclude).append(String.format("%n"));
}
throw new IllegalStateException(String
.format("The following classes could not be excluded because they are"
+ " not auto-configuration classes:%n%s", message));
}
/**

View File

@ -146,4 +146,9 @@ class ImportAutoConfigurationImportSelector
return super.getOrder() - 1;
}
@Override
protected void handleInvalidExcludes(List<String> invalidExcludes) {
// Ignore for test
}
}

View File

@ -116,6 +116,15 @@ public class ImportAutoConfigurationImportSelectorTests {
assertThat(imports).containsOnly(FreeMarkerAutoConfiguration.class.getName());
}
@Test
public void exclusionsWithoutImport() throws Exception {
AnnotationMetadata annotationMetadata = new SimpleMetadataReaderFactory()
.getMetadataReader(ExclusionWithoutImport.class.getName())
.getAnnotationMetadata();
String[] imports = this.importSelector.selectImports(annotationMetadata);
assertThat(imports).containsOnly(FreeMarkerAutoConfiguration.class.getName());
}
@Test
public void exclusionsAliasesAreApplied() throws Exception {
AnnotationMetadata annotationMetadata = new SimpleMetadataReaderFactory()
@ -149,6 +158,12 @@ public class ImportAutoConfigurationImportSelectorTests {
}
@ImportOne
@ImportAutoConfiguration(exclude = ThymeleafAutoConfiguration.class)
static class ExclusionWithoutImport {
}
@SelfAnnotating
static class ImportWithSelfAnnotatingAnnotation {