package org.forest.converter;

import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Assert;
import org.forest.converter.json.ForestFastjsonConverter;
import org.forest.converter.json.ForestJacksonConverter;
import org.junit.Test;

import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

/**
 * @author gongjun[dt_flys@hotmail.com]
 * @since 2017-05-08 23:26
 */
public class TestForestJacksonConverter {

    private ObjectMapper mapper = new ObjectMapper();
    {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
    }


    @Test
    public void testConvertToJson() {
        ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
        String text = forestJacksonConverter.convertToJson(new Integer[] {100, 10});
        Assert.assertEquals("[100,10]", text);
    }

    @Test
    public void testConvertToJava() {
        String jsonText = "{\"a\":1, \"b\":2}";
        ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
        Map result = forestJacksonConverter.convertToJavaObject(jsonText, Map.class);
        assertNotNull(result);
        assertEquals(1, result.get("a"));
        assertEquals(2, result.get("b"));

        result = forestJacksonConverter.convertToJavaObject(jsonText, mapper.constructType(Map.class));
        assertNotNull(result);
        assertEquals(1, result.get("a"));
        assertEquals(2, result.get("b"));

    }


}

@@ -319,32 +319,85 @@
 cksonConverter;%0A
+import org.forest.exceptions.ForestRuntimeException;%0A
 import org.junit
@@ -529,16 +529,65 @@
 NotNull;
+%0Aimport static junit.framework.Assert.assertTrue;
 %0A%0A/**%0A *
@@ -1807,23 +1807,766 @@
 .get(%22b%22));%0A
+    %7D%0A%0A%0A    @Test%0A    public void testConvertToJavaError() %7B%0A        String jsonText = %22%7B%5C%22a%5C%22:1, %22;%0A        boolean error = false;%0A        ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();%0A        try %7B%0A            forestJacksonConverter.convertToJavaObject(jsonText, Map.class);%0A        %7D catch (ForestRuntimeException e) %7B%0A            error = true;%0A            assertNotNull(e.getCause());%0A        %7D%0A        assertTrue(error);%0A%0A        error = false;%0A        try %7B%0A            forestJacksonConverter.convertToJavaObject(jsonText, mapper.constructType(Map.class));%0A        %7D catch (ForestRuntimeException e) %7B%0A            error = true;%0A            assertNotNull(e.getCause());%0A        %7D%0A        assertTrue(error);
 %0A    %7D%0A%0A%0A%7D%0A
@@ -2563,10 +2563,11 @@
     %7D%0A%0A%0A
+%0A
 %7D%0A

测试 fastjson converter