Implemented tests for the handling of empty arrays
authorKai Moritz <kai@juplo.de>
Wed, 22 Jun 2016 09:35:57 +0000 (11:35 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 22 Jun 2016 09:45:49 +0000 (11:45 +0200)
src/test/java/de/juplo/jackson/SimpleMapperTest.java
src/test/resources/array/empty/1.json [new file with mode: 0644]
src/test/resources/array/empty/2.json [new file with mode: 0644]
src/test/resources/array/empty/3.json [new file with mode: 0644]
src/test/resources/array/empty/4.json [new file with mode: 0644]

index 33fb8a3..4701f5b 100644 (file)
@@ -8,8 +8,15 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Spliterator;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -18,6 +25,10 @@ import org.junit.Test;
  */
 public class SimpleMapperTest
 {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(SimpleMapperTest.class);
+
+
   private final JsonFactory factory = new JsonFactory();
 
 
@@ -107,6 +118,168 @@ public class SimpleMapperTest
   }
 
 
+  @Test
+  public void testConvertEmptyArrayToArraySpliterator() throws Exception
+  {
+    Spliterator<Object> spliterator;
+
+    spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/1.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+    spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/2.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+    spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/3.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+    spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/4.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+  }
+
+  @Test
+  public void testConvertEmptyArrayToList() throws Exception
+  {
+    List<Object> list;
+
+    list = SimpleMapper.convertArray(get("/array/empty/1.json"));
+    assertEquals(0, list.size());
+    list = SimpleMapper.convertArray(get("/array/empty/2.json"));
+    assertEquals(0, list.size());
+    list = SimpleMapper.convertArray(get("/array/empty/3.json"));
+    assertEquals(0, list.size());
+    list = SimpleMapper.convertArray(get("/array/empty/4.json"));
+    assertEquals(0, list.size());
+  }
+
+  @Test
+  public void testConvertEmptyArrayToObjectSpliterator() throws Exception
+  {
+    try
+    {
+      SimpleMapper.getObjectSpliterator(get("/array/empty/1.json"));
+      fail("it must not be possible, to get an object-spliterator for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.getObjectSpliterator(get("/array/empty/2.json"));
+      fail("it must not be possible, to get an object-spliterator for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.getObjectSpliterator(get("/array/empty/3.json"));
+      fail("it must not be possible, to get an object-spliterator for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.getObjectSpliterator(get("/array/empty/4.json"));
+      fail("it must not be possible, to get an object-spliterator for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testConvertEmptyArrayToMap() throws Exception
+  {
+    try
+    {
+      SimpleMapper.convertObject(get("/array/empty/1.json"));
+      fail("it must not be possible, to get a map for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.convertObject(get("/array/empty/2.json"));
+      fail("it must not be possible, to get a map for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.convertObject(get("/array/empty/3.json"));
+      fail("it must not be possible, to get a map for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.convertObject(get("/array/empty/4.json"));
+      fail("it must not be possible, to get a map for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testConvertEmptyArray() throws Exception
+  {
+    Object object;
+
+    object = SimpleMapper.convert(get("/array/empty/1.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof List);
+    assertEquals(0, ((List)object).size());
+    object = SimpleMapper.convert(get("/array/empty/2.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof List);
+    assertEquals(0, ((List)object).size());
+    object = SimpleMapper.convert(get("/array/empty/3.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof List);
+    assertEquals(0, ((List)object).size());
+    object = SimpleMapper.convert(get("/array/empty/4.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof List);
+    assertEquals(0, ((List)object).size());
+  }
+
+
   private JsonParser get(String resource) throws IOException
   {
     InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);
diff --git a/src/test/resources/array/empty/1.json b/src/test/resources/array/empty/1.json
new file mode 100644 (file)
index 0000000..fe51488
--- /dev/null
@@ -0,0 +1 @@
+[]
diff --git a/src/test/resources/array/empty/2.json b/src/test/resources/array/empty/2.json
new file mode 100644 (file)
index 0000000..d9a9c8b
--- /dev/null
@@ -0,0 +1 @@
+[      ]
diff --git a/src/test/resources/array/empty/3.json b/src/test/resources/array/empty/3.json
new file mode 100644 (file)
index 0000000..ac897ac
--- /dev/null
@@ -0,0 +1,4 @@
+
+[
+]
+
diff --git a/src/test/resources/array/empty/4.json b/src/test/resources/array/empty/4.json
new file mode 100644 (file)
index 0000000..8a4a311
--- /dev/null
@@ -0,0 +1,2 @@
+
+  [ ]