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

https://github.com/alibaba/fastjson · Java · 208 lines · 169 code · 39 blank · 0 comment · 0 complexity · e9f936e8df9c2aa3b37209bdccb7ed3b 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 junit.framework.TestCase;
  10. public class ListStringFieldTest_stream extends TestCase {
  11. public void test_list() throws Exception {
  12. String text = "{\"values\":[\"a\",null,\"b\",\"ab\\\\c\\\"\"]}";
  13. JSONReader reader = new JSONReader(new StringReader(text));
  14. Model model = reader.readObject(Model.class);
  15. Assert.assertEquals(4, model.values.size());
  16. Assert.assertEquals("a", model.values.get(0));
  17. Assert.assertEquals(null, model.values.get(1));
  18. Assert.assertEquals("b", model.values.get(2));
  19. Assert.assertEquals("ab\\c\"", model.values.get(3));
  20. }
  21. public void test_null() throws Exception {
  22. String text = "{\"values\":null}";
  23. JSONReader reader = new JSONReader(new StringReader(text));
  24. Model model = reader.readObject(Model.class);
  25. Assert.assertNull(model.values);
  26. }
  27. public void test_empty() throws Exception {
  28. String text = "{\"values\":[]}";
  29. JSONReader reader = new JSONReader(new StringReader(text));
  30. Model model = reader.readObject(Model.class);
  31. Assert.assertEquals(0, model.values.size());
  32. }
  33. public void test_map_empty() throws Exception {
  34. String text = "{\"model\":{\"values\":[]}}";
  35. JSONReader reader = new JSONReader(new StringReader(text));
  36. Map<String, Model> map = reader.readObject(new TypeReference<Map<String, Model>>() {
  37. });
  38. Model model = (Model) map.get("model");
  39. Assert.assertEquals(0, model.values.size());
  40. }
  41. public void test_notMatch() throws Exception {
  42. String text = "{\"value\":[]}";
  43. JSONReader reader = new JSONReader(new StringReader(text));
  44. Model model = reader.readObject(Model.class);
  45. Assert.assertNull(model.values);
  46. }
  47. public void test_error() throws Exception {
  48. String text = "{\"values\":[1";
  49. JSONReader reader = new JSONReader(new StringReader(text));
  50. Exception error = null;
  51. try {
  52. reader.readObject(Model.class);
  53. } catch (JSONException ex) {
  54. error = ex;
  55. }
  56. Assert.assertNotNull(error);
  57. }
  58. public void test_error_1() throws Exception {
  59. String text = "{\"values\":[\"b\"[";
  60. JSONReader reader = new JSONReader(new StringReader(text));
  61. Exception error = null;
  62. try {
  63. reader.readObject(Model.class);
  64. } catch (JSONException ex) {
  65. error = ex;
  66. }
  67. Assert.assertNotNull(error);
  68. }
  69. public void test_error_2() throws Exception {
  70. String text = "{\"model\":{\"values\":[][";
  71. JSONReader reader = new JSONReader(new StringReader(text));
  72. Exception error = null;
  73. try {
  74. reader.readObject(new TypeReference<Map<String, Model>>() {
  75. });
  76. } catch (JSONException ex) {
  77. error = ex;
  78. }
  79. Assert.assertNotNull(error);
  80. }
  81. public void test_error_3() throws Exception {
  82. String text = "{\"model\":{\"values\":[]}[";
  83. JSONReader reader = new JSONReader(new StringReader(text));
  84. Exception error = null;
  85. try {
  86. reader.readObject(new TypeReference<Map<String, Model>>() {
  87. });
  88. } catch (JSONException ex) {
  89. error = ex;
  90. }
  91. Assert.assertNotNull(error);
  92. }
  93. public void test_error_4() throws Exception {
  94. String text = "{\"model\":{\"values\":[\"aaa]}[";
  95. JSONReader reader = new JSONReader(new StringReader(text));
  96. Exception error = null;
  97. try {
  98. reader.readObject(new TypeReference<Map<String, Model>>() {
  99. });
  100. } catch (JSONException ex) {
  101. error = ex;
  102. }
  103. Assert.assertNotNull(error);
  104. }
  105. public void test_error_n() throws Exception {
  106. String text = "{\"values\":[n";
  107. JSONReader reader = new JSONReader(new StringReader(text));
  108. Exception error = null;
  109. try {
  110. reader.readObject(Model.class);
  111. } catch (JSONException ex) {
  112. error = ex;
  113. }
  114. Assert.assertNotNull(error);
  115. }
  116. public void test_error_nu() throws Exception {
  117. String text = "{\"values\":[nu";
  118. JSONReader reader = new JSONReader(new StringReader(text));
  119. Exception error = null;
  120. try {
  121. reader.readObject(Model.class);
  122. } catch (JSONException ex) {
  123. error = ex;
  124. }
  125. Assert.assertNotNull(error);
  126. }
  127. public void test_error_nul() throws Exception {
  128. String text = "{\"values\":[nul";
  129. JSONReader reader = new JSONReader(new StringReader(text));
  130. Exception error = null;
  131. try {
  132. reader.readObject(Model.class);
  133. } catch (JSONException ex) {
  134. error = ex;
  135. }
  136. Assert.assertNotNull(error);
  137. }
  138. public void test_error_null() throws Exception {
  139. String text = "{\"values\":[null";
  140. JSONReader reader = new JSONReader(new StringReader(text));
  141. Exception error = null;
  142. try {
  143. reader.readObject(Model.class);
  144. } catch (JSONException ex) {
  145. error = ex;
  146. }
  147. Assert.assertNotNull(error);
  148. }
  149. public void test_error_rbacket() throws Exception {
  150. String text = "{\"values\":[null,]";
  151. JSONReader reader = new JSONReader(new StringReader(text));
  152. Exception error = null;
  153. try {
  154. reader.readObject(Model.class);
  155. } catch (JSONException ex) {
  156. error = ex;
  157. }
  158. Assert.assertNotNull(error);
  159. }
  160. public static class Model {
  161. private List<String> values;
  162. public List<String> getValues() {
  163. return values;
  164. }
  165. public void setValues(List<String> values) {
  166. this.values = values;
  167. }
  168. }
  169. }