From 79f55507b7a2a82bbc81fcb288939f277068292d Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 22 Jun 2016 11:46:19 +0200 Subject: [PATCH] Implemented tests for the handling of empty objects --- .../de/juplo/jackson/SimpleMapperTest.java | 162 ++++++++++++++++++ src/test/resources/object/empty/1.json | 1 + src/test/resources/object/empty/2.json | 1 + src/test/resources/object/empty/3.json | 4 + src/test/resources/object/empty/4.json | 2 + 5 files changed, 170 insertions(+) create mode 100644 src/test/resources/object/empty/1.json create mode 100644 src/test/resources/object/empty/2.json create mode 100644 src/test/resources/object/empty/3.json create mode 100644 src/test/resources/object/empty/4.json diff --git a/src/test/java/de/juplo/jackson/SimpleMapperTest.java b/src/test/java/de/juplo/jackson/SimpleMapperTest.java index 4701f5b..aad4709 100644 --- a/src/test/java/de/juplo/jackson/SimpleMapperTest.java +++ b/src/test/java/de/juplo/jackson/SimpleMapperTest.java @@ -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> 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 + { + 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 + { + 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 index 0000000..0967ef4 --- /dev/null +++ b/src/test/resources/object/empty/1.json @@ -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 index 0000000..c95d848 --- /dev/null +++ b/src/test/resources/object/empty/2.json @@ -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 index 0000000..41ed23b --- /dev/null +++ b/src/test/resources/object/empty/3.json @@ -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 index 0000000..10104a2 --- /dev/null +++ b/src/test/resources/object/empty/4.json @@ -0,0 +1,2 @@ + + { } -- 2.20.1