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
f7801797
Commit
f7801797
authored
Jul 26, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish contribution
Closes gh-13904
parent
df6feb3e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
35 additions
and
26 deletions
+35
-26
AnnotatedClassFinder.java
...ringframework/boot/test/context/AnnotatedClassFinder.java
+22
-15
SpringBootTestContextBootstrapper.java
.../boot/test/context/SpringBootTestContextBootstrapper.java
+1
-1
AnnotatedClassFinderTests.java
...ramework/boot/test/context/AnnotatedClassFinderTests.java
+5
-3
ExampleConfig.java
...ingframework/boot/test/context/example/ExampleConfig.java
+2
-2
Example.java
...ringframework/boot/test/context/example/scan/Example.java
+2
-2
SubExampleConfig.java
.../boot/test/context/example/scan/sub/SubExampleConfig.java
+3
-3
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/
SpringBootConfiguration
Finder.java
→
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/
AnnotatedClass
Finder.java
View file @
f7801797
...
...
@@ -23,20 +23,20 @@ import java.util.Map;
import
java.util.Set
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
;
import
org.springframework.core.type.filter.AnnotationTypeFilter
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ClassUtils
;
/**
*
Utility class to scan for a {@link SpringBootConfiguration} or custom annotation
.
*
Utility class to find a class annotated with a particular annotation in a hierarchy
.
*
* @author Phillip Webb
* @author Artsiom Yudovin
* @author Stephane Nicoll
* @since 2.1.0
*/
public
final
class
SpringBootConfiguration
Finder
{
public
final
class
AnnotatedClass
Finder
{
private
static
final
Map
<
String
,
Class
<?>>
cache
=
Collections
.
synchronizedMap
(
new
Cache
(
40
));
...
...
@@ -44,28 +44,35 @@ public final class SpringBootConfigurationFinder {
private
final
ClassPathScanningCandidateComponentProvider
scanner
;
/**
* Default constructor initializing finder to scan for a {@link SpringBootConfiguration} annotation.
* Create a new instance with the {@code annotationType} to find.
* @param annotationType the annotation to find
*/
public
SpringBootConfigurationFinder
()
{
this
(
SpringBootConfiguration
.
class
);
}
/**
* Customiable constructor allow to provide the custom annotation to scan for.
* @param annotationType Annotation to scan for.
*/
public
SpringBootConfigurationFinder
(
Class
<?
extends
Annotation
>
annotationType
)
{
public
AnnotatedClassFinder
(
Class
<?
extends
Annotation
>
annotationType
)
{
Assert
.
notNull
(
annotationType
,
"AnnotationType must not be null"
);
this
.
scanner
=
new
ClassPathScanningCandidateComponentProvider
(
false
);
this
.
scanner
.
addIncludeFilter
(
new
AnnotationTypeFilter
(
annotationType
));
this
.
scanner
.
addIncludeFilter
(
new
AnnotationTypeFilter
(
annotationType
));
this
.
scanner
.
setResourcePattern
(
"*.class"
);
}
/**
* Find the first {@link Class} that is annotated with the target annotation, starting
* from the package defined by the given {@code source} up to the root.
* @param source the source class to use to initiate the search
* @return the first {@link Class} annotated with the target annotation within the
* hierarchy defined by the given {@code source} or {@code null} if none is found.
*/
public
Class
<?>
findFromClass
(
Class
<?>
source
)
{
Assert
.
notNull
(
source
,
"Source must not be null"
);
return
findFromPackage
(
ClassUtils
.
getPackageName
(
source
));
}
/**
* Find the first {@link Class} that is annotated with the target annotation, starting
* from the package defined by the given {@code source} up to the root.
* @param source the source package to use to initiate the search
* @return the first {@link Class} annotated with the target annotation within the
* hierarchy defined by the given {@code source} or {@code null} if none is found.
*/
public
Class
<?>
findFromPackage
(
String
source
)
{
Assert
.
notNull
(
source
,
"Source must not be null"
);
Class
<?>
configuration
=
cache
.
get
(
source
);
...
...
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java
View file @
f7801797
...
...
@@ -238,7 +238,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
if
(
containsNonTestComponent
(
classes
)
||
mergedConfig
.
hasLocations
())
{
return
classes
;
}
Class
<?>
found
=
new
SpringBootConfigurationFinder
(
)
Class
<?>
found
=
new
AnnotatedClassFinder
(
SpringBootConfiguration
.
class
)
.
findFromClass
(
mergedConfig
.
getTestClass
());
Assert
.
state
(
found
!=
null
,
"Unable to find a @SpringBootConfiguration, you need to use "
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/
SpringBootConfiguration
FinderTests.java
→
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/
AnnotatedClass
FinderTests.java
View file @
f7801797
...
...
@@ -20,22 +20,24 @@ import org.junit.Rule;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.test.context.example.ExampleConfig
;
import
org.springframework.boot.test.context.example.scan.Example
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Tests for {@link
SpringBootConfiguration
Finder}.
* Tests for {@link
AnnotatedClass
Finder}.
*
* @author Phillip Webb
*/
public
class
SpringBootConfiguration
FinderTests
{
public
class
AnnotatedClass
FinderTests
{
@Rule
public
ExpectedException
thrown
=
ExpectedException
.
none
();
private
SpringBootConfigurationFinder
finder
=
new
SpringBootConfigurationFinder
();
private
AnnotatedClassFinder
finder
=
new
AnnotatedClassFinder
(
SpringBootConfiguration
.
class
);
@Test
public
void
findFromClassWhenSourceIsNullShouldThrowException
()
{
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/example/ExampleConfig.java
View file @
f7801797
...
...
@@ -17,10 +17,10 @@
package
org
.
springframework
.
boot
.
test
.
context
.
example
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.test.context.
SpringBootConfiguration
FinderTests
;
import
org.springframework.boot.test.context.
AnnotatedClass
FinderTests
;
/**
* Example config used in {@link
SpringBootConfiguration
FinderTests}.
* Example config used in {@link
AnnotatedClass
FinderTests}.
*
* @author Phillip Webb
*/
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/example/scan/Example.java
View file @
f7801797
...
...
@@ -16,10 +16,10 @@
package
org
.
springframework
.
boot
.
test
.
context
.
example
.
scan
;
import
org.springframework.boot.test.context.
SpringBootConfiguration
FinderTests
;
import
org.springframework.boot.test.context.
AnnotatedClass
FinderTests
;
/**
* Example class used in {@link
SpringBootConfiguration
FinderTests}.
* Example class used in {@link
AnnotatedClass
FinderTests}.
*
* @author Phillip Webb
*/
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/example/scan/sub/SubExampleConfig.java
View file @
f7801797
...
...
@@ -17,11 +17,11 @@
package
org
.
springframework
.
boot
.
test
.
context
.
example
.
scan
.
sub
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.test.context.
SpringBootConfiguration
FinderTests
;
import
org.springframework.boot.test.context.
AnnotatedClass
FinderTests
;
/**
* Example config used in {@link
SpringBootConfigurationFinderTests}. Should not be found
* s
ince s
canner should only search upwards.
* Example config used in {@link
AnnotatedClassFinderTests}. Should not be found since
* scanner should only search upwards.
*
* @author Phillip Webb
*/
...
...
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