Implement test-cases to test the handling of empty input
[simple-mapper] / src / test / java / de / juplo / jackson / SimpleMapperTest.java
1 package de.juplo.jackson;
2
3 import com.fasterxml.jackson.core.JsonFactory;
4 import com.fasterxml.jackson.core.JsonParser;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.Spliterator;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.fail;
15 import org.junit.Test;
16
17
18 /**
19  *
20  * @author Kai Moritz
21  */
22 public class SimpleMapperTest
23 {
24   private final JsonFactory factory = new JsonFactory();
25
26
27   @Test
28   public void testConvertEmptyInputToArraySpliterator() throws Exception
29   {
30     Spliterator<Object> spliterator;
31
32     spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json"));
33     assertFalse(
34         "The created splitter should have no entries",
35         spliterator.tryAdvance((Object t) ->
36         {
37           fail("The consumer should never be called!");
38         }));
39     spliterator = SimpleMapper.getArraySpliterator(get("/empty/2.json"));
40     assertFalse(
41         "The created splitter should have no entries",
42         spliterator.tryAdvance((Object t) ->
43         {
44           fail("The consumer should never be called!");
45         }));
46     spliterator = SimpleMapper.getArraySpliterator(get("/empty/3.json"));
47     assertFalse(
48         "The created splitter should have no entries",
49         spliterator.tryAdvance((Object t) ->
50         {
51           fail("The consumer should never be called!");
52         }));
53     spliterator = SimpleMapper.getArraySpliterator(get("/empty/4.json"));
54     assertFalse(
55         "The created splitter should have no entries",
56         spliterator.tryAdvance((Object t) ->
57         {
58           fail("The consumer should never be called!");
59         }));
60     spliterator = SimpleMapper.getArraySpliterator(get("/empty/5.json"));
61     assertFalse(
62         "The created splitter should have no entries",
63         spliterator.tryAdvance((Object t) ->
64         {
65           fail("The consumer should never be called!");
66         }));
67   }
68
69   @Test
70   public void testConvertEmptyInputToList() throws Exception
71   {
72     List<Object> list;
73
74     list = SimpleMapper.convertArray(get("/empty/1.json"));
75     assertEquals(0, list.size());
76     list = SimpleMapper.convertArray(get("/empty/2.json"));
77     assertEquals(0, list.size());
78     list = SimpleMapper.convertArray(get("/empty/3.json"));
79     assertEquals(0, list.size());
80     list = SimpleMapper.convertArray(get("/empty/4.json"));
81     assertEquals(0, list.size());
82     list = SimpleMapper.convertArray(get("/empty/5.json"));
83     assertEquals(0, list.size());
84   }
85
86   @Test
87   public void testConvertEmptyInputToObjectSpliterator() throws Exception
88   {
89     Spliterator<Entry<String, Object>> spliterator;
90
91     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json"));
92     assertFalse(
93         "The created splitter should have no entries",
94         spliterator.tryAdvance((Entry<String, Object> e) ->
95         {
96           fail("The consumer should never be called!");
97         }));
98     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/2.json"));
99     assertFalse(
100         "The created splitter should have no entries",
101         spliterator.tryAdvance((Entry<String, Object> e) ->
102         {
103           fail("The consumer should never be called!");
104         }));
105     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/3.json"));
106     assertFalse(
107         "The created splitter should have no entries",
108         spliterator.tryAdvance((Entry<String, Object> e) ->
109         {
110           fail("The consumer should never be called!");
111         }));
112     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/4.json"));
113     assertFalse(
114         "The created splitter should have no entries",
115         spliterator.tryAdvance((Entry<String, Object> e) ->
116         {
117           fail("The consumer should never be called!");
118         }));
119     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/5.json"));
120     assertFalse(
121         "The created splitter should have no entries",
122         spliterator.tryAdvance((Entry<String, Object> e) ->
123         {
124           fail("The consumer should never be called!");
125         }));
126   }
127
128   @Test
129   public void testConvertEmptyInputToMap() throws Exception
130   {
131     Map<String, Object> map;
132
133     map = SimpleMapper.convertObject(get("/empty/1.json"));
134     assertEquals(0, map.size());
135     map = SimpleMapper.convertObject(get("/empty/2.json"));
136     assertEquals(0, map.size());
137     map = SimpleMapper.convertObject(get("/empty/3.json"));
138     assertEquals(0, map.size());
139     map = SimpleMapper.convertObject(get("/empty/4.json"));
140     assertEquals(0, map.size());
141     map = SimpleMapper.convertObject(get("/empty/5.json"));
142     assertEquals(0, map.size());
143   }
144
145   @Test
146   public void testConvertEmptyInput() throws Exception
147   {
148     Object object;
149
150     object = SimpleMapper.convert(get("/empty/1.json"));
151     assertNull(object);
152     object = SimpleMapper.convert(get("/empty/2.json"));
153     assertNull(object);
154     object = SimpleMapper.convert(get("/empty/3.json"));
155     assertNull(object);
156     object = SimpleMapper.convert(get("/empty/4.json"));
157     assertNull(object);
158     object = SimpleMapper.convert(get("/empty/5.json"));
159     assertNull(object);
160   }
161
162
163   private JsonParser get(String resource) throws IOException
164   {
165     InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);
166     return factory.createParser(is);
167   }
168 }