From a34849cb8631fb9f82d4fdf3a9fa65bc9c20c04c Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 3 Mar 2013 14:54:25 +0100 Subject: [PATCH] #7 Adding integration test --- integrationtest/pom.xml | 82 +++++++++++++++++ .../main/java/org/mapstruct/itest/Source.java | 65 ++++++++++++++ .../mapstruct/itest/SourceTargetMapper.java | 35 ++++++++ .../main/java/org/mapstruct/itest/Target.java | 65 ++++++++++++++ .../org/mapstruct/itest/ConversionTest.java | 90 +++++++++++++++++++ parent/pom.xml | 6 ++ pom.xml | 3 +- processor/pom.xml | 17 ++-- 8 files changed, 350 insertions(+), 13 deletions(-) create mode 100644 integrationtest/pom.xml create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/Source.java create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/SourceTargetMapper.java create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/Target.java create mode 100644 integrationtest/src/test/java/org/mapstruct/itest/ConversionTest.java diff --git a/integrationtest/pom.xml b/integrationtest/pom.xml new file mode 100644 index 000000000..6455bc61e --- /dev/null +++ b/integrationtest/pom.xml @@ -0,0 +1,82 @@ + + + + 4.0.0 + + + org.mapstruct + mapstruct-parent + 1.0-SNAPSHOT + ../parent/pom.xml + + + mapstruct-integrationtest + jar + MapStruct Integration Test + + + + ${project.groupId} + mapstruct + provided + + + org.testng + testng + test + + + org.easytesting + fest-assert + test + + + + + + + org.bsc.maven + maven-processor-plugin + + ${project.build.directory}/generated-sources + + org.mapstruct.ap.MappingProcessor + + + + + process + generate-sources + + process + + + + + + ${project.groupId} + mapstruct-processor + ${project.version} + + + + + + diff --git a/integrationtest/src/main/java/org/mapstruct/itest/Source.java b/integrationtest/src/main/java/org/mapstruct/itest/Source.java new file mode 100644 index 000000000..20c37bd22 --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/Source.java @@ -0,0 +1,65 @@ +/** + * Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/) + * + * 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; + +public class Source { + + private int foo; + private Long bar; + private int qax; + private Long baz; + private int zip; + + public int getFoo() { + return foo; + } + + public void setFoo(int foo) { + this.foo = foo; + } + + public Long getBar() { + return bar; + } + + public void setBar(Long bar) { + this.bar = bar; + } + + public int getQax() { + return qax; + } + + public void setQax(int qax) { + this.qax = qax; + } + + public Long getBaz() { + return baz; + } + + public void setBaz(Long baz) { + this.baz = baz; + } + + public int getZip() { + return zip; + } + + public void setZip(int zip) { + this.zip = zip; + } +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/SourceTargetMapper.java b/integrationtest/src/main/java/org/mapstruct/itest/SourceTargetMapper.java new file mode 100644 index 000000000..7c6c3317d --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/SourceTargetMapper.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/) + * + * 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; + +import org.mapstruct.Mapper; +import org.mapstruct.Mappers; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; + +@Mapper +public interface SourceTargetMapper { + + public static SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + @Mappings({ + @Mapping(source = "qax", target = "baz"), + @Mapping(source = "baz", target = "qax") + }) + Target sourceToTarget(Source source); + + Source targetToSource(Target target); +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/Target.java b/integrationtest/src/main/java/org/mapstruct/itest/Target.java new file mode 100644 index 000000000..4822b6019 --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/Target.java @@ -0,0 +1,65 @@ +/** + * Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/) + * + * 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; + +public class Target { + + private Long foo; + private int bar; + private Long baz; + private int qax; + private String zip; + + public Long getFoo() { + return foo; + } + + public void setFoo(Long foo) { + this.foo = foo; + } + + public int getBar() { + return bar; + } + + public void setBar(int bar) { + this.bar = bar; + } + + public Long getBaz() { + return baz; + } + + public void setBaz(Long baz) { + this.baz = baz; + } + + public int getQax() { + return qax; + } + + public void setQax(int qax) { + this.qax = qax; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } +} diff --git a/integrationtest/src/test/java/org/mapstruct/itest/ConversionTest.java b/integrationtest/src/test/java/org/mapstruct/itest/ConversionTest.java new file mode 100644 index 000000000..5d98581c1 --- /dev/null +++ b/integrationtest/src/test/java/org/mapstruct/itest/ConversionTest.java @@ -0,0 +1,90 @@ +/** + * Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/) + * + * 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; + +import org.testng.annotations.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class ConversionTest { + + @Test + public void shouldApplyConversions() { + Source source = new Source(); + source.setFoo( 42 ); + source.setBar( 23L ); + source.setZip( 73 ); + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) ); + assertThat( target.getBar() ).isEqualTo( 23 ); + assertThat( target.getZip() ).isEqualTo( "73" ); + } + + @Test + public void shouldHandleNulls() { + Source source = new Source(); + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 0 ) ); + assertThat( target.getBar() ).isEqualTo( 0 ); + assertThat( target.getZip() ).isEqualTo( "0" ); + } + + @Test + public void shouldApplyConversionsToMappedProperties() { + Source source = new Source(); + source.setQax( 42 ); + source.setBaz( 23L ); + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getBaz() ).isEqualTo( Long.valueOf( 42 ) ); + assertThat( target.getQax() ).isEqualTo( 23 ); + } + + @Test + public void shouldApplyConversionsForReverseMapping() { + Target target = new Target(); + target.setFoo( 42L ); + target.setBar( 23 ); + target.setZip( "73" ); + + Source source = SourceTargetMapper.INSTANCE.targetToSource( target ); + + assertThat( source ).isNotNull(); + assertThat( source.getFoo() ).isEqualTo( 42 ); + assertThat( source.getBar() ).isEqualTo( 23 ); + assertThat( source.getZip() ).isEqualTo( 73 ); + } + + @Test + public void shouldApplyConversionsToMappedPropertiesForReverseMapping() { + Target target = new Target(); + target.setQax( 42 ); + target.setBaz( 23L ); + + Source source = SourceTargetMapper.INSTANCE.targetToSource( target ); + + assertThat( source ).isNotNull(); + assertThat( source.getBaz() ).isEqualTo( 42 ); + assertThat( source.getQax() ).isEqualTo( 23 ); + } +} diff --git a/parent/pom.xml b/parent/pom.xml index d01a3e72a..b1d894bb3 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -60,6 +60,11 @@ mapstruct ${project.version} + + ${project.groupId} + mapstruct-processor + ${project.version} + @@ -73,6 +78,7 @@ 1.6 1.6 + -proc:none diff --git a/pom.xml b/pom.xml index c59b9e0b0..e3df3b655 100644 --- a/pom.xml +++ b/pom.xml @@ -36,5 +36,6 @@ parent core processor - + integrationtest + diff --git a/processor/pom.xml b/processor/pom.xml index 154bcd497..ee1f93bac 100644 --- a/processor/pom.xml +++ b/processor/pom.xml @@ -45,6 +45,11 @@ hickory provided + + ${project.groupId} + mapstruct + provided + org.testng testng @@ -55,11 +60,6 @@ fest-assert test - - ${project.groupId} - mapstruct - - @@ -81,13 +81,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - - -proc:none - - org.bsc.maven maven-processor-plugin