mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#178 avoid unused imports in property-mappings
This commit is contained in:
parent
720bcd83c5
commit
e5b9ee2cb1
@ -120,6 +120,11 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- CDI, Weld, Arquillian -->
|
||||
<dependency>
|
||||
|
@ -79,6 +79,11 @@
|
||||
<artifactId>javax.inject</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -18,9 +18,14 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigDecimal} and {@link BigInteger}.
|
||||
@ -38,4 +43,9 @@ public class BigDecimalToBigIntegerConversion extends SimpleConversion {
|
||||
public String getFromConversionString(String targetReference, ConversionContext conversionContext) {
|
||||
return "new BigDecimal( " + targetReference + " )";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,16 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import java.math.BigInteger;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigInteger} and native number types.
|
||||
* Conversion between {@link BigDecimal} and native number types.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
@ -50,4 +55,9 @@ public class BigDecimalToPrimitiveConversion extends SimpleConversion {
|
||||
|
||||
return conversion.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigDecimal} and {@link String}.
|
||||
@ -37,4 +42,9 @@ public class BigDecimalToStringConversion extends SimpleConversion {
|
||||
public String getFromConversionString(String targetReference, ConversionContext conversionContext) {
|
||||
return "new BigDecimal( " + targetReference + " )";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import java.math.BigDecimal;
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.util.NativeTypes;
|
||||
|
||||
/**
|
||||
@ -53,4 +57,9 @@ public class BigDecimalToWrapperConversion extends SimpleConversion {
|
||||
|
||||
return conversion.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigInteger} and native number types.
|
||||
@ -55,4 +60,9 @@ public class BigIntegerToPrimitiveConversion extends SimpleConversion {
|
||||
|
||||
return conversion.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigInteger.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Conversion between {@link BigInteger} and {@link String}.
|
||||
@ -37,4 +42,9 @@ public class BigIntegerToStringConversion extends SimpleConversion {
|
||||
public String getFromConversionString(String targetReference, ConversionContext conversionContext) {
|
||||
return "new BigInteger( " + targetReference + " )";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigInteger.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import java.math.BigInteger;
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.util.NativeTypes;
|
||||
|
||||
/**
|
||||
@ -60,4 +64,9 @@ public class BigIntegerToWrapperConversion extends SimpleConversion {
|
||||
|
||||
return conversion.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTypeFactory().getType( BigInteger.class ) );
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,12 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Conversion between {@link String} and {@link Enum} types.
|
||||
@ -36,4 +41,9 @@ public class EnumStringConversion extends SimpleConversion {
|
||||
public String getFromConversionString(String targetReference, ConversionContext conversionContext) {
|
||||
return "Enum.valueOf( " + conversionContext.getTargetType().getName() + ".class, " + targetReference + " )";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet( conversionContext.getTargetType() );
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.TypeConversion;
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
|
||||
/**
|
||||
* Base class for {@link ConversionProvider}s creating {@link TypeConversion}s
|
||||
@ -32,6 +37,8 @@ public abstract class SimpleConversion implements ConversionProvider {
|
||||
@Override
|
||||
public TypeConversion to(String sourceReference, ConversionContext conversionContext) {
|
||||
return new TypeConversion(
|
||||
getToConversionImportTypes( conversionContext ),
|
||||
getToConversionExceptionTypes( conversionContext ),
|
||||
getToConversionString( sourceReference, conversionContext )
|
||||
);
|
||||
}
|
||||
@ -39,18 +46,49 @@ public abstract class SimpleConversion implements ConversionProvider {
|
||||
@Override
|
||||
public TypeConversion from(String targetReference, ConversionContext conversionContext) {
|
||||
return new TypeConversion(
|
||||
getFromConversionImportTypes( conversionContext ),
|
||||
getFromConversionExceptionTypes( conversionContext ),
|
||||
getFromConversionString( targetReference, conversionContext )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param conversionContext the conversion context
|
||||
* @return exception types required in the from-conversion
|
||||
*/
|
||||
protected List<Type> getFromConversionExceptionTypes(ConversionContext conversionContext) {
|
||||
return Collections.<Type> emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param conversionContext the conversion context
|
||||
* @return conversion types required in the from-conversion
|
||||
*/
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return Collections.<Type> emptySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param conversionContext the conversion context
|
||||
* @return exception types required in the to-conversion
|
||||
*/
|
||||
protected List<Type> getToConversionExceptionTypes(ConversionContext conversionContext) {
|
||||
return Collections.<Type> emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param conversionContext the conversion context
|
||||
* @return conversion types required in the to-conversion
|
||||
*/
|
||||
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
|
||||
return Collections.<Type> emptySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the conversion string from source to target.
|
||||
*
|
||||
* @param sourceReference A reference to the source object, e.g.
|
||||
* {@code beanName.getFoo()}.
|
||||
* @param conversionContext ConversionContext providing optional information required for creating
|
||||
the conversion.
|
||||
*
|
||||
* @param sourceReference A reference to the source object, e.g. {@code beanName.getFoo()}.
|
||||
* @param conversionContext ConversionContext providing optional information required for creating the conversion.
|
||||
* @return The conversion string from source to target.
|
||||
*/
|
||||
protected abstract String getToConversionString(String sourceReference, ConversionContext conversionContext);
|
||||
|
@ -125,8 +125,12 @@ public class PropertyMapping extends ModelElement {
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> importTypes = new HashSet<Type>();
|
||||
importTypes.add( sourceType );
|
||||
importTypes.add( targetType );
|
||||
|
||||
if ( isTargetAccessorSetter() && getMappingMethod() == null
|
||||
&& ( targetType.isCollectionType() || targetType.isMapType() ) ) {
|
||||
importTypes.addAll( targetType.getImportTypes() );
|
||||
}
|
||||
|
||||
if ( conversion != null && mappingMethod == null ) {
|
||||
importTypes.addAll( conversion.getImportTypes() );
|
||||
}
|
||||
|
@ -45,6 +45,7 @@ public class CalendarToXmlGregorianCalendar extends BuiltInMethod {
|
||||
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
|
||||
|
||||
this.importTypes = asSet(
|
||||
returnType,
|
||||
typeFactory.getType( DatatypeFactory.class ),
|
||||
typeFactory.getType( GregorianCalendar.class ),
|
||||
typeFactory.getType( DatatypeConfigurationException.class )
|
||||
|
@ -45,6 +45,7 @@ public class DateToXmlGregorianCalendar extends BuiltInMethod {
|
||||
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
|
||||
|
||||
this.importTypes = asSet(
|
||||
returnType,
|
||||
typeFactory.getType( GregorianCalendar.class ),
|
||||
typeFactory.getType( DatatypeFactory.class ),
|
||||
typeFactory.getType( DatatypeConfigurationException.class )
|
||||
|
@ -18,6 +18,10 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model.source.builtin;
|
||||
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.JAXBElement;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
@ -31,10 +35,12 @@ public class JaxbElemToValue extends BuiltInMethod {
|
||||
|
||||
private final Parameter parameter;
|
||||
private final Type returnType;
|
||||
private final Set<Type> importTypes;
|
||||
|
||||
public JaxbElemToValue(TypeFactory typeFactory) {
|
||||
this.parameter = new Parameter( "element", typeFactory.getType( JAXBElement.class ) );
|
||||
this.returnType = typeFactory.getType( Object.class );
|
||||
this.importTypes = asSet( typeFactory.getType( JAXBElement.class ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -55,4 +61,9 @@ public class JaxbElemToValue extends BuiltInMethod {
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,12 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model.source.builtin;
|
||||
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.JAXBElement;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
@ -33,11 +38,17 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMethod {
|
||||
private final Parameter parameter;
|
||||
private final Type returnType;
|
||||
private final Type genericParam;
|
||||
private final Set<Type> importTypes;
|
||||
|
||||
public ListOfJaxbElemToListOfValue(TypeFactory typeFactory) {
|
||||
this.parameter = new Parameter( "elementList", typeFactory.getType( List.class ) );
|
||||
this.returnType = typeFactory.getType( List.class );
|
||||
this.genericParam = typeFactory.getType( JAXBElement.class ).erasure();
|
||||
this.importTypes = asSet(
|
||||
returnType,
|
||||
typeFactory.getType( JAXBElement.class ),
|
||||
typeFactory.getType( ArrayList.class )
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -61,4 +72,9 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMethod {
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
||||
|
@ -49,6 +49,7 @@ public class StringToXmlGregorianCalendar extends BuiltInMethod {
|
||||
this.parameter = new Parameter( "date", typeFactory.getType( String.class ) );
|
||||
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
|
||||
this.importTypes = asSet(
|
||||
returnType,
|
||||
typeFactory.getType( Date.class ),
|
||||
typeFactory.getType( GregorianCalendar.class ),
|
||||
typeFactory.getType( SimpleDateFormat.class ),
|
||||
|
@ -18,7 +18,11 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model.source.builtin;
|
||||
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
@ -32,10 +36,12 @@ public class XmlGregorianCalendarToCalendar extends BuiltInMethod {
|
||||
|
||||
private final Parameter parameter;
|
||||
private final Type returnType;
|
||||
private final Set<Type> importTypes;
|
||||
|
||||
public XmlGregorianCalendarToCalendar(TypeFactory typeFactory) {
|
||||
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
|
||||
this.returnType = typeFactory.getType( Calendar.class );
|
||||
this.importTypes = asSet( returnType, typeFactory.getType( XMLGregorianCalendar.class ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -47,4 +53,9 @@ public class XmlGregorianCalendarToCalendar extends BuiltInMethod {
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,11 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model.source.builtin;
|
||||
|
||||
import static org.mapstruct.ap.util.Collections.asSet;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
@ -32,10 +36,12 @@ public class XmlGregorianCalendarToDate extends BuiltInMethod {
|
||||
|
||||
private final Parameter parameter;
|
||||
private final Type returnType;
|
||||
private final Set<Type> importTypes;
|
||||
|
||||
public XmlGregorianCalendarToDate(TypeFactory typeFactory) {
|
||||
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
|
||||
this.returnType = typeFactory.getType( Date.class );
|
||||
this.importTypes = asSet( returnType, typeFactory.getType( XMLGregorianCalendar.class ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -47,4 +53,9 @@ public class XmlGregorianCalendarToDate extends BuiltInMethod {
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ public class XmlGregorianCalendarToString extends BuiltInMethod {
|
||||
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
|
||||
this.returnType = typeFactory.getType( String.class );
|
||||
this.importTypes = asSet(
|
||||
typeFactory.getType( XMLGregorianCalendar.class ),
|
||||
typeFactory.getType( Date.class ),
|
||||
typeFactory.getType( SimpleDateFormat.class ) );
|
||||
}
|
||||
|
@ -20,12 +20,17 @@ package org.mapstruct.ap.test.imports;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.test.imports.from.Foo;
|
||||
import org.mapstruct.ap.test.imports.referenced.NotImportedDatatype;
|
||||
import org.mapstruct.ap.test.imports.referenced.Source;
|
||||
import org.mapstruct.ap.test.imports.referenced.Target;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||
|
||||
/**
|
||||
* Test for generating a mapper which references types whose names clash with names of used annotations and exceptions.
|
||||
@ -40,11 +45,14 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
List.class,
|
||||
Map.class,
|
||||
Foo.class,
|
||||
org.mapstruct.ap.test.imports.to.Foo.class
|
||||
org.mapstruct.ap.test.imports.to.Foo.class, Source.class, Target.class, NotImportedDatatype.class
|
||||
})
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
public class ConflictingTypesNamesTest {
|
||||
|
||||
@Rule
|
||||
public GeneratedSource generatedSource = new GeneratedSource();
|
||||
|
||||
@Test
|
||||
public void mapperImportingTypesWithConflictingNamesCanBeGenerated() {
|
||||
Named source = new Named();
|
||||
@ -62,4 +70,17 @@ public class ConflictingTypesNamesTest {
|
||||
assertThat( foo2 ).isNotNull();
|
||||
assertThat( foo2.getName() ).isEqualTo( "bar" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapperNoUnecessaryImports() {
|
||||
Source source = new Source();
|
||||
source.setNotImported( new NotImportedDatatype( 42 ) );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getNotImported() ).isSameAs( source.getNotImported() );
|
||||
|
||||
generatedSource.forMapper( SourceTargetMapper.class ).containsNoImportFor( NotImportedDatatype.class );
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ package org.mapstruct.ap.test.imports;
|
||||
import java.util.Date;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ap.test.imports.referenced.Source;
|
||||
import org.mapstruct.ap.test.imports.referenced.Target;
|
||||
import org.mapstruct.ap.test.imports.to.Foo;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@ -44,4 +46,6 @@ public interface SourceTargetMapper {
|
||||
java.util.List<Date> stringsToDates(java.util.List<String> stringDates);
|
||||
|
||||
java.util.Map<Date, Date> stringsToDates(java.util.Map<String, String> stringDates);
|
||||
|
||||
Target sourceToTarget(Source target);
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.imports.referenced;
|
||||
|
||||
/**
|
||||
* @author Andreas Gudian
|
||||
*
|
||||
*/
|
||||
public class NotImportedDatatype {
|
||||
private final int value;
|
||||
|
||||
public NotImportedDatatype(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.imports.referenced;
|
||||
|
||||
/**
|
||||
* @author Andreas Gudian
|
||||
*
|
||||
*/
|
||||
public class Source {
|
||||
private NotImportedDatatype notImported;
|
||||
|
||||
public NotImportedDatatype getNotImported() {
|
||||
return notImported;
|
||||
}
|
||||
|
||||
public void setNotImported(NotImportedDatatype notImported) {
|
||||
this.notImported = notImported;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.imports.referenced;
|
||||
|
||||
/**
|
||||
* @author Andreas Gudian
|
||||
*
|
||||
*/
|
||||
public class Target {
|
||||
private NotImportedDatatype notImported;
|
||||
|
||||
public NotImportedDatatype getNotImported() {
|
||||
return notImported;
|
||||
}
|
||||
|
||||
public void setNotImported(NotImportedDatatype notImported) {
|
||||
this.notImported = notImported;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.testutil.assertions;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.fest.assertions.Assertions;
|
||||
import org.fest.assertions.FileAssert;
|
||||
import org.fest.assertions.StringAssert;
|
||||
|
||||
/**
|
||||
* Allows to perform assertions on .java source files.
|
||||
*
|
||||
* @author Andreas Gudian
|
||||
*/
|
||||
public class JavaFileAssert extends FileAssert {
|
||||
/**
|
||||
* @param actual the actual file
|
||||
*/
|
||||
public JavaFileAssert(File actual) {
|
||||
super( actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return assertion on the file content
|
||||
*/
|
||||
public StringAssert content() {
|
||||
exists();
|
||||
isFile();
|
||||
|
||||
try {
|
||||
return Assertions.assertThat( FileUtils.readFileToString( actual ) );
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
failIfCustomMessageIsSet( e );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the specified class is imported in this Java file
|
||||
*
|
||||
* @param importedClass the class expected to be imported in this Java file
|
||||
*/
|
||||
public void containsImportFor(Class<?> importedClass) {
|
||||
content().contains( "import " + importedClass.getName() + ";" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the specified class is not imported in this Java file
|
||||
*
|
||||
* @param importedClass the class expected not to be imported in this Java file
|
||||
*/
|
||||
public void containsNoImportFor(Class<?> importedClass) {
|
||||
content().doesNotContain( "import " + importedClass.getName() + ";" );
|
||||
}
|
||||
}
|
@ -113,6 +113,10 @@ class CompilingStatement extends Statement {
|
||||
next.evaluate();
|
||||
}
|
||||
|
||||
static String getSourceOutputDir() {
|
||||
return COMPILATION_CACHE.get().lastSourceOutputDir;
|
||||
}
|
||||
|
||||
protected void setupCompiler() throws Exception {
|
||||
compiler = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
@ -287,6 +291,8 @@ class CompilingStatement extends Statement {
|
||||
}
|
||||
|
||||
setupCompiler();
|
||||
cache.lastSourceOutputDir = sourceOutputDir;
|
||||
|
||||
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
|
||||
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
|
||||
|
||||
@ -371,6 +377,7 @@ class CompilingStatement extends Statement {
|
||||
}
|
||||
|
||||
private static class CompilationCache {
|
||||
private String lastSourceOutputDir;
|
||||
private CompilationRequest lastRequest;
|
||||
private CompilationResultHolder lastResult;
|
||||
}
|
||||
|
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.testutil.runner;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.mapstruct.ap.testutil.assertions.JavaFileAssert;
|
||||
|
||||
/**
|
||||
* A {@link TestRule} to perform assertions on generated source files.
|
||||
* <p>
|
||||
* To add it to the test, use:
|
||||
*
|
||||
* <pre>
|
||||
* @Rule
|
||||
* public GeneratedSource generatedSources = new GeneratedSource();
|
||||
* </pre>
|
||||
*
|
||||
* @author Andreas Gudian
|
||||
*/
|
||||
public class GeneratedSource implements TestRule {
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
return base;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mapperClass the class annotated with {@code @Mapper}
|
||||
* @return an assert for the *Impl.java for the given mapper
|
||||
*/
|
||||
public JavaFileAssert forMapper(Class<?> mapperClass) {
|
||||
String generatedJavaFileName = mapperClass.getName().replace( '.', '/' ).concat( "Impl.java" );
|
||||
return forJavaFile( generatedJavaFileName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path the path relative to the source output directory of the java file to return an assert for
|
||||
* @return an assert for the file specified by the given path
|
||||
*/
|
||||
public JavaFileAssert forJavaFile(String path) {
|
||||
return new JavaFileAssert( new File( CompilingStatement.getSourceOutputDir() + "/" + path ) );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user