From 727f0b44e115691b6b7679dcbf455de159710e6b Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 22 Jun 2016 10:54:13 +0200 Subject: [PATCH] Unified handling of empty input: return always null --- .../java/de/juplo/jackson/SimpleMapper.java | 25 +---- .../de/juplo/jackson/SimpleMapperTest.java | 93 ++++--------------- 2 files changed, 24 insertions(+), 94 deletions(-) diff --git a/src/main/java/de/juplo/jackson/SimpleMapper.java b/src/main/java/de/juplo/jackson/SimpleMapper.java index d7f108c..3dd5509 100644 --- a/src/main/java/de/juplo/jackson/SimpleMapper.java +++ b/src/main/java/de/juplo/jackson/SimpleMapper.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.core.JsonLocation; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import java.io.IOException; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -15,7 +14,6 @@ import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Spliterator; import static java.util.Spliterator.IMMUTABLE; -import java.util.Spliterators; import java.util.function.Consumer; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -41,10 +39,7 @@ public abstract class SimpleMapper JsonToken token = parser.nextToken(); if (token == null) - { - LOG.warn("empty input"); - return Spliterators.emptySpliterator(); - } + return null; if (!JsonToken.START_ARRAY.equals(token)) fail(parser, "The root-element must be an array!"); @@ -140,10 +135,7 @@ public abstract class SimpleMapper JsonToken token = parser.nextToken(); if (token == null) - { - LOG.warn("empty input"); - return Spliterators.emptySpliterator(); - } + return null; if (!JsonToken.START_OBJECT.equals(token)) fail(parser, "The root-element must be an object!"); @@ -263,10 +255,7 @@ public abstract class SimpleMapper JsonToken token = parser.nextToken(); if (token == null) - { - LOG.warn("empty input"); - return Collections.EMPTY_LIST; - } + return null; if (!JsonToken.START_ARRAY.equals(token)) fail(parser, "The root-element must be an array!"); @@ -284,10 +273,7 @@ public abstract class SimpleMapper JsonToken token = parser.nextToken(); if (token == null) - { - LOG.warn("empty input"); - return Collections.EMPTY_MAP; - } + return null; if (!JsonToken.START_OBJECT.equals(token)) fail(parser, "The root-element must be an object!"); @@ -305,10 +291,7 @@ public abstract class SimpleMapper JsonToken token = parser.nextToken(); if (token == null) - { - LOG.warn("empty input"); return null; - } switch (token) { diff --git a/src/test/java/de/juplo/jackson/SimpleMapperTest.java b/src/test/java/de/juplo/jackson/SimpleMapperTest.java index 075acca..33fb8a3 100644 --- a/src/test/java/de/juplo/jackson/SimpleMapperTest.java +++ b/src/test/java/de/juplo/jackson/SimpleMapperTest.java @@ -8,10 +8,7 @@ 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.assertNull; -import static org.junit.Assert.fail; import org.junit.Test; @@ -30,40 +27,15 @@ public class SimpleMapperTest Spliterator spliterator; spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Object t) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getArraySpliterator(get("/empty/2.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Object t) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getArraySpliterator(get("/empty/3.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Object t) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getArraySpliterator(get("/empty/4.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Object t) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getArraySpliterator(get("/empty/5.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Object t) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); } @Test @@ -72,15 +44,15 @@ public class SimpleMapperTest List list; list = SimpleMapper.convertArray(get("/empty/1.json")); - assertEquals(0, list.size()); + assertNull(list); list = SimpleMapper.convertArray(get("/empty/2.json")); - assertEquals(0, list.size()); + assertNull(list); list = SimpleMapper.convertArray(get("/empty/3.json")); - assertEquals(0, list.size()); + assertNull(list); list = SimpleMapper.convertArray(get("/empty/4.json")); - assertEquals(0, list.size()); + assertNull(list); list = SimpleMapper.convertArray(get("/empty/5.json")); - assertEquals(0, list.size()); + assertNull(list); } @Test @@ -89,40 +61,15 @@ public class SimpleMapperTest Spliterator> spliterator; spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Entry e) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getObjectSpliterator(get("/empty/2.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Entry e) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getObjectSpliterator(get("/empty/3.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Entry e) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getObjectSpliterator(get("/empty/4.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Entry e) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); spliterator = SimpleMapper.getObjectSpliterator(get("/empty/5.json")); - assertFalse( - "The created splitter should have no entries", - spliterator.tryAdvance((Entry e) -> - { - fail("The consumer should never be called!"); - })); + assertNull(spliterator); } @Test @@ -131,15 +78,15 @@ public class SimpleMapperTest Map map; map = SimpleMapper.convertObject(get("/empty/1.json")); - assertEquals(0, map.size()); + assertNull(map); map = SimpleMapper.convertObject(get("/empty/2.json")); - assertEquals(0, map.size()); + assertNull(map); map = SimpleMapper.convertObject(get("/empty/3.json")); - assertEquals(0, map.size()); + assertNull(map); map = SimpleMapper.convertObject(get("/empty/4.json")); - assertEquals(0, map.size()); + assertNull(map); map = SimpleMapper.convertObject(get("/empty/5.json")); - assertEquals(0, map.size()); + assertNull(map); } @Test -- 2.20.1