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
99c0b456
Commit
99c0b456
authored
Feb 22, 2019
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish test to request the type under test
parent
c44a1e12
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
37 deletions
+85
-37
TypeUtilsTests.java
...framework/boot/configurationprocessor/TypeUtilsTests.java
+29
-34
RoundEnvironmentTester.java
...t/configurationprocessor/test/RoundEnvironmentTester.java
+49
-0
TestableAnnotationProcessor.java
...figurationprocessor/test/TestableAnnotationProcessor.java
+7
-3
No files found.
spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TypeUtilsTests.java
View file @
99c0b456
...
@@ -20,15 +20,12 @@ import java.io.IOException;
...
@@ -20,15 +20,12 @@ import java.io.IOException;
import
java.time.Duration
;
import
java.time.Duration
;
import
java.util.function.BiConsumer
;
import
java.util.function.BiConsumer
;
import
javax.annotation.processing.RoundEnvironment
;
import
javax.lang.model.element.Element
;
import
javax.lang.model.element.TypeElement
;
import
org.junit.Rule
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.rules.TemporaryFolder
;
import
org.junit.rules.TemporaryFolder
;
import
org.springframework.boot.configurationprocessor.TypeUtils.TypeDescriptor
;
import
org.springframework.boot.configurationprocessor.TypeUtils.TypeDescriptor
;
import
org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester
;
import
org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor
;
import
org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor
;
import
org.springframework.boot.configurationsample.generic.AbstractGenericProperties
;
import
org.springframework.boot.configurationsample.generic.AbstractGenericProperties
;
import
org.springframework.boot.configurationsample.generic.AbstractIntermediateGenericProperties
;
import
org.springframework.boot.configurationsample.generic.AbstractIntermediateGenericProperties
;
...
@@ -50,53 +47,51 @@ public class TypeUtilsTests {
...
@@ -50,53 +47,51 @@ public class TypeUtilsTests {
@Test
@Test
public
void
resolveTypeDescriptorOnConcreteClass
()
throws
IOException
{
public
void
resolveTypeDescriptorOnConcreteClass
()
throws
IOException
{
process
(
SimpleGenericProperties
.
class
,
(
roundEnv
,
typeUtils
)
->
{
process
(
SimpleGenericProperties
.
class
,
(
roundEnv
,
typeUtils
)
->
{
for
(
Element
rootElement
:
roundEnv
.
getRootElements
())
{
TypeDescriptor
typeDescriptor
=
typeUtils
.
resolveTypeDescriptor
(
TypeDescriptor
typeDescriptor
=
typeUtils
roundEnv
.
getRootElement
(
SimpleGenericProperties
.
class
));
.
resolveTypeDescriptor
((
TypeElement
)
rootElement
);
assertThat
(
assertThat
(
typeDescriptor
.
getGenerics
().
keySet
().
stream
(
)
typeDescriptor
.
getGenerics
().
keySet
().
stream
().
map
(
Object:
:
toString
)
)
.
map
(
Object:
:
toString
))
.
containsOnly
(
"A"
,
"B"
,
"C"
);
.
containsOnly
(
"A"
,
"B"
,
"C"
);
assertThat
(
typeDescriptor
.
resolveGeneric
(
"A"
))
assertThat
(
typeDescriptor
.
resolveGeneric
(
"A"
))
.
hasToString
(
String
.
class
.
getName
());
.
hasToString
(
String
.
class
.
getName
());
assertThat
(
typeDescriptor
.
resolveGeneric
(
"B"
))
assertThat
(
typeDescriptor
.
resolveGeneric
(
"B"
))
.
hasToString
(
Integer
.
class
.
getName
());
.
hasToString
(
Integer
.
class
.
getName
());
assertThat
(
typeDescriptor
.
resolveGeneric
(
"C"
))
assertThat
(
typeDescriptor
.
resolveGeneric
(
"C"
))
.
hasToString
(
Duration
.
class
.
getName
());
.
hasToString
(
Duration
.
class
.
getName
());
}
});
});
}
}
@Test
@Test
public
void
resolveTypeDescriptorOnIntermediateClass
()
throws
IOException
{
public
void
resolveTypeDescriptorOnIntermediateClass
()
throws
IOException
{
process
(
AbstractIntermediateGenericProperties
.
class
,
(
roundEnv
,
typeUtils
)
->
{
process
(
AbstractIntermediateGenericProperties
.
class
,
(
roundEnv
,
typeUtils
)
->
{
for
(
Element
rootElement
:
roundEnv
.
getRootElements
())
{
TypeDescriptor
typeDescriptor
=
typeUtils
.
resolveTypeDescriptor
(
TypeDescriptor
typeDescriptor
=
typeUtils
roundEnv
.
getRootElement
(
AbstractIntermediateGenericProperties
.
class
));
.
resolveTypeDescriptor
((
TypeElement
)
rootElement
);
assertThat
(
assertThat
(
typeDescriptor
.
getGenerics
().
keySet
().
stream
()
typeDescriptor
.
getGenerics
().
keySet
().
stream
().
map
(
Object:
:
toString
))
.
map
(
Object:
:
toString
)).
containsOnly
(
"A"
,
"B"
,
"C"
);
.
containsOnly
(
"A"
,
"B"
,
"C"
);
assertThat
(
typeDescriptor
.
resolveGeneric
(
"A"
))
assertThat
(
typeDescriptor
.
resolveGeneric
(
"A"
))
.
hasToString
(
String
.
class
.
getName
());
.
hasToString
(
String
.
class
.
getName
());
assertThat
(
typeDescriptor
.
resolveGeneric
(
"B"
))
assertThat
(
typeDescriptor
.
resolveGeneric
(
"B"
))
.
hasToString
(
Integer
.
class
.
getName
());
.
hasToString
(
Integer
.
class
.
getName
());
assertThat
(
typeDescriptor
.
resolveGeneric
(
"C"
)).
hasToString
(
"C"
);
assertThat
(
typeDescriptor
.
resolveGeneric
(
"C"
)).
hasToString
(
"C"
);
}
});
});
}
}
@Test
@Test
public
void
resolveTypeDescriptorWithOnlyGenerics
()
throws
IOException
{
public
void
resolveTypeDescriptorWithOnlyGenerics
()
throws
IOException
{
process
(
AbstractGenericProperties
.
class
,
(
roundEnv
,
typeUtils
)
->
{
process
(
AbstractGenericProperties
.
class
,
(
roundEnv
,
typeUtils
)
->
{
for
(
Element
rootElement
:
roundEnv
.
getRootElements
())
{
TypeDescriptor
typeDescriptor
=
typeUtils
.
resolveTypeDescriptor
(
TypeDescriptor
typeDescriptor
=
typeUtils
roundEnv
.
getRootElement
(
AbstractGenericProperties
.
class
));
.
resolveTypeDescriptor
((
TypeElement
)
rootElement
);
assertThat
(
assertThat
(
typeDescriptor
.
getGenerics
().
keySet
().
stream
(
)
typeDescriptor
.
getGenerics
().
keySet
().
stream
().
map
(
Object:
:
toString
)
)
.
map
(
Object:
:
toString
))
.
containsOnly
(
"A"
,
"B"
,
"C"
);
.
containsOnly
(
"A"
,
"B"
,
"C"
);
}
});
});
}
}
private
void
process
(
Class
<?>
target
,
private
void
process
(
Class
<?>
target
,
BiConsumer
<
RoundEnvironment
,
TypeUtils
>
consumer
)
throws
IOException
{
BiConsumer
<
RoundEnvironment
Tester
,
TypeUtils
>
consumer
)
throws
IOException
{
TestableAnnotationProcessor
<
TypeUtils
>
processor
=
new
TestableAnnotationProcessor
<>(
TestableAnnotationProcessor
<
TypeUtils
>
processor
=
new
TestableAnnotationProcessor
<>(
consumer
,
TypeUtils:
:
new
);
consumer
,
TypeUtils:
:
new
);
TestCompiler
compiler
=
new
TestCompiler
(
this
.
temporaryFolder
);
TestCompiler
compiler
=
new
TestCompiler
(
this
.
temporaryFolder
);
...
...
spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/RoundEnvironmentTester.java
0 → 100644
View file @
99c0b456
/*
* Copyright 2012-2019 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
.
boot
.
configurationprocessor
.
test
;
import
javax.annotation.processing.RoundEnvironment
;
import
javax.lang.model.element.TypeElement
;
/**
* A tester utility for {@link RoundEnvironment}.
*
* @author Stephane Nicoll
*/
public
class
RoundEnvironmentTester
{
private
final
RoundEnvironment
roundEnvironment
;
RoundEnvironmentTester
(
RoundEnvironment
roundEnvironment
)
{
this
.
roundEnvironment
=
roundEnvironment
;
}
/**
* Return the root {@link TypeElement} for the specified {@code type}.
* @param type the type of the class
* @return the {@link TypeElement}
*/
public
TypeElement
getRootElement
(
Class
<?>
type
)
{
return
(
TypeElement
)
this
.
roundEnvironment
.
getRootElements
().
stream
()
.
filter
((
element
)
->
element
.
toString
().
equals
(
type
.
getName
()))
.
findFirst
()
.
orElseThrow
(()
->
new
IllegalStateException
(
"No element found for "
+
type
+
" make sure it is included in the list of classes to compile"
));
}
}
spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestableAnnotationProcessor.java
View file @
99c0b456
...
@@ -39,13 +39,13 @@ import javax.lang.model.element.TypeElement;
...
@@ -39,13 +39,13 @@ import javax.lang.model.element.TypeElement;
@SupportedSourceVersion
(
SourceVersion
.
RELEASE_8
)
@SupportedSourceVersion
(
SourceVersion
.
RELEASE_8
)
public
class
TestableAnnotationProcessor
<
T
>
extends
AbstractProcessor
{
public
class
TestableAnnotationProcessor
<
T
>
extends
AbstractProcessor
{
private
final
BiConsumer
<
RoundEnvironment
,
T
>
consumer
;
private
final
BiConsumer
<
RoundEnvironment
Tester
,
T
>
consumer
;
private
final
Function
<
ProcessingEnvironment
,
T
>
factory
;
private
final
Function
<
ProcessingEnvironment
,
T
>
factory
;
private
T
target
;
private
T
target
;
public
TestableAnnotationProcessor
(
BiConsumer
<
RoundEnvironment
,
T
>
consumer
,
public
TestableAnnotationProcessor
(
BiConsumer
<
RoundEnvironment
Tester
,
T
>
consumer
,
Function
<
ProcessingEnvironment
,
T
>
factory
)
{
Function
<
ProcessingEnvironment
,
T
>
factory
)
{
this
.
consumer
=
consumer
;
this
.
consumer
=
consumer
;
this
.
factory
=
factory
;
this
.
factory
=
factory
;
...
@@ -59,7 +59,11 @@ public class TestableAnnotationProcessor<T> extends AbstractProcessor {
...
@@ -59,7 +59,11 @@ public class TestableAnnotationProcessor<T> extends AbstractProcessor {
@Override
@Override
public
boolean
process
(
Set
<?
extends
TypeElement
>
annotations
,
public
boolean
process
(
Set
<?
extends
TypeElement
>
annotations
,
RoundEnvironment
roundEnv
)
{
RoundEnvironment
roundEnv
)
{
this
.
consumer
.
accept
(
roundEnv
,
this
.
target
);
RoundEnvironmentTester
tester
=
new
RoundEnvironmentTester
(
roundEnv
);
if
(!
roundEnv
.
getRootElements
().
isEmpty
())
{
this
.
consumer
.
accept
(
tester
,
this
.
target
);
return
true
;
}
return
false
;
return
false
;
}
}
...
...
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