/src/test/java/com/alibaba/json/bvt/serializer/SerializeWriterTest.java

https://github.com/alibaba/fastjson · Java · 229 lines · 193 code · 36 blank · 0 comment · 0 complexity · 7e60c2bbe14433cc8f7a6624dcbc66c7 MD5 · raw file

  1. package com.alibaba.json.bvt.serializer;
  2. import java.io.StringWriter;
  3. import java.lang.reflect.Field;
  4. import org.junit.Assert;
  5. import com.alibaba.fastjson.serializer.SerializeWriter;
  6. import com.alibaba.fastjson.serializer.SerializerFeature;
  7. import junit.framework.TestCase;
  8. public class SerializeWriterTest extends TestCase {
  9. public void test_0() throws Exception {
  10. SerializeWriter out = new SerializeWriter(1);
  11. out.write('a');
  12. out.write('b');
  13. out.write('c');
  14. Assert.assertEquals("abc", out.toString());
  15. StringWriter writer = new StringWriter();
  16. out.writeTo(writer);
  17. Assert.assertEquals("abc", writer.toString());
  18. }
  19. public void test_1() throws Exception {
  20. SerializeWriter out = new SerializeWriter(1);
  21. out.write((int) 'a');
  22. out.write((int) 'b');
  23. out.write((int) 'c');
  24. out.write(new char[0], 0, 0);
  25. Assert.assertEquals("abc", out.toString());
  26. StringWriter writer = new StringWriter();
  27. out.writeTo(writer);
  28. Assert.assertEquals("abc", writer.toString());
  29. out.expandCapacity(128);
  30. }
  31. public void test_12() throws Exception {
  32. SerializeWriter out = new SerializeWriter(1);
  33. out.append("abc");
  34. Assert.assertEquals("abc", out.toString());
  35. Assert.assertEquals(3, out.toCharArray().length);
  36. Assert.assertEquals(3, out.size());
  37. Field field = SerializeWriter.class.getDeclaredField("count");
  38. field.setAccessible(true);
  39. field.setInt(out, 0);
  40. Assert.assertEquals("", out.toString());
  41. Assert.assertEquals(0, out.toCharArray().length);
  42. Assert.assertEquals(0, out.size());
  43. out.writeInt(Integer.MIN_VALUE);
  44. Assert.assertEquals(Integer.toString(Integer.MIN_VALUE), out.toString());
  45. out.flush();
  46. out.close();
  47. }
  48. public void test_13() throws Exception {
  49. SerializeWriter out = new SerializeWriter(1);
  50. out.writeInt(Integer.MIN_VALUE);
  51. Assert.assertEquals(Integer.toString(Integer.MIN_VALUE), out.toString());
  52. }
  53. public void test_13_long() throws Exception {
  54. SerializeWriter out = new SerializeWriter(1);
  55. out.writeLong(Long.MIN_VALUE);
  56. Assert.assertEquals(Long.toString(Long.MIN_VALUE), out.toString());
  57. }
  58. public void test_13_long_browser() throws Exception {
  59. SerializeWriter out = new SerializeWriter(SerializerFeature.BrowserCompatible);
  60. out.writeLong(Long.MIN_VALUE + 1);
  61. Assert.assertEquals("\"" + Long.toString(Long.MIN_VALUE + 1) + "\"", out.toString());
  62. }
  63. public void test_13_long_browser2() throws Exception {
  64. SerializeWriter out = new SerializeWriter(SerializerFeature.BrowserCompatible);
  65. out.writeLong(Long.MIN_VALUE);
  66. Assert.assertEquals("\"" + Long.toString(Long.MIN_VALUE) + "\"", out.toString());
  67. }
  68. public void test_14() throws Exception {
  69. SerializeWriter out = new SerializeWriter(1);
  70. out.writeInt(Integer.MAX_VALUE);
  71. Assert.assertEquals(Integer.toString(Integer.MAX_VALUE), out.toString());
  72. }
  73. public void test_14_long() throws Exception {
  74. SerializeWriter out = new SerializeWriter(1);
  75. out.writeLong(Long.MAX_VALUE);
  76. Assert.assertEquals(Long.toString(Long.MAX_VALUE), out.toString());
  77. }
  78. public void test_15() throws Exception {
  79. SerializeWriter out = new SerializeWriter(1);
  80. out.writeInt(Integer.MAX_VALUE);
  81. out.write(',');
  82. Assert.assertEquals(Integer.toString(Integer.MAX_VALUE) + ",", out.toString());
  83. }
  84. public void test_15_long() throws Exception {
  85. SerializeWriter out = new SerializeWriter(1);
  86. out.writeLong(Long.MAX_VALUE);
  87. out.write(',');
  88. Assert.assertEquals(Long.toString(Long.MAX_VALUE) + ",", out.toString());
  89. }
  90. public void test_16() throws Exception {
  91. SerializeWriter out = new SerializeWriter(1);
  92. out.writeInt(Integer.MIN_VALUE);
  93. out.write(',');
  94. Assert.assertEquals(Integer.toString(Integer.MIN_VALUE) + ",", out.toString());
  95. }
  96. public void test_16_long() throws Exception {
  97. SerializeWriter out = new SerializeWriter(1);
  98. out.writeLong(Long.MIN_VALUE);
  99. out.write(',');
  100. Assert.assertEquals(Long.toString(Long.MIN_VALUE) + ",", out.toString());
  101. }
  102. public void test_16_long_browser() throws Exception {
  103. SerializeWriter out = new SerializeWriter(SerializerFeature.BrowserCompatible);
  104. out.writeLong(Long.MIN_VALUE + 1);
  105. out.write(',');
  106. Assert.assertEquals("\"" + Long.toString(Long.MIN_VALUE + 1) + "\",", out.toString());
  107. }
  108. public void test_16_long_browser2() throws Exception {
  109. SerializeWriter out = new SerializeWriter(SerializerFeature.BrowserCompatible);
  110. out.writeLong(Long.MIN_VALUE);
  111. out.write(',');
  112. Assert.assertEquals("\"" + Long.toString(Long.MIN_VALUE) + "\",", out.toString());
  113. }
  114. public void test_17() throws Exception {
  115. SerializeWriter out = new SerializeWriter(1);
  116. out.append(null);
  117. Assert.assertEquals("null", out.toString());
  118. }
  119. public void test_18() throws Exception {
  120. SerializeWriter out = new SerializeWriter(1);
  121. out.append(null, 0, 4);
  122. Assert.assertEquals("null", out.toString());
  123. }
  124. public void test_19() throws Exception {
  125. SerializeWriter out = new SerializeWriter(1);
  126. out.append("abcd", 0, 4);
  127. Assert.assertEquals("abcd", out.toString());
  128. }
  129. public void test_20() throws Exception {
  130. SerializeWriter out = new SerializeWriter(1);
  131. out.write("abcd".toCharArray(), 0, 4);
  132. Assert.assertEquals("abcd", out.toString());
  133. }
  134. public void test_error_0() throws Exception {
  135. Exception error = null;
  136. try {
  137. new SerializeWriter(-1);
  138. } catch (IllegalArgumentException ex) {
  139. error = ex;
  140. }
  141. Assert.assertNotNull(error);
  142. }
  143. public void test_error_2() throws Exception {
  144. Exception error = null;
  145. try {
  146. SerializeWriter out = new SerializeWriter(16);
  147. out.write(new char[0], -1, 0);
  148. } catch (IndexOutOfBoundsException ex) {
  149. error = ex;
  150. }
  151. Assert.assertNotNull(error);
  152. }
  153. public void test_error_3() throws Exception {
  154. Exception error = null;
  155. try {
  156. SerializeWriter out = new SerializeWriter(16);
  157. out.write(new char[0], 2, 0);
  158. } catch (IndexOutOfBoundsException ex) {
  159. error = ex;
  160. }
  161. Assert.assertNotNull(error);
  162. }
  163. public void test_error_4() throws Exception {
  164. Exception error = null;
  165. try {
  166. SerializeWriter out = new SerializeWriter(16);
  167. out.write(new char[0], 0, -1);
  168. } catch (IndexOutOfBoundsException ex) {
  169. error = ex;
  170. }
  171. Assert.assertNotNull(error);
  172. }
  173. public void test_error_5() throws Exception {
  174. Exception error = null;
  175. try {
  176. SerializeWriter out = new SerializeWriter(16);
  177. out.write(new char[0], 0, 1);
  178. } catch (IndexOutOfBoundsException ex) {
  179. error = ex;
  180. }
  181. Assert.assertNotNull(error);
  182. }
  183. public void test_error_6() throws Exception {
  184. Exception error = null;
  185. try {
  186. SerializeWriter out = new SerializeWriter(16);
  187. out.write("abcdefg".toCharArray(), 1, 1 + Integer.MAX_VALUE);
  188. } catch (IndexOutOfBoundsException ex) {
  189. error = ex;
  190. }
  191. Assert.assertNotNull(error);
  192. }
  193. }