Implemented tests for the handling of empty objects
authorKai Moritz <kai@juplo.de>
Wed, 22 Jun 2016 09:46:19 +0000 (11:46 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 22 Jun 2016 13:52:29 +0000 (15:52 +0200)
src/test/java/de/juplo/jackson/SimpleMapperTest.java
src/test/resources/object/empty/1.json [new file with mode: 0644]
src/test/resources/object/empty/2.json [new file with mode: 0644]
src/test/resources/object/empty/3.json [new file with mode: 0644]
src/test/resources/object/empty/4.json [new file with mode: 0644]

index 4701f5b..aad4709 100644 (file)
@@ -280,6 +280,168 @@ public class SimpleMapperTest
   }
 
 
+  @Test
+  public void testConvertEmptyObjectToArraySpliterator() throws Exception
+  {
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/empty/1.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/empty/2.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/empty/3.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/empty/4.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testConvertEmptyObjectToList() throws Exception
+  {
+    try
+    {
+      SimpleMapper.convertArray(get("/object/empty/1.json"));
+      fail("it must not be possible, to get a list for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/empty/2.json"));
+      fail("it must not be possible, to get a list for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/empty/3.json"));
+      fail("it must not be possible, to get a list for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/empty/4.json"));
+      fail("it must not be possible, to get a list for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.info(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testConvertEmptyObjectToObjectSpliterator() throws Exception
+  {
+    Spliterator<Entry<String, Object>> spliterator;
+
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/1.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Entry<String, Object> e) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/2.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Entry<String, Object> e) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/3.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Entry<String, Object> e) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/4.json"));
+    assertFalse(
+        "The created splitter should have no entries",
+        spliterator.tryAdvance((Entry<String, Object> e) ->
+        {
+          fail("The consumer should never be called!");
+        }));
+  }
+
+  @Test
+  public void testConvertEmptyObjectToMap() throws Exception
+  {
+    Map<String, Object> map;
+
+    map = SimpleMapper.convertObject(get("/object/empty/1.json"));
+    assertEquals(0, map.size());
+    map = SimpleMapper.convertObject(get("/object/empty/2.json"));
+    assertEquals(0, map.size());
+    map = SimpleMapper.convertObject(get("/object/empty/3.json"));
+    assertEquals(0, map.size());
+    map = SimpleMapper.convertObject(get("/object/empty/4.json"));
+    assertEquals(0, map.size());
+  }
+
+  @Test
+  public void testConvertEmptyObject() throws Exception
+  {
+    Object object;
+
+    object = SimpleMapper.convert(get("/object/empty/1.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof Map);
+    assertEquals(0, ((Map)object).size());
+    object = SimpleMapper.convert(get("/object/empty/2.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof Map);
+    assertEquals(0, ((Map)object).size());
+    object = SimpleMapper.convert(get("/object/empty/3.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof Map);
+    assertEquals(0, ((Map)object).size());
+    object = SimpleMapper.convert(get("/object/empty/4.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof Map);
+    assertEquals(0, ((Map)object).size());
+  }
+
+
   private JsonParser get(String resource) throws IOException
   {
     InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);
diff --git a/src/test/resources/object/empty/1.json b/src/test/resources/object/empty/1.json
new file mode 100644 (file)
index 0000000..0967ef4
--- /dev/null
@@ -0,0 +1 @@
+{}
diff --git a/src/test/resources/object/empty/2.json b/src/test/resources/object/empty/2.json
new file mode 100644 (file)
index 0000000..c95d848
--- /dev/null
@@ -0,0 +1 @@
+{      }
diff --git a/src/test/resources/object/empty/3.json b/src/test/resources/object/empty/3.json
new file mode 100644 (file)
index 0000000..41ed23b
--- /dev/null
@@ -0,0 +1,4 @@
+
+{
+}
+
diff --git a/src/test/resources/object/empty/4.json b/src/test/resources/object/empty/4.json
new file mode 100644 (file)
index 0000000..10104a2
--- /dev/null
@@ -0,0 +1,2 @@
+
+  { }