1 package de.juplo.jackson;
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.ArrayList;
8 import java.util.Iterator;
9 import java.util.LinkedHashMap;
10 import java.util.List;
12 import java.util.Map.Entry;
13 import java.util.Spliterator;
14 import org.junit.Test;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
29 public class SimpleMapperTest
31 private static final Logger LOG =
32 LoggerFactory.getLogger(SimpleMapperTest.class);
35 private final JsonFactory factory = new JsonFactory();
39 public void testConvertEmptyInputToArraySpliterator() throws Exception
41 LOG.info("<-- Start Of New Test-Case!");
43 Spliterator<Object> spliterator;
45 spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json"));
46 assertNull(spliterator);
47 spliterator = SimpleMapper.getArraySpliterator(get("/empty/2.json"));
48 assertNull(spliterator);
49 spliterator = SimpleMapper.getArraySpliterator(get("/empty/3.json"));
50 assertNull(spliterator);
51 spliterator = SimpleMapper.getArraySpliterator(get("/empty/4.json"));
52 assertNull(spliterator);
53 spliterator = SimpleMapper.getArraySpliterator(get("/empty/5.json"));
54 assertNull(spliterator);
58 public void testConvertEmptyInputToList() throws Exception
60 LOG.info("<-- Start Of New Test-Case!");
64 list = SimpleMapper.convertArray(get("/empty/1.json"));
66 list = SimpleMapper.convertArray(get("/empty/2.json"));
68 list = SimpleMapper.convertArray(get("/empty/3.json"));
70 list = SimpleMapper.convertArray(get("/empty/4.json"));
72 list = SimpleMapper.convertArray(get("/empty/5.json"));
77 public void testConvertEmptyInputToObjectSpliterator() throws Exception
79 LOG.info("<-- Start Of New Test-Case!");
81 Spliterator<Entry<String, Object>> spliterator;
83 spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json"));
84 assertNull(spliterator);
85 spliterator = SimpleMapper.getObjectSpliterator(get("/empty/2.json"));
86 assertNull(spliterator);
87 spliterator = SimpleMapper.getObjectSpliterator(get("/empty/3.json"));
88 assertNull(spliterator);
89 spliterator = SimpleMapper.getObjectSpliterator(get("/empty/4.json"));
90 assertNull(spliterator);
91 spliterator = SimpleMapper.getObjectSpliterator(get("/empty/5.json"));
92 assertNull(spliterator);
96 public void testConvertEmptyInputToMap() throws Exception
98 LOG.info("<-- Start Of New Test-Case!");
100 Map<String, Object> map;
102 map = SimpleMapper.convertObject(get("/empty/1.json"));
104 map = SimpleMapper.convertObject(get("/empty/2.json"));
106 map = SimpleMapper.convertObject(get("/empty/3.json"));
108 map = SimpleMapper.convertObject(get("/empty/4.json"));
110 map = SimpleMapper.convertObject(get("/empty/5.json"));
115 public void testConvertEmptyInput() throws Exception
117 LOG.info("<-- Start Of New Test-Case!");
121 object = SimpleMapper.convert(get("/empty/1.json"));
123 object = SimpleMapper.convert(get("/empty/2.json"));
125 object = SimpleMapper.convert(get("/empty/3.json"));
127 object = SimpleMapper.convert(get("/empty/4.json"));
129 object = SimpleMapper.convert(get("/empty/5.json"));
135 public void testConvertEmptyArrayToArraySpliterator() throws Exception
137 LOG.info("<-- Start Of New Test-Case!");
139 Spliterator<Object> spliterator;
141 spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/1.json"));
143 "The created splitter should have no entries",
144 spliterator.tryAdvance((Object t) ->
146 fail("The consumer should never be called!");
148 spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/2.json"));
150 "The created splitter should have no entries",
151 spliterator.tryAdvance((Object t) ->
153 fail("The consumer should never be called!");
155 spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/3.json"));
157 "The created splitter should have no entries",
158 spliterator.tryAdvance((Object t) ->
160 fail("The consumer should never be called!");
162 spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/4.json"));
164 "The created splitter should have no entries",
165 spliterator.tryAdvance((Object t) ->
167 fail("The consumer should never be called!");
172 public void testConvertEmptyArrayToList() throws Exception
174 LOG.info("<-- Start Of New Test-Case!");
178 list = SimpleMapper.convertArray(get("/array/empty/1.json"));
179 assertEquals(0, list.size());
180 list = SimpleMapper.convertArray(get("/array/empty/2.json"));
181 assertEquals(0, list.size());
182 list = SimpleMapper.convertArray(get("/array/empty/3.json"));
183 assertEquals(0, list.size());
184 list = SimpleMapper.convertArray(get("/array/empty/4.json"));
185 assertEquals(0, list.size());
189 public void testConvertEmptyArrayToObjectSpliterator() throws Exception
191 LOG.info("<-- Start Of New Test-Case!");
195 SimpleMapper.getObjectSpliterator(get("/array/empty/1.json"));
196 fail("it must not be possible, to get an object-spliterator for an array");
198 catch(IllegalArgumentException e)
200 LOG.trace("expected exception", e);
205 SimpleMapper.getObjectSpliterator(get("/array/empty/2.json"));
206 fail("it must not be possible, to get an object-spliterator for an array");
208 catch(IllegalArgumentException e)
210 LOG.trace("expected exception", e);
215 SimpleMapper.getObjectSpliterator(get("/array/empty/3.json"));
216 fail("it must not be possible, to get an object-spliterator for an array");
218 catch(IllegalArgumentException e)
220 LOG.trace("expected exception", e);
225 SimpleMapper.getObjectSpliterator(get("/array/empty/4.json"));
226 fail("it must not be possible, to get an object-spliterator for an array");
228 catch(IllegalArgumentException e)
230 LOG.trace("expected exception", e);
235 public void testConvertEmptyArrayToMap() throws Exception
237 LOG.info("<-- Start Of New Test-Case!");
241 SimpleMapper.convertObject(get("/array/empty/1.json"));
242 fail("it must not be possible, to get a map for an array");
244 catch(IllegalArgumentException e)
246 LOG.trace("expected exception", e);
251 SimpleMapper.convertObject(get("/array/empty/2.json"));
252 fail("it must not be possible, to get a map for an array");
254 catch(IllegalArgumentException e)
256 LOG.trace("expected exception", e);
261 SimpleMapper.convertObject(get("/array/empty/3.json"));
262 fail("it must not be possible, to get a map for an array");
264 catch(IllegalArgumentException e)
266 LOG.trace("expected exception", e);
271 SimpleMapper.convertObject(get("/array/empty/4.json"));
272 fail("it must not be possible, to get a map for an array");
274 catch(IllegalArgumentException e)
276 LOG.trace("expected exception", e);
281 public void testConvertEmptyArray() throws Exception
283 LOG.info("<-- Start Of New Test-Case!");
287 object = SimpleMapper.convert(get("/array/empty/1.json"));
288 assertNotNull(object);
289 assertTrue("the returned object should be a list", object instanceof List);
290 assertEquals(0, ((List)object).size());
291 object = SimpleMapper.convert(get("/array/empty/2.json"));
292 assertNotNull(object);
293 assertTrue("the returned object should be a list", object instanceof List);
294 assertEquals(0, ((List)object).size());
295 object = SimpleMapper.convert(get("/array/empty/3.json"));
296 assertNotNull(object);
297 assertTrue("the returned object should be a list", object instanceof List);
298 assertEquals(0, ((List)object).size());
299 object = SimpleMapper.convert(get("/array/empty/4.json"));
300 assertNotNull(object);
301 assertTrue("the returned object should be a list", object instanceof List);
302 assertEquals(0, ((List)object).size());
307 public void testConvertEmptyObjectToArraySpliterator() throws Exception
309 LOG.info("<-- Start Of New Test-Case!");
313 SimpleMapper.getArraySpliterator(get("/object/empty/1.json"));
314 fail("it must not be possible, to get an array-spliterator for an object");
316 catch(IllegalArgumentException e)
318 LOG.trace("expected exception", e);
323 SimpleMapper.getArraySpliterator(get("/object/empty/2.json"));
324 fail("it must not be possible, to get an array-spliterator for an object");
326 catch(IllegalArgumentException e)
328 LOG.trace("expected exception", e);
333 SimpleMapper.getArraySpliterator(get("/object/empty/3.json"));
334 fail("it must not be possible, to get an array-spliterator for an object");
336 catch(IllegalArgumentException e)
338 LOG.trace("expected exception", e);
343 SimpleMapper.getArraySpliterator(get("/object/empty/4.json"));
344 fail("it must not be possible, to get an array-spliterator for an object");
346 catch(IllegalArgumentException e)
348 LOG.trace("expected exception", e);
353 public void testConvertEmptyObjectToList() throws Exception
355 LOG.info("<-- Start Of New Test-Case!");
359 SimpleMapper.convertArray(get("/object/empty/1.json"));
360 fail("it must not be possible, to get a list for an array");
362 catch(IllegalArgumentException e)
364 LOG.trace("expected exception", e);
369 SimpleMapper.convertArray(get("/object/empty/2.json"));
370 fail("it must not be possible, to get a list for an array");
372 catch(IllegalArgumentException e)
374 LOG.trace("expected exception", e);
379 SimpleMapper.convertArray(get("/object/empty/3.json"));
380 fail("it must not be possible, to get a list for an array");
382 catch(IllegalArgumentException e)
384 LOG.trace("expected exception", e);
389 SimpleMapper.convertArray(get("/object/empty/4.json"));
390 fail("it must not be possible, to get a list for an array");
392 catch(IllegalArgumentException e)
394 LOG.trace("expected exception", e);
399 public void testConvertEmptyObjectToObjectSpliterator() throws Exception
401 LOG.info("<-- Start Of New Test-Case!");
403 Spliterator<Entry<String, Object>> spliterator;
405 spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/1.json"));
407 "The created splitter should have no entries",
408 spliterator.tryAdvance((Entry<String, Object> e) ->
410 fail("The consumer should never be called!");
412 spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/2.json"));
414 "The created splitter should have no entries",
415 spliterator.tryAdvance((Entry<String, Object> e) ->
417 fail("The consumer should never be called!");
419 spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/3.json"));
421 "The created splitter should have no entries",
422 spliterator.tryAdvance((Entry<String, Object> e) ->
424 fail("The consumer should never be called!");
426 spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/4.json"));
428 "The created splitter should have no entries",
429 spliterator.tryAdvance((Entry<String, Object> e) ->
431 fail("The consumer should never be called!");
436 public void testConvertEmptyObjectToMap() throws Exception
438 LOG.info("<-- Start Of New Test-Case!");
440 Map<String, Object> map;
442 map = SimpleMapper.convertObject(get("/object/empty/1.json"));
443 assertEquals(0, map.size());
444 map = SimpleMapper.convertObject(get("/object/empty/2.json"));
445 assertEquals(0, map.size());
446 map = SimpleMapper.convertObject(get("/object/empty/3.json"));
447 assertEquals(0, map.size());
448 map = SimpleMapper.convertObject(get("/object/empty/4.json"));
449 assertEquals(0, map.size());
453 public void testConvertEmptyObject() throws Exception
455 LOG.info("<-- Start Of New Test-Case!");
459 object = SimpleMapper.convert(get("/object/empty/1.json"));
460 assertNotNull(object);
461 assertTrue("the returned object should be a list", object instanceof Map);
462 assertEquals(0, ((Map)object).size());
463 object = SimpleMapper.convert(get("/object/empty/2.json"));
464 assertNotNull(object);
465 assertTrue("the returned object should be a list", object instanceof Map);
466 assertEquals(0, ((Map)object).size());
467 object = SimpleMapper.convert(get("/object/empty/3.json"));
468 assertNotNull(object);
469 assertTrue("the returned object should be a list", object instanceof Map);
470 assertEquals(0, ((Map)object).size());
471 object = SimpleMapper.convert(get("/object/empty/4.json"));
472 assertNotNull(object);
473 assertTrue("the returned object should be a list", object instanceof Map);
474 assertEquals(0, ((Map)object).size());
479 public void testConvertArrayToArraySpliterator() throws Exception
481 LOG.info("<-- Start Of New Test-Case!");
483 Spliterator<Object> spliterator;
485 spliterator = SimpleMapper.getArraySpliterator(get("/array/1.json"));
486 checkArraySpliterator(spliterator);
487 spliterator = SimpleMapper.getArraySpliterator(get("/array/2.json"));
488 checkArraySpliterator(spliterator);
491 void checkArraySpliterator(Spliterator<Object> spliterator) throws Exception
493 assertNotNull(spliterator);
495 final ArrayList<Object> entries = new ArrayList<>(4);
496 for (int i = 0; i < 4; i++)
498 "The created splitter should have a " + (i+1) + ". entry",
499 spliterator.tryAdvance((Object t) -> { entries.add(t); })
503 "The created splitter should have no more entries",
504 spliterator.tryAdvance((Object t) ->
506 fail("The consumer should not have been called");
509 checkPartnerPageData(entries.get(3));
513 public void testConvertArrayToList() throws Exception
515 LOG.info("<-- Start Of New Test-Case!");
519 list = SimpleMapper.convertArray(get("/array/1.json"));
521 assertEquals(4, list.size());
522 checkPartnerPageData(list.get(3));
523 list = SimpleMapper.convertArray(get("/array/2.json"));
525 assertEquals(4, list.size());
526 checkPartnerPageData(list.get(3));
530 public void testConvertArrayToObjectSpliterator() throws Exception
532 LOG.info("<-- Start Of New Test-Case!");
536 SimpleMapper.getObjectSpliterator(get("/array/1.json"));
537 fail("it must not be possible, to get an object-spliterator for an array");
539 catch(IllegalArgumentException e)
541 LOG.trace("expected exception", e);
546 SimpleMapper.getObjectSpliterator(get("/array/2.json"));
547 fail("it must not be possible, to get an object-spliterator for an array");
549 catch(IllegalArgumentException e)
551 LOG.trace("expected exception", e);
556 public void testConvertArrayToMap() throws Exception
558 LOG.info("<-- Start Of New Test-Case!");
562 SimpleMapper.convertObject(get("/array/1.json"));
563 fail("it must not be possible, to get a map for an array");
565 catch(IllegalArgumentException e)
567 LOG.trace("expected exception", e);
572 SimpleMapper.convertObject(get("/array/2.json"));
573 fail("it must not be possible, to get a map for an array");
575 catch(IllegalArgumentException e)
577 LOG.trace("expected exception", e);
582 public void testConvertArray() throws Exception
584 LOG.info("<-- Start Of New Test-Case!");
589 object = SimpleMapper.convert(get("/array/1.json"));
590 assertNotNull(object);
591 assertTrue("the returned object should be a list", object instanceof List);
592 list = (List<Object>)object;
593 assertEquals(4, list.size());
594 checkPartnerPageData(list.get(3));
595 object = SimpleMapper.convert(get("/array/2.json"));
596 assertNotNull(object);
597 assertTrue("the returned object should be a list", object instanceof List);
598 list = (List<Object>)object;
599 assertEquals(4, list.size());
600 checkPartnerPageData(list.get(3));
605 public void testConvertObjectToArraySpliterator() throws Exception
607 LOG.info("<-- Start Of New Test-Case!");
611 SimpleMapper.getArraySpliterator(get("/object/1.json"));
612 fail("it must not be possible, to get an array-spliterator for an object");
614 catch(IllegalArgumentException e)
616 LOG.trace("expected exception", e);
621 SimpleMapper.getArraySpliterator(get("/object/2.json"));
622 fail("it must not be possible, to get an array-spliterator for an object");
624 catch(IllegalArgumentException e)
626 LOG.trace("expected exception", e);
631 public void testConvertObjectToList() throws Exception
633 LOG.info("<-- Start Of New Test-Case!");
637 SimpleMapper.convertArray(get("/object/1.json"));
638 fail("it must not be possible, to get a list for an object");
640 catch(IllegalArgumentException e)
642 LOG.trace("expected exception", e);
647 SimpleMapper.convertArray(get("/object/2.json"));
648 fail("it must not be possible, to get a list for an object");
650 catch(IllegalArgumentException e)
652 LOG.trace("expected exception", e);
657 public void testConvertObjectToObjectSpliterator() throws Exception
659 LOG.info("<-- Start Of New Test-Case!");
661 Spliterator<Entry<String, Object>> spliterator;
663 spliterator = SimpleMapper.getObjectSpliterator(get("/object/1.json"));
664 checkObjectSpliterator(spliterator);
665 spliterator = SimpleMapper.getObjectSpliterator(get("/object/2.json"));
666 checkObjectSpliterator(spliterator);
669 void checkObjectSpliterator(Spliterator<Entry<String, Object>> spliterator)
673 assertNotNull(spliterator);
675 final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
676 for (int i = 0; i < 4; i++)
678 "The created splitter should have a " + (i+1) + ". entry",
679 spliterator.tryAdvance((Entry<String, Object> e) -> {
680 map.put(e.getKey(), e.getValue());
684 "The created splitter should have no more entries",
685 spliterator.tryAdvance((Object t) ->
687 fail("The consumer should not have been called");
694 public void testConvertObjectToMap() throws Exception
696 LOG.info("<-- Start Of New Test-Case!");
698 Map<String, Object> map;
700 map = SimpleMapper.convertObject(get("/object/1.json"));
702 assertEquals(4, map.size());
704 map = SimpleMapper.convertObject(get("/object/2.json"));
706 assertEquals(4, map.size());
711 public void testConvertObject() throws Exception
713 LOG.info("<-- Start Of New Test-Case!");
716 Map<String, Object> map;
718 object = SimpleMapper.convert(get("/object/1.json"));
719 assertNotNull(object);
720 assertTrue("the returned object should be a map", object instanceof Map);
721 map = (Map<String, Object>)object;
722 assertEquals(4, map.size());
724 object = SimpleMapper.convert(get("/object/2.json"));
725 assertNotNull(object);
726 assertTrue("the returned object should be a map", object instanceof Map);
727 map = (Map<String, Object>)object;
728 assertEquals(4, map.size());
732 void checkVariables(Map<String, Object> map)
734 Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
735 Entry<String, Object> entry;
737 assertTrue("The map should have a first element", iterator.hasNext());
738 entry = iterator.next();
739 assertEquals("bootstrap", entry.getKey());
740 assertEquals("bootstrap.min.css", entry.getValue());
742 assertTrue("The map should have a second element", iterator.hasNext());
743 entry = iterator.next();
744 assertEquals("pages", entry.getKey());
746 "The pages-variable should be of type List",
747 entry.getValue() instanceof List
749 assertEquals(4, ((List)entry.getValue()).size());
750 checkPartnerPageData(((List)entry.getValue()).get(3));
752 assertTrue("The map should have a third element", iterator.hasNext());
753 entry = iterator.next();
754 assertEquals("footer", entry.getKey());
756 "The footer-variable should be of type List",
757 entry.getValue() instanceof List
759 assertEquals(3, ((List)entry.getValue()).size());
761 assertTrue("The map should have a third element", iterator.hasNext());
762 entry = iterator.next();
763 assertEquals("sponsorship", entry.getKey());
765 "The sponsorship-variable should be of type List",
766 entry.getValue() instanceof List
768 assertEquals(3, ((List)entry.getValue()).size());
772 void checkPartnerPageData(Object page)
774 assertTrue("The page-data should be of type Map", page instanceof Map);
775 Map<String, Object> map = (Map<String, Object>)page;
776 assertEquals(4, map.size());
777 assertEquals("/partner.html", map.get("uri"));
779 "The page-data should have an entry \"children\" of type Map",
780 map.get("children") instanceof Map
782 map = (Map<String, Object>)map.get("children");
783 assertEquals(6, map.size());
785 "The children-map should haven an entry \"/partner/juplo.html\" of type Map",
786 map.get("/partner/juplo.html") instanceof Map
788 map = (Map<String, Object>)map.get("/partner/juplo.html");
789 assertEquals(2, map.size());
791 "Wir sind Unterstützer der Nerd-Plattform juplo.de",
797 private JsonParser get(String resource) throws IOException
799 InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);
800 return factory.createParser(is);