package com.github.jsonldjava.utils;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Map;

import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;

public class JsonUtilsTest {

    @SuppressWarnings("unchecked")
    @Test
    public void fromStringTest() {
        final String testString = "{\"seq\":3,\"id\":\"e48dfa735d9fad88db6b7cd696002df7\",\"changes\":[{\"rev\":\"2-6aebf275bc3f29b67695c727d448df8e\"}]}";
        final String testFailure = "{{{{{{{{{{{";
        Object obj = null;

        try {
            obj = JsonUtils.fromString(testString);
        } catch (final Exception e) {
            assertTrue(false);
        }

        assertTrue(((Map<String, Object>) obj).containsKey("seq"));
        assertTrue(((Map<String, Object>) obj).get("seq") instanceof Number);

        try {
            obj = JsonUtils.fromString(testFailure);
            assertTrue(false);
        } catch (final Exception e) {
            assertTrue(true);
        }
    }

    @Test
    public void trailingContent_1() throws JsonParseException, IOException {
        trailingContent("{}");
    }

    @Test
    public void trailingContent_2() throws JsonParseException, IOException {
        trailingContent("{}  \t  \r \n  \r\n   ");
    }

    @Test(expected = JsonParseException.class)
    public void trailingContent_3() throws JsonParseException, IOException {
        trailingContent("{}x");
    }

    @Test(expected = JsonParseException.class)
    public void trailingContent_4() throws JsonParseException, IOException {
        trailingContent("{}   x");
    }

    @Test(expected = JsonParseException.class)
    public void trailingContent_5() throws JsonParseException, IOException {
        trailingContent("{} \"x\"");
    }

    @Test(expected = JsonParseException.class)
    public void trailingContent_6() throws JsonParseException, IOException {
        trailingContent("{} {}");
    }

    @Test(expected = JsonParseException.class)
    public void trailingContent_7() throws JsonParseException, IOException {
        trailingContent("{},{}");
    }

    @Test(expected = JsonParseException.class)
    public void trailingContent_8() throws JsonParseException, IOException {
        trailingContent("{},[]");
    }

    private void trailingContent(String string) throws JsonParseException, IOException {
        JsonUtils.fromString(string);
    }
}

@@ -75,16 +75,37 @@
 tTrue;%0A%0A
+import java.io.File;%0A
 import j
@@ -124,16 +124,68 @@
 eption;%0A
+import java.io.Reader;%0Aimport java.io.StringReader;%0A
 import j
@@ -261,30 +261,175 @@
 ore.Json
-ParseException
+Factory;%0Aimport com.fasterxml.jackson.core.JsonParseException;%0Aimport com.fasterxml.jackson.core.JsonParser;%0Aimport com.fasterxml.jackson.databind.ObjectMapper
 ;%0A%0Apubli
@@ -1235,32 +1235,389 @@
        %7D%0A    %7D%0A%0A
+    @Test%0A    public void testFromJsonParser() throws Exception %7B%0A        ObjectMapper jsonMapper = new ObjectMapper();%0A        JsonFactory jsonFactory = new JsonFactory(jsonMapper);%0A        Reader testInputString = new StringReader(%22%7B%7D%22);%0A        JsonParser jp = jsonFactory.createParser(testInputString);%0A        JsonUtils.fromJsonParser(jp );%0A    %7D%0A    %0A
     @Test%0A    pu

Add test for JsonUtils.fromJsonParser