/src/test/java/com/alibaba/json/bvt/parser/deser/list/ListStringFieldTest_stream_array.java

https://github.com/alibaba/fastjson · Java · 280 lines · 231 code · 49 blank · 0 comment · 0 complexity · 59bd5cfeeb90b4ad4c448dc5211213ea MD5 · raw file

  1. package com.alibaba.json.bvt.parser.deser.list;
  2. import java.io.StringReader;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.junit.Assert;
  6. import com.alibaba.fastjson.JSONException;
  7. import com.alibaba.fastjson.JSONReader;
  8. import com.alibaba.fastjson.TypeReference;
  9. import com.alibaba.fastjson.annotation.JSONType;
  10. import com.alibaba.fastjson.parser.Feature;
  11. import junit.framework.TestCase;
  12. public class ListStringFieldTest_stream_array extends TestCase {
  13. public void test_list() throws Exception {
  14. String text = "[[\"a\",null,\"b\",\"ab\\\\c\\\"a\"]]";
  15. JSONReader reader = new JSONReader(new StringReader(text));
  16. Model model = reader.readObject(Model.class);
  17. Assert.assertEquals(4, model.values.size());
  18. Assert.assertEquals("a", model.values.get(0));
  19. Assert.assertEquals(null, model.values.get(1));
  20. Assert.assertEquals("b", model.values.get(2));
  21. Assert.assertEquals("ab\\c\"a", model.values.get(3));
  22. }
  23. public void test_null() throws Exception {
  24. String text = "[null]";
  25. JSONReader reader = new JSONReader(new StringReader(text));
  26. Model model = reader.readObject(Model.class);
  27. Assert.assertNull(model.values);
  28. }
  29. public void test_empty() throws Exception {
  30. String text = "[[]]}";
  31. JSONReader reader = new JSONReader(new StringReader(text));
  32. Model model = reader.readObject(Model.class);
  33. Assert.assertEquals(0, model.values.size());
  34. }
  35. public void test_map_empty() throws Exception {
  36. String text = "{\"model\":[[]]}";
  37. JSONReader reader = new JSONReader(new StringReader(text));
  38. Map<String, Model> map = reader.readObject(new TypeReference<Map<String, Model>>() {
  39. });
  40. Model model = (Model) map.get("model");
  41. Assert.assertEquals(0, model.values.size());
  42. }
  43. public void test_map_empty_2() throws Exception {
  44. String text = "{\"model\":[[]],\"model2\":[[]]}";
  45. JSONReader reader = new JSONReader(new StringReader(text));
  46. Map<String, Model> map = reader.readObject(new TypeReference<Map<String, Model>>() {
  47. });
  48. Model model = (Model) map.get("model");
  49. Assert.assertEquals(0, model.values.size());
  50. Model model2 = (Model) map.get("model2");
  51. Assert.assertEquals(0, model2.values.size());
  52. }
  53. public void test_error() throws Exception {
  54. String text = "[[1{1,}";
  55. JSONReader reader = new JSONReader(new StringReader(text));
  56. Exception error = null;
  57. try {
  58. reader.readObject(Model.class);
  59. reader.close();
  60. } catch (JSONException ex) {
  61. error = ex;
  62. }
  63. Assert.assertNotNull(error);
  64. }
  65. public void test_error_0() throws Exception {
  66. String text = "[{";
  67. JSONReader reader = new JSONReader(new StringReader(text));
  68. Exception error = null;
  69. try {
  70. reader.readObject(Model.class);
  71. reader.close();
  72. } catch (JSONException ex) {
  73. error = ex;
  74. }
  75. Assert.assertNotNull(error);
  76. }
  77. public void test_error_n() throws Exception {
  78. String text = "[n";
  79. JSONReader reader = new JSONReader(new StringReader(text));
  80. Exception error = null;
  81. try {
  82. reader.readObject(Model.class);
  83. reader.close();
  84. } catch (JSONException ex) {
  85. error = ex;
  86. }
  87. Assert.assertNotNull(error);
  88. }
  89. public void test_error_nu() throws Exception {
  90. String text = "[nu";
  91. JSONReader reader = new JSONReader(new StringReader(text));
  92. Exception error = null;
  93. try {
  94. reader.readObject(Model.class);
  95. reader.close();
  96. } catch (JSONException ex) {
  97. error = ex;
  98. }
  99. Assert.assertNotNull(error);
  100. }
  101. public void test_error_nul() throws Exception {
  102. String text = "[nul";
  103. JSONReader reader = new JSONReader(new StringReader(text));
  104. Exception error = null;
  105. try {
  106. reader.readObject(Model.class);
  107. reader.close();
  108. } catch (JSONException ex) {
  109. error = ex;
  110. }
  111. Assert.assertNotNull(error);
  112. }
  113. public void test_error_null() throws Exception {
  114. String text = "[null";
  115. JSONReader reader = new JSONReader(new StringReader(text));
  116. Exception error = null;
  117. try {
  118. reader.readObject(Model.class);
  119. reader.close();
  120. } catch (JSONException ex) {
  121. error = ex;
  122. }
  123. Assert.assertNotNull(error);
  124. }
  125. public void test_error_fn() throws Exception {
  126. String text = "[[n";
  127. JSONReader reader = new JSONReader(new StringReader(text));
  128. Exception error = null;
  129. try {
  130. reader.readObject(Model.class);
  131. reader.close();
  132. } catch (JSONException ex) {
  133. error = ex;
  134. }
  135. Assert.assertNotNull(error);
  136. }
  137. public void test_error_fnu() throws Exception {
  138. String text = "[[nu";
  139. JSONReader reader = new JSONReader(new StringReader(text));
  140. Exception error = null;
  141. try {
  142. reader.readObject(Model.class);
  143. reader.close();
  144. } catch (JSONException ex) {
  145. error = ex;
  146. }
  147. Assert.assertNotNull(error);
  148. }
  149. public void test_error_fnul() throws Exception {
  150. String text = "[[nul";
  151. JSONReader reader = new JSONReader(new StringReader(text));
  152. Exception error = null;
  153. try {
  154. reader.readObject(Model.class);
  155. reader.close();
  156. } catch (JSONException ex) {
  157. error = ex;
  158. }
  159. Assert.assertNotNull(error);
  160. }
  161. public void test_error_fnull() throws Exception {
  162. String text = "[[null";
  163. JSONReader reader = new JSONReader(new StringReader(text));
  164. Exception error = null;
  165. try {
  166. reader.readObject(Model.class);
  167. reader.close();
  168. } catch (JSONException ex) {
  169. error = ex;
  170. }
  171. Assert.assertNotNull(error);
  172. }
  173. public void test_error_notclose() throws Exception {
  174. String text = "[[\"aaa";
  175. JSONReader reader = new JSONReader(new StringReader(text));
  176. Exception error = null;
  177. try {
  178. reader.readObject(Model.class);
  179. reader.close();
  180. } catch (JSONException ex) {
  181. error = ex;
  182. }
  183. Assert.assertNotNull(error);
  184. }
  185. public void test_error_1() throws Exception {
  186. String text = "[[\"b\"[[{";
  187. JSONReader reader = new JSONReader(new StringReader(text));
  188. Exception error = null;
  189. try {
  190. reader.readObject(Model.class);
  191. reader.close();
  192. } catch (JSONException ex) {
  193. error = ex;
  194. }
  195. Assert.assertNotNull(error);
  196. }
  197. public void test_error_2() throws Exception {
  198. String text = "{\"model\":[[][";
  199. JSONReader reader = new JSONReader(new StringReader(text));
  200. Exception error = null;
  201. try {
  202. reader.readObject(new TypeReference<Map<String, Model>>() {
  203. });
  204. } catch (JSONException ex) {
  205. error = ex;
  206. }
  207. Assert.assertNotNull(error);
  208. }
  209. public void test_error_3() throws Exception {
  210. String text = "{\"model\":[[]}[";
  211. JSONReader reader = new JSONReader(new StringReader(text));
  212. Exception error = null;
  213. try {
  214. reader.readObject(new TypeReference<Map<String, Model>>() {
  215. });
  216. } catch (JSONException ex) {
  217. error = ex;
  218. }
  219. Assert.assertNotNull(error);
  220. }
  221. @JSONType(parseFeatures = Feature.SupportArrayToBean)
  222. public static class Model {
  223. private List<String> values;
  224. public List<String> getValues() {
  225. return values;
  226. }
  227. public void setValues(List<String> values) {
  228. this.values = values;
  229. }
  230. }
  231. }