package org.forest.converter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.Assert;
import org.forest.converter.json.ForestFastjsonConverter;
import org.forest.exceptions.ForestRuntimeException;
import org.junit.Test;

import java.util.Map;

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

    @Test
    public void testSerializerFeature() {
        ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
        String defaultSerializerFeatureName = forestFastjsonConverter.getSerializerFeatureName();
        SerializerFeature defaultSerializerFeature = forestFastjsonConverter.getSerializerFeature();
        Assert.assertEquals(SerializerFeature.DisableCircularReferenceDetect.name(),
                defaultSerializerFeatureName);
        Assert.assertEquals(defaultSerializerFeature.name(),
                defaultSerializerFeatureName);

        forestFastjsonConverter.setSerializerFeatureName(SerializerFeature.WriteClassName.name());
        Assert.assertEquals(SerializerFeature.WriteClassName.name(),
                forestFastjsonConverter.getSerializerFeatureName());
        Assert.assertEquals(SerializerFeature.WriteClassName,
                forestFastjsonConverter.getSerializerFeature());

        forestFastjsonConverter.setSerializerFeature(SerializerFeature.BeanToArray);
        Assert.assertEquals(SerializerFeature.BeanToArray.name(),
                forestFastjsonConverter.getSerializerFeatureName());
        Assert.assertEquals(SerializerFeature.BeanToArray,
                forestFastjsonConverter.getSerializerFeature());
    }

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

    @Test
    public void testConverterError() {
        String badJsonText = "{\"a\"=1";
        ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
        boolean error = false;
        try {
            forestFastjsonConverter.convertToJavaObject(badJsonText, Map.class);
        } catch (ForestRuntimeException e) {
            error = true;
        }
        Assert.assertTrue(error);
    }

}

@@ -24,16 +24,59 @@
 erter;%0A%0A
+import com.alibaba.fastjson.TypeReference;%0A
 import c
@@ -314,16 +314,169 @@
 l.Map;%0A%0A
+import static junit.framework.Assert.assertEquals;%0Aimport static junit.framework.Assert.assertNotNull;%0Aimport static junit.framework.Assert.assertTrue;%0A%0A
 /**%0A * @
@@ -926,39 +926,32 @@
 ture();%0A        
-Assert.
 assertEquals(Ser
@@ -1051,39 +1051,32 @@
 eName);%0A        
-Assert.
 assertEquals(def
@@ -1252,39 +1252,32 @@
 ame());%0A        
-Assert.
 assertEquals(Ser
@@ -1383,39 +1383,32 @@
 ame());%0A        
-Assert.
 assertEquals(Ser
@@ -1589,39 +1589,32 @@
 Array);%0A        
-Assert.
 assertEquals(Ser
@@ -1717,39 +1717,32 @@
 ame());%0A        
-Assert.
 assertEquals(Ser
@@ -2068,31 +2068,24 @@
 %7D);%0A        
-Assert.
 assertEquals
@@ -2148,18 +2148,861 @@
 tConvert
-er
+ToJava() %7B%0A        String jsonText = %22%7B%5C%22a%5C%22:1, %5C%22b%5C%22:2%7D%22;%0A        ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();%0A        Map result = forestFastjsonConverter.convertToJavaObject(jsonText, Map.class);%0A        assertNotNull(result);%0A        assertEquals(1, result.get(%22a%22));%0A        assertEquals(2, result.get(%22b%22));%0A%0A        result = forestFastjsonConverter.convertToJavaObject(jsonText, new TypeReference%3CMap%3E() %7B%7D.getType());%0A        assertNotNull(result);%0A        assertEquals(1, result.get(%22a%22));%0A        assertEquals(2, result.get(%22b%22));%0A%0A        result = forestFastjsonConverter.convertToJavaObject(jsonText, new TypeReference%3CMap%3E() %7B%7D);%0A        assertNotNull(result);%0A        assertEquals(1, result.get(%22a%22));%0A        assertEquals(2, result.get(%22b%22));%0A%0A    %7D%0A%0A    @Test%0A    public void testConvertToJava
 Error() 
@@ -3039,17 +3039,17 @@
  %22%7B%5C%22a%5C%22
-=
+:
 1%22;%0A    
@@ -3342,43 +3342,658 @@
     
-%7D%0A        Assert.assertTrue(error);
+    assertNotNull(e.getCause());%0A        %7D%0A        assertTrue(error);%0A%0A        error = true;%0A        try %7B%0A            forestFastjsonConverter.convertToJavaObject(badJsonText, new TypeReference%3CMap%3E() %7B%7D.getType());%0A        %7D catch (ForestRuntimeException e) %7B%0A            error = true;%0A            assertNotNull(e.getCause());%0A        %7D%0A        assertTrue(error);%0A%0A        error = true;%0A        try %7B%0A            forestFastjsonConverter.convertToJavaObject(badJsonText, new TypeReference%3CMap%3E() %7B%7D);%0A        %7D catch (ForestRuntimeException e) %7B%0A            error = true;%0A            assertNotNull(e.getCause());%0A        %7D%0A        assertTrue(error);%0A
 %0A   

测试fastjson converter