diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/util/internal/ImmutableMapTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/util/internal/ImmutableMapTest.java index 0cfe918fef..1da1e22b4b 100644 --- a/surefire-api/src/test/java/org/apache/maven/surefire/util/internal/ImmutableMapTest.java +++ b/surefire-api/src/test/java/org/apache/maven/surefire/util/internal/ImmutableMapTest.java @@ -23,7 +23,9 @@ import org.junit.Before; import org.junit.Test; -import java.util.HashMap; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -31,6 +33,8 @@ import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; /** @@ -41,9 +45,9 @@ public class ImmutableMapTest private ImmutableMap map; @Before - public void setUp() throws Exception + public void setUp() { - Map backingMap = new HashMap<>(); + Map backingMap = new LinkedHashMap<>(); backingMap.put( "a", "1" ); backingMap.put( "x", null ); backingMap.put( "b", "2" ); @@ -54,7 +58,7 @@ public void setUp() throws Exception } @Test - public void testEntrySet() throws Exception + public void testEntrySet() { Set> entries = map.entrySet(); assertThat( entries, hasSize( 6 ) ); @@ -83,4 +87,78 @@ public void shouldNotModifyEntries() { map.entrySet().clear(); } -} \ No newline at end of file + + @Test + public void shouldSafelyEnumerateEntries() + { + Iterator> it = map.entrySet().iterator(); + + assertThat( it.hasNext(), is( true ) ); + Entry val = it.next(); + assertThat( val.getKey(), is( "a" ) ); + assertThat( val.getValue(), is( "1" ) ); + + assertThat( it.hasNext(), is( true ) ); + val = it.next(); + assertThat( val.getKey(), is( "x" ) ); + assertThat( val.getValue(), is( nullValue() ) ); + + assertThat( it.hasNext(), is( true ) ); + val = it.next(); + assertThat( val.getKey(), is( "b" ) ); + assertThat( val.getValue(), is( "2" ) ); + + assertThat( it.hasNext(), is( true ) ); + val = it.next(); + assertThat( val.getKey(), is( "c" ) ); + assertThat( val.getValue(), is( "3" ) ); + + assertThat( it.hasNext(), is( true ) ); + val = it.next(); + assertThat( val.getKey(), is( "" ) ); + assertThat( val.getValue(), is( "" ) ); + + assertThat( it.hasNext(), is( true ) ); + val = it.next(); + assertThat( val.getKey(), is( nullValue() ) ); + assertThat( val.getValue(), is( "1" ) ); + + assertThat( it.hasNext(), is( false ) ); + } + + @Test( expected = UnsupportedOperationException.class ) + public void shouldNotSetEntries() + { + map.entrySet().iterator().next().setValue( "" ); + } + + @Test( expected = UnsupportedOperationException.class ) + public void shouldNotRemove() + { + map.remove( "a" ); + } + + @Test( expected = UnsupportedOperationException.class ) + public void shouldNotRemoveNull() + { + map.remove( null ); + } + + @Test + public void shouldNotHaveEqualEntry() + { + Map map = new ImmutableMap<>( Collections.singletonMap( "k", "v" ) ); + Entry e = map.entrySet().iterator().next(); + assertThat( e, is( not( (Entry) null ) ) ); + assertThat( e, is( not( new Object() ) ) ); + } + + @Test + public void shouldHaveEqualEntry() + { + Map map = new ImmutableMap<>( Collections.singletonMap( "k", "v" ) ); + Entry e = map.entrySet().iterator().next(); + assertThat( e, is( e ) ); + assertThat( e, is( (Entry) new Node<>( "k", "v" ) ) ); + } +}