Skip to content

Commit

Permalink
100% coverage of ImmutableMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor17 committed Aug 6, 2019
1 parent c6694ed commit 5711f97
Showing 1 changed file with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
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;

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;

/**
Expand All @@ -41,9 +45,9 @@ public class ImmutableMapTest
private ImmutableMap<String, String> map;

@Before
public void setUp() throws Exception
public void setUp()
{
Map<String, String> backingMap = new HashMap<>();
Map<String, String> backingMap = new LinkedHashMap<>();
backingMap.put( "a", "1" );
backingMap.put( "x", null );
backingMap.put( "b", "2" );
Expand All @@ -54,7 +58,7 @@ public void setUp() throws Exception
}

@Test
public void testEntrySet() throws Exception
public void testEntrySet()
{
Set<Entry<String, String>> entries = map.entrySet();
assertThat( entries, hasSize( 6 ) );
Expand Down Expand Up @@ -83,4 +87,78 @@ public void shouldNotModifyEntries()
{
map.entrySet().clear();
}
}

@Test
public void shouldSafelyEnumerateEntries()
{
Iterator<Entry<String, String>> it = map.entrySet().iterator();

assertThat( it.hasNext(), is( true ) );
Entry<String, String> 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<String, String> map = new ImmutableMap<>( Collections.singletonMap( "k", "v" ) );
Entry<String, String> e = map.entrySet().iterator().next();
assertThat( e, is( not( (Entry<String, String>) null ) ) );
assertThat( e, is( not( new Object() ) ) );
}

@Test
public void shouldHaveEqualEntry()
{
Map<String, String> map = new ImmutableMap<>( Collections.singletonMap( "k", "v" ) );
Entry<String, String> e = map.entrySet().iterator().next();
assertThat( e, is( e ) );
assertThat( e, is( (Entry<String, String>) new Node<>( "k", "v" ) ) );
}
}

0 comments on commit 5711f97

Please sign in to comment.