From 8b45909a2529dae725c12d5328e3f3416f6daf34 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Mon, 23 Jun 2014 12:08:12 +0200 Subject: [PATCH] #227 adding some unit test --- .../collection/CollectionMappingTest.java | 21 +++++++- .../ap/test/collection/TestList.java | 46 ++++++++++++++++++ .../mapstruct/ap/test/collection/TestMap.java | 48 +++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/collection/TestList.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/collection/TestMap.java 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 index 93137e28f..3bf96b2f5 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/CollectionMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/CollectionMappingTest.java @@ -35,7 +35,7 @@ import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; -@WithClasses({ Source.class, Target.class, Colour.class, SourceTargetMapper.class }) +@WithClasses({ Source.class, Target.class, Colour.class, SourceTargetMapper.class, TestList.class, TestMap.class }) @RunWith(AnnotationProcessorTestRunner.class) public class CollectionMappingTest { @@ -107,6 +107,7 @@ public class CollectionMappingTest { target.getStringList().add( "Bill" ); assertThat( source.getStringList() ).containsExactly( "Bob", "Alice" ); + assertThat( source.getStringList() ).isNotEqualTo( target.getStringList() ); } @Test @@ -120,6 +121,13 @@ public class CollectionMappingTest { assertThat( source.getOtherStringList() ).containsExactly( "Bob", "Alice" ); + // prepare a test list to monitor add all behaviour + List testList = new TestList(); + testList.addAll( target.getOtherStringList() ); + TestList.setAddAllCalled( false ); + target.setOtherStringList( testList ); + + // prepare new source source.setOtherStringList( Arrays.asList( "Bob" ) ); List originalInstance = target.getOtherStringList(); @@ -127,6 +135,8 @@ public class CollectionMappingTest { assertThat( target.getOtherStringList() ).isSameAs( originalInstance ); assertThat( target.getOtherStringList() ).containsExactly( "Bob" ); + assertThat( TestList.isAddAllCalled() ).isTrue(); + TestList.setAddAllCalled( false ); } @Test @@ -342,12 +352,19 @@ public class CollectionMappingTest { assertThat( target.getOtherStringLongMap() ).hasSize( 3 ); source.getOtherStringLongMap().remove( "Alice" ); - Map originalInstance = target.getOtherStringLongMap(); + + // prepare a test list to monitor add all behaviour + Map originalInstance = new TestMap(); + originalInstance.putAll( target.getOtherStringLongMap() ); + TestMap.setPuttAllCalled( false ); + target.setOtherStringLongMap( originalInstance ); SourceTargetMapper.INSTANCE.sourceToTarget( source, target ); assertThat( target.getOtherStringLongMap() ).isSameAs( originalInstance ); assertThat( target.getOtherStringLongMap() ).hasSize( 1 ); + assertThat( TestMap.isPuttAllCalled() ).isTrue(); + TestMap.setPuttAllCalled( false ); } @Test diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/TestList.java b/processor/src/test/java/org/mapstruct/ap/test/collection/TestList.java new file mode 100644 index 000000000..d75e59164 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/TestList.java @@ -0,0 +1,46 @@ +/** + * Copyright 2012-2014 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; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * + * @author Sjaak Derksen + */ +public class TestList extends ArrayList { + + private static boolean addAllCalled = false; + + public static boolean isAddAllCalled() { + return addAllCalled; + } + + public static void setAddAllCalled( boolean addAllCalled ) { + TestList.addAllCalled = addAllCalled; + } + + @Override + public boolean addAll( Collection c ) { + addAllCalled = true; + return super.addAll( c ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/TestMap.java b/processor/src/test/java/org/mapstruct/ap/test/collection/TestMap.java new file mode 100644 index 000000000..91ddd69c8 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/TestMap.java @@ -0,0 +1,48 @@ +/** + * Copyright 2012-2014 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; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Sjaak Derksen + */ +public class TestMap extends HashMap { + + private static boolean puttAllCalled = false; + + public static boolean isPuttAllCalled() { + return puttAllCalled; + } + + public static void setPuttAllCalled( boolean puttAllCalled ) { + TestMap.puttAllCalled = puttAllCalled; + } + + @Override + public void putAll( Map m ) { + puttAllCalled = true; + super.putAll( m ); //To change body of generated methods, choose Tools | Templates. + } + + + +}