/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

  1. package org.nutz.mapl;
  2. import static org.junit.Assert.*;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.junit.Test;
  9. import org.nutz.json.Abc;
  10. import org.nutz.json.Json;
  11. import org.nutz.json.JsonFormat;
  12. import org.nutz.lang.Lang;
  13. import org.nutz.lang.Streams;
  14. import org.nutz.mapl.Mapl;
  15. /**
  16. * MapList??
  17. *
  18. * @author juqkai(juqkai@gmail.com)
  19. */
  20. public class MaplTest {
  21. /**
  22. * ??MAP??
  23. */
  24. @Test
  25. public void cellTest() {
  26. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"));
  27. assertEquals("dtri", Mapl.cell(dest, "company.name"));
  28. assertEquals("Dao", Mapl.cell(dest, "company.creator.name"));
  29. }
  30. /**
  31. * ????LIST???
  32. */
  33. @Test
  34. public void cellArrayTest() {
  35. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  36. assertEquals("1", Mapl.cell(dest, "users[0].name"));
  37. assertEquals("2", Mapl.cell(dest, "people[1].name"));
  38. }
  39. /**
  40. * ????LIST???
  41. */
  42. @Test
  43. public void cellArrayTest1() {
  44. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  45. assertEquals("1", Mapl.cell(dest, "users.0.name"));
  46. assertEquals("2", Mapl.cell(dest, "people.1.name"));
  47. }
  48. /**
  49. * ????LIST
  50. */
  51. @Test
  52. public void cellArrayTest2() {
  53. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  54. assertTrue(Mapl.cell(dest, "users") instanceof List);
  55. List<?> list = (List<?>) Mapl.cell(dest, "users");
  56. assertEquals(2, list.size());
  57. }
  58. /**
  59. * ?????LIST???
  60. */
  61. @Test
  62. public void cellArrayTest3() {
  63. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  64. assertTrue(Mapl.cell(dest, "users") instanceof List);
  65. List<?> list = (List<?>) Mapl.cell(dest, "users");
  66. assertEquals("1", Mapl.cell(list, "[0].name"));
  67. }
  68. /**
  69. * ??????
  70. */
  71. @Test
  72. public void includeFilterConvertTest() {
  73. List<String> paths = new ArrayList<String>();
  74. paths.add("users[].name");
  75. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  76. Object obj = Mapl.includeFilter(dest, paths);
  77. assertNull(Mapl.cell(obj, "people"));
  78. assertEquals("1", Mapl.cell(obj, "users[0].name"));
  79. }
  80. /**
  81. * ??????
  82. */
  83. @Test
  84. public void excludeFilterConvertTest() {
  85. List<String> paths = new ArrayList<String>();
  86. paths.add("users[].name");
  87. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  88. Object obj = Mapl.excludeFilter(dest, paths);
  89. assertNull(Mapl.cell(obj, "users[0].name"));
  90. assertEquals(12, Mapl.cell(obj, "users[0].age"));
  91. }
  92. /**
  93. * ??????
  94. */
  95. @Test
  96. public void excludeFilterConvertTest2() {
  97. List<String> paths = new ArrayList<String>();
  98. paths.add("users");
  99. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  100. Object obj = Mapl.excludeFilter(dest, paths);
  101. assertNull(Mapl.cell(obj, "users"));
  102. assertEquals(12, Mapl.cell(obj, "people[0].age"));
  103. }
  104. /**
  105. * ???MapList??
  106. */
  107. @Test
  108. public void objCompileTest() {
  109. Abc abc = new Abc();
  110. abc.id = 1;
  111. abc.name = "jk";
  112. Object obj = Mapl.toMaplist(abc);
  113. assertTrue(obj instanceof Map);
  114. assertEquals(1, Mapl.cell(obj, "id"));
  115. assertEquals("jk", Mapl.cell(obj, "name"));
  116. }
  117. /**
  118. * ???MapList??
  119. */
  120. @Test
  121. public void objCompileArrayTest() {
  122. Abc abc = new Abc();
  123. abc.id = 1;
  124. abc.name = "jk";
  125. Abc b = new Abc();
  126. b.id = 2;
  127. b.name = "juqkai";
  128. List<Abc> list = new ArrayList<Abc>();
  129. list.add(abc);
  130. list.add(b);
  131. Object obj = Mapl.toMaplist(list);
  132. assertTrue(obj instanceof List);
  133. assertEquals(1, Mapl.cell(obj, "[0].id"));
  134. assertEquals("juqkai", Mapl.cell(obj, "1.name"));
  135. }
  136. /**
  137. * ???MapList??????
  138. */
  139. @Test
  140. public void objCompileCircularReferenceTest() {
  141. A a = new A();
  142. B b = new B();
  143. a.b = b;
  144. b.a = a;
  145. Object obj = Mapl.toMaplist(a);
  146. assertTrue(obj instanceof Map);
  147. assertNotNull(Mapl.cell(obj, "b"));
  148. assertEquals("b", Mapl.cell(obj, "b.name"));
  149. assertEquals("a", Mapl.cell(obj, "b.a.name"));
  150. assertEquals(Mapl.cell(obj, "b.a.b"), Mapl.cell(obj, "b"));
  151. }
  152. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. /**
  154. * ??????
  155. */
  156. /**
  157. * ????
  158. */
  159. @Test
  160. public void structureConvertSimple() {
  161. String json = "{'name':'jk', 'age':12}";
  162. String model = "{'name':'??', 'age':'??'}";
  163. String dest = "{\"??\":\"jk\",\"??\":12}";
  164. Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
  165. assertEquals("jk", Mapl.cell(obj, "??"));
  166. assertEquals(12, Mapl.cell(obj, "??"));
  167. assertEquals(dest, Json.toJson(obj, new JsonFormat()));
  168. }
  169. /**
  170. * ????
  171. */
  172. @Test
  173. public void structureConvertSimpleArray() {
  174. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  175. String model = "{'user':[{'name':'user[].??', 'age':'user[].??'}]}";
  176. Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
  177. assertEquals("jk", Mapl.cell(obj, "user[0].??"));
  178. assertEquals("nutz", Mapl.cell(obj, "user[1].??"));
  179. assertEquals(12, Mapl.cell(obj, "user[0].??"));
  180. assertEquals(5, Mapl.cell(obj, "user[1].??"));
  181. }
  182. /**
  183. * ?????
  184. */
  185. @Test
  186. public void structureConvertMultiPath() {
  187. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  188. String model = "{'user':[{'name':['user[].??', 'people[].name'], 'age':['user[].??', 'people[].age']}]}";
  189. Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
  190. assertEquals("jk", Mapl.cell(obj, "user[0].??"));
  191. assertEquals("nutz", Mapl.cell(obj, "user[1].??"));
  192. assertEquals("jk", Mapl.cell(obj, "people[0].name"));
  193. assertEquals(5, Mapl.cell(obj, "people[1].age"));
  194. }
  195. /**
  196. * ????Array???
  197. */
  198. @Test
  199. public void structureConvertRoot2Array() {
  200. String json = "[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]";
  201. String model = "[{'name':['user[].??', 'people[].name'], 'age':['user[].??', 'people[].age']}]";
  202. String dest = "{\"people\":[{\"age\":12,\"name\":\"jk\"}, {\"age\":5,\"name\":\"nutz\"}],\"user\":[{\"??\":\"jk\",\"??\":12}, {\"??\":\"nutz\",\"??\":5}]}";
  203. Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
  204. assertEquals("jk", Mapl.cell(obj, "user[0].??"));
  205. assertEquals("nutz", Mapl.cell(obj, "user[1].??"));
  206. assertEquals("jk", Mapl.cell(obj, "people[0].name"));
  207. assertEquals(5, Mapl.cell(obj, "people[1].age"));
  208. assertEquals(dest, Json.toJson(obj, new JsonFormat()));
  209. }
  210. /**
  211. * Array????array??
  212. */
  213. @Test
  214. public void structureConvertArray2Root() {
  215. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  216. String model = "{'user':[{'name':['[].name'], 'age':'[].age'}]}";
  217. String dest = "[{\"age\":12,\"name\":\"jk\"}, {\"age\":5,\"name\":\"nutz\"}]";
  218. Object obj = Mapl.convert(Json.fromJson(Lang.inr(json)), Lang.inr(model));
  219. assertEquals("jk", Mapl.cell(obj, "[0].name"));
  220. assertEquals(5, Mapl.cell(obj, "[1].age"));
  221. assertEquals(dest, Json.toJson(obj, new JsonFormat()));
  222. }
  223. /**
  224. * ?????mapl?
  225. */
  226. @Test
  227. public void addItemTest() {
  228. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  229. Object obj = Json.fromJson(json);
  230. Mapl.put(obj, "user[0].test", "test");
  231. assertEquals("test", Mapl.cell(obj, "user[0].test"));
  232. }
  233. /**
  234. * ?????mapl?
  235. */
  236. @Test
  237. public void delItemTest() {
  238. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  239. Object obj = Json.fromJson(json);
  240. Mapl.del(obj, "user[0].age");
  241. assertNull(Mapl.cell(obj, "user[0].age"));
  242. assertEquals(1, ((Map<?, ?>) Mapl.cell(obj, "user[0]")).size());
  243. }
  244. /**
  245. * ?????mapl?
  246. */
  247. @Test
  248. public void updateItemTest() {
  249. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  250. Object obj = Json.fromJson(json);
  251. Mapl.update(obj, "user[0].name", "test");
  252. assertEquals("test", Mapl.cell(obj, "user[0].name"));
  253. }
  254. @Test
  255. public void issue243Test() {
  256. String json = "{'user':[{'name':'jk', 'age':12},{'name':'nutz', 'age':5}]}";
  257. Object obj = Json.fromJson(json);
  258. Object item = Mapl.cell(obj, "user[]");
  259. assertFalse(item instanceof List);
  260. assertTrue(item instanceof Map);
  261. }
  262. @Test
  263. public void issue243Test2() {
  264. List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
  265. HashMap<String, Object> map = new HashMap<String, Object>();
  266. List<String> items = new LinkedList<String>();
  267. items.add(new String("aa"));
  268. items.add(new String("bb"));
  269. map.put("id", 0);
  270. map.put("items", items);
  271. list.add(map);
  272. assertEquals(Mapl.cell(list, "[0].items.0"), "aa");
  273. assertTrue(Mapl.cell(list, "[0].items[0]").equals("aa"));
  274. assertTrue(Mapl.cell(list, "[0].items[1]").equals("bb"));
  275. assertTrue(Mapl.cell(list, "[0].items[]").equals("aa"));
  276. assertTrue(Mapl.cell(list, "[0].items").equals(items));
  277. }
  278. @Test
  279. public void testIssue322() {
  280. String json = "{name:'nutz', age:12, address:[{area:1,name:'abc'},{area:2,name:'123'}]}";
  281. Object obj = Json.fromJson(json);
  282. Object newobj = Mapl.excludeFilter(obj, Lang.list("age", "address[].area"));
  283. JsonFormat jf = new JsonFormat(true);
  284. assertEquals("{\"address\":[{\"name\":\"abc\"}, {\"name\":\"123\"}],\"name\":\"nutz\"}",
  285. Json.toJson(newobj, jf));
  286. }
  287. /**
  288. * ???????????????
  289. */
  290. @Test
  291. public void excludeFilterConvertTest_MultiplePath1() {
  292. List<String> paths = new ArrayList<String>();
  293. paths.add("users[].name");
  294. paths.add("people[].age");
  295. Object dest = Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"));
  296. Object obj = Mapl.excludeFilter(dest, paths);
  297. assertNotNull(Mapl.cell(obj, "users"));
  298. assertEquals(12, Mapl.cell(obj, "users[0].age"));
  299. assertEquals("1", Mapl.cell(obj, "people[0].name"));
  300. }
  301. /**
  302. * ??????????????????????????
  303. */
  304. @Test
  305. public void excludeFilterConvertTest_MultiplePath2() {
  306. String json = "{name:'nutz', age:12, address:[{area:1,name:'abc'},{area:2,name:'123'}]}";
  307. Object obj = Json.fromJson(json);
  308. List<String> list = new ArrayList<String>();
  309. list.add("age");
  310. list.add("address[].area");
  311. Object newobj = Mapl.excludeFilter(obj, list);
  312. assertNull(Mapl.cell(newobj, "age"));
  313. assertEquals("nutz", Mapl.cell(newobj, "name"));
  314. assertNull(Mapl.cell(newobj, "address[0].area"));
  315. assertEquals("abc", Mapl.cell(newobj, "address[0].name"));
  316. }
  317. }