package com.alibaba.json.bvt.path;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import junit.framework.TestCase;

public class JSONPath_reverse_test extends TestCase
{
    public void test_reserve() throws Exception {
        JSONObject object = JSON.parseObject("{\"id\":1001,\"name\":\"ljw\",\"age\":50}");

        assertEquals("[1001,\"ljw\"]", JSONPath.reserveToArray(object, "id", "name").toString());
        assertEquals("[\"ljw\",1001]", JSONPath.reserveToArray(object, "name", "id").toString());
        assertEquals("[\"ljw\",[\"ljw\",1001,50]]", JSONPath.reserveToArray(object, "name", "*").toString());
    }

    public void test_reserve2() throws Exception {
        JSONObject object = JSON.parseObject("{\"id\":1001,\"name\":\"ljw\",\"age\":50}");

        assertEquals("{\"id\":1001,\"name\":\"ljw\"}", JSONPath.reserveToObject(object, "id", "name").toString());
        assertEquals("{\"name\":\"ljw\",\"id\":1001}", JSONPath.reserveToObject(object, "name", "id").toString());
    }


    public void test_reserve3() throws Exception {
        JSONObject object = JSON.parseObject("{\"player\":{\"id\":1001,\"name\":\"ljw\",\"age\":50}}");

        assertEquals("{\"player\":{\"name\":\"ljw\",\"id\":1001}}", JSONPath.reserveToObject(object, "player.id", "player.name").toString());
        assertEquals("{\"player\":{\"name\":\"ljw\",\"id\":1001}}", JSONPath.reserveToObject(object, "player.name", "player.id", "ab.c").toString());
    }


}

@@ -141,16 +141,74 @@
 ONPath;%0A
+import com.alibaba.fastjson.serializer.SerializerFeature;%0A
 import j
@@ -232,16 +232,16 @@
 stCase;%0A
-
 %0Apublic 
@@ -617,32 +617,151 @@
 %22).toString());%0A
+        String text = JSON.toJSONString(JSONPath.reserveToArray(object, %22name%22, %22*%22), SerializerFeature.MapSortField);%0A
         assertEq
@@ -750,33 +750,43 @@
 ;%0A        assert
-E
+True(text.e
 quals(%22%5B%5C%22ljw%5C%22,
@@ -808,64 +808,318 @@
 0%5D%5D%22
-, JSONPath.reserveToArray(object, %22name%22, %22*%22).toString(
+)%0A                %7C%7C text.equals(%22%5B%5C%22ljw%5C%22,%5B%5C%22ljw%5C%22,50,1001%5D%5D%22)%0A                %7C%7C text.equals(%22%5B%5C%22ljw%5C%22,%5B50,1001,%5C%22ljw%5C%22%5D%5D%22)%0A                %7C%7C text.equals(%22%5B%5C%22ljw%5C%22,%5B1001,50,%5C%22ljw%5C%22%5D%5D%22)%0A                %7C%7C text.equals(%22%5B%5C%22ljw%5C%22,%5B1001,%5C%22ljw%5C%22,50%5D%5D%22)%0A                %7C%7C text.equals(%22%5B%5C%22ljw%5C%22,%5B50,%5C%22ljw%5C%22,1001%5D%5D%22
 ));%0A
@@ -1674,68 +1674,40 @@
     
-assertEquals(%22%7B%5C%22player%5C%22:%7B%5C%22name%5C%22:%5C%22ljw%5C%22,%5C%22id%5C%22:1001%7D%7D%22, 
+String text = JSON.toJSONString(
 JSON
@@ -1758,35 +1758,56 @@
 layer.name%22)
-.toString()
+, SerializerFeature.MapSortField
 );%0A        a
@@ -1834,16 +1834,28 @@
 er%5C%22:%7B%5C%22
+id%5C%22:1001,%5C%22
 name%5C%22:%5C
@@ -1864,25 +1864,53 @@
 jw%5C%22
-,%5C%22id%5C%22:1001%7D%7D%22, 
+%7D%7D%22, text);%0A        text = JSON.toJSONString(
 JSON
@@ -1973,27 +1973,123 @@
  %22ab.c%22)
-.toString()
+, SerializerFeature.MapSortField);%0A        assertEquals(%22%7B%5C%22player%5C%22:%7B%5C%22id%5C%22:1001,%5C%22name%5C%22:%5C%22ljw%5C%22%7D%7D%22, text
 );%0A    %7D

Fix flaky test JSONPath_reverse_test