From c77dd2a1e918c24f64b0937e40407b062e4f58d2 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 22 Jun 2016 11:35:57 +0200 Subject: [PATCH] Implemented tests for the handling of empty arrays --- .../de/juplo/jackson/SimpleMapperTest.java | 173 ++++++++++++++++++ src/test/resources/array/empty/1.json | 1 + src/test/resources/array/empty/2.json | 1 + src/test/resources/array/empty/3.json | 4 + src/test/resources/array/empty/4.json | 2 + 5 files changed, 181 insertions(+) create mode 100644 src/test/resources/array/empty/1.json create mode 100644 src/test/resources/array/empty/2.json create mode 100644 src/test/resources/array/empty/3.json create mode 100644 src/test/resources/array/empty/4.json diff --git a/src/test/java/de/juplo/jackson/SimpleMapperTest.java b/src/test/java/de/juplo/jackson/SimpleMapperTest.java index 33fb8a3..4701f5b 100644 --- a/src/test/java/de/juplo/jackson/SimpleMapperTest.java +++ b/src/test/java/de/juplo/jackson/SimpleMapperTest.java @@ -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 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 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 index 0000000..fe51488 --- /dev/null +++ b/src/test/resources/array/empty/1.json @@ -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 index 0000000..d9a9c8b --- /dev/null +++ b/src/test/resources/array/empty/2.json @@ -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 index 0000000..ac897ac --- /dev/null +++ b/src/test/resources/array/empty/3.json @@ -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 index 0000000..8a4a311 --- /dev/null +++ b/src/test/resources/array/empty/4.json @@ -0,0 +1,2 @@ + + [ ] -- 2.20.1