From 5ef53878f4b40588b5094c31931f1aef9c9aa513 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Tue, 30 Apr 2013 22:58:50 +0200 Subject: [PATCH] #6 Mapping null collections as null instead of an empty collection --- .../main/resources/mapper-implementation.ftl | 4 +- .../collection/CollectionMappingTest.java | 48 +++++++++++++++++++ .../mapstruct/ap/test/collection/Source.java | 31 ++++++++++++ .../test/collection/SourceTargetMapper.java | 28 +++++++++++ .../mapstruct/ap/test/collection/Target.java | 31 ++++++++++++ 5 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/collection/CollectionMappingTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/collection/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/collection/SourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/collection/Target.java diff --git a/processor/src/main/resources/mapper-implementation.ftl b/processor/src/main/resources/mapper-implementation.ftl index 660439c6f..567375ee2 100644 --- a/processor/src/main/resources/mapper-implementation.ftl +++ b/processor/src/main/resources/mapper-implementation.ftl @@ -38,7 +38,7 @@ public class ${implementationName} implements ${interfaceName} { @Override public ${beanMapping.targetType.name}<${beanMapping.targetType.elementType.name}> ${beanMapping.mappingMethod.name}(${beanMapping.sourceType.name}<${beanMapping.sourceType.elementType.name}> ${beanMapping.mappingMethod.parameterName}) { if( ${beanMapping.mappingMethod.parameterName} == null ) { - return new ${beanMapping.targetType.name}<${beanMapping.targetType.elementType.name}>(); + return null; } ${beanMapping.targetType.name}<${beanMapping.targetType.elementType.name}> ${beanMapping.targetType.name?uncap_first} = new ${beanMapping.targetType.name}<${beanMapping.targetType.elementType.name}>(); @@ -87,7 +87,7 @@ public class ${implementationName} implements ${interfaceName} { @Override public ${beanMapping.sourceType.name}<${beanMapping.sourceType.elementType.name}> ${beanMapping.reverseMappingMethod.name}(${beanMapping.targetType.name}<${beanMapping.targetType.elementType.name}> ${beanMapping.reverseMappingMethod.parameterName}) { if( ${beanMapping.reverseMappingMethod.parameterName} == null ) { - return new ${beanMapping.sourceType.name}<${beanMapping.sourceType.elementType.name}>(); + return null; } ${beanMapping.sourceType.name}<${beanMapping.sourceType.elementType.name}> ${beanMapping.sourceType.name?uncap_first} = new ${beanMapping.sourceType.name}<${beanMapping.sourceType.elementType.name}>(); diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/CollectionMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/CollectionMappingTest.java new file mode 100644 index 000000000..dd848373e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/CollectionMappingTest.java @@ -0,0 +1,48 @@ +/** + * 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.ap.test.collection; + +import java.util.Arrays; +import java.util.List; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.MapperTestBase; +import org.testng.annotations.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class CollectionMappingTest extends MapperTestBase { + + @Override + protected List> getTestClasses() { + return Arrays.>asList( + Source.class, + Target.class, + SourceTargetMapper.class + ); + } + + @Test + @IssueKey("6") + public void shouldMapNullList() { + Source source = new Source(); + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getStringList() ).isNull(); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/Source.java b/processor/src/test/java/org/mapstruct/ap/test/collection/Source.java new file mode 100644 index 000000000..c4ed2cfa6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/Source.java @@ -0,0 +1,31 @@ +/** + * 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.ap.test.collection; + +import java.util.List; + +public class Source { + + private List stringList; + + public List getStringList() { + return stringList; + } + + public void setStringList(List stringList) { + this.stringList = stringList; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/SourceTargetMapper.java new file mode 100644 index 000000000..02ed7fba6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/SourceTargetMapper.java @@ -0,0 +1,28 @@ +/** + * 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.ap.test.collection; + + +import org.mapstruct.Mapper; +import org.mapstruct.Mappers; + +@Mapper +public interface SourceTargetMapper { + + public static SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + Target sourceToTarget(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/Target.java b/processor/src/test/java/org/mapstruct/ap/test/collection/Target.java new file mode 100644 index 000000000..d21dd5a6b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/Target.java @@ -0,0 +1,31 @@ +/** + * 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.ap.test.collection; + +import java.util.List; + +public class Target { + + private List stringList; + + public List getStringList() { + return stringList; + } + + public void setStringList(List stringList) { + this.stringList = stringList; + } +}