From 8713af90d540e7bb01f8ba1fd99f6c04e41a7433 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 11 Oct 2017 16:23:41 -0600 Subject: [PATCH] Add unit tests to increase code coverage. This closes #29. --- .../collections4/IteratorUtilsTest.java | 70 ++++++ .../commons/collections4/MapUtilsTest.java | 236 ++++++++++++++++++ 2 files changed, 306 insertions(+) diff --git a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java index 70252a6e0..d31ea02ad 100644 --- a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java @@ -32,16 +32,22 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import java.util.Map; import java.util.NoSuchElementException; +import java.util.Vector; import org.apache.commons.collections4.iterators.EmptyIterator; import org.apache.commons.collections4.iterators.EmptyListIterator; import org.apache.commons.collections4.iterators.EmptyMapIterator; import org.apache.commons.collections4.iterators.EmptyOrderedIterator; import org.apache.commons.collections4.iterators.EmptyOrderedMapIterator; +import org.apache.commons.collections4.iterators.EnumerationIterator; +import org.apache.commons.collections4.iterators.NodeListIterator; +import org.apache.commons.collections4.iterators.ObjectArrayIterator; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Node; @@ -178,6 +184,13 @@ public class IteratorUtilsTest { list.add(null); final Object[] result = IteratorUtils.toArray(list.iterator()); assertEquals(list, Arrays.asList(result)); + + try { + IteratorUtils.toArray(null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } } @Test @@ -188,6 +201,20 @@ public class IteratorUtilsTest { list.add(null); final String[] result = IteratorUtils.toArray(list.iterator(), String.class); assertEquals(list, Arrays.asList(result)); + + try { + IteratorUtils.toArray(list.iterator(), null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } + + try { + IteratorUtils.toArray(null, String.class); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } } @Test @@ -1088,5 +1115,48 @@ public class IteratorUtilsTest { } assertTrue(!iterator.hasNext()); } + + @Test + public void testGetIterator() { + final Object[] objArray = {"a", "b", "c"}; + final Map inMap = new HashMap(); + final Node[] nodes = createNodes(); + final NodeList nodeList = createNodeList(nodes); + + assertTrue("returns empty iterator when null passed", IteratorUtils.getIterator(null) instanceof EmptyIterator); + assertTrue("returns Iterator when Iterator directly ", IteratorUtils.getIterator(iterableA.iterator()) instanceof Iterator); + assertTrue("returns Iterator when iterable passed", IteratorUtils.getIterator(iterableA) instanceof Iterator); + assertTrue("returns ObjectArrayIterator when Object array passed", IteratorUtils.getIterator(objArray) instanceof ObjectArrayIterator); + assertTrue("returns Iterator when Map passed", IteratorUtils.getIterator(inMap) instanceof Iterator); + assertTrue("returns NodeListIterator when nodeList passed", IteratorUtils.getIterator(nodeList) instanceof NodeListIterator); + assertTrue("returns EnumerationIterator when Enumeration passed", IteratorUtils.getIterator(new Vector().elements()) instanceof EnumerationIterator); + + } + + @Test + public void testToListIterator() { + final List list = new ArrayList<>(); + list.add(Integer.valueOf(0)); + list.add(Integer.valueOf(1)); + list.add(Integer.valueOf(2)); + final Iterator iterator = list.iterator(); + + final ListIterator liItr = IteratorUtils.toListIterator(iterator); + int expected = 0; + while(liItr.hasNext()){ + assertEquals(expected, liItr.next().intValue()); + ++expected; + } + } + + @Test + public void testToListIteratorNull() { + try { + IteratorUtils.toListIterator(null); + fail("Expecting NullPointerException"); + } catch (final NullPointerException ex) { + // success + } + } } diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index ebb879024..1a595a58a 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -32,6 +32,7 @@ import java.util.Iterator; import java.util.List; import java.util.ListResourceBundle; import java.util.Map; +import java.util.Properties; import java.util.ResourceBundle; import java.util.Set; import java.util.TreeMap; @@ -239,6 +240,9 @@ public class MapUtilsTest { assertEquals(true, test.containsKey("BLUE")); assertEquals("#0000FF", test.get("BLUE")); assertEquals(3, test.size()); + + test = MapUtils.putAll(new HashMap(), null); + assertEquals(0, test.size()); // map entry test = MapUtils.putAll(new HashMap(), new Object[] { @@ -910,4 +914,236 @@ public class MapUtilsTest { map.put("B", "2"); assertEquals(2, MapUtils.size(map)); } + + @Test + public void testToProperties() { + final Map in = new HashMap(); + in.put("key1", "A"); + in.put("key2", "B"); + in.put("key3", "C"); + + final Properties out = MapUtils.toProperties(in); + + assertEquals(in.get("key1"), out.get("key1")); + assertEquals(in.get("key2"), out.get("key2")); + assertEquals(in.get("key3"), out.get("key3")); + } + + @Test + public void testToPropertiesEmpty() { + final Map in = null; + final Properties out = MapUtils.toProperties(in); + + assertEquals(out.size(), 0); + } + + @Test + public void testgetDoubleValue() { + final Map in = new HashMap(); + in.put("key", 2.0); + + assertEquals(MapUtils.getDoubleValue(in,"key", 0.0), 2.0, 0); + assertEquals(MapUtils.getDoubleValue(in,"key"), 2.0, 0); + assertEquals(MapUtils.getDoubleValue(in,"noKey", 1.0), 1.0, 0); + assertEquals(MapUtils.getDoubleValue(in,"noKey"), 0, 0); + assertEquals(MapUtils.getDouble(in,"key", 0.0), 2.0, 0); + assertEquals(MapUtils.getDouble(in,"noKey", 1.0), 1.0, 0); + + + final Map inStr = new HashMap(); + inStr.put("str1", "2.0"); + + assertEquals(MapUtils.getDoubleValue(inStr,"str1", 0.0), 2.0, 0); + } + + @Test + public void testgetFloatValue() { + final Map in = new HashMap(); + in.put("key", 2.0f); + + assertEquals(MapUtils.getFloatValue(in,"key", 0.0f), 2.0, 0); + assertEquals(MapUtils.getFloatValue(in,"key"), 2.0, 0); + assertEquals(MapUtils.getFloatValue(in,"noKey", 1.0f), 1.0, 0); + assertEquals(MapUtils.getFloatValue(in,"noKey"), 0, 0); + assertEquals(MapUtils.getFloat(in,"key", 0.0f), 2.0, 0); + assertEquals(MapUtils.getFloat(in,"noKey", 1.0f), 1.0, 0); + + final Map inStr = new HashMap(); + inStr.put("str1", "2.0"); + + assertEquals( MapUtils.getFloatValue(inStr,"str1", 0.0f), 2.0, 0); + } + + @Test + public void testgetLongValue() { + final Map in = new HashMap(); + in.put("key", 2L); + + assertEquals(MapUtils.getLongValue(in,"key", 0L), 2.0, 0); + assertEquals(MapUtils.getLongValue(in,"key"), 2.0, 0); + assertEquals(MapUtils.getLongValue(in,"noKey", 1L), 1, 0); + assertEquals(MapUtils.getLongValue(in,"noKey"), 0, 0); + assertEquals(MapUtils.getLong(in,"key", 0L), 2.0, 0); + assertEquals(MapUtils.getLong(in,"noKey", 1L), 1, 0); + + final Map inStr = new HashMap(); + inStr.put("str1", "2"); + + assertEquals(MapUtils.getLongValue(inStr,"str1", 0L), 2, 0); + assertEquals(MapUtils.getLong(inStr, "str1", 1L), 2, 0); + + } + + @Test + public void testgetIntValue() { + final Map in = new HashMap(); + in.put("key", 2); + + assertEquals(MapUtils.getIntValue(in,"key", 0), 2, 0); + assertEquals(MapUtils.getIntValue(in,"key"), 2, 0); + assertEquals(MapUtils.getIntValue(in,"noKey", 0), 0, 0); + assertEquals(MapUtils.getIntValue(in,"noKey"), 0, 0); + assertEquals(MapUtils.getInteger(in,"key", 0), 2, 0); + assertEquals(MapUtils.getInteger(in,"noKey", 0), 0, 0); + + final Map inStr = new HashMap(); + inStr.put("str1", "2"); + + assertEquals(MapUtils.getIntValue(inStr,"str1", 0), 2, 0); + } + + @Test + public void testgetShortValue() { + final Map in = new HashMap(); + final short val = 10; + in.put("key", val); + + assertEquals(MapUtils.getShortValue(in,"key", val), val, 0); + assertEquals(MapUtils.getShortValue(in,"key"), val, 0); + assertEquals(MapUtils.getShortValue(in,"noKey", val), val, 0); + assertEquals(MapUtils.getShortValue(in,"noKey"), 0, 0); + assertEquals(MapUtils.getShort(in,"key", val), val, 0); + assertEquals(MapUtils.getShort(in,"noKey", val), val, 0); + + final Map inStr = new HashMap(); + inStr.put("str1", "10"); + + assertEquals(MapUtils.getShortValue(inStr,"str1", val), val, 0); + } + + @Test + public void testgetByteValue() { + final Map in = new HashMap(); + final byte val = 100; + in.put("key", val); + + assertEquals(MapUtils.getByteValue(in,"key", val), val, 0); + assertEquals(MapUtils.getByteValue(in,"key"), val, 0); + assertEquals(MapUtils.getByteValue(in,"noKey", val), val, 0); + assertEquals(MapUtils.getByteValue(in,"noKey"), 0, 0); + assertEquals(MapUtils.getByte(in,"key", val), val, 0); + assertEquals(MapUtils.getByte(in,"noKey", val), val, 0); + + + final Map inStr = new HashMap(); + inStr.put("str1", "100"); + + assertEquals(MapUtils.getByteValue(inStr,"str1", val), val, 0); + } + + @Test + public void testgetNumber() { + final Map in = new HashMap(); + final Number val = 1000; + in.put("key", val); + + assertEquals(MapUtils.getNumber(in,"key", val).intValue(), val.intValue(), 0); + assertEquals(MapUtils.getNumber(in,"noKey", val).intValue(), val.intValue(), 0); + + } + + @Test + public void testgetString() { + final Map in = new HashMap(); + in.put("key", "str"); + + assertEquals(MapUtils.getString(in,"key", "defualt"), "str"); + assertEquals(MapUtils.getString(in,"key"), "str"); + assertEquals(MapUtils.getString(null,"key"), null); + assertEquals(MapUtils.getString(in,"noKey", "default"), "default"); + assertEquals(MapUtils.getString(null,"noKey", "default"), "default"); + + } + + @Test + public void testgetObject() { + final Map in = new HashMap(); + in.put("key", "str"); + + assertEquals(MapUtils.getObject(in,"key", "defualt"), "str"); + assertEquals(MapUtils.getObject(in,"key"), "str"); + assertEquals(MapUtils.getObject(null,"key"), null); + assertEquals(MapUtils.getObject(in,"noKey", "default"), "default"); + assertEquals(MapUtils.getObject(null,"noKey", "default"), "default"); + + } + + @Test + public void testgetBooleanValue() { + final Map in = new HashMap(); + in.put("key", true); + + assertTrue(MapUtils.getBooleanValue(in,"key", true)); + assertTrue(MapUtils.getBooleanValue(in,"key")); + assertTrue(MapUtils.getBooleanValue(in,"noKey", true)); + assertTrue(!MapUtils.getBooleanValue(in,"noKey")); + assertTrue(MapUtils.getBoolean(in,"key", true)); + assertTrue(MapUtils.getBoolean(in,"noKey", true)); + assertEquals(MapUtils.getBoolean(null,"noKey"),null); + + + + final Map inStr = new HashMap(); + inStr.put("str1", "true"); + + assertTrue(MapUtils.getBooleanValue(inStr,"str1", true)); + assertTrue(MapUtils.getBoolean(inStr,"str1", true)); + + + } + + @Test + public void testgetMap() { + final Map> in = new HashMap>(); + final Map valMap = new HashMap(); + valMap.put("key1", "value1"); + in.put("key1", valMap); + final Map outValue = MapUtils.getMap(in,"key1", null); + + assertEquals( outValue.get("key1"), "value1"); + assertEquals( outValue.get("key2"), null); + assertEquals( MapUtils.getMap(in,"key2", null), null); + assertEquals( MapUtils.getMap(null,"key2", null), null); + } + + @Test + public void testSafeAddToMap() { + + final Map inMap = new HashMap(); + + MapUtils.safeAddToMap(inMap,"key1", "value1"); + MapUtils.safeAddToMap(inMap,"key2", null); + assertEquals( inMap.get("key1"), "value1"); + assertEquals( inMap.get("key2"), ""); + } + + @Test + public void testOrderedMap() { + final Map inMap = new HashMap(); + inMap.put("key1", "value1"); + inMap.put("key2", "value2"); + Map map = MapUtils.orderedMap(inMap); + assertTrue("returned object should be a OrderedMap", map instanceof OrderedMap); + } + }