X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fjackson%2FSimpleMapperTest.java;h=e5769e49c5fdd6a185bcaecfb9b7a15b294a817d;hb=13847b2c147ae7a4efc3f5a6aa7ecdeff2b678de;hp=4701f5b5140e3cb22eb5859fc2c895853c3219c0;hpb=c77dd2a1e918c24f64b0937e40407b062e4f58d2;p=simple-mapper diff --git a/src/test/java/de/juplo/jackson/SimpleMapperTest.java b/src/test/java/de/juplo/jackson/SimpleMapperTest.java index 4701f5b..e5769e4 100644 --- a/src/test/java/de/juplo/jackson/SimpleMapperTest.java +++ b/src/test/java/de/juplo/jackson/SimpleMapperTest.java @@ -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 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 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> 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 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 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 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> spliterator; + + spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/1.json")); + assertFalse( + "The created splitter should have no entries", + spliterator.tryAdvance((Entry 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 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 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 e) -> + { + fail("The consumer should never be called!"); + })); + } + + @Test + public void testConvertEmptyObjectToMap() throws Exception + { + LOG.info("<-- Start Of New Test-Case!"); + + Map 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 spliterator; + + spliterator = SimpleMapper.getArraySpliterator(get("/array/1.json")); + checkArraySpliterator(spliterator); + spliterator = SimpleMapper.getArraySpliterator(get("/array/2.json")); + checkArraySpliterator(spliterator); + } + + void checkArraySpliterator(Spliterator spliterator) throws Exception + { + assertNotNull(spliterator); + + final ArrayList 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 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 list; + + object = SimpleMapper.convert(get("/array/1.json")); + assertNotNull(object); + assertTrue("the returned object should be a list", object instanceof List); + list = (List)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; + 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> spliterator; + + spliterator = SimpleMapper.getObjectSpliterator(get("/object/1.json")); + checkObjectSpliterator(spliterator); + spliterator = SimpleMapper.getObjectSpliterator(get("/object/2.json")); + checkObjectSpliterator(spliterator); + } + + void checkObjectSpliterator(Spliterator> spliterator) + throws + Exception + { + assertNotNull(spliterator); + + final LinkedHashMap map = new LinkedHashMap<>(); + for (int i = 0; i < 4; i++) + assertTrue( + "The created splitter should have a " + (i+1) + ". entry", + spliterator.tryAdvance((Entry 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 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 map; + + object = SimpleMapper.convert(get("/object/1.json")); + assertNotNull(object); + assertTrue("the returned object should be a map", object instanceof Map); + map = (Map)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)object; + assertEquals(4, map.size()); + checkVariables(map); + } + + void checkVariables(Map map) + { + Iterator> iterator = map.entrySet().iterator(); + Entry 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 map = (Map)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)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)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);