diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java index 28bc2f9bd..d0c6dcb63 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java @@ -151,6 +151,7 @@ public class Strings { */ public static String getSaveVariableName(String name, Collection existingVariableNames) { name = decapitalize( sanitizeIdentifierName( name ) ); + name = joinAndCamelize( extractParts( name ) ); Set conflictingNames = new HashSet( KEYWORDS ); conflictingNames.addAll( existingVariableNames ); @@ -169,4 +170,16 @@ public class Strings { public static String sanitizeIdentifierName(String identifier) { return identifier.replace( "[]", "Array" ); } + + /** + * It removes the dots from the name and creates an {@link Iterable} from them. + * + * E.q. for the name {@code props.font} it will return an {@link Iterable} containing the {@code props} and + * {@code font} + * @param name the name that needs to be parsed into parts + * @return an {@link Iterable} containing all the parts of the name. + */ + static Iterable extractParts(String name) { + return Arrays.asList( name.split( "\\." ) ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java b/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java new file mode 100644 index 000000000..2bf21b5e6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java @@ -0,0 +1,99 @@ +/** + * Copyright 2012-2016 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.internal.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.Test; + +/** + * @author Filip Hrisafov + */ +public class StringsTest { + @Test + public void testCapitalize() throws Exception { + assertThat( Strings.capitalize( null ) ).isNull(); + assertThat( Strings.capitalize( "c" ) ).isEqualTo( "C" ); + assertThat( Strings.capitalize( "capitalize" ) ).isEqualTo( "Capitalize" ); + assertThat( Strings.capitalize( "AlreadyCapitalized" ) ).isEqualTo( "AlreadyCapitalized" ); + assertThat( Strings.capitalize( "notCapitalized" ) ).isEqualTo( "NotCapitalized" ); + } + + @Test + public void testDecapitalize() throws Exception { + assertThat( Strings.decapitalize( null ) ).isNull(); + assertThat( Strings.decapitalize( "c" ) ).isEqualTo( "c" ); + assertThat( Strings.decapitalize( "capitalize" ) ).isEqualTo( "capitalize" ); + assertThat( Strings.decapitalize( "AlreadyCapitalized" ) ).isEqualTo( "alreadyCapitalized" ); + assertThat( Strings.decapitalize( "notCapitalized" ) ).isEqualTo( "notCapitalized" ); + } + + @Test + public void testJoin() throws Exception { + assertThat( Strings.join( new ArrayList(), "-" ) ).isEqualTo( "" ); + assertThat( Strings.join( Arrays.asList( "Hello", "World" ), "-" ) ).isEqualTo( "Hello-World" ); + assertThat( Strings.join( Arrays.asList( "Hello" ), "-" ) ).isEqualTo( "Hello" ); + } + + @Test + public void testJoinAndCamelize() throws Exception { + assertThat( Strings.joinAndCamelize( new ArrayList() ) ).isEqualTo( "" ); + assertThat( Strings.joinAndCamelize( Arrays.asList( "Hello", "World" ) ) ).isEqualTo( "HelloWorld" ); + assertThat( Strings.joinAndCamelize( Arrays.asList( "Hello", "world" ) ) ).isEqualTo( "HelloWorld" ); + assertThat( Strings.joinAndCamelize( Arrays.asList( "hello", "world" ) ) ).isEqualTo( "helloWorld" ); + } + + @Test + public void testIsEmpty() throws Exception { + assertThat( Strings.isEmpty( null ) ).isTrue(); + assertThat( Strings.isEmpty( "" ) ).isTrue(); + assertThat( Strings.isEmpty( " " ) ).isFalse(); + assertThat( Strings.isEmpty( "not empty" ) ).isFalse(); + } + + @Test + public void testGetSaveVariableNameWithArrayExistingVariables() throws Exception { + assertThat( Strings.getSaveVariableName( "int[]" ) ).isEqualTo( "intArray" ); + assertThat( Strings.getSaveVariableName( "Extends" ) ).isEqualTo( "extends_" ); + assertThat( Strings.getSaveVariableName( "class" ) ).isEqualTo( "class_" ); + assertThat( Strings.getSaveVariableName( "Class" ) ).isEqualTo( "class_" ); + assertThat( Strings.getSaveVariableName( "Case" ) ).isEqualTo( "case_" ); + assertThat( Strings.getSaveVariableName( "Synchronized" ) ).isEqualTo( "synchronized_" ); + assertThat( Strings.getSaveVariableName( "prop", "prop", "prop_" ) ).isEqualTo( "prop__" ); + } + + @Test + public void testGetSaveVariableNameWithCollection() throws Exception { + assertThat( Strings.getSaveVariableName( "int[]", new ArrayList() ) ).isEqualTo( "intArray" ); + assertThat( Strings.getSaveVariableName( "Extends", new ArrayList() ) ).isEqualTo( "extends_" ); + assertThat( Strings.getSaveVariableName( "prop", Arrays.asList( "prop", "prop_" ) ) ).isEqualTo( "prop__" ); + assertThat( Strings.getSaveVariableName( "prop.font", Arrays.asList( "propFont", "propFont_" ) ) ) + .isEqualTo( "propFont__" ); + } + + @Test + public void testSanitizeIdentifierName() throws Exception { + assertThat( Strings.sanitizeIdentifierName( "test" ) ).isEqualTo( "test" ); + assertThat( Strings.sanitizeIdentifierName( "int[]" ) ).isEqualTo( "intArray" ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedproperties/simple/SimpleMapper.java b/processor/src/test/java/org/mapstruct/ap/test/nestedproperties/simple/SimpleMapper.java index 7934760b7..b018eca5f 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedproperties/simple/SimpleMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedproperties/simple/SimpleMapper.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.test.nestedproperties.simple; +import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; @@ -42,4 +43,7 @@ public interface SimpleMapper { @Mapping( target = "stringValue", source = "props.stringValue" ) } ) TargetObject toTargetObject(SourceRoot sourceRoot); + @InheritInverseConfiguration + SourceRoot toSourceRoot(TargetObject targetObject); + }