Refined logging-statements of SimpleMapperTest
[simple-mapper] / src / test / java / de / juplo / jackson / SimpleMapperTest.java
index 4701f5b..e5769e4 100644 (file)
@@ -4,19 +4,22 @@ import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonParser;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Spliterator;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 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;
 
 
 /**
@@ -35,6 +38,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToArraySpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Object> spliterator;
 
     spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json"));
@@ -52,6 +57,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToList() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     List<Object> list;
 
     list = SimpleMapper.convertArray(get("/empty/1.json"));
@@ -69,6 +76,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToObjectSpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Entry<String, Object>> spliterator;
 
     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json"));
@@ -86,6 +95,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToMap() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Map<String, Object> map;
 
     map = SimpleMapper.convertObject(get("/empty/1.json"));
@@ -103,6 +114,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInput() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Object object;
 
     object = SimpleMapper.convert(get("/empty/1.json"));
@@ -121,6 +134,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyArrayToArraySpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Object> spliterator;
 
     spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/1.json"));
@@ -156,6 +171,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyArrayToList() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     List<Object> list;
 
     list = SimpleMapper.convertArray(get("/array/empty/1.json"));
@@ -171,6 +188,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyArrayToObjectSpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.getObjectSpliterator(get("/array/empty/1.json"));
@@ -178,7 +197,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -188,7 +207,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -198,7 +217,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -208,13 +227,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertEmptyArrayToMap() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.convertObject(get("/array/empty/1.json"));
@@ -222,7 +243,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -232,7 +253,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -242,7 +263,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -252,13 +273,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertEmptyArray() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Object object;
 
     object = SimpleMapper.convert(get("/array/empty/1.json"));
@@ -280,6 +303,497 @@ public class SimpleMapperTest
   }
 
 
+  @Test
+  public void testConvertEmptyObjectToArraySpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    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.trace("expected exception", e);
+    }
+
+    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.trace("expected exception", e);
+    }
+
+    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.trace("expected exception", e);
+    }
+
+    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.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertEmptyObjectToList() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    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.trace("expected exception", e);
+    }
+
+    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.trace("expected exception", e);
+    }
+
+    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.trace("expected exception", e);
+    }
+
+    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.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertEmptyObjectToObjectSpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    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
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    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
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    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());
+  }
+
+
+  @Test
+  public void testConvertArrayToArraySpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Spliterator<Object> spliterator;
+
+    spliterator = SimpleMapper.getArraySpliterator(get("/array/1.json"));
+    checkArraySpliterator(spliterator);
+    spliterator = SimpleMapper.getArraySpliterator(get("/array/2.json"));
+    checkArraySpliterator(spliterator);
+  }
+
+  void checkArraySpliterator(Spliterator<Object> spliterator) throws Exception
+  {
+    assertNotNull(spliterator);
+
+    final ArrayList<Object> entries = new ArrayList<>(4);
+    for (int i = 0; i < 4; i++)
+      assertTrue(
+          "The created splitter should have a " + (i+1) + ". entry",
+          spliterator.tryAdvance((Object t) -> { entries.add(t); })
+          );
+
+    assertFalse(
+        "The created splitter should have no more entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should not have been called");
+        }));
+
+    checkPartnerPageData(entries.get(3));
+  }
+
+  @Test
+  public void testConvertArrayToList() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    List<Object> list;
+
+    list = SimpleMapper.convertArray(get("/array/1.json"));
+    assertNotNull(list);
+    assertEquals(4, list.size());
+    checkPartnerPageData(list.get(3));
+    list = SimpleMapper.convertArray(get("/array/2.json"));
+    assertNotNull(list);
+    assertEquals(4, list.size());
+    checkPartnerPageData(list.get(3));
+  }
+
+  @Test
+  public void testConvertArrayToObjectSpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    try
+    {
+      SimpleMapper.getObjectSpliterator(get("/array/1.json"));
+      fail("it must not be possible, to get an object-spliterator for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+
+    try
+    {
+      SimpleMapper.getObjectSpliterator(get("/array/2.json"));
+      fail("it must not be possible, to get an object-spliterator for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertArrayToMap() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    try
+    {
+      SimpleMapper.convertObject(get("/array/1.json"));
+      fail("it must not be possible, to get a map for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+
+    try
+    {
+      SimpleMapper.convertObject(get("/array/2.json"));
+      fail("it must not be possible, to get a map for an array");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertArray() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Object object;
+    List<Object> list;
+
+    object = SimpleMapper.convert(get("/array/1.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof List);
+    list = (List<Object>)object;
+    assertEquals(4, list.size());
+    checkPartnerPageData(list.get(3));
+    object = SimpleMapper.convert(get("/array/2.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a list", object instanceof List);
+    list = (List<Object>)object;
+    assertEquals(4, list.size());
+    checkPartnerPageData(list.get(3));
+  }
+
+
+  @Test
+  public void testConvertObjectToArraySpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/1.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/2.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertObjectToList() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/1.json"));
+      fail("it must not be possible, to get a list for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/2.json"));
+      fail("it must not be possible, to get a list for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertObjectToObjectSpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Spliterator<Entry<String, Object>> spliterator;
+
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/1.json"));
+    checkObjectSpliterator(spliterator);
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/2.json"));
+    checkObjectSpliterator(spliterator);
+  }
+
+  void checkObjectSpliterator(Spliterator<Entry<String, Object>> spliterator)
+      throws
+        Exception
+  {
+    assertNotNull(spliterator);
+
+    final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
+    for (int i = 0; i < 4; i++)
+      assertTrue(
+          "The created splitter should have a " + (i+1) + ". entry",
+          spliterator.tryAdvance((Entry<String, Object> e) -> {
+            map.put(e.getKey(), e.getValue());
+          }));
+
+    assertFalse(
+        "The created splitter should have no more entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should not have been called");
+        }));
+
+    checkVariables(map);
+  }
+
+  @Test
+  public void testConvertObjectToMap() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Map<String, Object> map;
+
+    map = SimpleMapper.convertObject(get("/object/1.json"));
+    assertNotNull(map);
+    assertEquals(4, map.size());
+    checkVariables(map);
+    map = SimpleMapper.convertObject(get("/object/2.json"));
+    assertNotNull(map);
+    assertEquals(4, map.size());
+    checkVariables(map);
+  }
+
+  @Test
+  public void testConvertObject() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Object object;
+    Map<String, Object> map;
+
+    object = SimpleMapper.convert(get("/object/1.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a map", object instanceof Map);
+    map = (Map<String, Object>)object;
+    assertEquals(4, map.size());
+    checkVariables(map);
+    object = SimpleMapper.convert(get("/object/2.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a map", object instanceof Map);
+    map = (Map<String, Object>)object;
+    assertEquals(4, map.size());
+    checkVariables(map);
+  }
+
+  void checkVariables(Map<String, Object> map)
+  {
+    Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
+    Entry<String, Object> entry;
+
+    assertTrue("The map should have a first element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("bootstrap", entry.getKey());
+    assertEquals("bootstrap.min.css", entry.getValue());
+
+    assertTrue("The map should have a second element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("pages", entry.getKey());
+    assertTrue(
+        "The pages-variable should be of type List",
+        entry.getValue() instanceof List
+        );
+    assertEquals(4, ((List)entry.getValue()).size());
+    checkPartnerPageData(((List)entry.getValue()).get(3));
+
+    assertTrue("The map should have a third element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("footer", entry.getKey());
+    assertTrue(
+        "The footer-variable should be of type List",
+        entry.getValue() instanceof List
+        );
+    assertEquals(3, ((List)entry.getValue()).size());
+
+    assertTrue("The map should have a third element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("sponsorship", entry.getKey());
+    assertTrue(
+        "The sponsorship-variable should be of type List",
+        entry.getValue() instanceof List
+        );
+    assertEquals(3, ((List)entry.getValue()).size());
+  }
+
+
+  void checkPartnerPageData(Object page)
+  {
+    assertTrue("The page-data should be of type Map", page instanceof Map);
+    Map<String, Object> map = (Map<String, Object>)page;
+    assertEquals(4, map.size());
+    assertEquals("/partner.html", map.get("uri"));
+    assertTrue(
+        "The page-data should have an entry \"children\" of type Map",
+        map.get("children") instanceof Map
+        );
+    map = (Map<String, Object>)map.get("children");
+    assertEquals(6, map.size());
+    assertTrue(
+        "The children-map should haven an entry \"/partner/juplo.html\" of type Map",
+        map.get("/partner/juplo.html") instanceof Map
+        );
+    map = (Map<String, Object>)map.get("/partner/juplo.html");
+    assertEquals(2, map.size());
+    assertEquals(
+        "Wir sind Unterstützer der Nerd-Plattform juplo.de",
+        map.get("title")
+        );
+  }
+
+
   private JsonParser get(String resource) throws IOException
   {
     InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);