11package org .mapstruct .extensions .spring .converter ;
22
3+ import static java .lang .Boolean .TRUE ;
34import static java .nio .charset .StandardCharsets .UTF_8 ;
5+ import static java .util .function .UnaryOperator .identity ;
46import static javax .lang .model .SourceVersion .RELEASE_8 ;
57import static javax .lang .model .SourceVersion .RELEASE_9 ;
68import static org .apache .commons .io .IOUtils .resourceToString ;
1719import java .time .ZoneId ;
1820import java .time .ZonedDateTime ;
1921import java .util .List ;
22+ import java .util .Map ;
23+ import java .util .function .UnaryOperator ;
2024import javax .annotation .processing .ProcessingEnvironment ;
2125import javax .lang .model .SourceVersion ;
2226import javax .lang .model .element .TypeElement ;
@@ -46,22 +50,24 @@ class ConversionServiceAdapterGeneratorTest {
4650
4751 @ Nested
4852 class DefaultProcessingEnvironment {
53+ @ Mock private ProcessingEnvironment processingEnvironment ;
54+
4955 @ BeforeEach
5056 void initWithProcessingEnvironment () {
51- final var processingEnvironment = mock (ProcessingEnvironment .class );
5257 given (processingEnvironment .getElementUtils ()).willReturn (elements );
5358 given (processingEnvironment .getSourceVersion ())
54- .will ((Answer <SourceVersion >)
55- (invocation ) -> {
56- if (isAtLeastJava9 ) {
57- return RELEASE_9 ;
58- } else {
59- return RELEASE_8 ;
60- }
61- });
59+ .will (
60+ (Answer <SourceVersion >)
61+ (invocation ) -> {
62+ if (isAtLeastJava9 ) {
63+ return RELEASE_9 ;
64+ } else {
65+ return RELEASE_8 ;
66+ }
67+ });
6268 underTest .init (processingEnvironment );
6369 }
64-
70+
6571 @ Nested
6672 class Java8Generated {
6773 @ BeforeEach
@@ -83,6 +89,15 @@ void shouldGenerateMatchingOutputWhenUsingCustomConversionService() throws IOExc
8389 .shouldGenerateMatchingOutputWhenUsingCustomConversionService (
8490 "ConversionServiceAdapterCustomBeanJava8Generated.java" );
8591 }
92+
93+ @ Test
94+ void shouldSuppressDateGenerationWhenProcessingEnvironmentHasSuppressionSetToTrue ()
95+ throws IOException {
96+ given (processingEnvironment .getOptions ())
97+ .willReturn (Map .of ("mapstruct.suppressGeneratorTimestamp" , String .valueOf (TRUE )));
98+ ConversionServiceAdapterGeneratorTest .this .shouldGenerateMatchingOutput (
99+ "ConversionServiceAdapterJava8GeneratedNoDate.java" );
100+ }
86101 }
87102
88103 @ Nested
@@ -106,6 +121,15 @@ void shouldGenerateMatchingOutputWhenUsingCustomConversionService() throws IOExc
106121 .shouldGenerateMatchingOutputWhenUsingCustomConversionService (
107122 "ConversionServiceAdapterCustomBeanJava9PlusGenerated.java" );
108123 }
124+
125+ @ Test
126+ void shouldSuppressDateGenerationWhenProcessingEnvironmentHasSuppressionSetToTrue ()
127+ throws IOException {
128+ given (processingEnvironment .getOptions ())
129+ .willReturn (Map .of ("mapstruct.suppressGeneratorTimestamp" , String .valueOf (TRUE )));
130+ ConversionServiceAdapterGeneratorTest .this .shouldGenerateMatchingOutput (
131+ "ConversionServiceAdapterJava9PlusGeneratedNoDate.java" );
132+ }
109133 }
110134
111135 @ Nested
@@ -130,23 +154,26 @@ void shouldGenerateMatchingOutputWhenUsingCustomConversionService() throws IOExc
130154 }
131155 }
132156
133- void shouldGenerateMatchingOutput (final String expectedContentFileName ) throws IOException {
134- // Given
157+ void shouldGenerateMatchingOutput (
158+ final String expectedContentFileName ,
159+ final UnaryOperator <ConversionServiceAdapterDescriptor > descriptorDecorator )
160+ throws IOException {
135161 final ConversionServiceAdapterDescriptor descriptor =
136- new ConversionServiceAdapterDescriptor ()
137- .adapterClassName (
138- ClassName .get (
139- ConversionServiceAdapterGeneratorTest .class .getPackage ().getName (),
140- "ConversionServiceAdapter" ))
141- .fromToMappings (
142- List .of (
143- Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" )),
144- Pair .of (
145- ParameterizedTypeName .get (
146- ClassName .get (List .class ), ClassName .get ("test" , "Car" )),
147- ParameterizedTypeName .get (
148- ClassName .get (List .class ), ClassName .get ("test" , "CarDto" )))))
149- .lazyAnnotatedConversionServiceBean (true );
162+ descriptorDecorator .apply (
163+ new ConversionServiceAdapterDescriptor ()
164+ .adapterClassName (
165+ ClassName .get (
166+ ConversionServiceAdapterGeneratorTest .class .getPackage ().getName (),
167+ "ConversionServiceAdapter" ))
168+ .fromToMappings (
169+ List .of (
170+ Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" )),
171+ Pair .of (
172+ ParameterizedTypeName .get (
173+ ClassName .get (List .class ), ClassName .get ("test" , "Car" )),
174+ ParameterizedTypeName .get (
175+ ClassName .get (List .class ), ClassName .get ("test" , "CarDto" )))))
176+ .lazyAnnotatedConversionServiceBean (true ));
150177 final StringWriter outputWriter = new StringWriter ();
151178
152179 // When
@@ -157,33 +184,15 @@ void shouldGenerateMatchingOutput(final String expectedContentFileName) throws I
157184 .isEqualToIgnoringWhitespace (resourceToString ('/' + expectedContentFileName , UTF_8 ));
158185 }
159186
187+ void shouldGenerateMatchingOutput (final String expectedContentFileName ) throws IOException {
188+ shouldGenerateMatchingOutput (expectedContentFileName , identity ());
189+ }
190+
160191 void shouldGenerateMatchingOutputWhenUsingCustomConversionService (
161192 final String expectedContentFileName ) throws IOException {
162- // Given
163- final ConversionServiceAdapterDescriptor descriptor =
164- new ConversionServiceAdapterDescriptor ()
165- .adapterClassName (
166- ClassName .get (
167- ConversionServiceAdapterGeneratorTest .class .getPackage ().getName (),
168- "ConversionServiceAdapter" ))
169- .conversionServiceBeanName ("myConversionService" )
170- .fromToMappings (
171- List .of (
172- Pair .of (ClassName .get ("test" , "Car" ), ClassName .get ("test" , "CarDto" )),
173- Pair .of (
174- ParameterizedTypeName .get (
175- ClassName .get (List .class ), ClassName .get ("test" , "Car" )),
176- ParameterizedTypeName .get (
177- ClassName .get (List .class ), ClassName .get ("test" , "CarDto" )))))
178- .lazyAnnotatedConversionServiceBean (true );
179- final StringWriter outputWriter = new StringWriter ();
180-
181- // When
182- underTest .writeConversionServiceAdapter (descriptor , outputWriter );
183-
184- // Then
185- then (outputWriter .toString ())
186- .isEqualToIgnoringWhitespace (resourceToString ('/' + expectedContentFileName , UTF_8 ));
193+ shouldGenerateMatchingOutput (
194+ expectedContentFileName ,
195+ descriptor -> descriptor .conversionServiceBeanName ("myConversionService" ));
187196 }
188197
189198 @ Nested
0 commit comments