22
33import com .squareup .javapoet .ClassName ;
44import org .apache .commons .lang3 .tuple .Pair ;
5+ import org .junit .jupiter .api .BeforeEach ;
6+ import org .junit .jupiter .api .Nested ;
57import org .junit .jupiter .api .Test ;
8+ import org .junit .jupiter .api .extension .ExtendWith ;
9+ import org .mockito .Mock ;
10+ import org .mockito .junit .jupiter .MockitoExtension ;
611
12+ import javax .lang .model .element .TypeElement ;
13+ import javax .lang .model .util .Elements ;
714import java .io .IOException ;
815import java .io .StringWriter ;
916import java .time .Clock ;
1421import static java .util .Collections .singletonList ;
1522import static org .apache .commons .io .IOUtils .resourceToString ;
1623import static org .assertj .core .api .BDDAssertions .then ;
24+ import static org .mockito .BDDMockito .given ;
25+ import static org .mockito .Mockito .mock ;
1726
27+ @ ExtendWith (MockitoExtension .class )
1828class ConversionServiceAdapterGeneratorTest {
29+ @ Mock private Elements elements ;
30+
31+ private boolean isAtLeastJava9 ;
32+
1933 private static final Clock FIXED_CLOCK =
2034 Clock .fixed (
2135 ZonedDateTime .of (2020 , 3 , 29 , 15 , 21 , 34 , (int ) (236 * Math .pow (10 , 6 )), ZoneId .of ("Z" ))
@@ -24,46 +38,120 @@ class ConversionServiceAdapterGeneratorTest {
2438 private final ConversionServiceAdapterGenerator generator =
2539 new ConversionServiceAdapterGenerator (FIXED_CLOCK );
2640
27- @ Test
28- void shouldGenerateMatchingOutput () throws IOException {
41+ @ Nested
42+ class Java8Generated {
43+ @ BeforeEach
44+ void initElements () {
45+ isAtLeastJava9 = false ;
46+ given (elements .getTypeElement ("javax.annotation.Generated" ))
47+ .willReturn (mock (TypeElement .class ));
48+ }
49+
50+ @ Test
51+ void shouldGenerateMatchingOutput () throws IOException {
52+ ConversionServiceAdapterGeneratorTest .this .shouldGenerateMatchingOutput (
53+ "ConversionServiceAdapterJava8Generated.java" );
54+ }
55+
56+ @ Test
57+ void shouldGenerateMatchingOutputWhenUsingCustomConversionService () throws IOException {
58+ ConversionServiceAdapterGeneratorTest .this
59+ .shouldGenerateMatchingOutputWhenUsingCustomConversionService (
60+ "ConversionServiceAdapterCustomBeanJava8Generated.java" );
61+ }
62+ }
63+
64+ @ Nested
65+ class Java9PlusGenerated {
66+ @ BeforeEach
67+ void initElements () {
68+ isAtLeastJava9 = true ;
69+ given (elements .getTypeElement ("javax.annotation.processing.Generated" ))
70+ .willReturn (mock (TypeElement .class ));
71+ }
72+
73+ @ Test
74+ void shouldGenerateMatchingOutput () throws IOException {
75+ ConversionServiceAdapterGeneratorTest .this .shouldGenerateMatchingOutput (
76+ "ConversionServiceAdapterJava9PlusGenerated.java" );
77+ }
78+
79+ @ Test
80+ void shouldGenerateMatchingOutputWhenUsingCustomConversionService () throws IOException {
81+ ConversionServiceAdapterGeneratorTest .this
82+ .shouldGenerateMatchingOutputWhenUsingCustomConversionService (
83+ "ConversionServiceAdapterCustomBeanJava9PlusGenerated.java" );
84+ }
85+ }
86+
87+ @ Nested
88+ class NoGenerated {
89+ @ BeforeEach
90+ void initElements () {
91+ isAtLeastJava9 = false ;
92+ }
93+
94+ @ Test
95+ void shouldGenerateMatchingOutput () throws IOException {
96+ ConversionServiceAdapterGeneratorTest .this .shouldGenerateMatchingOutput (
97+ "ConversionServiceAdapterNoGenerated.java" );
98+ }
99+
100+ @ Test
101+ void shouldGenerateMatchingOutputWhenUsingCustomConversionService () throws IOException {
102+ ConversionServiceAdapterGeneratorTest .this
103+ .shouldGenerateMatchingOutputWhenUsingCustomConversionService (
104+ "ConversionServiceAdapterCustomBeanNoGenerated.java" );
105+ }
106+ }
107+
108+ void shouldGenerateMatchingOutput (final String expectedContentFileName ) throws IOException {
29109 // Given
30- final ConversionServiceAdapterDescriptor descriptor = new ConversionServiceAdapterDescriptor ();
31- descriptor .adapterClassName (
32- ClassName .get (
33- ConversionServiceAdapterGeneratorTest .class .getPackage ().getName (),
34- "ConversionServiceAdapter" ));
35- descriptor .fromToMappings (
36- singletonList (Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" ))));
37- descriptor .lazyAnnotatedConversionServiceBean (true );
110+ final ConversionServiceAdapterDescriptor descriptor =
111+ new ConversionServiceAdapterDescriptor ()
112+ .adapterClassName (
113+ ClassName .get (
114+ ConversionServiceAdapterGeneratorTest .class .getPackage ().getName (),
115+ "ConversionServiceAdapter" ))
116+ .fromToMappings (
117+ singletonList (
118+ Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" ))))
119+ .lazyAnnotatedConversionServiceBean (true )
120+ .elementUtils (elements )
121+ .sourceVersionAtLeast9 (isAtLeastJava9 );
38122 final StringWriter outputWriter = new StringWriter ();
39123
40124 // When
41125 generator .writeConversionServiceAdapter (descriptor , outputWriter );
42126
43127 // Then
44128 then (outputWriter .toString ())
45- .isEqualToIgnoringWhitespace (resourceToString ("/ConversionServiceAdapter.java" , UTF_8 ));
129+ .isEqualToIgnoringWhitespace (resourceToString ('/' + expectedContentFileName , UTF_8 ));
46130 }
47131
48- @ Test
49- void shouldGenerateMatchingOutputWhenUsingCustomConversionService ( ) throws IOException {
132+ void shouldGenerateMatchingOutputWhenUsingCustomConversionService (
133+ final String expectedContentFileName ) throws IOException {
50134 // Given
51- final ConversionServiceAdapterDescriptor descriptor = new ConversionServiceAdapterDescriptor ();
52- descriptor .adapterClassName (
53- ClassName .get (
135+ final ConversionServiceAdapterDescriptor descriptor =
136+ new ConversionServiceAdapterDescriptor ()
137+ .adapterClassName (
138+ ClassName .get (
54139 ConversionServiceAdapterGeneratorTest .class .getPackage ().getName (),
55- "ConversionServiceAdapter" ));
56- descriptor .conversionServiceBeanName ("myConversionService" );
57- descriptor .fromToMappings (
58- singletonList (Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" ))));
59- descriptor .lazyAnnotatedConversionServiceBean (true );
140+ "ConversionServiceAdapter" ))
141+ .conversionServiceBeanName ("myConversionService" )
142+ .fromToMappings (
143+ singletonList (
144+ Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" ))))
145+ .lazyAnnotatedConversionServiceBean (true )
146+ .elementUtils (elements )
147+ .sourceVersionAtLeast9 (isAtLeastJava9 );
60148 final StringWriter outputWriter = new StringWriter ();
61149
62150 // When
63151 generator .writeConversionServiceAdapter (descriptor , outputWriter );
64152
65153 // Then
66154 then (outputWriter .toString ())
67- .isEqualToIgnoringWhitespace (resourceToString ("/ConversionServiceAdapterCustomBean.java" , UTF_8 ));
155+ .isEqualToIgnoringWhitespace (resourceToString ('/' + expectedContentFileName , UTF_8 ));
68156 }
69157}
0 commit comments