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;
/**
*/
public class SimpleMapperTest
{
+ private static final Logger LOG =
+ LoggerFactory.getLogger(SimpleMapperTest.class);
+
+
private final JsonFactory factory = new JsonFactory();
}
+ @Test
+ public void testConvertEmptyArrayToArraySpliterator() throws Exception
+ {
+ Spliterator<Object> 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<Object> 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);