From ec6913618ea1e5c92e4e092beb0f234be1fc293e Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Wed, 4 Jan 2017 09:32:02 +0100 Subject: [PATCH] #1007 Add additional generated code tests --- ...ssue913SetterMapperForCollectionsTest.java | 14 +- .../ap/test/collection/adder/AdderTest.java | 18 +- .../string/StringConversionTest.java | 10 +- .../test/updatemethods/UpdateMethodsTest.java | 13 +- .../selection/ExternalSelectionTest.java | 16 +- .../DomainDtoWithNcvsAlwaysMapperImpl.java | 252 ++++++++++++++++ .../DomainDtoWithNvmsDefaultMapperImpl.java | 273 +++++++++++++++++ .../_913/DomainDtoWithNvmsNullMapperImpl.java | 276 ++++++++++++++++++ .../DomainDtoWithPresenceCheckMapperImpl.java | 252 ++++++++++++++++ .../adder/Source2Target2MapperImpl.java | 49 ++++ .../adder/SourceTargetMapperImpl.java | 213 ++++++++++++++ ...SourceTargetMapperStrategyDefaultImpl.java | 55 ++++ ...rgetMapperStrategySetterPreferredImpl.java | 55 ++++ .../string/SourceTargetMapperImpl.java | 134 +++++++++ .../test/updatemethods/CompanyMapperImpl.java | 71 +++++ .../updatemethods/OrganizationMapperImpl.java | 98 +++++++ .../selection/DepartmentMapperImpl.java | 82 ++++++ .../selection/ExternalMapperImpl.java | 40 +++ .../selection/OrganizationMapper1Impl.java | 54 ++++ .../selection/OrganizationMapper2Impl.java | 53 ++++ .../selection/OrganizationMapper3Impl.java | 53 ++++ 21 files changed, 2070 insertions(+), 11 deletions(-) create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/Source2Target2MapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategyDefaultImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategySetterPreferredImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/string/SourceTargetMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/CompanyMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/OrganizationMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/DepartmentMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/ExternalMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper1Impl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper2Impl.java create mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3Impl.java diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_913/Issue913SetterMapperForCollectionsTest.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_913/Issue913SetterMapperForCollectionsTest.java index 20b9df33b..66c4194ef 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bugs/_913/Issue913SetterMapperForCollectionsTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_913/Issue913SetterMapperForCollectionsTest.java @@ -20,12 +20,16 @@ package org.mapstruct.ap.test.bugs._913; import java.util.HashSet; import java.util.Set; -import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; 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; + +import static org.assertj.core.api.Assertions.assertThat; /** * All these test cases test the possible combinations in the SetterMapperForCollections. @@ -47,6 +51,14 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @IssueKey( "913" ) public class Issue913SetterMapperForCollectionsTest { + @Rule + public final GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor( + DomainDtoWithNvmsNullMapper.class, + DomainDtoWithNvmsDefaultMapper.class, + DomainDtoWithPresenceCheckMapper.class, + DomainDtoWithNcvsAlwaysMapper.class + ); + /** * The null value mapping strategy on type level (Mapper) should generate forged methods for the * conversion from string to long that return null in the entire mapper, so also for the forged diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java index 1f74d5ec0..36c06bde8 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java @@ -18,13 +18,10 @@ */ package org.mapstruct.ap.test.collection.adder; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.util.ArrayList; import java.util.Arrays; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.collection.adder._target.AdderUsageObserver; @@ -46,6 +43,11 @@ import org.mapstruct.ap.test.collection.adder.source.SourceTeeth; 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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Sjaak Derksen @@ -75,6 +77,13 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @RunWith(AnnotationProcessorTestRunner.class) public class AdderTest { + @Rule + public final GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor( + SourceTargetMapper.class, + SourceTargetMapperStrategyDefault.class, + SourceTargetMapperStrategySetterPreferred.class + ); + @IssueKey("241") @Test public void testAdd() throws DogException { @@ -252,6 +261,7 @@ public class AdderTest { Foo.class } ) public void testMissingImport() throws DogException { + generatedSource.addComparisonToFixtureFor( Source2Target2Mapper.class ); Source2 source = new Source2(); source.setAttributes( Arrays.asList( new Foo() ) ); diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/string/StringConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/string/StringConversionTest.java index 4624e8a80..b68b31378 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/conversion/string/StringConversionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/string/StringConversionTest.java @@ -18,13 +18,15 @@ */ package org.mapstruct.ap.test.conversion.string; -import static org.assertj.core.api.Assertions.assertThat; - +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; 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; + +import static org.assertj.core.api.Assertions.assertThat; @WithClasses({ Source.class, @@ -36,6 +38,10 @@ public class StringConversionTest { private static final String STRING_CONSTANT = "String constant"; + @Rule + public final GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor( + SourceTargetMapper.class ); + @Test public void shouldApplyStringConversions() { Source source = new Source(); diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/UpdateMethodsTest.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/UpdateMethodsTest.java index b70372319..ca7ceaec4 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/UpdateMethodsTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/UpdateMethodsTest.java @@ -18,8 +18,7 @@ */ package org.mapstruct.ap.test.updatemethods; -import static org.assertj.core.api.Assertions.assertThat; - +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.testutil.IssueKey; @@ -28,6 +27,9 @@ import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult; import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic; import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; +import org.mapstruct.ap.testutil.runner.GeneratedSource; + +import static org.assertj.core.api.Assertions.assertThat; /** * @@ -52,11 +54,15 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; }) public class UpdateMethodsTest { + @Rule + public final GeneratedSource generatedSource = new GeneratedSource(); + @Test @WithClasses( { OrganizationMapper.class } ) public void testPreferUpdateMethod() { + generatedSource.addComparisonToFixtureFor( OrganizationMapper.class ); OrganizationEntity organizationEntity = new OrganizationEntity(); CompanyEntity companyEntity = new CompanyEntity(); @@ -85,6 +91,7 @@ public class UpdateMethodsTest { OrganizationMapper.class } ) public void testUpdateMethodClearsExistingValues() { + generatedSource.addComparisonToFixtureFor( OrganizationMapper.class ); OrganizationEntity organizationEntity = new OrganizationEntity(); CompanyEntity companyEntity = new CompanyEntity(); @@ -109,6 +116,7 @@ public class UpdateMethodsTest { OrganizationMapper.class }) public void testPreferUpdateMethodSourceObjectNotDefined() { + generatedSource.addComparisonToFixtureFor( OrganizationMapper.class ); OrganizationEntity organizationEntity = new OrganizationEntity(); @@ -134,6 +142,7 @@ public class UpdateMethodsTest { DepartmentInBetween.class } ) public void testPreferUpdateMethodEncapsulatingCreateMethod() { + generatedSource.addComparisonToFixtureFor( CompanyMapper.class ); CompanyEntity companyEntity = new CompanyEntity(); diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java index ffd247ab1..eced906a9 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java @@ -18,12 +18,11 @@ */ package org.mapstruct.ap.test.updatemethods.selection; -import static org.assertj.core.api.Assertions.assertThat; - import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.updatemethods.BossDto; @@ -41,6 +40,9 @@ import org.mapstruct.ap.test.updatemethods.SecretaryEntity; 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; + +import static org.assertj.core.api.Assertions.assertThat; /** * @@ -68,11 +70,19 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; }) public class ExternalSelectionTest { + @Rule + public final GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor( + OrganizationMapper1.class, + ExternalMapper.class, + DepartmentMapper.class + ); + @Test @WithClasses({ OrganizationMapper1.class }) public void shouldSelectGeneratedExternalMapper() { + generatedSource.addComparisonToFixtureFor( OrganizationMapper1.class ); CompanyEntity entity = new CompanyEntity(); CompanyDto dto = new CompanyDto(); @@ -85,6 +95,7 @@ public class ExternalSelectionTest { }) @IssueKey("604") public void shouldSelectGeneratedExternalMapperWithImportForPropertyType() { + generatedSource.addComparisonToFixtureFor( OrganizationMapper3.class ); BossEntity entity = new BossEntity(); BossDto dto = new BossDto(); @@ -96,6 +107,7 @@ public class ExternalSelectionTest { OrganizationMapper2.class }) public void shouldSelectGeneratedHandWrittenExternalMapper() { + generatedSource.addComparisonToFixtureFor( OrganizationMapper2.class ); CompanyEntity entity = new CompanyEntity(); CompanyDto dto = new CompanyDto(); diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java new file mode 100644 index 000000000..f144faa2d --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java @@ -0,0 +1,252 @@ +/** + * 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.test.bugs._913; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:07:28+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class DomainDtoWithNcvsAlwaysMapperImpl implements DomainDtoWithNcvsAlwaysMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(DtoWithPresenceCheck source) { + if ( source == null ) { + return null; + } + + Domain domain = new Domain(); + + if ( source.hasStringsInitialized() ) { + Set set = stringListToLongSet( source.getStringsInitialized() ); + domain.setLongsInitialized( set ); + } + if ( source.hasStrings() ) { + Set set_ = stringListToLongSet( source.getStrings() ); + domain.setLongs( set_ ); + } + if ( source.hasStrings() ) { + List list = source.getStrings(); + domain.setStrings( new HashSet( list ) + ); + } + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + domain.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + domain.setStringsInitialized( new HashSet( list__ ) + ); + } + + return domain; + } + + @Override + public void update(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return; + } + + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + } + else { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + } + + @Override + public Domain updateWithReturn(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return null; + } + + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + } + else { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return null; + } + + Set set = new HashSet(); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java new file mode 100644 index 000000000..d6ba35d85 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java @@ -0,0 +1,273 @@ +/** + * 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.test.bugs._913; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:07:27+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class DomainDtoWithNvmsDefaultMapperImpl implements DomainDtoWithNvmsDefaultMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(Dto source) { + + Domain domain = new Domain(); + + if ( source != null ) { + Set set = stringListToLongSet( source.getStringsInitialized() ); + if ( set != null ) { + domain.setLongsInitialized( set ); + } + Set set_ = stringListToLongSet( source.getStrings() ); + if ( set_ != null ) { + domain.setLongs( set_ ); + } + List list = source.getStrings(); + if ( list != null ) { + domain.setStrings( new HashSet( list ) + ); + } + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + domain.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + domain.setStringsInitialized( new HashSet( list__ ) + ); + } + } + + return domain; + } + + @Override + public void update(Dto source, Domain target) { + + if ( source != null ) { + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( null ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( null ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + else { + target.setLongsInitialized( null ); + } + } + else { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + else { + target.setStringsInitialized( null ); + } + } + else { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + } + } + + @Override + public Domain updateWithReturn(Dto source, Domain target) { + + if ( source != null ) { + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( null ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( null ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + else { + target.setLongsInitialized( null ); + } + } + else { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + else { + target.setStringsInitialized( null ); + } + } + else { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return new HashSet(); + } + + Set set = new HashSet(); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java new file mode 100644 index 000000000..cc67c73c3 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java @@ -0,0 +1,276 @@ +/** + * 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.test.bugs._913; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:07:28+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class DomainDtoWithNvmsNullMapperImpl implements DomainDtoWithNvmsNullMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(Dto source) { + if ( source == null ) { + return null; + } + + Domain domain = new Domain(); + + Set set = stringListToLongSet( source.getStringsInitialized() ); + if ( set != null ) { + domain.setLongsInitialized( set ); + } + Set set_ = stringListToLongSet( source.getStrings() ); + if ( set_ != null ) { + domain.setLongs( set_ ); + } + List list = source.getStrings(); + if ( list != null ) { + domain.setStrings( new HashSet( list ) + ); + } + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + domain.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + domain.setStringsInitialized( new HashSet( list__ ) + ); + } + + return domain; + } + + @Override + public void update(Dto source, Domain target) { + if ( source == null ) { + return; + } + + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( null ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( null ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + else { + target.setLongsInitialized( null ); + } + } + else { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + else { + target.setStringsInitialized( null ); + } + } + else { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + } + + @Override + public Domain updateWithReturn(Dto source, Domain target) { + if ( source == null ) { + return null; + } + + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( null ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( null ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + else { + target.setLongsInitialized( null ); + } + } + else { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + if ( set_ != null ) { + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list_ = source.getStringsWithDefault(); + if ( list_ != null ) { + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + else { + target.setStringsInitialized( null ); + } + } + else { + List list__ = source.getStringsInitialized(); + if ( list__ != null ) { + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return null; + } + + Set set = new HashSet(); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java new file mode 100644 index 000000000..9f38733cd --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java @@ -0,0 +1,252 @@ +/** + * 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.test.bugs._913; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:07:28+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class DomainDtoWithPresenceCheckMapperImpl implements DomainDtoWithPresenceCheckMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(DtoWithPresenceCheck source) { + if ( source == null ) { + return null; + } + + Domain domain = new Domain(); + + if ( source.hasStringsInitialized() ) { + Set set = stringListToLongSet( source.getStringsInitialized() ); + domain.setLongsInitialized( set ); + } + if ( source.hasStrings() ) { + Set set_ = stringListToLongSet( source.getStrings() ); + domain.setLongs( set_ ); + } + if ( source.hasStrings() ) { + List list = source.getStrings(); + domain.setStrings( new HashSet( list ) + ); + } + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + domain.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + domain.setStringsInitialized( new HashSet( list__ ) + ); + } + + return domain; + } + + @Override + public void update(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return; + } + + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + } + else { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + } + + @Override + public Domain updateWithReturn(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return null; + } + + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + } + else { + if ( source.hasStrings() ) { + Set set = stringListToLongSet( source.getStrings() ); + target.setLongs( set ); + } + } + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new HashSet( list ) + ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set_ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + Set set_ = stringListToLongSet( source.getStringsInitialized() ); + target.setLongsInitialized( set_ ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list_ ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list_ = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list_ ) + ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list__ ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list__ = source.getStringsInitialized(); + target.setStringsInitialized( new HashSet( list__ ) + ); + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return null; + } + + Set set = new HashSet(); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/Source2Target2MapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/Source2Target2MapperImpl.java new file mode 100644 index 000000000..efafeedf3 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/Source2Target2MapperImpl.java @@ -0,0 +1,49 @@ +/** + * 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.test.collection.adder; + +import javax.annotation.Generated; +import org.mapstruct.ap.test.collection.adder._target.Target2; +import org.mapstruct.ap.test.collection.adder.source.Foo; +import org.mapstruct.ap.test.collection.adder.source.Source2; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:10:39+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class Source2Target2MapperImpl extends Source2Target2Mapper { + + @Override + public Target2 toTarget(Source2 source) { + if ( source == null ) { + return null; + } + + Target2 target2 = new Target2(); + + if ( source.getAttributes() != null ) { + for ( Foo attribute : source.getAttributes() ) { + target2.addAttribute( attribute ); + } + } + + return target2; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperImpl.java new file mode 100644 index 000000000..c64697535 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperImpl.java @@ -0,0 +1,213 @@ +/** + * 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.test.collection.adder; + +import java.util.List; +import javax.annotation.Generated; +import org.mapstruct.ap.test.collection.adder._target.IndoorPet; +import org.mapstruct.ap.test.collection.adder._target.Target; +import org.mapstruct.ap.test.collection.adder._target.TargetDali; +import org.mapstruct.ap.test.collection.adder._target.TargetHuman; +import org.mapstruct.ap.test.collection.adder._target.TargetOnlyGetter; +import org.mapstruct.ap.test.collection.adder._target.TargetViaTargetType; +import org.mapstruct.ap.test.collection.adder.source.SingleElementSource; +import org.mapstruct.ap.test.collection.adder.source.Source; +import org.mapstruct.ap.test.collection.adder.source.SourceTeeth; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:10:39+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class SourceTargetMapperImpl implements SourceTargetMapper { + + private final PetMapper petMapper = new PetMapper(); + private final TeethMapper teethMapper = new TeethMapper(); + + @Override + public Target toTarget(Source source) throws DogException { + if ( source == null ) { + return null; + } + + Target target = new Target(); + + if ( source.getPets() != null ) { + try { + for ( String pet : source.getPets() ) { + target.addPet( petMapper.toPet( pet ) ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + } + + return target; + } + + @Override + public Source toSource(Target source) { + if ( source == null ) { + return null; + } + + Source source_ = new Source(); + + try { + List list = petMapper.toSourcePets( source.getPets() ); + if ( list != null ) { + source_.setPets( list ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + catch ( DogException e ) { + throw new RuntimeException( e ); + } + + return source_; + } + + @Override + public void toExistingTarget(Source source, Target target) { + if ( source == null ) { + return; + } + + if ( source.getPets() != null ) { + try { + for ( String pet : source.getPets() ) { + target.addPet( petMapper.toPet( pet ) ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + catch ( DogException e ) { + throw new RuntimeException( e ); + } + } + } + + @Override + public TargetDali toTargetDali(SourceTeeth source) { + if ( source == null ) { + return null; + } + + TargetDali targetDali = new TargetDali(); + + if ( source.getTeeth() != null ) { + for ( String teeth : source.getTeeth() ) { + targetDali.addTeeth( teethMapper.toTooth( teeth ) ); + } + } + + return targetDali; + } + + @Override + public TargetHuman toTargetHuman(SourceTeeth source) { + if ( source == null ) { + return null; + } + + TargetHuman targetHuman = new TargetHuman(); + + if ( source.getTeeth() != null ) { + for ( String tooth : source.getTeeth() ) { + targetHuman.addTooth( teethMapper.toTooth( tooth ) ); + } + } + + return targetHuman; + } + + @Override + public TargetOnlyGetter toTargetOnlyGetter(Source source) throws DogException { + if ( source == null ) { + return null; + } + + TargetOnlyGetter targetOnlyGetter = new TargetOnlyGetter(); + + if ( source.getPets() != null ) { + try { + for ( String pet : source.getPets() ) { + targetOnlyGetter.addPet( petMapper.toPet( pet ) ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + } + + return targetOnlyGetter; + } + + @Override + public TargetViaTargetType toTargetViaTargetType(Source source) { + if ( source == null ) { + return null; + } + + TargetViaTargetType targetViaTargetType = new TargetViaTargetType(); + + if ( source.getPets() != null ) { + try { + for ( String pet : source.getPets() ) { + targetViaTargetType.addPet( petMapper.toPet( pet, IndoorPet.class ) ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + catch ( DogException e ) { + throw new RuntimeException( e ); + } + } + + return targetViaTargetType; + } + + @Override + public Target fromSingleElementSource(SingleElementSource source) { + if ( source == null ) { + return null; + } + + Target target = new Target(); + + if ( source.getPet() != null ) { + try { + target.addPet( petMapper.toPet( source.getPet() ) ); + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + catch ( DogException e ) { + throw new RuntimeException( e ); + } + } + + return target; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategyDefaultImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategyDefaultImpl.java new file mode 100644 index 000000000..2926f33e4 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategyDefaultImpl.java @@ -0,0 +1,55 @@ +/** + * 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.test.collection.adder; + +import java.util.List; +import javax.annotation.Generated; +import org.mapstruct.ap.test.collection.adder._target.Target; +import org.mapstruct.ap.test.collection.adder.source.Source; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:10:39+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class SourceTargetMapperStrategyDefaultImpl implements SourceTargetMapperStrategyDefault { + + private final PetMapper petMapper = new PetMapper(); + + @Override + public Target shouldFallBackToAdder(Source source) throws DogException { + if ( source == null ) { + return null; + } + + Target target = new Target(); + + try { + List list = petMapper.toPets( source.getPets() ); + if ( list != null ) { + target.setPets( list ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + + return target; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategySetterPreferredImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategySetterPreferredImpl.java new file mode 100644 index 000000000..83fce867a --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperStrategySetterPreferredImpl.java @@ -0,0 +1,55 @@ +/** + * 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.test.collection.adder; + +import javax.annotation.Generated; +import org.mapstruct.ap.test.collection.adder._target.TargetWithoutSetter; +import org.mapstruct.ap.test.collection.adder.source.Source; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:10:39+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class SourceTargetMapperStrategySetterPreferredImpl implements SourceTargetMapperStrategySetterPreferred { + + private final PetMapper petMapper = new PetMapper(); + + @Override + public TargetWithoutSetter toTargetDontUseAdder(Source source) throws DogException { + if ( source == null ) { + return null; + } + + TargetWithoutSetter targetWithoutSetter = new TargetWithoutSetter(); + + if ( source.getPets() != null ) { + try { + for ( String pet : source.getPets() ) { + targetWithoutSetter.addPet( petMapper.toPet( pet ) ); + } + } + catch ( CatException e ) { + throw new RuntimeException( e ); + } + } + + return targetWithoutSetter; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/string/SourceTargetMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/string/SourceTargetMapperImpl.java new file mode 100644 index 000000000..308104644 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/string/SourceTargetMapperImpl.java @@ -0,0 +1,134 @@ +/** + * 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.test.conversion.string; + +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:22:52+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class SourceTargetMapperImpl implements SourceTargetMapper { + + @Override + public Target sourceToTarget(Source source) { + if ( source == null ) { + return null; + } + + Target target = new Target(); + + target.setB( String.valueOf( source.getB() ) ); + if ( source.getBb() != null ) { + target.setBb( String.valueOf( source.getBb() ) ); + } + target.setS( String.valueOf( source.getS() ) ); + if ( source.getSs() != null ) { + target.setSs( String.valueOf( source.getSs() ) ); + } + target.setI( String.valueOf( source.getI() ) ); + if ( source.getIi() != null ) { + target.setIi( String.valueOf( source.getIi() ) ); + } + target.setL( String.valueOf( source.getL() ) ); + if ( source.getLl() != null ) { + target.setLl( String.valueOf( source.getLl() ) ); + } + target.setF( String.valueOf( source.getF() ) ); + if ( source.getFf() != null ) { + target.setFf( String.valueOf( source.getFf() ) ); + } + target.setD( String.valueOf( source.getD() ) ); + if ( source.getDd() != null ) { + target.setDd( String.valueOf( source.getDd() ) ); + } + target.setBool( String.valueOf( source.getBool() ) ); + if ( source.getBoolBool() != null ) { + target.setBoolBool( String.valueOf( source.getBoolBool() ) ); + } + target.setC( String.valueOf( source.getC() ) ); + if ( source.getCc() != null ) { + target.setCc( source.getCc().toString() ); + } + + return target; + } + + @Override + public Source targetToSource(Target target) { + if ( target == null ) { + return null; + } + + Source source = new Source(); + + if ( target.getB() != null ) { + source.setB( Byte.parseByte( target.getB() ) ); + } + if ( target.getBb() != null ) { + source.setBb( Byte.parseByte( target.getBb() ) ); + } + if ( target.getS() != null ) { + source.setS( Short.parseShort( target.getS() ) ); + } + if ( target.getSs() != null ) { + source.setSs( Short.parseShort( target.getSs() ) ); + } + if ( target.getI() != null ) { + source.setI( Integer.parseInt( target.getI() ) ); + } + if ( target.getIi() != null ) { + source.setIi( Integer.parseInt( target.getIi() ) ); + } + if ( target.getL() != null ) { + source.setL( Long.parseLong( target.getL() ) ); + } + if ( target.getLl() != null ) { + source.setLl( Long.parseLong( target.getLl() ) ); + } + if ( target.getF() != null ) { + source.setF( Float.parseFloat( target.getF() ) ); + } + if ( target.getFf() != null ) { + source.setFf( Float.parseFloat( target.getFf() ) ); + } + if ( target.getD() != null ) { + source.setD( Double.parseDouble( target.getD() ) ); + } + if ( target.getDd() != null ) { + source.setDd( Double.parseDouble( target.getDd() ) ); + } + if ( target.getBool() != null ) { + source.setBool( Boolean.parseBoolean( target.getBool() ) ); + } + if ( target.getBoolBool() != null ) { + source.setBoolBool( Boolean.parseBoolean( target.getBoolBool() ) ); + } + if ( target.getC() != null ) { + source.setC( target.getC().charAt( 0 ) ); + } + if ( target.getCc() != null ) { + source.setCc( target.getCc().charAt( 0 ) ); + } + source.setObject( target.getObject() ); + + return source; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/CompanyMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/CompanyMapperImpl.java new file mode 100644 index 000000000..d1e7159c6 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/CompanyMapperImpl.java @@ -0,0 +1,71 @@ +/** + * 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.test.updatemethods; + +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:11:46+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class CompanyMapperImpl implements CompanyMapper { + + private final DepartmentEntityFactory departmentEntityFactory = new DepartmentEntityFactory(); + + @Override + public void toCompanyEntity(CompanyDto dto, CompanyEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + if ( dto.getDepartment() != null ) { + if ( entity.getDepartment() == null ) { + entity.setDepartment( departmentEntityFactory.createDepartmentEntity() ); + } + toDepartmentEntity( toInBetween( dto.getDepartment() ), entity.getDepartment() ); + } + else { + entity.setDepartment( null ); + } + } + + @Override + public DepartmentInBetween toInBetween(DepartmentDto dto) { + if ( dto == null ) { + return null; + } + + DepartmentInBetween departmentInBetween_ = new DepartmentInBetween(); + + departmentInBetween_.setName( dto.getName() ); + + return departmentInBetween_; + } + + @Override + public void toDepartmentEntity(DepartmentInBetween dto, DepartmentEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/OrganizationMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/OrganizationMapperImpl.java new file mode 100644 index 000000000..d2c64af1d --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/OrganizationMapperImpl.java @@ -0,0 +1,98 @@ +/** + * 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.test.updatemethods; + +import javax.annotation.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:11:45+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class OrganizationMapperImpl implements OrganizationMapper { + + private final DepartmentEntityFactory departmentEntityFactory = new DepartmentEntityFactory(); + + @Override + public void toOrganizationEntity(OrganizationDto dto, OrganizationEntity entity) { + if ( dto == null ) { + return; + } + + if ( dto.getCompany() != null ) { + if ( entity.getCompany() == null ) { + entity.setCompany( new CompanyEntity() ); + } + toCompanyEntity( dto.getCompany(), entity.getCompany() ); + } + else { + entity.setCompany( null ); + } + + if ( entity.getType() == null ) { + entity.setType( new OrganizationTypeEntity() ); + } + toName( "commercial", entity.getType() ); + if ( entity.getTypeNr() == null ) { + entity.setTypeNr( new OrganizationTypeNrEntity() ); + } + toNumber( Integer.parseInt( "5" ), entity.getTypeNr() ); + } + + @Override + public void toCompanyEntity(CompanyDto dto, CompanyEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + entity.setDepartment( toDepartmentEntity( dto.getDepartment() ) ); + } + + @Override + public DepartmentEntity toDepartmentEntity(DepartmentDto dto) { + if ( dto == null ) { + return null; + } + + DepartmentEntity departmentEntity_ = departmentEntityFactory.createDepartmentEntity(); + + departmentEntity_.setName( dto.getName() ); + + return departmentEntity_; + } + + @Override + public void toName(String type, OrganizationTypeEntity entity) { + if ( type == null ) { + return; + } + + entity.setType( type ); + } + + @Override + public void toNumber(Integer number, OrganizationTypeNrEntity entity) { + if ( number == null ) { + return; + } + + entity.setNumber( number ); + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/DepartmentMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/DepartmentMapperImpl.java new file mode 100644 index 000000000..2150b37bb --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/DepartmentMapperImpl.java @@ -0,0 +1,82 @@ +/** + * 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.test.updatemethods.selection; + +import java.util.ArrayList; +import java.util.HashMap; +import javax.annotation.Generated; +import org.mapstruct.ap.test.updatemethods.DepartmentDto; +import org.mapstruct.ap.test.updatemethods.DepartmentEntity; +import org.mapstruct.ap.test.updatemethods.EmployeeDto; +import org.mapstruct.ap.test.updatemethods.EmployeeEntity; +import org.mapstruct.ap.test.updatemethods.SecretaryDto; +import org.mapstruct.ap.test.updatemethods.SecretaryEntity; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:17:50+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class DepartmentMapperImpl implements DepartmentMapper { + + private final ExternalHandWrittenMapper externalHandWrittenMapper = new ExternalHandWrittenMapper(); + + @Override + public void toDepartmentEntity(DepartmentDto dto, DepartmentEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + if ( entity.getEmployees() == null ) { + entity.setEmployees( new ArrayList() ); + } + externalHandWrittenMapper.toEmployeeEntityList( dto.getEmployees(), entity.getEmployees() ); + if ( entity.getSecretaryToEmployee() == null ) { + entity.setSecretaryToEmployee( new HashMap() ); + } + externalHandWrittenMapper.toSecretaryEmployeeEntityMap( dto.getSecretaryToEmployee(), entity.getSecretaryToEmployee() ); + } + + @Override + public EmployeeEntity toEmployeeEntity(EmployeeDto dto) { + if ( dto == null ) { + return null; + } + + EmployeeEntity employeeEntity = new EmployeeEntity(); + + employeeEntity.setName( dto.getName() ); + + return employeeEntity; + } + + @Override + public SecretaryEntity toSecretaryEntity(SecretaryDto dto) { + if ( dto == null ) { + return null; + } + + SecretaryEntity secretaryEntity = new SecretaryEntity(); + + secretaryEntity.setName( dto.getName() ); + + return secretaryEntity; + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/ExternalMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/ExternalMapperImpl.java new file mode 100644 index 000000000..00a5c2228 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/ExternalMapperImpl.java @@ -0,0 +1,40 @@ +/** + * 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.test.updatemethods.selection; + +import javax.annotation.Generated; +import org.mapstruct.ap.test.updatemethods.DepartmentDto; +import org.mapstruct.ap.test.updatemethods.DepartmentEntity; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:17:50+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class ExternalMapperImpl implements ExternalMapper { + + @Override + public void toDepartmentEntity(DepartmentDto dto, DepartmentEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper1Impl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper1Impl.java new file mode 100644 index 000000000..fd9501fac --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper1Impl.java @@ -0,0 +1,54 @@ +/** + * 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.test.updatemethods.selection; + +import javax.annotation.Generated; +import org.mapstruct.ap.test.updatemethods.CompanyDto; +import org.mapstruct.ap.test.updatemethods.CompanyEntity; +import org.mapstruct.ap.test.updatemethods.DepartmentEntityFactory; +import org.mapstruct.factory.Mappers; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:17:50+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class OrganizationMapper1Impl implements OrganizationMapper1 { + + private final ExternalMapper externalMapper = Mappers.getMapper( ExternalMapper.class ); + private final DepartmentEntityFactory departmentEntityFactory = new DepartmentEntityFactory(); + + @Override + public void toCompanyEntity(CompanyDto dto, CompanyEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + if ( dto.getDepartment() != null ) { + if ( entity.getDepartment() == null ) { + entity.setDepartment( departmentEntityFactory.createDepartmentEntity() ); + } + externalMapper.toDepartmentEntity( dto.getDepartment(), entity.getDepartment() ); + } + else { + entity.setDepartment( null ); + } + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper2Impl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper2Impl.java new file mode 100644 index 000000000..a0a426f77 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper2Impl.java @@ -0,0 +1,53 @@ +/** + * 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.test.updatemethods.selection; + +import javax.annotation.Generated; +import org.mapstruct.ap.test.updatemethods.CompanyDto; +import org.mapstruct.ap.test.updatemethods.CompanyEntity; +import org.mapstruct.ap.test.updatemethods.DepartmentEntityFactory; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:17:47+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class OrganizationMapper2Impl implements OrganizationMapper2 { + + private final ExternalHandWrittenMapper externalHandWrittenMapper = new ExternalHandWrittenMapper(); + private final DepartmentEntityFactory departmentEntityFactory = new DepartmentEntityFactory(); + + @Override + public void toCompanyEntity(CompanyDto dto, CompanyEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + if ( dto.getDepartment() != null ) { + if ( entity.getDepartment() == null ) { + entity.setDepartment( departmentEntityFactory.createDepartmentEntity() ); + } + externalHandWrittenMapper.toDepartmentEntity( dto.getDepartment(), entity.getDepartment() ); + } + else { + entity.setDepartment( null ); + } + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3Impl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3Impl.java new file mode 100644 index 000000000..830fcde81 --- /dev/null +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3Impl.java @@ -0,0 +1,53 @@ +/** + * 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.test.updatemethods.selection; + +import javax.annotation.Generated; +import org.mapstruct.ap.test.updatemethods.BossDto; +import org.mapstruct.ap.test.updatemethods.BossEntity; +import org.mapstruct.ap.test.updatemethods.ConstructableDepartmentEntity; +import org.mapstruct.factory.Mappers; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2016-12-30T19:17:49+0100", + comments = "version: , compiler: javac, environment: Java 1.8.0_112 (Oracle Corporation)" +) +public class OrganizationMapper3Impl implements OrganizationMapper3 { + + private final ExternalMapper externalMapper = Mappers.getMapper( ExternalMapper.class ); + + @Override + public void toBossEntity(BossDto dto, BossEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + if ( dto.getDepartment() != null ) { + if ( entity.getDepartment() == null ) { + entity.setDepartment( new ConstructableDepartmentEntity() ); + } + externalMapper.toDepartmentEntity( dto.getDepartment(), entity.getDepartment() ); + } + else { + entity.setDepartment( null ); + } + } +}