+++ /dev/null
-package de.juplo.autoconfigure;
-
-
-import com.fasterxml.jackson.core.JsonFactory;
-import de.juplo.jackson.SimpleMapperService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.context.annotation.Bean;
-
-
-
-/**
- *
- * @author Kai Moritz
- */
-@ConditionalOnMissingBean(SimpleMapperService.class)
-public class SimpleMapperServiceAutoConfiguration
-{
- private final Logger LOG =
- LoggerFactory.getLogger(SimpleMapperServiceAutoConfiguration.class);
-
-
- @Bean
- public SimpleMapperService simpleMapperService(JsonFactory factory)
- {
- LOG.info("No SimpleMapperService configured: creating instance.");
- return new SimpleMapperService(factory);
- }
-
- @Bean
- @ConditionalOnMissingBean(JsonFactory.class)
- public JsonFactory jsonFactory()
- {
- LOG.info("No JsonFactory configured: configuring default factory.");
- return new JsonFactory();
- }
-}
+++ /dev/null
-package de.juplo.jackson;
-
-
-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.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.NoSuchElementException;
-import java.util.Spliterator;
-import static java.util.Spliterator.IMMUTABLE;
-import java.util.function.Consumer;
-import java.util.stream.Stream;
-import java.util.stream.StreamSupport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
-
-/**
- *
- * @author kai
- */
-public abstract class SimpleMapper
-{
- private static final Logger LOG =
- LoggerFactory.getLogger(SimpleMapper.class);
-
-
- public static Spliterator<Object> getArraySpliterator(final JsonParser parser)
- throws
- IOException
- {
- JsonToken token = parser.nextToken();
-
- if (token == null)
- return null;
-
- if (!JsonToken.START_ARRAY.equals(token))
- fail(parser, "The root-element must be an array!");
-
- return new Spliterator<Object>()
- {
- @Override
- public boolean tryAdvance(Consumer<? super Object> action)
- {
- try
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- fail(parser, "Unexpected end of data!");
- if (JsonToken.END_ARRAY.equals(token))
- {
- if (parser.nextToken() != null)
- fail(parser, "unexpected data after parsed array");
- return false;
- }
- action.accept(convertInternal(parser));
- return true;
- }
- catch (IOException e)
- {
- throw new IllegalArgumentException(e);
- }
- }
-
- @Override
- public Spliterator<Object> trySplit()
- {
- return null;
- }
-
- @Override
- public long estimateSize()
- {
- return Long.MAX_VALUE;
- }
-
- @Override
- public int characteristics()
- {
- return IMMUTABLE;
- }
- };
- }
-
- public static Stream<Object> getArrayStream(final JsonParser parser)
- throws
- IOException
- {
- return StreamSupport.stream(getArraySpliterator(parser), false);
- }
-
- public static Iterator<Object> getArrayIterator(final JsonParser parser)
- throws
- IOException
- {
- Spliterator<Object> spliterator = getArraySpliterator(parser);
- return new Iterator<Object>()
- {
- private Object next = null;
-
-
- @Override
- public boolean hasNext()
- {
- if (next != null)
- return true;
-
- return spliterator.tryAdvance((Object o) -> { next = o; });
- }
-
- @Override
- public Object next()
- {
- if (next == null && !hasNext())
- throw new NoSuchElementException();
- Object o = next;
- next = null;
- return o;
- }
- };
- }
-
-
- public static Spliterator<Entry<String, Object>> getObjectSpliterator(final JsonParser parser)
- throws
- IOException
- {
- JsonToken token = parser.nextToken();
-
- if (token == null)
- return null;
-
- if (!JsonToken.START_OBJECT.equals(token))
- fail(parser, "The root-element must be an object!");
-
- return new Spliterator<Entry<String, Object>>()
- {
- @Override
- public boolean tryAdvance(Consumer<? super Entry<String, Object>> action)
- {
- try
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- fail(parser, "Unexpected end of data!");
- if (JsonToken.END_OBJECT.equals(token))
- {
- if (parser.nextToken() != null)
- fail(parser, "unexpected data after parsed object");
- return false;
- }
- if (!JsonToken.FIELD_NAME.equals(token))
- fail(parser, "expected a field-name");
- final String key = parser.getText();
- parser.nextToken();
- final Object value = convertInternal(parser);
- action.accept(new Entry<String, Object>()
- {
- @Override
- public String getKey()
- {
- return key;
- }
-
- @Override
- public Object getValue()
- {
- return value;
- }
-
- @Override
- public Object setValue(Object value)
- {
- throw new UnsupportedOperationException("Not supported.");
- }
- });
- return true;
- }
- catch (IOException e)
- {
- throw new IllegalArgumentException(e);
- }
- }
-
- @Override
- public Spliterator<Entry<String, Object>> trySplit()
- {
- return null;
- }
-
- @Override
- public long estimateSize()
- {
- return Long.MAX_VALUE;
- }
-
- @Override
- public int characteristics()
- {
- return IMMUTABLE;
- }
- };
- }
-
- public static Stream<Entry<String, Object>> getObjectStream(final JsonParser parser)
- throws
- IOException
- {
- return StreamSupport.stream(getObjectSpliterator(parser), false);
- }
-
- public static Iterator<Entry<String, Object>> getObjectIterator(
- final JsonParser parser
- )
- throws
- IOException
- {
- Spliterator<Entry<String, Object>> spliterator = getObjectSpliterator(parser);
- return new Iterator<Entry<String, Object>>()
- {
- private Entry<String, Object> next = null;
-
-
- @Override
- public boolean hasNext()
- {
- if (next != null)
- return true;
-
- return spliterator.tryAdvance((Entry<String, Object> e) -> { next = e; });
- }
-
- @Override
- public Entry<String, Object> next()
- {
- if (next == null && !hasNext())
- throw new NoSuchElementException();
- Entry<String, Object> e = next;
- next = null;
- return e;
- }
- };
- }
-
-
- public static List<Object> convertArray(JsonParser parser) throws IOException
- {
- JsonToken token = parser.nextToken();
-
- if (token == null)
- return null;
-
- if (!JsonToken.START_ARRAY.equals(token))
- fail(parser, "The root-element must be an array!");
-
- List<Object> array = convertArrayInternal(parser);
-
- if (parser.nextToken() != null)
- fail(parser, "unexpected data after parsed array");
-
- return array;
- }
-
- public static Map<String, Object> convertObject(JsonParser parser) throws IOException
- {
- JsonToken token = parser.nextToken();
-
- if (token == null)
- return null;
-
- if (!JsonToken.START_OBJECT.equals(token))
- fail(parser, "The root-element must be an object!");
-
- Map<String, Object> object = convertObjectInternal(parser);
-
- if (parser.nextToken() != null)
- fail(parser, "unexpected data after parsed object");
-
- return object;
- }
-
- public static Object convert(JsonParser parser) throws IOException
- {
- JsonToken token = parser.nextToken();
-
- if (token == null)
- return null;
-
- switch (token)
- {
- case START_ARRAY:
- case START_OBJECT:
- break;
- default:
- fail(parser, "The root-element must be either an object or an array!");
- }
-
- Object object = convertInternal(parser);
-
- if (parser.nextToken() != null)
- fail(parser, "unexpected data after parsed object");
-
- return object;
- }
-
-
- static Object convertInternal(JsonParser parser) throws IOException
- {
- JsonToken token = parser.getCurrentToken();
- if (token == null)
- {
- fail(parser, "unexpected EOF");
- return null; // << Will never be reached, because fail always throws an exception
- }
-
- switch (token)
- {
- case VALUE_STRING: return parser.getText();
- case VALUE_NUMBER_INT: return parser.getIntValue();
- case VALUE_NUMBER_FLOAT: return parser.getDoubleValue();
- case START_OBJECT: return convertObjectInternal(parser);
- case START_ARRAY: return convertArrayInternal(parser);
- case VALUE_TRUE: return Boolean.TRUE;
- case VALUE_FALSE: return Boolean.FALSE;
- case VALUE_NULL: return null;
- }
-
- fail(parser, "unexpected token " + token);
- return null; // << Will never be reached, because fail always throws an exception
- }
-
-
- static Map<String, Object> convertObjectInternal(JsonParser parser)
- throws
- IOException
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
-
- Map<String, Object> map = new LinkedHashMap<>();
-
- while (!JsonToken.END_OBJECT.equals(token))
- {
- if (!JsonToken.FIELD_NAME.equals(token))
- fail(parser, "expected a field-name");
-
- String name = parser.getText();
- parser.nextToken();
- Object value = convertInternal(parser);
- map.put(name, value);
-
- token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
- }
-
- return map;
- }
-
- static List<Object> convertArrayInternal(JsonParser parser) throws IOException
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
-
- List<Object> list = new LinkedList<>();
-
- while (!JsonToken.END_ARRAY.equals(token))
- {
- list.add(convertInternal(parser));
-
- token = parser.nextToken();
- if (token == null)
- fail(parser, "unexpected EOF");
- }
-
- return list;
- }
-
-
- static void fail(JsonParser parser, String message)
- {
- JsonLocation location = parser.getCurrentLocation();
- LOG.error(
- "{} at char-offset {} (line {}, column {})",
- message,
- location.getCharOffset(),
- location.getLineNr(),
- location.getColumnNr()
- );
- throw new IllegalArgumentException("Cannot parse JSON: " + message);
- }
-}
+++ /dev/null
-package de.juplo.jackson;
-
-
-import com.fasterxml.jackson.core.JsonFactory;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.net.URL;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Spliterator;
-import java.util.stream.Stream;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.util.Assert;
-
-
-
-/**
- *
- * @author kai
- */
-public class SimpleMapperService
- extends
- SimpleMapper
- implements
- InitializingBean
-{
- @Autowired(required = false)
- private JsonFactory factory;
-
-
- public SimpleMapperService() {}
-
- public SimpleMapperService(JsonFactory factory)
- {
- this.factory = factory;
- }
-
-
- @Override
- public void afterPropertiesSet() throws Exception
- {
- Assert.notNull(factory, "The attribute factory must be set!");
- }
-
- public JsonFactory getFactory()
- {
- return factory;
- }
-
-
- public Spliterator<Object> getArraySpliterator(File file)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(file));
- }
-
- public Spliterator<Object> getArraySpliterator(InputStream is)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(is));
- }
-
- public Spliterator<Object> getArraySpliterator(Reader r)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(r));
- }
-
- public Spliterator<Object> getArraySpliterator(String content)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(content));
- }
-
- public Spliterator<Object> getArraySpliterator(URL url)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(url));
- }
-
- public Spliterator<Object> getArraySpliterator(byte[] data)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(data));
- }
-
- public Spliterator<Object> getArraySpliterator(char[] content)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(content));
- }
-
- public Spliterator<Object> getArraySpliterator(byte[] data, int offset, int len)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(data, offset, len));
- }
-
- public Spliterator<Object> getArraySpliterator(char[] data, int offset, int len)
- throws
- IOException
- {
- return getArraySpliterator(factory.createParser(data, offset, len));
- }
-
- public Stream<Object> getArrayStream(File file)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(file));
- }
-
- public Stream<Object> getArrayStream(InputStream is)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(is));
- }
-
- public Stream<Object> getArrayStream(Reader r)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(r));
- }
-
- public Stream<Object> getArrayStream(String content)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(content));
- }
-
- public Stream<Object> getArrayStream(URL url)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(url));
- }
-
- public Stream<Object> getArrayStream(byte[] data)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(data));
- }
-
- public Stream<Object> getArrayStream(char[] content)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(content));
- }
-
- public Stream<Object> getArrayStream(byte[] data, int offset, int len)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(data, offset, len));
- }
-
- public Stream<Object> getArrayStream(char[] data, int offset, int len)
- throws
- IOException
- {
- return getArrayStream(factory.createParser(data, offset, len));
- }
-
- public Iterator<Object> getArrayIterator(File file)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(file));
- }
-
- public Iterator<Object> getArrayIterator(InputStream is)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(is));
- }
-
- public Iterator<Object> getArrayIterator(Reader r)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(r));
- }
-
- public Iterator<Object> getArrayIterator(String content)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(content));
- }
-
- public Iterator<Object> getArrayIterator(URL url)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(url));
- }
-
- public Iterator<Object> getArrayIterator(byte[] data)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(data));
- }
-
- public Iterator<Object> getArrayIterator(char[] content)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(content));
- }
-
- public Iterator<Object> getArrayIterator(byte[] data, int offset, int len)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(data, offset, len));
- }
-
- public Iterator<Object> getArrayIterator(char[] data, int offset, int len)
- throws
- IOException
- {
- return getArrayIterator(factory.createParser(data, offset, len));
- }
-
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(File file)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(file));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(InputStream is)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(is));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(Reader r)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(r));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(String content)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(content));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(URL url)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(url));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(byte[] data)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(data));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(char[] content)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(content));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(byte[] data, int offset, int len)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(data, offset, len));
- }
-
- public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(char[] data, int offset, int len)
- throws
- IOException
- {
- return getObjectSpliterator(factory.createParser(data, offset, len));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(File file)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(file));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(InputStream is)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(is));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(Reader r)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(r));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(String content)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(content));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(URL url)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(url));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(byte[] data)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(data));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(char[] content)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(content));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(byte[] data, int offset, int len)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(data, offset, len));
- }
-
- public Stream<Map.Entry<String, Object>> getObjectStream(char[] data, int offset, int len)
- throws
- IOException
- {
- return getObjectStream(factory.createParser(data, offset, len));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(File file)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(file));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(InputStream is)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(is));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(Reader r)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(r));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(String content)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(content));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(URL url)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(url));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(byte[] data)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(data));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(char[] content)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(content));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(byte[] data, int offset, int len)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(data, offset, len));
- }
-
- public Iterator<Map.Entry<String, Object>> getObjectIterator(char[] data, int offset, int len)
- throws
- IOException
- {
- return getObjectIterator(factory.createParser(data, offset, len));
- }
-
-
- public List<Object> convertArray(File file) throws Exception
- {
- return convertArray(factory.createParser(file));
- }
-
- public List<Object> convertArray(InputStream is) throws Exception
- {
- return convertArray(factory.createParser(is));
- }
-
- public List<Object> convertArray(Reader r) throws Exception
- {
- return convertArray(factory.createParser(r));
- }
-
- public List<Object> convertArray(String content) throws Exception
- {
- return convertArray(factory.createParser(content));
- }
-
- public List<Object> convertArray(URL url) throws Exception
- {
- return convertArray(factory.createParser(url));
- }
-
- public List<Object> convertArray(byte[] data) throws Exception
- {
- return convertArray(factory.createParser(data));
- }
-
- public List<Object> convertArray(char[] content) throws Exception
- {
- return convertArray(factory.createParser(content));
- }
-
- public List<Object> convertArray(byte[] data, int offset, int len)
- throws
- Exception
- {
- return convertArray(factory.createParser(data, offset, len));
- }
-
- public List<Object> convertArray(char[] content, int offset, int len)
- throws
- Exception
- {
- return convertArray(factory.createParser(content, offset, len));
- }
-
- public Map<String, Object> convertObject(File file) throws Exception
- {
- return convertObject(factory.createParser(file));
- }
-
- public Map<String, Object> convertObject(InputStream is) throws Exception
- {
- return convertObject(factory.createParser(is));
- }
-
- public Map<String, Object> convertObject(Reader r) throws Exception
- {
- return convertObject(factory.createParser(r));
- }
-
- public Map<String, Object> convertObject(String content) throws Exception
- {
- return convertObject(factory.createParser(content));
- }
-
- public Map<String, Object> convertObject(URL url) throws Exception
- {
- return convertObject(factory.createParser(url));
- }
-
- public Map<String, Object> convertObject(byte[] data) throws Exception
- {
- return convertObject(factory.createParser(data));
- }
-
- public Map<String, Object> convertObject(char[] content) throws Exception
- {
- return convertObject(factory.createParser(content));
- }
-
- public Map<String, Object> convertObject(byte[] data, int offset, int len)
- throws
- Exception
- {
- return convertObject(factory.createParser(data, offset, len));
- }
-
- public Map<String, Object> convertObject(char[] content, int offset, int len)
- throws
- Exception
- {
- return convertObject(factory.createParser(content, offset, len));
- }
-
- public Object convert(File file) throws Exception
- {
- return convert(factory.createParser(file));
- }
-
- public Object convert(InputStream is) throws Exception
- {
- return convert(factory.createParser(is));
- }
-
- public Object convert(Reader r) throws Exception
- {
- return convert(factory.createParser(r));
- }
-
- public Object convert(String content) throws Exception
- {
- return convert(factory.createParser(content));
- }
-
- public Object convert(URL url) throws Exception
- {
- return convert(factory.createParser(url));
- }
-
- public Object convert(byte[] data) throws Exception
- {
- return convert(factory.createParser(data));
- }
-
- public Object convert(char[] content) throws Exception
- {
- return convert(factory.createParser(content));
- }
-
- public Object convert(byte[] data, int offset, int len)
- throws
- Exception
- {
- return convert(factory.createParser(data, offset, len));
- }
-
- public Object convert(char[] content, int offset, int len)
- throws
- Exception
- {
- return convert(factory.createParser(content, offset, len));
- }
-}
--- /dev/null
+package de.juplo.simplemapper;
+
+
+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.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.NoSuchElementException;
+import java.util.Spliterator;
+import static java.util.Spliterator.IMMUTABLE;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+/**
+ *
+ * @author kai
+ */
+public abstract class SimpleMapper
+{
+ private static final Logger LOG =
+ LoggerFactory.getLogger(SimpleMapper.class);
+
+
+ public static Spliterator<Object> getArraySpliterator(final JsonParser parser)
+ throws
+ IOException
+ {
+ JsonToken token = parser.nextToken();
+
+ if (token == null)
+ return null;
+
+ if (!JsonToken.START_ARRAY.equals(token))
+ fail(parser, "The root-element must be an array!");
+
+ return new Spliterator<Object>()
+ {
+ @Override
+ public boolean tryAdvance(Consumer<? super Object> action)
+ {
+ try
+ {
+ JsonToken token = parser.nextToken();
+ if (token == null)
+ fail(parser, "Unexpected end of data!");
+ if (JsonToken.END_ARRAY.equals(token))
+ {
+ if (parser.nextToken() != null)
+ fail(parser, "unexpected data after parsed array");
+ return false;
+ }
+ action.accept(convertInternal(parser));
+ return true;
+ }
+ catch (IOException e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ @Override
+ public Spliterator<Object> trySplit()
+ {
+ return null;
+ }
+
+ @Override
+ public long estimateSize()
+ {
+ return Long.MAX_VALUE;
+ }
+
+ @Override
+ public int characteristics()
+ {
+ return IMMUTABLE;
+ }
+ };
+ }
+
+ public static Stream<Object> getArrayStream(final JsonParser parser)
+ throws
+ IOException
+ {
+ return StreamSupport.stream(getArraySpliterator(parser), false);
+ }
+
+ public static Iterator<Object> getArrayIterator(final JsonParser parser)
+ throws
+ IOException
+ {
+ Spliterator<Object> spliterator = getArraySpliterator(parser);
+ return new Iterator<Object>()
+ {
+ private Object next = null;
+
+
+ @Override
+ public boolean hasNext()
+ {
+ if (next != null)
+ return true;
+
+ return spliterator.tryAdvance((Object o) -> { next = o; });
+ }
+
+ @Override
+ public Object next()
+ {
+ if (next == null && !hasNext())
+ throw new NoSuchElementException();
+ Object o = next;
+ next = null;
+ return o;
+ }
+ };
+ }
+
+
+ public static Spliterator<Entry<String, Object>> getObjectSpliterator(final JsonParser parser)
+ throws
+ IOException
+ {
+ JsonToken token = parser.nextToken();
+
+ if (token == null)
+ return null;
+
+ if (!JsonToken.START_OBJECT.equals(token))
+ fail(parser, "The root-element must be an object!");
+
+ return new Spliterator<Entry<String, Object>>()
+ {
+ @Override
+ public boolean tryAdvance(Consumer<? super Entry<String, Object>> action)
+ {
+ try
+ {
+ JsonToken token = parser.nextToken();
+ if (token == null)
+ fail(parser, "Unexpected end of data!");
+ if (JsonToken.END_OBJECT.equals(token))
+ {
+ if (parser.nextToken() != null)
+ fail(parser, "unexpected data after parsed object");
+ return false;
+ }
+ if (!JsonToken.FIELD_NAME.equals(token))
+ fail(parser, "expected a field-name");
+ final String key = parser.getText();
+ parser.nextToken();
+ final Object value = convertInternal(parser);
+ action.accept(new Entry<String, Object>()
+ {
+ @Override
+ public String getKey()
+ {
+ return key;
+ }
+
+ @Override
+ public Object getValue()
+ {
+ return value;
+ }
+
+ @Override
+ public Object setValue(Object value)
+ {
+ throw new UnsupportedOperationException("Not supported.");
+ }
+ });
+ return true;
+ }
+ catch (IOException e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ @Override
+ public Spliterator<Entry<String, Object>> trySplit()
+ {
+ return null;
+ }
+
+ @Override
+ public long estimateSize()
+ {
+ return Long.MAX_VALUE;
+ }
+
+ @Override
+ public int characteristics()
+ {
+ return IMMUTABLE;
+ }
+ };
+ }
+
+ public static Stream<Entry<String, Object>> getObjectStream(final JsonParser parser)
+ throws
+ IOException
+ {
+ return StreamSupport.stream(getObjectSpliterator(parser), false);
+ }
+
+ public static Iterator<Entry<String, Object>> getObjectIterator(
+ final JsonParser parser
+ )
+ throws
+ IOException
+ {
+ Spliterator<Entry<String, Object>> spliterator = getObjectSpliterator(parser);
+ return new Iterator<Entry<String, Object>>()
+ {
+ private Entry<String, Object> next = null;
+
+
+ @Override
+ public boolean hasNext()
+ {
+ if (next != null)
+ return true;
+
+ return spliterator.tryAdvance((Entry<String, Object> e) -> { next = e; });
+ }
+
+ @Override
+ public Entry<String, Object> next()
+ {
+ if (next == null && !hasNext())
+ throw new NoSuchElementException();
+ Entry<String, Object> e = next;
+ next = null;
+ return e;
+ }
+ };
+ }
+
+
+ public static List<Object> convertArray(JsonParser parser) throws IOException
+ {
+ JsonToken token = parser.nextToken();
+
+ if (token == null)
+ return null;
+
+ if (!JsonToken.START_ARRAY.equals(token))
+ fail(parser, "The root-element must be an array!");
+
+ List<Object> array = convertArrayInternal(parser);
+
+ if (parser.nextToken() != null)
+ fail(parser, "unexpected data after parsed array");
+
+ return array;
+ }
+
+ public static Map<String, Object> convertObject(JsonParser parser) throws IOException
+ {
+ JsonToken token = parser.nextToken();
+
+ if (token == null)
+ return null;
+
+ if (!JsonToken.START_OBJECT.equals(token))
+ fail(parser, "The root-element must be an object!");
+
+ Map<String, Object> object = convertObjectInternal(parser);
+
+ if (parser.nextToken() != null)
+ fail(parser, "unexpected data after parsed object");
+
+ return object;
+ }
+
+ public static Object convert(JsonParser parser) throws IOException
+ {
+ JsonToken token = parser.nextToken();
+
+ if (token == null)
+ return null;
+
+ switch (token)
+ {
+ case START_ARRAY:
+ case START_OBJECT:
+ break;
+ default:
+ fail(parser, "The root-element must be either an object or an array!");
+ }
+
+ Object object = convertInternal(parser);
+
+ if (parser.nextToken() != null)
+ fail(parser, "unexpected data after parsed object");
+
+ return object;
+ }
+
+
+ static Object convertInternal(JsonParser parser) throws IOException
+ {
+ JsonToken token = parser.getCurrentToken();
+ if (token == null)
+ {
+ fail(parser, "unexpected EOF");
+ return null; // << Will never be reached, because fail always throws an exception
+ }
+
+ switch (token)
+ {
+ case VALUE_STRING: return parser.getText();
+ case VALUE_NUMBER_INT: return parser.getIntValue();
+ case VALUE_NUMBER_FLOAT: return parser.getDoubleValue();
+ case START_OBJECT: return convertObjectInternal(parser);
+ case START_ARRAY: return convertArrayInternal(parser);
+ case VALUE_TRUE: return Boolean.TRUE;
+ case VALUE_FALSE: return Boolean.FALSE;
+ case VALUE_NULL: return null;
+ }
+
+ fail(parser, "unexpected token " + token);
+ return null; // << Will never be reached, because fail always throws an exception
+ }
+
+
+ static Map<String, Object> convertObjectInternal(JsonParser parser)
+ throws
+ IOException
+ {
+ JsonToken token = parser.nextToken();
+ if (token == null)
+ fail(parser, "unexpected EOF");
+
+ Map<String, Object> map = new LinkedHashMap<>();
+
+ while (!JsonToken.END_OBJECT.equals(token))
+ {
+ if (!JsonToken.FIELD_NAME.equals(token))
+ fail(parser, "expected a field-name");
+
+ String name = parser.getText();
+ parser.nextToken();
+ Object value = convertInternal(parser);
+ map.put(name, value);
+
+ token = parser.nextToken();
+ if (token == null)
+ fail(parser, "unexpected EOF");
+ }
+
+ return map;
+ }
+
+ static List<Object> convertArrayInternal(JsonParser parser) throws IOException
+ {
+ JsonToken token = parser.nextToken();
+ if (token == null)
+ fail(parser, "unexpected EOF");
+
+ List<Object> list = new LinkedList<>();
+
+ while (!JsonToken.END_ARRAY.equals(token))
+ {
+ list.add(convertInternal(parser));
+
+ token = parser.nextToken();
+ if (token == null)
+ fail(parser, "unexpected EOF");
+ }
+
+ return list;
+ }
+
+
+ static void fail(JsonParser parser, String message)
+ {
+ JsonLocation location = parser.getCurrentLocation();
+ LOG.error(
+ "{} at char-offset {} (line {}, column {})",
+ message,
+ location.getCharOffset(),
+ location.getLineNr(),
+ location.getColumnNr()
+ );
+ throw new IllegalArgumentException("Cannot parse JSON: " + message);
+ }
+}
--- /dev/null
+package de.juplo.simplemapper;
+
+
+import com.fasterxml.jackson.core.JsonFactory;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Spliterator;
+import java.util.stream.Stream;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.Assert;
+
+
+
+/**
+ *
+ * @author kai
+ */
+public class SimpleMapperService
+ extends
+ SimpleMapper
+ implements
+ InitializingBean
+{
+ @Autowired(required = false)
+ private JsonFactory factory;
+
+
+ public SimpleMapperService() {}
+
+ public SimpleMapperService(JsonFactory factory)
+ {
+ this.factory = factory;
+ }
+
+
+ @Override
+ public void afterPropertiesSet() throws Exception
+ {
+ Assert.notNull(factory, "The attribute factory must be set!");
+ }
+
+ public JsonFactory getFactory()
+ {
+ return factory;
+ }
+
+
+ public Spliterator<Object> getArraySpliterator(File file)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(file));
+ }
+
+ public Spliterator<Object> getArraySpliterator(InputStream is)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(is));
+ }
+
+ public Spliterator<Object> getArraySpliterator(Reader r)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(r));
+ }
+
+ public Spliterator<Object> getArraySpliterator(String content)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(content));
+ }
+
+ public Spliterator<Object> getArraySpliterator(URL url)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(url));
+ }
+
+ public Spliterator<Object> getArraySpliterator(byte[] data)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(data));
+ }
+
+ public Spliterator<Object> getArraySpliterator(char[] content)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(content));
+ }
+
+ public Spliterator<Object> getArraySpliterator(byte[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(data, offset, len));
+ }
+
+ public Spliterator<Object> getArraySpliterator(char[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getArraySpliterator(factory.createParser(data, offset, len));
+ }
+
+ public Stream<Object> getArrayStream(File file)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(file));
+ }
+
+ public Stream<Object> getArrayStream(InputStream is)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(is));
+ }
+
+ public Stream<Object> getArrayStream(Reader r)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(r));
+ }
+
+ public Stream<Object> getArrayStream(String content)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(content));
+ }
+
+ public Stream<Object> getArrayStream(URL url)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(url));
+ }
+
+ public Stream<Object> getArrayStream(byte[] data)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(data));
+ }
+
+ public Stream<Object> getArrayStream(char[] content)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(content));
+ }
+
+ public Stream<Object> getArrayStream(byte[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(data, offset, len));
+ }
+
+ public Stream<Object> getArrayStream(char[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getArrayStream(factory.createParser(data, offset, len));
+ }
+
+ public Iterator<Object> getArrayIterator(File file)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(file));
+ }
+
+ public Iterator<Object> getArrayIterator(InputStream is)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(is));
+ }
+
+ public Iterator<Object> getArrayIterator(Reader r)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(r));
+ }
+
+ public Iterator<Object> getArrayIterator(String content)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(content));
+ }
+
+ public Iterator<Object> getArrayIterator(URL url)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(url));
+ }
+
+ public Iterator<Object> getArrayIterator(byte[] data)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(data));
+ }
+
+ public Iterator<Object> getArrayIterator(char[] content)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(content));
+ }
+
+ public Iterator<Object> getArrayIterator(byte[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(data, offset, len));
+ }
+
+ public Iterator<Object> getArrayIterator(char[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getArrayIterator(factory.createParser(data, offset, len));
+ }
+
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(File file)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(file));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(InputStream is)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(is));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(Reader r)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(r));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(String content)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(content));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(URL url)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(url));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(byte[] data)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(data));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(char[] content)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(content));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(byte[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(data, offset, len));
+ }
+
+ public Spliterator<Map.Entry<String, Object>> getObjectSpliterator(char[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getObjectSpliterator(factory.createParser(data, offset, len));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(File file)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(file));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(InputStream is)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(is));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(Reader r)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(r));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(String content)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(content));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(URL url)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(url));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(byte[] data)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(data));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(char[] content)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(content));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(byte[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(data, offset, len));
+ }
+
+ public Stream<Map.Entry<String, Object>> getObjectStream(char[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getObjectStream(factory.createParser(data, offset, len));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(File file)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(file));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(InputStream is)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(is));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(Reader r)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(r));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(String content)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(content));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(URL url)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(url));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(byte[] data)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(data));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(char[] content)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(content));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(byte[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(data, offset, len));
+ }
+
+ public Iterator<Map.Entry<String, Object>> getObjectIterator(char[] data, int offset, int len)
+ throws
+ IOException
+ {
+ return getObjectIterator(factory.createParser(data, offset, len));
+ }
+
+
+ public List<Object> convertArray(File file) throws Exception
+ {
+ return convertArray(factory.createParser(file));
+ }
+
+ public List<Object> convertArray(InputStream is) throws Exception
+ {
+ return convertArray(factory.createParser(is));
+ }
+
+ public List<Object> convertArray(Reader r) throws Exception
+ {
+ return convertArray(factory.createParser(r));
+ }
+
+ public List<Object> convertArray(String content) throws Exception
+ {
+ return convertArray(factory.createParser(content));
+ }
+
+ public List<Object> convertArray(URL url) throws Exception
+ {
+ return convertArray(factory.createParser(url));
+ }
+
+ public List<Object> convertArray(byte[] data) throws Exception
+ {
+ return convertArray(factory.createParser(data));
+ }
+
+ public List<Object> convertArray(char[] content) throws Exception
+ {
+ return convertArray(factory.createParser(content));
+ }
+
+ public List<Object> convertArray(byte[] data, int offset, int len)
+ throws
+ Exception
+ {
+ return convertArray(factory.createParser(data, offset, len));
+ }
+
+ public List<Object> convertArray(char[] content, int offset, int len)
+ throws
+ Exception
+ {
+ return convertArray(factory.createParser(content, offset, len));
+ }
+
+ public Map<String, Object> convertObject(File file) throws Exception
+ {
+ return convertObject(factory.createParser(file));
+ }
+
+ public Map<String, Object> convertObject(InputStream is) throws Exception
+ {
+ return convertObject(factory.createParser(is));
+ }
+
+ public Map<String, Object> convertObject(Reader r) throws Exception
+ {
+ return convertObject(factory.createParser(r));
+ }
+
+ public Map<String, Object> convertObject(String content) throws Exception
+ {
+ return convertObject(factory.createParser(content));
+ }
+
+ public Map<String, Object> convertObject(URL url) throws Exception
+ {
+ return convertObject(factory.createParser(url));
+ }
+
+ public Map<String, Object> convertObject(byte[] data) throws Exception
+ {
+ return convertObject(factory.createParser(data));
+ }
+
+ public Map<String, Object> convertObject(char[] content) throws Exception
+ {
+ return convertObject(factory.createParser(content));
+ }
+
+ public Map<String, Object> convertObject(byte[] data, int offset, int len)
+ throws
+ Exception
+ {
+ return convertObject(factory.createParser(data, offset, len));
+ }
+
+ public Map<String, Object> convertObject(char[] content, int offset, int len)
+ throws
+ Exception
+ {
+ return convertObject(factory.createParser(content, offset, len));
+ }
+
+ public Object convert(File file) throws Exception
+ {
+ return convert(factory.createParser(file));
+ }
+
+ public Object convert(InputStream is) throws Exception
+ {
+ return convert(factory.createParser(is));
+ }
+
+ public Object convert(Reader r) throws Exception
+ {
+ return convert(factory.createParser(r));
+ }
+
+ public Object convert(String content) throws Exception
+ {
+ return convert(factory.createParser(content));
+ }
+
+ public Object convert(URL url) throws Exception
+ {
+ return convert(factory.createParser(url));
+ }
+
+ public Object convert(byte[] data) throws Exception
+ {
+ return convert(factory.createParser(data));
+ }
+
+ public Object convert(char[] content) throws Exception
+ {
+ return convert(factory.createParser(content));
+ }
+
+ public Object convert(byte[] data, int offset, int len)
+ throws
+ Exception
+ {
+ return convert(factory.createParser(data, offset, len));
+ }
+
+ public Object convert(char[] content, int offset, int len)
+ throws
+ Exception
+ {
+ return convert(factory.createParser(content, offset, len));
+ }
+}
--- /dev/null
+package de.juplo.simplemapper;
+
+
+import com.fasterxml.jackson.core.JsonFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+
+
+
+/**
+ *
+ * @author Kai Moritz
+ */
+@ConditionalOnMissingBean(SimpleMapperService.class)
+public class SimpleMapperServiceAutoConfiguration
+{
+ private final Logger LOG =
+ LoggerFactory.getLogger(SimpleMapperServiceAutoConfiguration.class);
+
+
+ @Bean
+ public SimpleMapperService simpleMapperService(JsonFactory factory)
+ {
+ LOG.info("No SimpleMapperService configured: creating instance.");
+ return new SimpleMapperService(factory);
+ }
+
+ @Bean
+ @ConditionalOnMissingBean(JsonFactory.class)
+ public JsonFactory jsonFactory()
+ {
+ LOG.info("No JsonFactory configured: configuring default factory.");
+ return new JsonFactory();
+ }
+}
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=de.juplo.autoconfigure.SimpleMapperServiceAutoConfiguration
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=de.juplo.simplemapper.SimpleMapperServiceAutoConfiguration
+++ /dev/null
-package de.juplo.autoconfigure;
-
-
-import com.fasterxml.jackson.core.JsonFactory;
-import de.juplo.jackson.SimpleMapperService;
-import static org.junit.Assert.assertEquals;
-import org.junit.Test;
-import org.springframework.context.annotation.Configuration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-import org.springframework.beans.factory.NoSuchBeanDefinitionException;
-import org.springframework.context.annotation.Bean;
-
-
-
-public class SimpleMapperServiceAutoConfigurationTest
-{
- private final Logger LOG =
- LoggerFactory.getLogger(SimpleMapperServiceAutoConfigurationTest.class);
-
-
- @Test
- public void emptyConfiguration()
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- ConfigurableApplicationContext context = load(EmptyConfiguration.class);
-
- SimpleMapperService service = context.getBean(SimpleMapperService.class);
- assertNotNull(service);
- assertNotNull(service.getFactory());
- JsonFactory factory = context.getBean(JsonFactory.class);
- assertNotNull(factory);
- assertEquals(factory, service.getFactory());
-
- context.close();
- }
-
- @Test
- public void factoryConfigured()
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- ConfigurableApplicationContext context = load(FactoryConfigured.class);
-
- SimpleMapperService service = context.getBean(SimpleMapperService.class);
- assertNotNull(service);
- assertNotNull(service.getFactory());
- JsonFactory factory = context.getBean(JsonFactory.class);
- assertNotNull(factory);
- assertEquals(FactoryConfigured.factory, factory);
- assertEquals(factory, service.getFactory());
-
- context.close();
- }
-
- @Test
- public void serviceConfigured()
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- ConfigurableApplicationContext context = load(ServiceConfigured.class);
-
- SimpleMapperService service = context.getBean(SimpleMapperService.class);
- assertNotNull(service);
- assertNotNull(service.getFactory());
- assertEquals(ServiceConfigured.factory, service.getFactory());
- try
- {
- context.getBean(JsonFactory.class);
- fail("A bean of type JsonFactory was found!");
- }
- catch(NoSuchBeanDefinitionException e)
- {
- LOG.trace("expected exception", e);
- }
-
- context.close();
- }
-
-
- @Configuration
- static class EmptyConfiguration
- {
- }
-
- @Configuration
- static class FactoryConfigured
- {
- static JsonFactory factory = new JsonFactory();
-
-
- @Bean
- public JsonFactory factory()
- {
- return factory;
- }
- }
-
- @Configuration
- static class ServiceConfigured
- {
- static JsonFactory factory = new JsonFactory();
- static SimpleMapperService service = new SimpleMapperService(factory);
-
-
- @Bean
- public SimpleMapperService service()
- {
- return service;
- }
- }
-
-
- private ConfigurableApplicationContext load(Class<?> config, String... pairs)
- {
- AnnotationConfigApplicationContext ctx =
- new AnnotationConfigApplicationContext();
- ctx.register(config);
- ctx.register(SimpleMapperServiceAutoConfiguration.class); // << Does not work as expected, if the autoconfiguration is registered before the configuration!
- AutoConfigurationReportLoggingInitializer report =
- new AutoConfigurationReportLoggingInitializer();
- report.initialize(ctx);
- ctx.refresh();
- return ctx;
- }
-}
+++ /dev/null
-package de.juplo.jackson;
-
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonParser;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Spliterator;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-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;
-
-
-/**
- *
- * @author Kai Moritz
- */
-public class SimpleMapperTest
-{
- private static final Logger LOG =
- LoggerFactory.getLogger(SimpleMapperTest.class);
-
-
- private final JsonFactory factory = new JsonFactory();
-
-
- @Test
- public void testConvertEmptyInputToArraySpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Spliterator<Object> spliterator;
-
- spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getArraySpliterator(get("/empty/2.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getArraySpliterator(get("/empty/3.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getArraySpliterator(get("/empty/4.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getArraySpliterator(get("/empty/5.json"));
- assertNull(spliterator);
- }
-
- @Test
- public void testConvertEmptyInputToList() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- List<Object> list;
-
- list = SimpleMapper.convertArray(get("/empty/1.json"));
- assertNull(list);
- list = SimpleMapper.convertArray(get("/empty/2.json"));
- assertNull(list);
- list = SimpleMapper.convertArray(get("/empty/3.json"));
- assertNull(list);
- list = SimpleMapper.convertArray(get("/empty/4.json"));
- assertNull(list);
- list = SimpleMapper.convertArray(get("/empty/5.json"));
- assertNull(list);
- }
-
- @Test
- public void testConvertEmptyInputToObjectSpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Spliterator<Entry<String, Object>> spliterator;
-
- spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getObjectSpliterator(get("/empty/2.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getObjectSpliterator(get("/empty/3.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getObjectSpliterator(get("/empty/4.json"));
- assertNull(spliterator);
- spliterator = SimpleMapper.getObjectSpliterator(get("/empty/5.json"));
- assertNull(spliterator);
- }
-
- @Test
- public void testConvertEmptyInputToMap() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Map<String, Object> map;
-
- map = SimpleMapper.convertObject(get("/empty/1.json"));
- assertNull(map);
- map = SimpleMapper.convertObject(get("/empty/2.json"));
- assertNull(map);
- map = SimpleMapper.convertObject(get("/empty/3.json"));
- assertNull(map);
- map = SimpleMapper.convertObject(get("/empty/4.json"));
- assertNull(map);
- map = SimpleMapper.convertObject(get("/empty/5.json"));
- assertNull(map);
- }
-
- @Test
- public void testConvertEmptyInput() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Object object;
-
- object = SimpleMapper.convert(get("/empty/1.json"));
- assertNull(object);
- object = SimpleMapper.convert(get("/empty/2.json"));
- assertNull(object);
- object = SimpleMapper.convert(get("/empty/3.json"));
- assertNull(object);
- object = SimpleMapper.convert(get("/empty/4.json"));
- assertNull(object);
- object = SimpleMapper.convert(get("/empty/5.json"));
- assertNull(object);
- }
-
-
- @Test
- public void testConvertEmptyArrayToArraySpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- 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
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- 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
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- 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.trace("expected exception", e);
- }
-
- 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.trace("expected exception", e);
- }
-
- 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.trace("expected exception", e);
- }
-
- 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.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertEmptyArrayToMap() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- 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.trace("expected exception", e);
- }
-
- 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.trace("expected exception", e);
- }
-
- 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.trace("expected exception", e);
- }
-
- 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.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertEmptyArray() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- 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());
- }
-
-
- @Test
- public void testConvertEmptyObjectToArraySpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- try
- {
- SimpleMapper.getArraySpliterator(get("/object/empty/1.json"));
- fail("it must not be possible, to get an array-spliterator for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.getArraySpliterator(get("/object/empty/2.json"));
- fail("it must not be possible, to get an array-spliterator for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.getArraySpliterator(get("/object/empty/3.json"));
- fail("it must not be possible, to get an array-spliterator for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.getArraySpliterator(get("/object/empty/4.json"));
- fail("it must not be possible, to get an array-spliterator for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertEmptyObjectToList() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- try
- {
- SimpleMapper.convertArray(get("/object/empty/1.json"));
- fail("it must not be possible, to get a list for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.convertArray(get("/object/empty/2.json"));
- fail("it must not be possible, to get a list for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.convertArray(get("/object/empty/3.json"));
- fail("it must not be possible, to get a list for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.convertArray(get("/object/empty/4.json"));
- fail("it must not be possible, to get a list for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertEmptyObjectToObjectSpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Spliterator<Entry<String, Object>> spliterator;
-
- spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/1.json"));
- assertFalse(
- "The created splitter should have no entries",
- spliterator.tryAdvance((Entry<String, Object> e) ->
- {
- fail("The consumer should never be called!");
- }));
- spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/2.json"));
- assertFalse(
- "The created splitter should have no entries",
- spliterator.tryAdvance((Entry<String, Object> e) ->
- {
- fail("The consumer should never be called!");
- }));
- spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/3.json"));
- assertFalse(
- "The created splitter should have no entries",
- spliterator.tryAdvance((Entry<String, Object> e) ->
- {
- fail("The consumer should never be called!");
- }));
- spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/4.json"));
- assertFalse(
- "The created splitter should have no entries",
- spliterator.tryAdvance((Entry<String, Object> e) ->
- {
- fail("The consumer should never be called!");
- }));
- }
-
- @Test
- public void testConvertEmptyObjectToMap() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Map<String, Object> map;
-
- map = SimpleMapper.convertObject(get("/object/empty/1.json"));
- assertEquals(0, map.size());
- map = SimpleMapper.convertObject(get("/object/empty/2.json"));
- assertEquals(0, map.size());
- map = SimpleMapper.convertObject(get("/object/empty/3.json"));
- assertEquals(0, map.size());
- map = SimpleMapper.convertObject(get("/object/empty/4.json"));
- assertEquals(0, map.size());
- }
-
- @Test
- public void testConvertEmptyObject() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Object object;
-
- object = SimpleMapper.convert(get("/object/empty/1.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a list", object instanceof Map);
- assertEquals(0, ((Map)object).size());
- object = SimpleMapper.convert(get("/object/empty/2.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a list", object instanceof Map);
- assertEquals(0, ((Map)object).size());
- object = SimpleMapper.convert(get("/object/empty/3.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a list", object instanceof Map);
- assertEquals(0, ((Map)object).size());
- object = SimpleMapper.convert(get("/object/empty/4.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a list", object instanceof Map);
- assertEquals(0, ((Map)object).size());
- }
-
-
- @Test
- public void testConvertArrayToArraySpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Spliterator<Object> spliterator;
-
- spliterator = SimpleMapper.getArraySpliterator(get("/array/1.json"));
- checkArraySpliterator(spliterator);
- spliterator = SimpleMapper.getArraySpliterator(get("/array/2.json"));
- checkArraySpliterator(spliterator);
- }
-
- void checkArraySpliterator(Spliterator<Object> spliterator) throws Exception
- {
- assertNotNull(spliterator);
-
- final ArrayList<Object> entries = new ArrayList<>(4);
- for (int i = 0; i < 4; i++)
- assertTrue(
- "The created splitter should have a " + (i+1) + ". entry",
- spliterator.tryAdvance((Object t) -> { entries.add(t); })
- );
-
- assertFalse(
- "The created splitter should have no more entries",
- spliterator.tryAdvance((Object t) ->
- {
- fail("The consumer should not have been called");
- }));
-
- checkPartnerPageData(entries.get(3));
- }
-
- @Test
- public void testConvertArrayToList() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- List<Object> list;
-
- list = SimpleMapper.convertArray(get("/array/1.json"));
- assertNotNull(list);
- assertEquals(4, list.size());
- checkPartnerPageData(list.get(3));
- list = SimpleMapper.convertArray(get("/array/2.json"));
- assertNotNull(list);
- assertEquals(4, list.size());
- checkPartnerPageData(list.get(3));
- }
-
- @Test
- public void testConvertArrayToObjectSpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- try
- {
- SimpleMapper.getObjectSpliterator(get("/array/1.json"));
- fail("it must not be possible, to get an object-spliterator for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.getObjectSpliterator(get("/array/2.json"));
- fail("it must not be possible, to get an object-spliterator for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertArrayToMap() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- try
- {
- SimpleMapper.convertObject(get("/array/1.json"));
- fail("it must not be possible, to get a map for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.convertObject(get("/array/2.json"));
- fail("it must not be possible, to get a map for an array");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertArray() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Object object;
- List<Object> list;
-
- object = SimpleMapper.convert(get("/array/1.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a list", object instanceof List);
- list = (List<Object>)object;
- assertEquals(4, list.size());
- checkPartnerPageData(list.get(3));
- object = SimpleMapper.convert(get("/array/2.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a list", object instanceof List);
- list = (List<Object>)object;
- assertEquals(4, list.size());
- checkPartnerPageData(list.get(3));
- }
-
-
- @Test
- public void testConvertObjectToArraySpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- try
- {
- SimpleMapper.getArraySpliterator(get("/object/1.json"));
- fail("it must not be possible, to get an array-spliterator for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.getArraySpliterator(get("/object/2.json"));
- fail("it must not be possible, to get an array-spliterator for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertObjectToList() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- try
- {
- SimpleMapper.convertArray(get("/object/1.json"));
- fail("it must not be possible, to get a list for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
-
- try
- {
- SimpleMapper.convertArray(get("/object/2.json"));
- fail("it must not be possible, to get a list for an object");
- }
- catch(IllegalArgumentException e)
- {
- LOG.trace("expected exception", e);
- }
- }
-
- @Test
- public void testConvertObjectToObjectSpliterator() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Spliterator<Entry<String, Object>> spliterator;
-
- spliterator = SimpleMapper.getObjectSpliterator(get("/object/1.json"));
- checkObjectSpliterator(spliterator);
- spliterator = SimpleMapper.getObjectSpliterator(get("/object/2.json"));
- checkObjectSpliterator(spliterator);
- }
-
- void checkObjectSpliterator(Spliterator<Entry<String, Object>> spliterator)
- throws
- Exception
- {
- assertNotNull(spliterator);
-
- final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
- for (int i = 0; i < 4; i++)
- assertTrue(
- "The created splitter should have a " + (i+1) + ". entry",
- spliterator.tryAdvance((Entry<String, Object> e) -> {
- map.put(e.getKey(), e.getValue());
- }));
-
- assertFalse(
- "The created splitter should have no more entries",
- spliterator.tryAdvance((Object t) ->
- {
- fail("The consumer should not have been called");
- }));
-
- checkVariables(map);
- }
-
- @Test
- public void testConvertObjectToMap() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Map<String, Object> map;
-
- map = SimpleMapper.convertObject(get("/object/1.json"));
- assertNotNull(map);
- assertEquals(4, map.size());
- checkVariables(map);
- map = SimpleMapper.convertObject(get("/object/2.json"));
- assertNotNull(map);
- assertEquals(4, map.size());
- checkVariables(map);
- }
-
- @Test
- public void testConvertObject() throws Exception
- {
- LOG.info("<-- Start Of New Test-Case!");
-
- Object object;
- Map<String, Object> map;
-
- object = SimpleMapper.convert(get("/object/1.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a map", object instanceof Map);
- map = (Map<String, Object>)object;
- assertEquals(4, map.size());
- checkVariables(map);
- object = SimpleMapper.convert(get("/object/2.json"));
- assertNotNull(object);
- assertTrue("the returned object should be a map", object instanceof Map);
- map = (Map<String, Object>)object;
- assertEquals(4, map.size());
- checkVariables(map);
- }
-
- void checkVariables(Map<String, Object> map)
- {
- Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
- Entry<String, Object> entry;
-
- assertTrue("The map should have a first element", iterator.hasNext());
- entry = iterator.next();
- assertEquals("bootstrap", entry.getKey());
- assertEquals("bootstrap.min.css", entry.getValue());
-
- assertTrue("The map should have a second element", iterator.hasNext());
- entry = iterator.next();
- assertEquals("pages", entry.getKey());
- assertTrue(
- "The pages-variable should be of type List",
- entry.getValue() instanceof List
- );
- assertEquals(4, ((List)entry.getValue()).size());
- checkPartnerPageData(((List)entry.getValue()).get(3));
-
- assertTrue("The map should have a third element", iterator.hasNext());
- entry = iterator.next();
- assertEquals("footer", entry.getKey());
- assertTrue(
- "The footer-variable should be of type List",
- entry.getValue() instanceof List
- );
- assertEquals(3, ((List)entry.getValue()).size());
-
- assertTrue("The map should have a third element", iterator.hasNext());
- entry = iterator.next();
- assertEquals("sponsorship", entry.getKey());
- assertTrue(
- "The sponsorship-variable should be of type List",
- entry.getValue() instanceof List
- );
- assertEquals(3, ((List)entry.getValue()).size());
- }
-
-
- void checkPartnerPageData(Object page)
- {
- assertTrue("The page-data should be of type Map", page instanceof Map);
- Map<String, Object> map = (Map<String, Object>)page;
- assertEquals(4, map.size());
- assertEquals("/partner.html", map.get("uri"));
- assertTrue(
- "The page-data should have an entry \"children\" of type Map",
- map.get("children") instanceof Map
- );
- map = (Map<String, Object>)map.get("children");
- assertEquals(6, map.size());
- assertTrue(
- "The children-map should haven an entry \"/partner/juplo.html\" of type Map",
- map.get("/partner/juplo.html") instanceof Map
- );
- map = (Map<String, Object>)map.get("/partner/juplo.html");
- assertEquals(2, map.size());
- assertEquals(
- "Wir sind Unterstützer der Nerd-Plattform juplo.de",
- map.get("title")
- );
- }
-
-
- private JsonParser get(String resource) throws IOException
- {
- InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);
- return factory.createParser(is);
- }
-}
--- /dev/null
+package de.juplo.simplemapper;
+
+
+import com.fasterxml.jackson.core.JsonFactory;
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import org.springframework.context.annotation.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.context.annotation.Bean;
+
+
+
+public class SimpleMapperServiceAutoConfigurationTest
+{
+ private final Logger LOG =
+ LoggerFactory.getLogger(SimpleMapperServiceAutoConfigurationTest.class);
+
+
+ @Test
+ public void emptyConfiguration()
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ ConfigurableApplicationContext context = load(EmptyConfiguration.class);
+
+ SimpleMapperService service = context.getBean(SimpleMapperService.class);
+ assertNotNull(service);
+ assertNotNull(service.getFactory());
+ JsonFactory factory = context.getBean(JsonFactory.class);
+ assertNotNull(factory);
+ assertEquals(factory, service.getFactory());
+
+ context.close();
+ }
+
+ @Test
+ public void factoryConfigured()
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ ConfigurableApplicationContext context = load(FactoryConfigured.class);
+
+ SimpleMapperService service = context.getBean(SimpleMapperService.class);
+ assertNotNull(service);
+ assertNotNull(service.getFactory());
+ JsonFactory factory = context.getBean(JsonFactory.class);
+ assertNotNull(factory);
+ assertEquals(FactoryConfigured.factory, factory);
+ assertEquals(factory, service.getFactory());
+
+ context.close();
+ }
+
+ @Test
+ public void serviceConfigured()
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ ConfigurableApplicationContext context = load(ServiceConfigured.class);
+
+ SimpleMapperService service = context.getBean(SimpleMapperService.class);
+ assertNotNull(service);
+ assertNotNull(service.getFactory());
+ assertEquals(ServiceConfigured.factory, service.getFactory());
+ try
+ {
+ context.getBean(JsonFactory.class);
+ fail("A bean of type JsonFactory was found!");
+ }
+ catch(NoSuchBeanDefinitionException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ context.close();
+ }
+
+
+ @Configuration
+ static class EmptyConfiguration
+ {
+ }
+
+ @Configuration
+ static class FactoryConfigured
+ {
+ static JsonFactory factory = new JsonFactory();
+
+
+ @Bean
+ public JsonFactory factory()
+ {
+ return factory;
+ }
+ }
+
+ @Configuration
+ static class ServiceConfigured
+ {
+ static JsonFactory factory = new JsonFactory();
+ static SimpleMapperService service = new SimpleMapperService(factory);
+
+
+ @Bean
+ public SimpleMapperService service()
+ {
+ return service;
+ }
+ }
+
+
+ private ConfigurableApplicationContext load(Class<?> config, String... pairs)
+ {
+ AnnotationConfigApplicationContext ctx =
+ new AnnotationConfigApplicationContext();
+ ctx.register(config);
+ ctx.register(SimpleMapperServiceAutoConfiguration.class); // << Does not work as expected, if the autoconfiguration is registered before the configuration!
+ AutoConfigurationReportLoggingInitializer report =
+ new AutoConfigurationReportLoggingInitializer();
+ report.initialize(ctx);
+ ctx.refresh();
+ return ctx;
+ }
+}
--- /dev/null
+package de.juplo.simplemapper;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Spliterator;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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;
+
+
+/**
+ *
+ * @author Kai Moritz
+ */
+public class SimpleMapperTest
+{
+ private static final Logger LOG =
+ LoggerFactory.getLogger(SimpleMapperTest.class);
+
+
+ private final JsonFactory factory = new JsonFactory();
+
+
+ @Test
+ public void testConvertEmptyInputToArraySpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Spliterator<Object> spliterator;
+
+ spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getArraySpliterator(get("/empty/2.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getArraySpliterator(get("/empty/3.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getArraySpliterator(get("/empty/4.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getArraySpliterator(get("/empty/5.json"));
+ assertNull(spliterator);
+ }
+
+ @Test
+ public void testConvertEmptyInputToList() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ List<Object> list;
+
+ list = SimpleMapper.convertArray(get("/empty/1.json"));
+ assertNull(list);
+ list = SimpleMapper.convertArray(get("/empty/2.json"));
+ assertNull(list);
+ list = SimpleMapper.convertArray(get("/empty/3.json"));
+ assertNull(list);
+ list = SimpleMapper.convertArray(get("/empty/4.json"));
+ assertNull(list);
+ list = SimpleMapper.convertArray(get("/empty/5.json"));
+ assertNull(list);
+ }
+
+ @Test
+ public void testConvertEmptyInputToObjectSpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Spliterator<Entry<String, Object>> spliterator;
+
+ spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getObjectSpliterator(get("/empty/2.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getObjectSpliterator(get("/empty/3.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getObjectSpliterator(get("/empty/4.json"));
+ assertNull(spliterator);
+ spliterator = SimpleMapper.getObjectSpliterator(get("/empty/5.json"));
+ assertNull(spliterator);
+ }
+
+ @Test
+ public void testConvertEmptyInputToMap() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Map<String, Object> map;
+
+ map = SimpleMapper.convertObject(get("/empty/1.json"));
+ assertNull(map);
+ map = SimpleMapper.convertObject(get("/empty/2.json"));
+ assertNull(map);
+ map = SimpleMapper.convertObject(get("/empty/3.json"));
+ assertNull(map);
+ map = SimpleMapper.convertObject(get("/empty/4.json"));
+ assertNull(map);
+ map = SimpleMapper.convertObject(get("/empty/5.json"));
+ assertNull(map);
+ }
+
+ @Test
+ public void testConvertEmptyInput() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Object object;
+
+ object = SimpleMapper.convert(get("/empty/1.json"));
+ assertNull(object);
+ object = SimpleMapper.convert(get("/empty/2.json"));
+ assertNull(object);
+ object = SimpleMapper.convert(get("/empty/3.json"));
+ assertNull(object);
+ object = SimpleMapper.convert(get("/empty/4.json"));
+ assertNull(object);
+ object = SimpleMapper.convert(get("/empty/5.json"));
+ assertNull(object);
+ }
+
+
+ @Test
+ public void testConvertEmptyArrayToArraySpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ 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
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ 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
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ 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.trace("expected exception", e);
+ }
+
+ 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.trace("expected exception", e);
+ }
+
+ 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.trace("expected exception", e);
+ }
+
+ 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.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertEmptyArrayToMap() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ 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.trace("expected exception", e);
+ }
+
+ 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.trace("expected exception", e);
+ }
+
+ 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.trace("expected exception", e);
+ }
+
+ 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.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertEmptyArray() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ 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());
+ }
+
+
+ @Test
+ public void testConvertEmptyObjectToArraySpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ try
+ {
+ SimpleMapper.getArraySpliterator(get("/object/empty/1.json"));
+ fail("it must not be possible, to get an array-spliterator for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.getArraySpliterator(get("/object/empty/2.json"));
+ fail("it must not be possible, to get an array-spliterator for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.getArraySpliterator(get("/object/empty/3.json"));
+ fail("it must not be possible, to get an array-spliterator for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.getArraySpliterator(get("/object/empty/4.json"));
+ fail("it must not be possible, to get an array-spliterator for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertEmptyObjectToList() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ try
+ {
+ SimpleMapper.convertArray(get("/object/empty/1.json"));
+ fail("it must not be possible, to get a list for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.convertArray(get("/object/empty/2.json"));
+ fail("it must not be possible, to get a list for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.convertArray(get("/object/empty/3.json"));
+ fail("it must not be possible, to get a list for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.convertArray(get("/object/empty/4.json"));
+ fail("it must not be possible, to get a list for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertEmptyObjectToObjectSpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Spliterator<Entry<String, Object>> spliterator;
+
+ spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/1.json"));
+ assertFalse(
+ "The created splitter should have no entries",
+ spliterator.tryAdvance((Entry<String, Object> e) ->
+ {
+ fail("The consumer should never be called!");
+ }));
+ spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/2.json"));
+ assertFalse(
+ "The created splitter should have no entries",
+ spliterator.tryAdvance((Entry<String, Object> e) ->
+ {
+ fail("The consumer should never be called!");
+ }));
+ spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/3.json"));
+ assertFalse(
+ "The created splitter should have no entries",
+ spliterator.tryAdvance((Entry<String, Object> e) ->
+ {
+ fail("The consumer should never be called!");
+ }));
+ spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/4.json"));
+ assertFalse(
+ "The created splitter should have no entries",
+ spliterator.tryAdvance((Entry<String, Object> e) ->
+ {
+ fail("The consumer should never be called!");
+ }));
+ }
+
+ @Test
+ public void testConvertEmptyObjectToMap() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Map<String, Object> map;
+
+ map = SimpleMapper.convertObject(get("/object/empty/1.json"));
+ assertEquals(0, map.size());
+ map = SimpleMapper.convertObject(get("/object/empty/2.json"));
+ assertEquals(0, map.size());
+ map = SimpleMapper.convertObject(get("/object/empty/3.json"));
+ assertEquals(0, map.size());
+ map = SimpleMapper.convertObject(get("/object/empty/4.json"));
+ assertEquals(0, map.size());
+ }
+
+ @Test
+ public void testConvertEmptyObject() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Object object;
+
+ object = SimpleMapper.convert(get("/object/empty/1.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a list", object instanceof Map);
+ assertEquals(0, ((Map)object).size());
+ object = SimpleMapper.convert(get("/object/empty/2.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a list", object instanceof Map);
+ assertEquals(0, ((Map)object).size());
+ object = SimpleMapper.convert(get("/object/empty/3.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a list", object instanceof Map);
+ assertEquals(0, ((Map)object).size());
+ object = SimpleMapper.convert(get("/object/empty/4.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a list", object instanceof Map);
+ assertEquals(0, ((Map)object).size());
+ }
+
+
+ @Test
+ public void testConvertArrayToArraySpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Spliterator<Object> spliterator;
+
+ spliterator = SimpleMapper.getArraySpliterator(get("/array/1.json"));
+ checkArraySpliterator(spliterator);
+ spliterator = SimpleMapper.getArraySpliterator(get("/array/2.json"));
+ checkArraySpliterator(spliterator);
+ }
+
+ void checkArraySpliterator(Spliterator<Object> spliterator) throws Exception
+ {
+ assertNotNull(spliterator);
+
+ final ArrayList<Object> entries = new ArrayList<>(4);
+ for (int i = 0; i < 4; i++)
+ assertTrue(
+ "The created splitter should have a " + (i+1) + ". entry",
+ spliterator.tryAdvance((Object t) -> { entries.add(t); })
+ );
+
+ assertFalse(
+ "The created splitter should have no more entries",
+ spliterator.tryAdvance((Object t) ->
+ {
+ fail("The consumer should not have been called");
+ }));
+
+ checkPartnerPageData(entries.get(3));
+ }
+
+ @Test
+ public void testConvertArrayToList() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ List<Object> list;
+
+ list = SimpleMapper.convertArray(get("/array/1.json"));
+ assertNotNull(list);
+ assertEquals(4, list.size());
+ checkPartnerPageData(list.get(3));
+ list = SimpleMapper.convertArray(get("/array/2.json"));
+ assertNotNull(list);
+ assertEquals(4, list.size());
+ checkPartnerPageData(list.get(3));
+ }
+
+ @Test
+ public void testConvertArrayToObjectSpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ try
+ {
+ SimpleMapper.getObjectSpliterator(get("/array/1.json"));
+ fail("it must not be possible, to get an object-spliterator for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.getObjectSpliterator(get("/array/2.json"));
+ fail("it must not be possible, to get an object-spliterator for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertArrayToMap() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ try
+ {
+ SimpleMapper.convertObject(get("/array/1.json"));
+ fail("it must not be possible, to get a map for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.convertObject(get("/array/2.json"));
+ fail("it must not be possible, to get a map for an array");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertArray() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Object object;
+ List<Object> list;
+
+ object = SimpleMapper.convert(get("/array/1.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a list", object instanceof List);
+ list = (List<Object>)object;
+ assertEquals(4, list.size());
+ checkPartnerPageData(list.get(3));
+ object = SimpleMapper.convert(get("/array/2.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a list", object instanceof List);
+ list = (List<Object>)object;
+ assertEquals(4, list.size());
+ checkPartnerPageData(list.get(3));
+ }
+
+
+ @Test
+ public void testConvertObjectToArraySpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ try
+ {
+ SimpleMapper.getArraySpliterator(get("/object/1.json"));
+ fail("it must not be possible, to get an array-spliterator for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.getArraySpliterator(get("/object/2.json"));
+ fail("it must not be possible, to get an array-spliterator for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertObjectToList() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ try
+ {
+ SimpleMapper.convertArray(get("/object/1.json"));
+ fail("it must not be possible, to get a list for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+
+ try
+ {
+ SimpleMapper.convertArray(get("/object/2.json"));
+ fail("it must not be possible, to get a list for an object");
+ }
+ catch(IllegalArgumentException e)
+ {
+ LOG.trace("expected exception", e);
+ }
+ }
+
+ @Test
+ public void testConvertObjectToObjectSpliterator() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Spliterator<Entry<String, Object>> spliterator;
+
+ spliterator = SimpleMapper.getObjectSpliterator(get("/object/1.json"));
+ checkObjectSpliterator(spliterator);
+ spliterator = SimpleMapper.getObjectSpliterator(get("/object/2.json"));
+ checkObjectSpliterator(spliterator);
+ }
+
+ void checkObjectSpliterator(Spliterator<Entry<String, Object>> spliterator)
+ throws
+ Exception
+ {
+ assertNotNull(spliterator);
+
+ final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
+ for (int i = 0; i < 4; i++)
+ assertTrue(
+ "The created splitter should have a " + (i+1) + ". entry",
+ spliterator.tryAdvance((Entry<String, Object> e) -> {
+ map.put(e.getKey(), e.getValue());
+ }));
+
+ assertFalse(
+ "The created splitter should have no more entries",
+ spliterator.tryAdvance((Object t) ->
+ {
+ fail("The consumer should not have been called");
+ }));
+
+ checkVariables(map);
+ }
+
+ @Test
+ public void testConvertObjectToMap() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Map<String, Object> map;
+
+ map = SimpleMapper.convertObject(get("/object/1.json"));
+ assertNotNull(map);
+ assertEquals(4, map.size());
+ checkVariables(map);
+ map = SimpleMapper.convertObject(get("/object/2.json"));
+ assertNotNull(map);
+ assertEquals(4, map.size());
+ checkVariables(map);
+ }
+
+ @Test
+ public void testConvertObject() throws Exception
+ {
+ LOG.info("<-- Start Of New Test-Case!");
+
+ Object object;
+ Map<String, Object> map;
+
+ object = SimpleMapper.convert(get("/object/1.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a map", object instanceof Map);
+ map = (Map<String, Object>)object;
+ assertEquals(4, map.size());
+ checkVariables(map);
+ object = SimpleMapper.convert(get("/object/2.json"));
+ assertNotNull(object);
+ assertTrue("the returned object should be a map", object instanceof Map);
+ map = (Map<String, Object>)object;
+ assertEquals(4, map.size());
+ checkVariables(map);
+ }
+
+ void checkVariables(Map<String, Object> map)
+ {
+ Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
+ Entry<String, Object> entry;
+
+ assertTrue("The map should have a first element", iterator.hasNext());
+ entry = iterator.next();
+ assertEquals("bootstrap", entry.getKey());
+ assertEquals("bootstrap.min.css", entry.getValue());
+
+ assertTrue("The map should have a second element", iterator.hasNext());
+ entry = iterator.next();
+ assertEquals("pages", entry.getKey());
+ assertTrue(
+ "The pages-variable should be of type List",
+ entry.getValue() instanceof List
+ );
+ assertEquals(4, ((List)entry.getValue()).size());
+ checkPartnerPageData(((List)entry.getValue()).get(3));
+
+ assertTrue("The map should have a third element", iterator.hasNext());
+ entry = iterator.next();
+ assertEquals("footer", entry.getKey());
+ assertTrue(
+ "The footer-variable should be of type List",
+ entry.getValue() instanceof List
+ );
+ assertEquals(3, ((List)entry.getValue()).size());
+
+ assertTrue("The map should have a third element", iterator.hasNext());
+ entry = iterator.next();
+ assertEquals("sponsorship", entry.getKey());
+ assertTrue(
+ "The sponsorship-variable should be of type List",
+ entry.getValue() instanceof List
+ );
+ assertEquals(3, ((List)entry.getValue()).size());
+ }
+
+
+ void checkPartnerPageData(Object page)
+ {
+ assertTrue("The page-data should be of type Map", page instanceof Map);
+ Map<String, Object> map = (Map<String, Object>)page;
+ assertEquals(4, map.size());
+ assertEquals("/partner.html", map.get("uri"));
+ assertTrue(
+ "The page-data should have an entry \"children\" of type Map",
+ map.get("children") instanceof Map
+ );
+ map = (Map<String, Object>)map.get("children");
+ assertEquals(6, map.size());
+ assertTrue(
+ "The children-map should haven an entry \"/partner/juplo.html\" of type Map",
+ map.get("/partner/juplo.html") instanceof Map
+ );
+ map = (Map<String, Object>)map.get("/partner/juplo.html");
+ assertEquals(2, map.size());
+ assertEquals(
+ "Wir sind Unterstützer der Nerd-Plattform juplo.de",
+ map.get("title")
+ );
+ }
+
+
+ private JsonParser get(String resource) throws IOException
+ {
+ InputStream is = SimpleMapperTest.class.getResourceAsStream(resource);
+ return factory.createParser(is);
+ }
+}