/test/org/nutz/mapl/MaplTest.java
http://nutz.googlecode.com/ · Java · 353 lines · 245 code · 36 blank · 72 comment · 0 complexity · 6676c5140bad5b1da54048039864d4e6 MD5 · raw file
- package org.nutz.mapl;
- import static org.junit.Assert.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import org.junit.Test;
- import org.nutz.json.Abc;
- import org.nutz.json.Json;
- import org.nutz.json.JsonFormat;
- import org.nutz.lang.Lang;
- import org.nutz.lang.Streams;
- import org.nutz.mapl.Mapl;
- /**
- * MapList??
- *
- * @author juqkai(juqkai@gmail.com)
- */
- public class MaplTest {
- /**
- * ??MAP??
- */
- @Test
- public void cellTest() {
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"));
- assertEquals("dtri", Mapl.cell(dest, "company.name"));
- assertEquals("Dao", Mapl.cell(dest, "company.creator.name"));
- }
- /**
- * ????LIST???
- */
- @Test
- public void cellArrayTest() {
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- assertEquals("1", Mapl.cell(dest, "users[0].name"));
- assertEquals("2", Mapl.cell(dest, "people[1].name"));
- }
- /**
- * ????LIST???
- */
- @Test
- public void cellArrayTest1() {
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- assertEquals("1", Mapl.cell(dest, "users.0.name"));
- assertEquals("2", Mapl.cell(dest, "people.1.name"));
- }
- /**
- * ????LIST
- */
- @Test
- public void cellArrayTest2() {
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- assertTrue(Mapl.cell(dest, "users") instanceof List);
- List<?> list = (List<?>) Mapl.cell(dest, "users");
- assertEquals(2, list.size());
- }
- /**
- * ?????LIST???
- */
- @Test
- public void cellArrayTest3() {
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- assertTrue(Mapl.cell(dest, "users") instanceof List);
- List<?> list = (List<?>) Mapl.cell(dest, "users");
- assertEquals("1", Mapl.cell(list, "[0].name"));
- }
- /**
- * ??????
- */
- @Test
- public void includeFilterConvertTest() {
- List<String> paths = new ArrayList<String>();
- paths.add("users[].name");
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- Object obj = Mapl.includeFilter(dest, paths);
- assertNull(Mapl.cell(obj, "people"));
- assertEquals("1", Mapl.cell(obj, "users[0].name"));
- }
- /**
- * ??????
- */
- @Test
- public void excludeFilterConvertTest() {
- List<String> paths = new ArrayList<String>();
- paths.add("users[].name");
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- Object obj = Mapl.excludeFilter(dest, paths);
- assertNull(Mapl.cell(obj, "users[0].name"));
- assertEquals(12, Mapl.cell(obj, "users[0].age"));
- }
- /**
- * ??????
- */
- @Test
- public void excludeFilterConvertTest2() {
- List<String> paths = new ArrayList<String>();
- paths.add("users");
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- Object obj = Mapl.excludeFilter(dest, paths);
- assertNull(Mapl.cell(obj, "users"));
- assertEquals(12, Mapl.cell(obj, "people[0].age"));
- }
- /**
- * ???MapList??
- */
- @Test
- public void objCompileTest() {
- Abc abc = new Abc();
- abc.id = 1;
- abc.name = "jk";
- Object obj = Mapl.toMaplist(abc);
- assertTrue(obj instanceof Map);
- assertEquals(1, Mapl.cell(obj, "id"));
- assertEquals("jk", Mapl.cell(obj, "name"));
- }
- /**
- * ???MapList??
- */
- @Test
- public void objCompileArrayTest() {
- Abc abc = new Abc();
- abc.id = 1;
- abc.name = "jk";
- Abc b = new Abc();
- b.id = 2;
- b.name = "juqkai";
- List<Abc> list = new ArrayList<Abc>();
- list.add(abc);
- list.add(b);
- Object obj = Mapl.toMaplist(list);
- assertTrue(obj instanceof List);
- assertEquals(1, Mapl.cell(obj, "[0].id"));
- assertEquals("juqkai", Mapl.cell(obj, "1.name"));
- }
- /**
- * ???MapList??????
- */
- @Test
- public void objCompileCircularReferenceTest() {
- A a = new A();
- B b = new B();
- a.b = b;
- b.a = a;
- Object obj = Mapl.toMaplist(a);
- assertTrue(obj instanceof Map);
- assertNotNull(Mapl.cell(obj, "b"));
- assertEquals("b", Mapl.cell(obj, "b.name"));
- assertEquals("a", Mapl.cell(obj, "b.a.name"));
- assertEquals(Mapl.cell(obj, "b.a.b"), Mapl.cell(obj, "b"));
- }
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- /**
- * ??????
- */
- /**
- * ????
- */
- @Test
- public void structureConvertSimple() {
- String json = "{'name':'jk', 'age':12}";
- String model = "{'name':'??', 'age':'??'}";
- String dest = "{\"??\":\"jk\",\"??\":12}";
- Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
- assertEquals("jk", Mapl.cell(obj, "??"));
- assertEquals(12, Mapl.cell(obj, "??"));
- assertEquals(dest, Json.toJson(obj, new JsonFormat()));
- }
- /**
- * ????
- */
- @Test
- public void structureConvertSimpleArray() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- String model = "{'user':[{'name':'user[].??', 'age':'user[].??'}]}";
- Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
- assertEquals("jk", Mapl.cell(obj, "user[0].??"));
- assertEquals("nutz", Mapl.cell(obj, "user[1].??"));
- assertEquals(12, Mapl.cell(obj, "user[0].??"));
- assertEquals(5, Mapl.cell(obj, "user[1].??"));
- }
- /**
- * ?????
- */
- @Test
- public void structureConvertMultiPath() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- String model = "{'user':[{'name':['user[].??', 'people[].name'], 'age':['user[].??', 'people[].age']}]}";
- Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
- assertEquals("jk", Mapl.cell(obj, "user[0].??"));
- assertEquals("nutz", Mapl.cell(obj, "user[1].??"));
- assertEquals("jk", Mapl.cell(obj, "people[0].name"));
- assertEquals(5, Mapl.cell(obj, "people[1].age"));
- }
- /**
- * ????Array???
- */
- @Test
- public void structureConvertRoot2Array() {
- String json = "[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]";
- String model = "[{'name':['user[].??', 'people[].name'], 'age':['user[].??', 'people[].age']}]";
- String dest = "{\"people\":[{\"age\":12,\"name\":\"jk\"}, {\"age\":5,\"name\":\"nutz\"}],\"user\":[{\"??\":\"jk\",\"??\":12}, {\"??\":\"nutz\",\"??\":5}]}";
- Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
- assertEquals("jk", Mapl.cell(obj, "user[0].??"));
- assertEquals("nutz", Mapl.cell(obj, "user[1].??"));
- assertEquals("jk", Mapl.cell(obj, "people[0].name"));
- assertEquals(5, Mapl.cell(obj, "people[1].age"));
- assertEquals(dest, Json.toJson(obj, new JsonFormat()));
- }
- /**
- * Array????array??
- */
- @Test
- public void structureConvertArray2Root() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- String model = "{'user':[{'name':['[].name'], 'age':'[].age'}]}";
- String dest = "[{\"age\":12,\"name\":\"jk\"}, {\"age\":5,\"name\":\"nutz\"}]";
- Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
- assertEquals("jk", Mapl.cell(obj, "[0].name"));
- assertEquals(5, Mapl.cell(obj, "[1].age"));
- assertEquals(dest, Json.toJson(obj, new JsonFormat()));
- }
- /**
- * ?????mapl?
- */
- @Test
- public void addItemTest() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- Object obj = Json.fromJson(json);
- Mapl.put(obj, "user[0].test", "test");
- assertEquals("test", Mapl.cell(obj, "user[0].test"));
- }
- /**
- * ?????mapl?
- */
- @Test
- public void delItemTest() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- Object obj = Json.fromJson(json);
- Mapl.del(obj, "user[0].age");
- assertNull(Mapl.cell(obj, "user[0].age"));
- assertEquals(1, ((Map<?, ?>) Mapl.cell(obj, "user[0]")).size());
- }
- /**
- * ?????mapl?
- */
- @Test
- public void updateItemTest() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- Object obj = Json.fromJson(json);
- Mapl.update(obj, "user[0].name", "test");
- assertEquals("test", Mapl.cell(obj, "user[0].name"));
- }
- @Test
- public void issue243Test() {
- String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
- Object obj = Json.fromJson(json);
- Object item = Mapl.cell(obj, "user[]");
- assertFalse(item instanceof List);
- assertTrue(item instanceof Map);
- }
- @Test
- public void issue243Test2() {
- List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
- HashMap<String, Object> map = new HashMap<String, Object>();
- List<String> items = new LinkedList<String>();
- items.add(new String("aa"));
- items.add(new String("bb"));
- map.put("id", 0);
- map.put("items", items);
- list.add(map);
- assertEquals(Mapl.cell(list, "[0].items.0"), "aa");
- assertTrue(Mapl.cell(list, "[0].items[0]").equals("aa"));
- assertTrue(Mapl.cell(list, "[0].items[1]").equals("bb"));
- assertTrue(Mapl.cell(list, "[0].items[]").equals("aa"));
- assertTrue(Mapl.cell(list, "[0].items").equals(items));
- }
- @Test
- public void testIssue322() {
- String json = "{name:'nutz', age:12, address:[{area:1,name:'abc'},{area:2,name:'123'}]}";
- Object obj = Json.fromJson(json);
- Object newobj = Mapl.excludeFilter(obj, Lang.list("age", "address[].area"));
- JsonFormat jf = new JsonFormat(true);
- assertEquals("{\"address\":[{\"name\":\"abc\"}, {\"name\":\"123\"}],\"name\":\"nutz\"}",
- Json.toJson(newobj, jf));
- }
- /**
- * ???????????????
- */
- @Test
- public void excludeFilterConvertTest_MultiplePath1() {
- List<String> paths = new ArrayList<String>();
- paths.add("users[].name");
- paths.add("people[].age");
- Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
- Object obj = Mapl.excludeFilter(dest, paths);
- assertNotNull(Mapl.cell(obj, "users"));
- assertEquals(12, Mapl.cell(obj, "users[0].age"));
- assertEquals("1", Mapl.cell(obj, "people[0].name"));
- }
- /**
- * ??????????????????????????
- */
- @Test
- public void excludeFilterConvertTest_MultiplePath2() {
- String json = "{name:'nutz', age:12, address:[{area:1,name:'abc'},{area:2,name:'123'}]}";
- Object obj = Json.fromJson(json);
- List<String> list = new ArrayList<String>();
- list.add("age");
- list.add("address[].area");
- Object newobj = Mapl.excludeFilter(obj, list);
- assertNull(Mapl.cell(newobj, "age"));
- assertEquals("nutz", Mapl.cell(newobj, "name"));
- assertNull(Mapl.cell(newobj, "address[0].area"));
- assertEquals("abc", Mapl.cell(newobj, "address[0].name"));
- }
- }