diff --git a/.gitignore b/.gitignore index cf4403c4e..afbd80b6e 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,8 @@ nb-configuration.xml # Build -/*/target +/**/target/ test-output -/target/ + +# Misc. +.DS_Store diff --git a/naming-strategy-test/pom.xml b/naming-strategy-test/pom.xml new file mode 100644 index 000000000..0c075b810 --- /dev/null +++ b/naming-strategy-test/pom.xml @@ -0,0 +1,45 @@ + + + + + 4.0.0 + + + org.mapstruct + mapstruct-parent + 1.0.0-SNAPSHOT + ../parent/pom.xml + + + mapstruct-naming-strategy-integrationtest-aggregator + pom + MapStruct Naming Strategy Integration Test Aggregator + + + true + + + + strategy + usage + + diff --git a/naming-strategy-test/strategy/pom.xml b/naming-strategy-test/strategy/pom.xml new file mode 100644 index 000000000..6b4a9ae7b --- /dev/null +++ b/naming-strategy-test/strategy/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + + + org.mapstruct + mapstruct-parent + 1.0.0-SNAPSHOT + ../../parent/pom.xml + + + mapstruct-naming-strategy-integrationtest-strategy + jar + + + + ${project.groupId} + mapstruct-processor + ${project.version} + provided + + + + + + + com.mycila.maven-license-plugin + maven-license-plugin + +
${basedir}/../../etc/license.txt
+ + XML_STYLE + +
+
+
+
+
diff --git a/naming-strategy-test/strategy/src/main/java/org/mapstruct/itest/naming/CustomAccessorNamingStrategy.java b/naming-strategy-test/strategy/src/main/java/org/mapstruct/itest/naming/CustomAccessorNamingStrategy.java new file mode 100644 index 000000000..ef0b8e7b7 --- /dev/null +++ b/naming-strategy-test/strategy/src/main/java/org/mapstruct/itest/naming/CustomAccessorNamingStrategy.java @@ -0,0 +1,84 @@ +/** + * Copyright 2012-2015 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.itest.naming; + +import java.beans.Introspector; + +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.type.TypeKind; + +import org.mapstruct.ap.spi.AccessorNamingStrategy; +import org.mapstruct.ap.spi.MethodType; + +/** + * A custom {@link AccessorNamingStrategy} recognizing getters in the form of {@code property()} and setters in the + * form of {@code withProperty(value)}. + * + * @author Gunnar Morling + */ +public class CustomAccessorNamingStrategy implements AccessorNamingStrategy { + + @Override + public MethodType getMethodType(ExecutableElement method) { + if ( isGetterMethod( method ) ) { + return MethodType.GETTER; + } + else if ( isSetterMethod( method ) ) { + return MethodType.SETTER; + } + else if ( isAdderMethod( method ) ) { + return MethodType.ADDER; + } + else { + return MethodType.OTHER; + } + } + + private boolean isGetterMethod(ExecutableElement method) { + return method.getReturnType().getKind() != TypeKind.VOID; + } + + public boolean isSetterMethod(ExecutableElement method) { + String methodName = method.getSimpleName().toString(); + + return methodName.startsWith( "with" ) && methodName.length() > 4; + } + + public boolean isAdderMethod(ExecutableElement method) { + String methodName = method.getSimpleName().toString(); + return methodName.startsWith( "add" ) && methodName.length() > 3; + } + + @Override + public String getPropertyName(ExecutableElement getterOrSetterMethod) { + String methodName = getterOrSetterMethod.getSimpleName().toString(); + return Introspector.decapitalize( methodName.startsWith( "with" ) ? methodName.substring( 4 ) : methodName ); + } + + @Override + public String getElementName(ExecutableElement adderMethod) { + String methodName = adderMethod.getSimpleName().toString(); + return Introspector.decapitalize( methodName.substring( 3 ) ); + } + + @Override + public String getCollectionGetterName(String property) { + return property.substring( 0, 1 ).toUpperCase() + property.substring( 1 ); + } +} diff --git a/naming-strategy-test/strategy/src/main/resources/META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy b/naming-strategy-test/strategy/src/main/resources/META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy new file mode 100644 index 000000000..967e6b57c --- /dev/null +++ b/naming-strategy-test/strategy/src/main/resources/META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy @@ -0,0 +1 @@ +org.mapstruct.itest.naming.CustomAccessorNamingStrategy diff --git a/naming-strategy-test/usage/pom.xml b/naming-strategy-test/usage/pom.xml new file mode 100644 index 000000000..2239fd480 --- /dev/null +++ b/naming-strategy-test/usage/pom.xml @@ -0,0 +1,102 @@ + + + + 4.0.0 + + + org.mapstruct + mapstruct-parent + 1.0.0-SNAPSHOT + ../../parent/pom.xml + + + mapstruct-naming-strategy-integrationtest-usage + jar + + + + ${project.groupId} + mapstruct-jdk8 + ${project.version} + provided + + + + org.easytesting + fest-assert + test + + + junit + junit + test + + + + + + + com.mycila.maven-license-plugin + maven-license-plugin + +
${basedir}/../../etc/license.txt
+ + XML_STYLE + +
+
+ + org.bsc.maven + maven-processor-plugin + + + ${project.build.directory}/generated-sources + + + org.mapstruct.ap.MappingProcessor + + + + + process + generate-sources + + process + + + + + + org.mapstruct + mapstruct-processor + ${project.version} + + + ${project.groupId} + mapstruct-naming-strategy-integrationtest-strategy + ${project.version} + + + +
+
+
diff --git a/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayer.java b/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayer.java new file mode 100644 index 000000000..38977e62b --- /dev/null +++ b/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayer.java @@ -0,0 +1,46 @@ +/** + * Copyright 2012-2015 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.itest.naming; + +/** + * @author Gunnar Morling + */ +public class GolfPlayer { + + private double handicap; + private String name; + + public double handicap() { + return handicap; + } + + public GolfPlayer withHandicap(double handicap) { + this.handicap = handicap; + return this; + } + + public String name() { + return name; + } + + public GolfPlayer withName(String name) { + this.name = name; + return this; + } +} diff --git a/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayerDto.java b/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayerDto.java new file mode 100644 index 000000000..2bbcc939b --- /dev/null +++ b/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayerDto.java @@ -0,0 +1,44 @@ +/** + * Copyright 2012-2015 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.itest.naming; + +/** + * @author Gunnar Morling + */ +public class GolfPlayerDto { + + private double handicap; + private String name; + + public double handicap() { + return handicap; + } + + public void withHandicap(double handicap) { + this.handicap = handicap; + } + + public String name() { + return name; + } + + public void withName(String name) { + this.name = name; + } +} diff --git a/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayerMapper.java b/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayerMapper.java new file mode 100644 index 000000000..2f93868b3 --- /dev/null +++ b/naming-strategy-test/usage/src/main/java/org/mapstruct/itest/naming/GolfPlayerMapper.java @@ -0,0 +1,30 @@ +/** + * Copyright 2012-2015 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.itest.naming; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface GolfPlayerMapper { + + GolfPlayerMapper INSTANCE = Mappers.getMapper( GolfPlayerMapper.class ); + + GolfPlayerDto golfPlayerToDto(GolfPlayer player); +} diff --git a/naming-strategy-test/usage/src/test/java/org/mapstruct/itest/naming/NamingTest.java b/naming-strategy-test/usage/src/test/java/org/mapstruct/itest/naming/NamingTest.java new file mode 100644 index 000000000..e44fa70ae --- /dev/null +++ b/naming-strategy-test/usage/src/test/java/org/mapstruct/itest/naming/NamingTest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2012-2015 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.itest.naming; + +import static org.fest.assertions.Assertions.assertThat; + +import org.junit.Test; +import org.mapstruct.itest.naming.GolfPlayer; +import org.mapstruct.itest.naming.GolfPlayerDto; +import org.mapstruct.itest.naming.GolfPlayerMapper; + +/** + * Test for using a custom naming strategy. + * + * @author Gunnar Morling + */ +public class NamingTest { + + @Test + public void shouldApplyCustomNamingStrategy() { + GolfPlayer player = new GolfPlayer() + .withName( "Jared" ) + .withHandicap( 9.2D ); + + GolfPlayerDto dto = GolfPlayerMapper.INSTANCE.golfPlayerToDto( player ); + + assertThat( dto ).isNotNull(); + assertThat( dto.name() ).isEqualTo( "Jared" ); + assertThat( dto.handicap() ).isEqualTo( 9.2D ); + } +} diff --git a/pom.xml b/pom.xml index 3ea91b66d..587b9eb38 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,7 @@ core-jdk8 processor integrationtest + naming-strategy-test