#227 adding some unit test

This commit is contained in:
sjaakd 2014-06-23 12:08:12 +02:00
parent cd1037a663
commit 8b45909a25
3 changed files with 113 additions and 2 deletions

View File

@ -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<String> testList = new TestList<String>();
testList.addAll( target.getOtherStringList() );
TestList.setAddAllCalled( false );
target.setOtherStringList( testList );
// prepare new source
source.setOtherStringList( Arrays.asList( "Bob" ) );
List<String> 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<String, Long> originalInstance = target.getOtherStringLongMap();
// prepare a test list to monitor add all behaviour
Map<String, Long> originalInstance = new TestMap<String, Long>();
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

View File

@ -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<E> extends ArrayList<E> {
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 );
}
}

View File

@ -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<K, V> extends HashMap<K, V> {
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.
}
}