forked from mapstruct/mapstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapstruct#1792 Annotation processor option for default injection stra…
…tegy
- Loading branch information
1 parent
d018aed
commit 750ce48
Showing
16 changed files
with
518 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...test/injectionstrategy/jsr330/_default/CustomerJsr330DefaultCompileOptionFieldMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.ap.test.injectionstrategy.jsr330._default; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity; | ||
|
||
/** | ||
* @author Andrei Arlou | ||
*/ | ||
@Mapper(componentModel = "jsr330", uses = GenderJsr330DefaultCompileOptionFieldMapper.class) | ||
public interface CustomerJsr330DefaultCompileOptionFieldMapper { | ||
|
||
CustomerDto asTarget(CustomerEntity customerEntity); | ||
} |
25 changes: 25 additions & 0 deletions
25
...p/test/injectionstrategy/jsr330/_default/GenderJsr330DefaultCompileOptionFieldMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.ap.test.injectionstrategy.jsr330._default; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.ValueMapping; | ||
import org.mapstruct.ValueMappings; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.Gender; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto; | ||
|
||
/** | ||
* @author Andrei Arlou | ||
*/ | ||
@Mapper(componentModel = "jsr330") | ||
public interface GenderJsr330DefaultCompileOptionFieldMapper { | ||
|
||
@ValueMappings({ | ||
@ValueMapping(source = "MALE", target = "M"), | ||
@ValueMapping(source = "FEMALE", target = "F") | ||
}) | ||
GenderDto mapToDto(Gender gender); | ||
} |
94 changes: 94 additions & 0 deletions
94
.../ap/test/injectionstrategy/jsr330/_default/Jsr330DefaultCompileOptionFieldMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.ap.test.injectionstrategy.jsr330._default; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.Gender; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto; | ||
import org.mapstruct.ap.testutil.WithClasses; | ||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; | ||
import org.mapstruct.ap.testutil.runner.GeneratedSource; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import static java.lang.System.lineSeparator; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test field injection for component model jsr330. | ||
* Default value option mapstruct.defaultInjectionStrategy is "field" | ||
* | ||
* @author Andrei Arlou | ||
*/ | ||
@WithClasses({ | ||
CustomerDto.class, | ||
CustomerEntity.class, | ||
Gender.class, | ||
GenderDto.class, | ||
CustomerJsr330DefaultCompileOptionFieldMapper.class, | ||
GenderJsr330DefaultCompileOptionFieldMapper.class | ||
}) | ||
@RunWith(AnnotationProcessorTestRunner.class) | ||
@ComponentScan(basePackageClasses = CustomerJsr330DefaultCompileOptionFieldMapper.class) | ||
@Configuration | ||
public class Jsr330DefaultCompileOptionFieldMapperTest { | ||
|
||
@Rule | ||
public final GeneratedSource generatedSource = new GeneratedSource(); | ||
|
||
@Inject | ||
@Named | ||
private CustomerJsr330DefaultCompileOptionFieldMapper customerMapper; | ||
private ConfigurableApplicationContext context; | ||
|
||
@Before | ||
public void springUp() { | ||
context = new AnnotationConfigApplicationContext( getClass() ); | ||
context.getAutowireCapableBeanFactory().autowireBean( this ); | ||
} | ||
|
||
@After | ||
public void springDown() { | ||
if ( context != null ) { | ||
context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldConvertToTarget() { | ||
// given | ||
CustomerEntity customerEntity = new CustomerEntity(); | ||
customerEntity.setName( "Samuel" ); | ||
customerEntity.setGender( Gender.MALE ); | ||
|
||
// when | ||
CustomerDto customerDto = customerMapper.asTarget( customerEntity ); | ||
|
||
// then | ||
assertThat( customerDto ).isNotNull(); | ||
assertThat( customerDto.getName() ).isEqualTo( "Samuel" ); | ||
assertThat( customerDto.getGender() ).isEqualTo( GenderDto.M ); | ||
} | ||
|
||
@Test | ||
public void shouldHaveFieldInjection() { | ||
generatedSource.forMapper( CustomerJsr330DefaultCompileOptionFieldMapper.class ) | ||
.content() | ||
.contains( "@Inject" + lineSeparator() + " private GenderJsr330DefaultCompileOptionFieldMapper" ) | ||
.doesNotContain( "public CustomerJsr330DefaultCompileOptionFieldMapperImpl(" ); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...trategy/jsr330/compileoptionconstructor/CustomerJsr330CompileOptionConstructorMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.ap.test.injectionstrategy.jsr330.compileoptionconstructor; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity; | ||
|
||
/** | ||
* @author Andrei Arlou | ||
*/ | ||
@Mapper( componentModel = "jsr330", | ||
uses = GenderJsr330CompileOptionConstructorMapper.class ) | ||
public interface CustomerJsr330CompileOptionConstructorMapper { | ||
|
||
@Mapping(source = "gender", target = "gender") | ||
CustomerDto asTarget(CustomerEntity customerEntity); | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...nstrategy/jsr330/compileoptionconstructor/GenderJsr330CompileOptionConstructorMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.ap.test.injectionstrategy.jsr330.compileoptionconstructor; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.ValueMapping; | ||
import org.mapstruct.ValueMappings; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.Gender; | ||
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto; | ||
|
||
/** | ||
* @author Andrei Arlou | ||
*/ | ||
@Mapper(componentModel = "jsr330") | ||
public interface GenderJsr330CompileOptionConstructorMapper { | ||
|
||
@ValueMappings({ | ||
@ValueMapping(source = "MALE", target = "M"), | ||
@ValueMapping(source = "FEMALE", target = "F") | ||
}) | ||
GenderDto mapToDto(Gender gender); | ||
} |
Oops, something went wrong.