#2060: MapStruct should work properly on the module path

This commit is contained in:
Filip Hrisafov 2020-04-05 15:14:35 +02:00
parent 4f76208c62
commit 853ff7f74f
8 changed files with 168 additions and 4 deletions

View File

@ -130,4 +130,10 @@ public class MavenIntegrationTest {
void targetTypeGenerationTest() {
}
@ProcessorTest(baseDir = "moduleInfoTest")
@EnabledForJreRange(min = JRE.JAVA_11)
void moduleInfoTest() {
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright MapStruct Authors.
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-it-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>modulesTest</artifactId>
<packaging>jar</packaging>
</project>

View File

@ -0,0 +1,9 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
module test.mapstruct {
requires org.mapstruct;
exports org.mapstruct.itest.modules;
}

View File

@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.modules;
/**
* @author Filip Hrisafov
*/
public class CustomerDto {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.modules;
/**
* @author Filip Hrisafov
*/
public class CustomerEntity {
private String name;
private String mail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}

View File

@ -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.itest.modules;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface CustomerMapper {
CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
@Mapping(target = "mail", source = "email")
CustomerEntity fromDto(CustomerDto record);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.modules;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.mapstruct.itest.modules.CustomerDto;
import org.mapstruct.itest.modules.CustomerEntity;
import org.mapstruct.itest.modules.CustomerMapper;
public class ModulesTest {
@Test
public void shouldMapRecord() {
CustomerDto dto = new CustomerDto();
dto.setName( "Kermit" );
dto.setEmail( "kermit@test.com" );
CustomerEntity customer = CustomerMapper.INSTANCE.fromDto( dto );
assertThat( customer ).isNotNull();
assertThat( customer.getName() ).isEqualTo( "Kermit" );
assertThat( customer.getMail() ).isEqualTo( "kermit@test.com" );
}
}

View File

@ -184,9 +184,8 @@ public class Conversions {
register( Enum.class, String.class, new EnumStringConversion() );
register( Date.class, String.class, new DateToStringConversion() );
register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() );
register( Date.class, Time.class, new DateToSqlTimeConversion() );
register( Date.class, java.sql.Date.class, new DateToSqlDateConversion() );
register( Date.class, Timestamp.class, new DateToSqlTimestampConversion() );
registerJavaTimeSqlConversions();
// java.util.Currency <~> String
register( Currency.class, String.class, new CurrencyToStringConversion() );
@ -227,15 +226,28 @@ public class Conversions {
register( ZonedDateTime.class, Date.class, new JavaZonedDateTimeToDateConversion() );
register( LocalDateTime.class, Date.class, new JavaLocalDateTimeToDateConversion() );
register( LocalDate.class, Date.class, new JavaLocalDateToDateConversion() );
register( LocalDate.class, java.sql.Date.class, new JavaLocalDateToSqlDateConversion() );
register( Instant.class, Date.class, new JavaInstantToDateConversion() );
}
private void registerJavaTimeSqlConversions() {
if ( isJavaSqlAvailable() ) {
register( LocalDate.class, java.sql.Date.class, new JavaLocalDateToSqlDateConversion() );
register( Date.class, Time.class, new DateToSqlTimeConversion() );
register( Date.class, java.sql.Date.class, new DateToSqlDateConversion() );
register( Date.class, Timestamp.class, new DateToSqlTimestampConversion() );
}
}
private boolean isJodaTimeAvailable() {
return typeFactory.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN );
}
private boolean isJavaSqlAvailable() {
return typeFactory.isTypeAvailable( "java.sql.Date" );
}
private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) {
if ( sourceType.isPrimitive() && targetType.isPrimitive() ) {
register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) );