Skip to content

Commit

Permalink
Checked instance type for insertion order test
Browse files Browse the repository at this point in the history
  • Loading branch information
Family authored and Family committed Oct 6, 2020
1 parent 4ab7d29 commit d01324a
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/test/java/org/apache/commons/collections4/MapBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.TreeMap;

/**
* Test Cases for Map Builder
Expand All @@ -37,7 +39,6 @@ void setComparator() {
myMap.put("B", 2);
myMap.put("Y", 26);
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).setComparator(null).build();
Assert.assertEquals(myMap, builderMap);

// Reverse comparator
builderMap = new MapBuilder().setData(myMap).setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).setComparator(Comparator.reverseOrder()).build();
Expand All @@ -56,51 +57,39 @@ void setIterationOrder() {
myMap.put("B", 2);
myMap.put("Y", 26);
Map<String, Integer> builderMap = new MapBuilder().setData(myMap).setIterationOrder(MapBuilder.KeyOrder.UNORDERED).build();
Assert.assertEquals(myMap, builderMap);
Assert.assertTrue(builderMap instanceof HashMap);

//Key Order = INSERTION ORDER
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.INSERTION_ORDER).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "A");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "Y");
Assert.assertTrue(builderMap instanceof LinkedHashMap);

//Key Order = NATURAL ORDER
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.NATURAL_ORDER).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "A");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "Y");
Assert.assertTrue(builderMap instanceof TreeMap);

//Key Order = COMPARATOR ORDER and null comparator
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "A");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "Y");
Assert.assertTrue(builderMap instanceof TreeMap);

//Key Order = COMPARATOR ORDER and valid comparator
builderMap = new MapBuilder().setIterationOrder(MapBuilder.KeyOrder.COMPARATOR_ORDER).setComparator(Comparator.reverseOrder()).build();
builderMap.put("A", 1);
builderMap.put("X", 24);
builderMap.put("B", 2);
builderMap.put("Y", 26);
Assert.assertEquals(builderMap.keySet().stream().findFirst().get(), "Y");
Assert.assertEquals(builderMap.keySet().stream().skip(1).findFirst().get(), "X");
Assert.assertEquals(builderMap.keySet().stream().skip(2).findFirst().get(), "B");
Assert.assertEquals(builderMap.keySet().stream().skip(3).findFirst().get(), "A");
Assert.assertTrue(builderMap instanceof TreeMap);
}

@Test
Expand All @@ -116,7 +105,6 @@ void setImmutable() {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
Assert.assertEquals(myMap, builderMap);
}

@Test
Expand Down

0 comments on commit d01324a

Please sign in to comment.