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

https://github.com/alibaba/fastjson · Java · 144 lines · 100 code · 32 blank · 12 comment · 2 complexity · ea76df85b825cfd8ef404d559a8a602e MD5 · raw file

  1. package com.alibaba.json.bvt.serializer;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.Reader;
  5. import java.io.StringReader;
  6. import java.io.Writer;
  7. import java.sql.Clob;
  8. import java.sql.SQLException;
  9. import junit.framework.TestCase;
  10. import org.junit.Assert;
  11. import com.alibaba.fastjson.JSON;
  12. import com.alibaba.fastjson.serializer.SerializerFeature;
  13. public class ClobSerializerTest extends TestCase {
  14. public void test_clob() throws Exception {
  15. Assert.assertEquals("\"abcdefg中国\"",
  16. JSON.toJSONString(new MockClob("abcdefg中国")));
  17. }
  18. public void test_clob_null() throws Exception {
  19. Assert.assertEquals("{\"value\":null}", JSON.toJSONString(new VO(),
  20. SerializerFeature.WriteMapNullValue));
  21. }
  22. public void test_clob_error() throws Exception {
  23. Exception error = null;
  24. try {
  25. JSON.toJSONString(new MockClob(new SQLException()));
  26. } catch (Exception ex) {
  27. error = ex;
  28. }
  29. Assert.assertNotNull(error);
  30. }
  31. @SuppressWarnings("unused")
  32. private static class VO {
  33. private Clob value;
  34. public Clob getValue() {
  35. return value;
  36. }
  37. public void setValue(Clob value) {
  38. this.value = value;
  39. }
  40. }
  41. public static class MockClob implements Clob {
  42. private final String text;
  43. private SQLException error;
  44. public MockClob(String text) {
  45. this.text = text;
  46. }
  47. public MockClob(SQLException error) {
  48. this.text = null;
  49. this.error = error;
  50. }
  51. public SQLException getError() {
  52. return error;
  53. }
  54. public void setError(SQLException error) {
  55. this.error = error;
  56. }
  57. public long length() throws SQLException {
  58. // TODO Auto-generated method stub
  59. return 0;
  60. }
  61. public String getSubString(long pos, int length) throws SQLException {
  62. // TODO Auto-generated method stub
  63. return null;
  64. }
  65. public Reader getCharacterStream() throws SQLException {
  66. if (error != null) {
  67. throw error;
  68. }
  69. return new StringReader(text);
  70. }
  71. public InputStream getAsciiStream() throws SQLException {
  72. // TODO Auto-generated method stub
  73. return null;
  74. }
  75. public long position(String searchstr, long start) throws SQLException {
  76. // TODO Auto-generated method stub
  77. return 0;
  78. }
  79. public long position(Clob searchstr, long start) throws SQLException {
  80. // TODO Auto-generated method stub
  81. return 0;
  82. }
  83. public int setString(long pos, String str) throws SQLException {
  84. // TODO Auto-generated method stub
  85. return 0;
  86. }
  87. public int setString(long pos, String str, int offset, int len)
  88. throws SQLException {
  89. // TODO Auto-generated method stub
  90. return 0;
  91. }
  92. public OutputStream setAsciiStream(long pos) throws SQLException {
  93. // TODO Auto-generated method stub
  94. return null;
  95. }
  96. public Writer setCharacterStream(long pos) throws SQLException {
  97. // TODO Auto-generated method stub
  98. return null;
  99. }
  100. public void truncate(long len) throws SQLException {
  101. // TODO Auto-generated method stub
  102. }
  103. public void free() throws SQLException {
  104. // TODO Auto-generated method stub
  105. }
  106. public Reader getCharacterStream(long pos, long length)
  107. throws SQLException {
  108. // TODO Auto-generated method stub
  109. return null;
  110. }
  111. }
  112. }