/src/test/java/com/alibaba/json/bvt/parser/DefaultExtJSONParser_parseArray.java

https://github.com/flydream/fastjson · Java · 285 lines · 246 code · 39 blank · 0 comment · 0 complexity · 809e1e5a1955f1b91eca0f28ce73776a MD5 · raw file

  1. package com.alibaba.json.bvt.parser;
  2. import java.lang.reflect.Type;
  3. import java.math.BigDecimal;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import junit.framework.Assert;
  8. import junit.framework.TestCase;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.alibaba.fastjson.TypeReference;
  11. import com.alibaba.fastjson.parser.DefaultExtJSONParser;
  12. import com.alibaba.fastjson.parser.Feature;
  13. import com.alibaba.fastjson.parser.JSONToken;
  14. public class DefaultExtJSONParser_parseArray extends TestCase {
  15. public void test_0() throws Exception {
  16. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,,,3]");
  17. List list = new ArrayList();
  18. parser.parseArray(int.class, list);
  19. Assert.assertEquals("[1, 2, 3]", list.toString());
  20. }
  21. public void test_1() throws Exception {
  22. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,3]");
  23. parser.config(Feature.AllowArbitraryCommas, true);
  24. List list = new ArrayList();
  25. parser.parseArray(int.class, list);
  26. Assert.assertEquals("[1, 2, 3]", list.toString());
  27. }
  28. public void test_2() throws Exception {
  29. DefaultExtJSONParser parser = new DefaultExtJSONParser("['1','2','3']");
  30. parser.config(Feature.AllowArbitraryCommas, true);
  31. List list = new ArrayList();
  32. parser.parseArray(String.class, list);
  33. Assert.assertEquals("[1, 2, 3]", list.toString());
  34. Assert.assertEquals("1", list.get(0));
  35. }
  36. public void test_3() throws Exception {
  37. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,3]");
  38. parser.config(Feature.AllowArbitraryCommas, true);
  39. List list = new ArrayList();
  40. parser.parseArray(BigDecimal.class, list);
  41. Assert.assertEquals("[1, 2, 3]", list.toString());
  42. Assert.assertEquals(new BigDecimal("1"), list.get(0));
  43. }
  44. public void test_4() throws Exception {
  45. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,3,null]");
  46. parser.config(Feature.AllowArbitraryCommas, true);
  47. List list = new ArrayList();
  48. parser.parseArray(BigDecimal.class, list);
  49. Assert.assertEquals("[1, 2, 3, null]", list.toString());
  50. Assert.assertEquals(new BigDecimal("1"), list.get(0));
  51. }
  52. public void test_5() throws Exception {
  53. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,3,null]");
  54. Object[] array = parser.parseArray(new Type[] { Integer.class, BigDecimal.class, Long.class, String.class });
  55. Assert.assertEquals(new Integer(1), array[0]);
  56. Assert.assertEquals(new BigDecimal("2"), array[1]);
  57. Assert.assertEquals(new Long(3), array[2]);
  58. Assert.assertEquals(null, array[3]);
  59. }
  60. public void test_error() throws Exception {
  61. DefaultExtJSONParser parser = new DefaultExtJSONParser("{}");
  62. Exception error = null;
  63. try {
  64. parser.parseArray(new ArrayList());
  65. } catch (Exception ex) {
  66. error = ex;
  67. }
  68. Assert.assertNotNull(error);
  69. }
  70. public void test_6() throws Exception {
  71. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1.2]");
  72. parser.config(Feature.UseBigDecimal, false);
  73. ArrayList list = new ArrayList();
  74. parser.parseArray(list);
  75. Assert.assertEquals(Double.valueOf(1.2), list.get(0));
  76. }
  77. public void test_7() throws Exception {
  78. DefaultExtJSONParser parser = new DefaultExtJSONParser("[\"2011-01-09T13:49:53.254\", \"xxx\", true, false, null, {}]");
  79. parser.config(Feature.AllowISO8601DateFormat, true);
  80. ArrayList list = new ArrayList();
  81. parser.parseArray(list);
  82. Assert.assertEquals(new Date(1294552193254L), list.get(0));
  83. Assert.assertEquals("xxx", list.get(1));
  84. Assert.assertEquals(Boolean.TRUE, list.get(2));
  85. Assert.assertEquals(Boolean.FALSE, list.get(3));
  86. Assert.assertEquals(null, list.get(4));
  87. Assert.assertEquals(new JSONObject(), list.get(5));
  88. }
  89. public void test_8() throws Exception {
  90. DefaultExtJSONParser parser = new DefaultExtJSONParser("\"2011-01-09T13:49:53.254\"");
  91. parser.config(Feature.AllowISO8601DateFormat, true);
  92. Object value = parser.parse();
  93. Assert.assertEquals(new Date(1294552193254L), value);
  94. }
  95. public void test_9() throws Exception {
  96. DefaultExtJSONParser parser = new DefaultExtJSONParser("");
  97. parser.config(Feature.AllowISO8601DateFormat, true);
  98. Object value = parser.parse();
  99. Assert.assertEquals(null, value);
  100. }
  101. public void test_error_2() throws Exception {
  102. DefaultExtJSONParser parser = new DefaultExtJSONParser("{}");
  103. Exception error = null;
  104. try {
  105. parser.accept(JSONToken.NULL);
  106. } catch (Exception ex) {
  107. error = ex;
  108. }
  109. Assert.assertNotNull(error);
  110. }
  111. public void test_10() throws Exception {
  112. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,3]");
  113. Object[] array = parser.parseArray(new Type[] { Integer[].class });
  114. Integer[] values = (Integer[]) array[0];
  115. Assert.assertEquals(new Integer(1), values[0]);
  116. Assert.assertEquals(new Integer(2), values[1]);
  117. Assert.assertEquals(new Integer(3), values[2]);
  118. }
  119. public void test_11() throws Exception {
  120. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1]");
  121. Object[] array = parser.parseArray(new Type[] { String.class });
  122. Assert.assertEquals("1", array[0]);
  123. }
  124. public void test_12() throws Exception {
  125. DefaultExtJSONParser parser = new DefaultExtJSONParser("['1']");
  126. Object[] array = parser.parseArray(new Type[] { int.class });
  127. Assert.assertEquals(new Integer(1), array[0]);
  128. }
  129. public void test_13() throws Exception {
  130. DefaultExtJSONParser parser = new DefaultExtJSONParser("['1']");
  131. Object[] array = parser.parseArray(new Type[] { Integer.class });
  132. Assert.assertEquals(new Integer(1), array[0]);
  133. }
  134. public void test_14() throws Exception {
  135. DefaultExtJSONParser parser = new DefaultExtJSONParser("[]");
  136. Object[] array = parser.parseArray(new Type[] {});
  137. Assert.assertEquals(0, array.length);
  138. }
  139. public void test_15() throws Exception {
  140. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,null]");
  141. ArrayList list = new ArrayList();
  142. parser.config(Feature.AllowISO8601DateFormat, false);
  143. parser.parseArray(String.class, list);
  144. Assert.assertEquals("1", list.get(0));
  145. Assert.assertEquals(null, list.get(1));
  146. }
  147. public void test_16() throws Exception {
  148. DefaultExtJSONParser parser = new DefaultExtJSONParser("[[1]]");
  149. parser.config(Feature.AllowISO8601DateFormat, false);
  150. Object[] array = parser.parseArray(new Type[] { new TypeReference<List<Integer>>() {
  151. }.getType() });
  152. Assert.assertEquals(new Integer(1), ((List<Integer>) (array[0])).get(0));
  153. }
  154. public void test_17() throws Exception {
  155. DefaultExtJSONParser parser = new DefaultExtJSONParser("[]");
  156. Object[] array = parser.parseArray(new Type[] { Integer[].class });
  157. Integer[] values = (Integer[]) array[0];
  158. Assert.assertEquals(0, values.length);
  159. }
  160. public void test_18() throws Exception {
  161. DefaultExtJSONParser parser = new DefaultExtJSONParser("null");
  162. parser.config(Feature.AllowISO8601DateFormat, false);
  163. List<Integer> list = (List<Integer>) parser.parseArrayWithType(new TypeReference<List<Integer>>() {
  164. }.getType());
  165. Assert.assertEquals(null, list);
  166. }
  167. public void test_error_var() throws Exception {
  168. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,null }");
  169. parser.config(Feature.AllowISO8601DateFormat, false);
  170. Exception error = null;
  171. try {
  172. Object[] array = parser.parseArray(new Type[] { Integer[].class });
  173. } catch (Exception ex) {
  174. error = ex;
  175. }
  176. Assert.assertNotNull(error);
  177. }
  178. public void test_error_3() throws Exception {
  179. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,null }");
  180. ArrayList list = new ArrayList();
  181. parser.config(Feature.AllowISO8601DateFormat, false);
  182. Exception error = null;
  183. try {
  184. parser.parseArray(String.class, list);
  185. } catch (Exception ex) {
  186. error = ex;
  187. }
  188. Assert.assertNotNull(error);
  189. }
  190. public void test_error_4() throws Exception {
  191. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,null }");
  192. parser.config(Feature.AllowISO8601DateFormat, false);
  193. Exception error = null;
  194. try {
  195. parser.parseArray(new Type[] { String.class });
  196. } catch (Exception ex) {
  197. error = ex;
  198. }
  199. Assert.assertNotNull(error);
  200. }
  201. public void test_error_5() throws Exception {
  202. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,null }");
  203. ArrayList list = new ArrayList();
  204. parser.config(Feature.AllowISO8601DateFormat, false);
  205. Exception error = null;
  206. try {
  207. parser.parseArray(String.class, list);
  208. } catch (Exception ex) {
  209. error = ex;
  210. }
  211. Assert.assertNotNull(error);
  212. }
  213. public void test_error_6() throws Exception {
  214. DefaultExtJSONParser parser = new DefaultExtJSONParser("{1,null }");
  215. parser.config(Feature.AllowISO8601DateFormat, false);
  216. Exception error = null;
  217. try {
  218. parser.parseArray(new Type[] { String.class });
  219. } catch (Exception ex) {
  220. error = ex;
  221. }
  222. Assert.assertNotNull(error);
  223. }
  224. public void test_error_7() throws Exception {
  225. DefaultExtJSONParser parser = new DefaultExtJSONParser("{1}");
  226. parser.config(Feature.AllowISO8601DateFormat, false);
  227. Exception error = null;
  228. try {
  229. parser.parseArray(new Type[] {});
  230. } catch (Exception ex) {
  231. error = ex;
  232. }
  233. Assert.assertNotNull(error);
  234. }
  235. public void test_error_8() throws Exception {
  236. DefaultExtJSONParser parser = new DefaultExtJSONParser("[1,2,3 4]");
  237. parser.config(Feature.AllowISO8601DateFormat, false);
  238. Exception error = null;
  239. try {
  240. parser.parseArray(new Type[] { Integer.class });
  241. } catch (Exception ex) {
  242. error = ex;
  243. }
  244. Assert.assertNotNull(error);
  245. }
  246. }