package app.hongs.test;

import app.hongs.util.Dict;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * 测试 app.hongs.util.Dict
 * @author Hongs
 */
public class TestDict extends TestCase {

    private Map dict;

    public TestDict() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    @Override
    public void setUp() {
        dict = new HashMap();
        Map  sub1 = new HashMap();
        List sub2 = new ArrayList();
        List sub3 = new ArrayList();
        Set  sub4 = new HashSet();
        dict.put("sub1", sub1);
        sub1.put("key1", "xx");
        sub1.put("sub2", sub2);
        sub1.put("sub3", sub3);
        sub2.add("sub2");
        sub2.add( sub4 );
        sub4.add("sub4");
        Map m;
        m = new HashMap();
        m.put("a", 1);
        sub3.add(m);
        m = new HashMap();
        m.put("a", 2);
        sub3.add(m);
        /**
         *  {
         *      "sub1": {
         *          "key1": "xx",
         *          "sub2": [
         *              "sub2,
         *              [
         *                  "sub4"
         *              ]
         *          ],
         *          "sub3": [
         *              {
         *                  "a": 1
         *              },
         *              {
         *                  "a": 2
         *              }
         *          ]
         *      }
         *  }
         */
    }

    @After
    @Override
    public void tearDown() {
        dict.clear();
    }

    @Test
    public void testGet() {
        Object x;
        List   y;
        Set    z;

        //Data.dumps(dict);

        x = Dict.get(dict, null, "sub1", "key1");
        assertEquals("xx", x);

        x = Dict.get(dict, null, "sub1", "sub2", 1);
        z = new HashSet(  ); z.add("sub4");
        assertEquals(z, x);

        x = Dict.get(dict, null, "sub1", "sub2", 1, null);
        y = new ArrayList(); y.add("sub4");
        assertEquals(y, x);

        x = Dict.get(dict, null, "sub1", "sub3", null, "a");
        y = new ArrayList(); y.add(1); y.add(2);
        assertEquals(y, x);
    }

    @Test
    public void testPut() {
        Object x;
        List   y;
        Set    z;

        Dict.put(dict, "yy", "sub1", "key1");
        x = Dict.get(dict, null, "sub1", "key1");
        assertEquals("yy", x);

        z = new HashSet(  ); z.add(1);
        Dict.put(dict, z, "sub1", "sub2", 1);
        x = Dict.get(dict, null, "sub1", "sub2", 1);
        assertEquals(z, x);

        y = new ArrayList(); y.add(1); y.add(2);
        Dict.put(dict, 2, "sub1", "sub2", 1, null);
        x = Dict.get(dict, null, "sub1", "sub2", 1, null);
        assertEquals(y, x);

        y = new ArrayList(); y.add(1); y.add(2); y.add(3);
        Dict.put(dict, 3, "sub1", "sub3", null, "a");
        x = Dict.get(dict, null, "sub1", "sub3", null, "a");
        assertEquals(y, x);

        //Data.dumps(dict);
    }

}

@@ -70,24 +70,49 @@
 .ArrayList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -3283,12 +3283,356 @@
 );%0A    %7D
+%0A    %0A    public void testSplitKeys() %7B%0A        Object%5B%5D a = new Object%5B %5D %7B%22%22, %22a%22, %22b%22, %22c%22, %22d%22, %22!e%22, %22!f%22, %22g%22, %22!h%22, null, %22x.y!z%22, %22def%22%7D;%0A        Object%5B%5D b = Dict.splitKeys(%22.a.b%5Bc%5D%5Bd%5D.!e!f%5Bg%5D!h%5B%5D%5Bx.y!z%5D.def%22);%0A        app.hongs.util.Data.print(a);%0A        app.hongs.util.Data.print(b);%0A        assertTrue( Arrays.equals(a, b) );%0A    %7D
 %0A%0A%7D%0A

重写 splitKeys(原名 parsePath), 取消正则拆分不再多次遍历