/src/test/java/com/alibaba/json/bvt/JSONObjectTest3.java

https://github.com/alibaba/fastjson · Java · 182 lines · 140 code · 42 blank · 0 comment · 0 complexity · 8c99b9f3da2b0c4e912a0793c91277be MD5 · raw file

  1. package com.alibaba.json.bvt;
  2. import org.junit.Assert;
  3. import junit.framework.TestCase;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONException;
  6. import com.alibaba.fastjson.annotation.JSONField;
  7. public class JSONObjectTest3 extends TestCase {
  8. public void test_0() throws Exception {
  9. String text = "{value:'123',big:false}";
  10. Bean bean = JSON.parseObject(text, Bean.class);
  11. Assert.assertEquals("123", bean.getValue());
  12. Assert.assertEquals(false, bean.isBig());
  13. Assert.assertEquals(123, bean.getIntValue());
  14. bean.setBig(true);
  15. Assert.assertEquals(true, bean.isBig());
  16. bean.setID(567);
  17. Assert.assertEquals(567, bean.getID());
  18. }
  19. public void test_error_0() throws Exception {
  20. String text = "{value:'123',big:false}";
  21. Bean bean = JSON.parseObject(text, Bean.class);
  22. JSONException error = null;
  23. try {
  24. bean.f();
  25. } catch (JSONException ex) {
  26. error = ex;
  27. }
  28. Assert.assertNotNull(error);
  29. }
  30. public void test_error_1() throws Exception {
  31. String text = "{value:'123',big:false}";
  32. Bean bean = JSON.parseObject(text, Bean.class);
  33. JSONException error = null;
  34. try {
  35. bean.f(1);
  36. } catch (JSONException ex) {
  37. error = ex;
  38. }
  39. Assert.assertNotNull(error);
  40. }
  41. public void test_error_2() throws Exception {
  42. String text = "{value:'123',big:false}";
  43. Bean bean = JSON.parseObject(text, Bean.class);
  44. JSONException error = null;
  45. try {
  46. bean.get();
  47. } catch (JSONException ex) {
  48. error = ex;
  49. }
  50. Assert.assertNotNull(error);
  51. }
  52. public void test_error_3() throws Exception {
  53. String text = "{value:'123',big:false}";
  54. Bean bean = JSON.parseObject(text, Bean.class);
  55. JSONException error = null;
  56. try {
  57. bean.is();
  58. } catch (JSONException ex) {
  59. error = ex;
  60. }
  61. Assert.assertNotNull(error);
  62. }
  63. public void test_error_4() throws Exception {
  64. String text = "{value:'123',big:false}";
  65. Bean bean = JSON.parseObject(text, Bean.class);
  66. Exception error = null;
  67. try {
  68. bean.f(1, 2);
  69. } catch (UnsupportedOperationException ex) {
  70. error = ex;
  71. }
  72. Assert.assertNotNull(error);
  73. }
  74. public void test_error_5() throws Exception {
  75. String text = "{value:'123',big:false}";
  76. Bean bean = JSON.parseObject(text, Bean.class);
  77. JSONException error = null;
  78. try {
  79. bean.getA();
  80. } catch (JSONException ex) {
  81. error = ex;
  82. }
  83. Assert.assertNotNull(error);
  84. }
  85. public void test_error_6() throws Exception {
  86. String text = "{value:'123',big:false}";
  87. Bean bean = JSON.parseObject(text, Bean.class);
  88. JSONException error = null;
  89. try {
  90. bean.f1(1);
  91. } catch (JSONException ex) {
  92. error = ex;
  93. }
  94. Assert.assertNotNull(error);
  95. }
  96. public void test_error_7() throws Exception {
  97. String text = "{value:'123',big:false}";
  98. Bean bean = JSON.parseObject(text, Bean.class);
  99. JSONException error = null;
  100. try {
  101. bean.set(1);
  102. } catch (JSONException ex) {
  103. error = ex;
  104. }
  105. Assert.assertNotNull(error);
  106. }
  107. public void test_error_8() throws Exception {
  108. String text = "{value:'123',big:false}";
  109. Bean bean = JSON.parseObject(text, Bean.class);
  110. JSONException error = null;
  111. try {
  112. bean.xx();
  113. } catch (JSONException ex) {
  114. error = ex;
  115. }
  116. Assert.assertNotNull(error);
  117. }
  118. public static interface Bean {
  119. String getValue();
  120. void setValue(String value);
  121. boolean isBig();
  122. @JSONField
  123. void setBig(boolean value);
  124. @JSONField(name = "value")
  125. int getIntValue();
  126. @JSONField(name = "id")
  127. void setID(int value);
  128. @JSONField(name = "id")
  129. int getID();
  130. Object get();
  131. Object xx();
  132. void set(int i);
  133. boolean is();
  134. void getA();
  135. void f();
  136. Object f(int a);
  137. void f1(int a);
  138. void f(int a, int b);
  139. }
  140. }