mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#235 java expressions and unit test, simplify getconstantmapping, reorg unit test
This commit is contained in:
parent
87f7f55539
commit
b41aa588a1
@ -42,8 +42,9 @@ public @interface Mapping {
|
||||
* <li>The source name of the configured property as defined by the JavaBeans specification.</li>
|
||||
* <li>When used to map an enum constant, the name of the constant member is to be given<./li>.
|
||||
* </ol>
|
||||
* Either this attribute or {@link #constant()} may be specified for a given mapping, but not both at the same
|
||||
* time.
|
||||
* Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping,
|
||||
* but not two at the same time. If this attribute is given, the target property must be specified via
|
||||
* {@link #target()}.
|
||||
*
|
||||
* @return The source name of the configured property or enum constant.
|
||||
*/
|
||||
@ -70,11 +71,26 @@ public @interface Mapping {
|
||||
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
|
||||
* method or built-in conversion.
|
||||
* <p>
|
||||
* Either this attribute or {@link #source()} may be specified for a given mapping, but not both at the same time.
|
||||
* If this attribute is given, the target property must be specified via {@link #target()}.
|
||||
* Either this attribute or {@link #source()} or {@link #expression()} may be specified for a given mapping,
|
||||
* but not two at the same time. If this attribute is given, the target property must be specified via
|
||||
* {@link #target()}.
|
||||
*
|
||||
* @return A constant {@code String} constant specifying the value for the designated target property
|
||||
*/
|
||||
String constant() default "";
|
||||
|
||||
/**
|
||||
* An expression {@link String} based on which the specified target property is to be set.
|
||||
*
|
||||
* The format is determined by a type of expression. For instance:
|
||||
* {@code expression = "java(new org.example.TimeAndFormat( s.getTime(), s.getFormat() ))")} will insert the java
|
||||
* expression in the designated {@link #target()} property.
|
||||
* <p>
|
||||
* Either this attribute or {@link #source()} or {@link #constant()} may be specified for a given mapping,
|
||||
* but not two at the same time. If this attribute is given, the target property must be specified via
|
||||
* {@link #target()}.
|
||||
*
|
||||
* @return A constant {@code String} constant specifying the value for the designated target property
|
||||
*/
|
||||
String expression() default "";
|
||||
}
|
||||
|
@ -40,8 +40,9 @@ public @interface Mapping {
|
||||
* <li>The source name of the configured property as defined by the JavaBeans specification.</li>
|
||||
* <li>When used to map an enum constant, the name of the constant member is to be given<./li>.
|
||||
* </ol>
|
||||
* Either this attribute or {@link #constant()} may be specified for a given mapping, but not both at the same
|
||||
* time.
|
||||
* Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping,
|
||||
* but not two at the same time. If this attribute is given, the target property must be specified via
|
||||
* {@link #target()}.
|
||||
*
|
||||
* @return The source name of the configured property or enum constant.
|
||||
*/
|
||||
@ -68,11 +69,27 @@ public @interface Mapping {
|
||||
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
|
||||
* method or built-in conversion.
|
||||
* <p>
|
||||
* Either this attribute or {@link #source()} may be specified for a given mapping, but not both at the same time.
|
||||
* If this attribute is given, the target property must be specified via {@link #target()}.
|
||||
* Either this attribute or {@link #source()} or {@link #expression()} may be specified for a given mapping,
|
||||
* but not two at the same time. If this attribute is given, the target property must be specified via
|
||||
* {@link #target()}.
|
||||
*
|
||||
* @return A constant {@code String} constant specifying the value for the designated target property
|
||||
*/
|
||||
String constant() default "";
|
||||
|
||||
/**
|
||||
* An expression {@link String} based on which the specified target property is to be set.
|
||||
*
|
||||
* The format is determined by a type of expression. For instance:
|
||||
* {@code expression = "java(new org.example.TimeAndFormat( s.getTime(), s.getFormat() ))")} will insert the java
|
||||
* expression in the designated {@link #target()} property.
|
||||
* <p>
|
||||
* Either this attribute or {@link #source()} or {@link #constant()} may be specified for a given mapping,
|
||||
* but not two at the same time. If this attribute is given, the target property must be specified via
|
||||
* {@link #target()}.
|
||||
*
|
||||
* @return A constant {@code String} constant specifying the value for the designated target property
|
||||
*/
|
||||
String expression() default "";
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
@ -39,10 +41,14 @@ import org.mapstruct.ap.util.AnnotationProcessingException;
|
||||
*/
|
||||
public class Mapping {
|
||||
|
||||
private static final Pattern JAVA_EXPRESSION = Pattern.compile( "^java\\((.*)\\)$" );
|
||||
|
||||
private final String sourceName;
|
||||
private final String sourceParameterName;
|
||||
private final String sourcePropertyName;
|
||||
private final String constant;
|
||||
private final String expression;
|
||||
private final String javaExpression;
|
||||
private final String targetName;
|
||||
private final String dateFormat;
|
||||
private final AnnotationMirror mirror;
|
||||
@ -74,10 +80,12 @@ public class Mapping {
|
||||
mappingPrism.values.source()
|
||||
);
|
||||
|
||||
if ( mappingPrism.source().isEmpty() && mappingPrism.constant().isEmpty() ) {
|
||||
if ( mappingPrism.source().isEmpty() &&
|
||||
mappingPrism.constant().isEmpty() &&
|
||||
mappingPrism.expression().isEmpty() ) {
|
||||
messager.printMessage(
|
||||
Diagnostic.Kind.ERROR,
|
||||
"Either define a source or a constant in a Mapping",
|
||||
"Either define a source, a constant or an epression in a Mapping",
|
||||
element
|
||||
);
|
||||
return null;
|
||||
@ -85,17 +93,33 @@ public class Mapping {
|
||||
else if ( !mappingPrism.source().isEmpty() && !mappingPrism.constant().isEmpty() ) {
|
||||
messager.printMessage(
|
||||
Diagnostic.Kind.ERROR,
|
||||
"Source and constant are both defined in Mapping, either define a source or an expression",
|
||||
"Source and constant are both defined in Mapping, either define a source or a constant",
|
||||
element
|
||||
);
|
||||
return null;
|
||||
}
|
||||
else if ( !mappingPrism.source().isEmpty() && !mappingPrism.expression().isEmpty() ) {
|
||||
messager.printMessage(
|
||||
Diagnostic.Kind.ERROR,
|
||||
"Source and expression are both defined in Mapping, either define a source or an expression",
|
||||
element
|
||||
);
|
||||
return null;
|
||||
}
|
||||
else if ( !mappingPrism.expression().isEmpty() && !mappingPrism.constant().isEmpty() ) {
|
||||
messager.printMessage(
|
||||
Diagnostic.Kind.ERROR,
|
||||
"Expression and constant are both defined in Mapping, either define an expression or a constant",
|
||||
element
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Mapping(
|
||||
mappingPrism.source(),
|
||||
sourceNameParts != null ? sourceNameParts[0] : null,
|
||||
sourceNameParts != null ? sourceNameParts[1] : mappingPrism.source(),
|
||||
mappingPrism.constant(),
|
||||
mappingPrism.expression(),
|
||||
mappingPrism.target(),
|
||||
mappingPrism.dateFormat(),
|
||||
mappingPrism.mirror,
|
||||
@ -124,12 +148,15 @@ public class Mapping {
|
||||
}
|
||||
|
||||
private Mapping(String sourceName, String sourceParameterName, String sourcePropertyName, String constant,
|
||||
String targetName, String dateFormat, AnnotationMirror mirror,
|
||||
String expression, String targetName, String dateFormat, AnnotationMirror mirror,
|
||||
AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) {
|
||||
this.sourceName = sourceName;
|
||||
this.sourceParameterName = sourceParameterName;
|
||||
this.sourcePropertyName = sourcePropertyName;
|
||||
this.constant = constant;
|
||||
this.expression = expression;
|
||||
Matcher javaExpressionMatcher = JAVA_EXPRESSION.matcher( expression );
|
||||
this.javaExpression = javaExpressionMatcher.matches() ? javaExpressionMatcher.group( 1 ) : "";
|
||||
this.targetName = targetName.equals( "" ) ? sourceName : targetName;
|
||||
this.dateFormat = dateFormat;
|
||||
this.mirror = mirror;
|
||||
@ -169,6 +196,9 @@ public class Mapping {
|
||||
return constant;
|
||||
}
|
||||
|
||||
public String getJavaExpression() {
|
||||
return javaExpression;
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return targetName;
|
||||
@ -200,6 +230,7 @@ public class Mapping {
|
||||
null,
|
||||
targetName,
|
||||
constant,
|
||||
expression,
|
||||
sourceName,
|
||||
dateFormat,
|
||||
mirror,
|
||||
|
@ -406,13 +406,30 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
// check if there's a mapping defined
|
||||
Mapping mapping = method.getMappingByTargetPropertyName( targetPropertyName );
|
||||
String dateFormat = null;
|
||||
boolean isSourceConstant = false;
|
||||
String sourceConstant = null;
|
||||
|
||||
String sourcePropertyName;
|
||||
if ( mapping != null ) {
|
||||
dateFormat = mapping.getDateFormat();
|
||||
isSourceConstant = !mapping.getConstant().isEmpty();
|
||||
sourceConstant = "\"" + mapping.getConstant() + "\"";
|
||||
|
||||
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||
|
||||
// check constants first
|
||||
if ( !mapping.getConstant().isEmpty() ) {
|
||||
return getConstantMapping(
|
||||
mapperReferences,
|
||||
methods,
|
||||
method,
|
||||
"\"" + mapping.getConstant() + "\"",
|
||||
targetAccessor,
|
||||
dateFormat
|
||||
);
|
||||
}
|
||||
|
||||
if ( !mapping.getJavaExpression().isEmpty() ) {
|
||||
return getJavaExpressionMapping( method, mapping.getJavaExpression(), targetAccessor );
|
||||
}
|
||||
}
|
||||
|
||||
sourcePropertyName = mapping.getSourcePropertyName();
|
||||
}
|
||||
else {
|
||||
@ -421,18 +438,6 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
|
||||
List<ExecutableElement> sourceGetters = parameter.getType().getGetters();
|
||||
|
||||
// check constants first
|
||||
if ( isSourceConstant ) {
|
||||
return getConstantMapping(
|
||||
mapperReferences,
|
||||
methods,
|
||||
method,
|
||||
sourceConstant,
|
||||
targetAccessor,
|
||||
dateFormat
|
||||
);
|
||||
}
|
||||
|
||||
// then iterate over source accessors (assuming the source is a bean)
|
||||
for ( ExecutableElement sourceAccessor : sourceGetters ) {
|
||||
|
||||
@ -676,6 +681,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
|
||||
}
|
||||
else if ( mappedProperty.getConstant().isEmpty() &&
|
||||
mappedProperty.getJavaExpression().isEmpty() &&
|
||||
!hasSourceProperty( method, mappedProperty.getSourcePropertyName() ) ) {
|
||||
messager.printMessage(
|
||||
Kind.ERROR,
|
||||
@ -843,7 +849,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
List<SourceMethod> methods,
|
||||
SourceMethod method,
|
||||
String constantExpression,
|
||||
ExecutableElement targetAcessor,
|
||||
ExecutableElement targetAccessor,
|
||||
String dateFormat) {
|
||||
|
||||
// source
|
||||
@ -851,14 +857,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
Type sourceType = typeFactory.getType( String.class );
|
||||
|
||||
// target
|
||||
Type targetType = null;
|
||||
if ( Executables.isSetterMethod( targetAcessor ) ) {
|
||||
targetType = typeFactory.getSingleParameter( targetAcessor ).getType();
|
||||
}
|
||||
else if ( Executables.isGetterMethod( targetAcessor ) ) {
|
||||
targetType = typeFactory.getReturnType( targetAcessor );
|
||||
}
|
||||
String targetPropertyName = Executables.getPropertyName( targetAcessor );
|
||||
Type targetType = typeFactory.getSingleParameter( targetAccessor ).getType();
|
||||
String targetPropertyName = Executables.getPropertyName( targetAccessor );
|
||||
|
||||
Assignment assignment = mappingResolver.getTargetAssignment(
|
||||
method,
|
||||
@ -874,13 +874,6 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
|
||||
if ( assignment != null ) {
|
||||
|
||||
// create a new Map or Collection implementation if no method or type conversion
|
||||
if ( targetType != null && ( targetType.isCollectionType() || targetType.isMapType() ) ) {
|
||||
if ( assignment.getType() == DIRECT ) {
|
||||
assignment = new NewCollectionOrMapWrapper( assignment, targetType.getImportTypes() );
|
||||
}
|
||||
}
|
||||
|
||||
// target accessor is setter, so decorate assignment as setter
|
||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||
}
|
||||
@ -898,6 +891,20 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
);
|
||||
}
|
||||
|
||||
return new PropertyMapping( targetAccessor.getSimpleName().toString(), targetType, assignment );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link PropertyMapping} representing the mapping of the given java expression into the target
|
||||
* property.
|
||||
*/
|
||||
private PropertyMapping getJavaExpressionMapping(SourceMethod method,
|
||||
String javaExpression,
|
||||
ExecutableElement targetAcessor) {
|
||||
|
||||
Type targetType = typeFactory.getSingleParameter( targetAcessor ).getType();
|
||||
Assignment assignment = AssignmentFactory.createSimple( javaExpression );
|
||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||
return new PropertyMapping( targetAcessor.getSimpleName().toString(), targetType, assignment );
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 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.source.constants;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper(uses = StringListMapper.class)
|
||||
public interface ErroneousMapper3 {
|
||||
|
||||
ErroneousMapper3 INSTANCE = Mappers.getMapper( ErroneousMapper3.class );
|
||||
|
||||
@Mappings( {
|
||||
@Mapping( target = "stringConstant", constant = "stringConstant"),
|
||||
@Mapping( target = "integerConstant", expression = "java('test')", constant = "14"),
|
||||
@Mapping( target = "longWrapperConstant", constant = "3001"),
|
||||
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", constant = "09-01-2014"),
|
||||
@Mapping( target = "nameConstants", constant = "jack-jill-tom" )
|
||||
} )
|
||||
Target sourceToTarget(Source s);
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 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.source.constants;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper(uses = StringListMapper.class)
|
||||
public interface ErroneousMapper4 {
|
||||
|
||||
ErroneousMapper4 INSTANCE = Mappers.getMapper( ErroneousMapper4.class );
|
||||
|
||||
@Mappings( {
|
||||
@Mapping( target = "stringConstant", constant = "stringConstant"),
|
||||
@Mapping( source = "test" , target = "integerConstant", expression = "java('test')"),
|
||||
@Mapping( target = "longWrapperConstant", constant = "3001"),
|
||||
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", constant = "09-01-2014"),
|
||||
@Mapping( target = "nameConstants", constant = "jack-jill-tom" )
|
||||
} )
|
||||
Target sourceToTarget(Source s);
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
/**
|
||||
*
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -96,14 +96,64 @@ public class SourceConstantsTest {
|
||||
@Diagnostic(type = ErroneousMapper1.class,
|
||||
kind = Kind.ERROR,
|
||||
line = 42,
|
||||
messageRegExp = "Source and constant are both defined in Mapping, either define a source or an "
|
||||
+ "expression"),
|
||||
messageRegExp = "Source and constant are both defined in Mapping, either define a source or a "
|
||||
+ "constant"),
|
||||
@Diagnostic(type = ErroneousMapper1.class,
|
||||
kind = Kind.WARNING,
|
||||
line = 42,
|
||||
messageRegExp = "Unmapped target property: \"integerConstant\"")
|
||||
}
|
||||
)
|
||||
public void errorOnSourceAndConstant() throws ParseException {
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey( "187" )
|
||||
@WithClasses( {
|
||||
Source.class,
|
||||
Target.class,
|
||||
ErroneousMapper3.class,
|
||||
StringListMapper.class
|
||||
} )
|
||||
@ExpectedCompilationOutcome(
|
||||
value = CompilationResult.FAILED,
|
||||
diagnostics = {
|
||||
@Diagnostic(type = ErroneousMapper3.class,
|
||||
kind = Kind.ERROR,
|
||||
line = 42,
|
||||
messageRegExp = "Expression and constant are both defined in Mapping, either define an expression or a "
|
||||
+ "constant"),
|
||||
@Diagnostic(type = ErroneousMapper3.class,
|
||||
kind = Kind.WARNING,
|
||||
line = 42,
|
||||
messageRegExp = "Unmapped target property: \"integerConstant\"")
|
||||
}
|
||||
)
|
||||
public void errorOnConstantAndExpression() throws ParseException {
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey( "187" )
|
||||
@WithClasses( {
|
||||
Source.class,
|
||||
Target.class,
|
||||
ErroneousMapper4.class,
|
||||
StringListMapper.class
|
||||
} )
|
||||
@ExpectedCompilationOutcome(
|
||||
value = CompilationResult.FAILED,
|
||||
diagnostics = {
|
||||
@Diagnostic(type = ErroneousMapper4.class,
|
||||
kind = Kind.ERROR,
|
||||
line = 42,
|
||||
messageRegExp = "Source and expression are both defined in Mapping, either define a source or an "
|
||||
+ "expression"),
|
||||
@Diagnostic(type = ErroneousMapper4.class,
|
||||
kind = Kind.WARNING,
|
||||
line = 42,
|
||||
messageRegExp = "Unmapped target property: \"integerConstant\"")
|
||||
}
|
||||
)
|
||||
public void errorOnSourceAndExpression() throws ParseException {
|
||||
}
|
||||
|
||||
@ -121,7 +171,7 @@ public class SourceConstantsTest {
|
||||
@Diagnostic(type = ErroneousMapper2.class,
|
||||
kind = Kind.ERROR,
|
||||
line = 42,
|
||||
messageRegExp = "Either define a source or a constant in a Mapping"),
|
||||
messageRegExp = "Either define a source, a constant or an epression in a Mapping"),
|
||||
@Diagnostic(type = ErroneousMapper2.class,
|
||||
kind = Kind.WARNING,
|
||||
line = 42,
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.sourceconstants;
|
||||
package org.mapstruct.ap.test.source.constants;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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.source.expressions.java;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@WithClasses( {Source.class, Target.class, SourceTargetMapper.class, TimeAndFormat.class } )
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
public class JavaExpressionTest {
|
||||
|
||||
@Test
|
||||
public void testJavaExpressionInsertion() throws ParseException {
|
||||
Source source = new Source();
|
||||
String format = "dd-MM-yyyy,hh:mm:ss";
|
||||
Date time = getTime( format, "09-01-2014,01:35:03");
|
||||
|
||||
source.setFormat( format );
|
||||
source.setTime( time );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getTimeAndFormat().getTime() ).isEqualTo( time );
|
||||
assertThat( target.getTimeAndFormat().getFormat() ).isEqualTo( format );
|
||||
}
|
||||
|
||||
private Date getTime(String format, String date) throws ParseException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat( format );
|
||||
Date result = dateFormat.parse( date );
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 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.source.expressions.java;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Source {
|
||||
|
||||
private String format;
|
||||
private Date time;
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 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.source.expressions.java;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper
|
||||
public interface SourceTargetMapper {
|
||||
|
||||
|
||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||
|
||||
@Mapping(target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
|
||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))")
|
||||
Target sourceToTarget(Source s);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 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.source.expressions.java;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Target {
|
||||
|
||||
private TimeAndFormat timeAndFormat;
|
||||
|
||||
public TimeAndFormat getTimeAndFormat() {
|
||||
return timeAndFormat;
|
||||
}
|
||||
|
||||
public void setTimeAndFormat(TimeAndFormat timeAndFormat) {
|
||||
this.timeAndFormat = timeAndFormat;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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.source.expressions.java;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class TimeAndFormat {
|
||||
private Date time;
|
||||
private String format;
|
||||
|
||||
public TimeAndFormat( Date time, String format ) {
|
||||
this.time = time;
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String tfFormat) {
|
||||
this.format = tfFormat;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user