-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
90 name collision with same class name in different packages (#92)
* Prepared for specifying non default adapter method name. * Fixes #90
- Loading branch information
Showing
24 changed files
with
354 additions
and
88 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
annotations/src/main/java/org/mapstruct/extensions/spring/AdapterMethodName.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,22 @@ | ||
package org.mapstruct.extensions.spring; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.SOURCE; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Overrides the default method name generated in the Adapter class. To be used exclusively on a | ||
* {@link org.springframework.core.convert.converter.Converter} annotated as {@code @Mapper}. | ||
*/ | ||
@Target(TYPE) | ||
@Retention(SOURCE) | ||
public @interface AdapterMethodName { | ||
/** | ||
* The method name to be used instead of the default. | ||
* | ||
* @return The method name to be used instead of the default. | ||
*/ | ||
String value(); | ||
} |
10 changes: 6 additions & 4 deletions
10
annotations/src/main/java/org/mapstruct/extensions/spring/ExternalConversion.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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
package org.mapstruct.extensions.spring; | ||
|
||
import java.lang.annotation.ElementType; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.SOURCE; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Allows the specification of a conversion that is available via the {@link | ||
* org.springframework.core.convert.ConversionService ConversionService}, but is <em>not</em> | ||
* declared as a MapStruct mapper within the scope of the {@link SpringMapperConfig}. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.SOURCE) | ||
@Target(TYPE) | ||
@Retention(SOURCE) | ||
public @interface ExternalConversion { | ||
Class<?> sourceType(); | ||
|
||
Class<?> targetType(); | ||
String adapterMethodName() default ""; | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...mapstruct/extensions/spring/example/externalconversions/ConversionServiceAdapterTest.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,37 @@ | ||
package org.mapstruct.extensions.spring.example.externalconversions; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.sql.Blob; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.TypeDescriptor; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ConversionServiceAdapterTest { | ||
@Mock private ConversionService conversionService; | ||
|
||
@InjectMocks private ConversionServiceAdapter conversionServiceAdapter; | ||
|
||
@Test | ||
void shouldCallConversionServiceFromGeneratedMethodWithOverriddenMethodName() { | ||
final var blob = mock(Blob.class); | ||
final var expectedBytes = "Hello World!".getBytes(UTF_8); | ||
given( | ||
conversionService.convert( | ||
blob, TypeDescriptor.valueOf(Blob.class), TypeDescriptor.valueOf(byte[].class))) | ||
.willReturn(expectedBytes); | ||
|
||
final var actualBytes = conversionServiceAdapter.blob2Bytes(blob); | ||
|
||
then(actualBytes).isSameAs(expectedBytes); | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
.../noconfig/src/main/java/org/mapstruct/extensions/spring/example/noconfig/WheelMapper.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
39 changes: 39 additions & 0 deletions
39
...g/src/test/java/org/mapstruct/extensions/spring/example/ConversionServiceAdapterTest.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,39 @@ | ||
package org.mapstruct.extensions.spring.example; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.mapstruct.extensions.spring.example.WheelPosition.RIGHT_FRONT; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mapstruct.extensions.spring.converter.ConversionServiceAdapter; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.TypeDescriptor; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ConversionServiceAdapterTest { | ||
@Mock private ConversionService conversionService; | ||
|
||
@InjectMocks private ConversionServiceAdapter conversionServiceAdapter; | ||
|
||
@Test | ||
void shouldCallConversionServiceFromGeneratedMethodWithOverriddenMethodName() { | ||
final var wheel = new Wheel(); | ||
wheel.setPosition(RIGHT_FRONT); | ||
wheel.setDiameter(16); | ||
final var expectedDto = new WheelDto(); | ||
expectedDto.setDiameter(16); | ||
expectedDto.setPosition("RIGHT_FRONT"); | ||
given( | ||
conversionService.convert( | ||
wheel, TypeDescriptor.valueOf(Wheel.class), TypeDescriptor.valueOf(WheelDto.class))) | ||
.willReturn(expectedDto); | ||
|
||
final var actualDto = conversionServiceAdapter.toDto(wheel); | ||
|
||
then(actualDto).isSameAs(expectedDto); | ||
} | ||
} |
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
Oops, something went wrong.