Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
a9ffdca6
Commit
a9ffdca6
authored
Nov 14, 2016
by
Madhura Bhave
Committed by
Andy Wilkinson
Nov 22, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate excluded autoconfiguration classes
Closes gh-7407 See gh-6865
parent
c7a566fd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
0 deletions
+70
-0
EnableAutoConfigurationImportSelector.java
.../autoconfigure/EnableAutoConfigurationImportSelector.java
+18
-0
EnableAutoConfigurationImportSelectorTests.java
...configure/EnableAutoConfigurationImportSelectorTests.java
+52
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelector.java
View file @
a9ffdca6
...
...
@@ -85,6 +85,7 @@ public class EnableAutoConfigurationImportSelector
attributes
);
configurations
=
removeDuplicates
(
configurations
);
Set
<
String
>
exclusions
=
getExclusions
(
metadata
,
attributes
);
checkExcludedClasses
(
configurations
,
exclusions
);
configurations
.
removeAll
(
exclusions
);
configurations
=
sort
(
configurations
);
recordWithConditionEvaluationReport
(
configurations
,
exclusions
);
...
...
@@ -157,6 +158,23 @@ public class EnableAutoConfigurationImportSelector
return
EnableAutoConfiguration
.
class
;
}
private
void
checkExcludedClasses
(
List
<
String
>
configurations
,
Set
<
String
>
exclusions
)
{
StringBuilder
message
=
new
StringBuilder
();
for
(
String
exclusion
:
exclusions
)
{
if
(
ClassUtils
.
isPresent
(
exclusion
,
getClass
().
getClassLoader
())
&&
!
configurations
.
contains
(
exclusion
))
{
message
.
append
(
"\t- "
).
append
(
exclusion
).
append
(
"\n"
);
}
}
if
(!
message
.
toString
().
isEmpty
())
{
throw
new
IllegalStateException
(
"The following classes could not be excluded because they are not auto-configuration classes:\n"
+
message
.
toString
());
}
}
/**
* Return any exclusions that limit the candidate configurations.
* @param metadata the source metadata
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelectorTests.java
View file @
a9ffdca6
...
...
@@ -19,7 +19,9 @@ package org.springframework.boot.autoconfigure;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
...
...
@@ -29,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor
import
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration
;
import
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration
;
import
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.annotation.AnnotationAttributes
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.support.SpringFactoriesLoader
;
...
...
@@ -60,6 +63,9 @@ public class EnableAutoConfigurationImportSelectorTests {
@Mock
private
AnnotationAttributes
annotationAttributes
;
@Rule
public
ExpectedException
expected
=
ExpectedException
.
none
();
@Before
public
void
setup
()
{
MockitoAnnotations
.
initMocks
(
this
);
...
...
@@ -166,6 +172,47 @@ public class EnableAutoConfigurationImportSelectorTests {
assertThat
(
imports
).
isEmpty
();
}
@Test
public
void
nonAutoConfigurationClassExclusionsShouldThrowException
()
throws
Exception
{
this
.
expected
.
expect
(
IllegalStateException
.
class
);
configureExclusions
(
new
String
[]
{
TestConfiguration
.
class
.
getName
()
},
new
String
[
0
],
new
String
[
0
]);
this
.
importSelector
.
selectImports
(
this
.
annotationMetadata
);
}
@Test
public
void
nonAutoConfigurationClassNameExclusionsWhenPresentOnClassPathShouldThrowException
()
throws
Exception
{
this
.
expected
.
expect
(
IllegalStateException
.
class
);
configureExclusions
(
new
String
[
0
],
new
String
[]
{
"org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelectorTests.TestConfiguration"
},
new
String
[
0
]);
this
.
importSelector
.
selectImports
(
this
.
annotationMetadata
);
}
@Test
public
void
nonAutoConfigurationPropertyExclusionsWhenPresentOnClassPathShouldThrowException
()
throws
Exception
{
this
.
expected
.
expect
(
IllegalStateException
.
class
);
configureExclusions
(
new
String
[
0
],
new
String
[
0
],
new
String
[]
{
"org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelectorTests.TestConfiguration"
});
this
.
importSelector
.
selectImports
(
this
.
annotationMetadata
);
}
@Test
public
void
nameAndPropertyExclusionsWhenNotPresentOnClasspathShouldNotThrowException
()
throws
Exception
{
configureExclusions
(
new
String
[
0
],
new
String
[]
{
"org.springframework.boot.autoconfigure.DoesNotExist1"
},
new
String
[]
{
"org.springframework.boot.autoconfigure.DoesNotExist2"
});
this
.
importSelector
.
selectImports
(
this
.
annotationMetadata
);
assertThat
(
ConditionEvaluationReport
.
get
(
this
.
beanFactory
).
getExclusions
())
.
contains
(
"org.springframework.boot.autoconfigure.DoesNotExist1"
);
assertThat
(
ConditionEvaluationReport
.
get
(
this
.
beanFactory
).
getExclusions
())
.
contains
(
"org.springframework.boot.autoconfigure.DoesNotExist2"
);
}
private
void
configureExclusions
(
String
[]
classExclusion
,
String
[]
nameExclusion
,
String
[]
propertyExclusion
)
{
String
annotationName
=
EnableAutoConfiguration
.
class
.
getName
();
...
...
@@ -187,4 +234,9 @@ public class EnableAutoConfigurationImportSelectorTests {
getClass
().
getClassLoader
());
}
@Configuration
private
class
TestConfiguration
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment