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

https://github.com/alibaba/fastjson · Java · 161 lines · 122 code · 39 blank · 0 comment · 4 complexity · f02332dba368c41373e080db70a8df5e MD5 · raw file

  1. package com.alibaba.json.bvt.serializer;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.junit.Assert;
  5. import junit.framework.TestCase;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
  8. public class SimplePropertyPreFilterTest extends TestCase {
  9. private VO vo;
  10. private A a;
  11. private Map<String, Object> map;
  12. protected void setUp() throws Exception {
  13. vo = new VO();
  14. vo.setId(123);
  15. vo.setName("sandzhangtoo");
  16. a = new A();
  17. a.setId(123);
  18. a.setName("sandzhangtoo");
  19. map = new HashMap<String, Object>();
  20. map.put("id", 123);
  21. map.put("name", "sandzhangtoo");
  22. map.put(null, null);
  23. }
  24. public void test_name() throws Exception {
  25. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class, "name");
  26. Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONString(vo, filter));
  27. }
  28. public void test_name_0() throws Exception {
  29. SimplePropertyPreFilter filter = new SimplePropertyPreFilter("name");
  30. Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONString(vo, filter));
  31. }
  32. public void test_name_a() throws Exception {
  33. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class, "name");
  34. Assert.assertEquals(VO.class, filter.getClazz());
  35. Assert.assertEquals(1, filter.getIncludes().size());
  36. Assert.assertTrue(filter.apply(null, null, null));
  37. String text = JSON.toJSONString(a, filter);
  38. Assert.assertTrue("{\"id\":123,\"name\":\"sandzhangtoo\"}".equals(text) || "{\"name\":\"sandzhangtoo\",\"id\":123}".equals(text));
  39. }
  40. public void test_name_a1() throws Exception {
  41. SimplePropertyPreFilter filter = new SimplePropertyPreFilter("name");
  42. Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONString(a, filter));
  43. }
  44. public void test_id() throws Exception {
  45. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class, "id");
  46. Assert.assertEquals("{\"id\":123}", JSON.toJSONString(vo, filter));
  47. }
  48. public void test_id_0() throws Exception {
  49. SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id");
  50. Assert.assertEquals("{\"id\":123}", JSON.toJSONString(vo, filter));
  51. }
  52. public void test_map() throws Exception {
  53. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class, "name");
  54. String text = JSON.toJSONString(map, filter);
  55. Assert.assertTrue("{\"id\":123,\"name\":\"sandzhangtoo\"}".equals(text) || "{\"name\":\"sandzhangtoo\",\"id\":123}".equals(text));
  56. }
  57. public void test_map_id() throws Exception {
  58. SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id");
  59. Assert.assertEquals("{\"id\":123}", JSON.toJSONString(map, filter));
  60. }
  61. public void test_map_name() throws Exception {
  62. SimplePropertyPreFilter filter = new SimplePropertyPreFilter("name");
  63. Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONString(map, filter));
  64. }
  65. public void test_all() throws Exception {
  66. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class);
  67. String text = JSON.toJSONString(vo, filter);
  68. Assert.assertTrue("{\"id\":123,\"name\":\"sandzhangtoo\"}".equals(text) || "{\"name\":\"sandzhangtoo\",\"id\":123}".equals(text));
  69. }
  70. public void test_all_map() throws Exception {
  71. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class);
  72. String text = JSON.toJSONString(map, filter);
  73. Assert.assertTrue("{\"id\":123,\"name\":\"sandzhangtoo\"}".equals(text) || "{\"name\":\"sandzhangtoo\",\"id\":123}".equals(text));
  74. }
  75. public void test_exclude_id() throws Exception {
  76. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class);
  77. filter.getExcludes().add("id");
  78. Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONString(vo, filter));
  79. }
  80. public void test_exclude_id_map() throws Exception {
  81. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class);
  82. filter.getExcludes().add("id");
  83. Assert.assertEquals("{\"name\":\"sandzhangtoo\"}", JSON.toJSONString(vo, filter));
  84. }
  85. public void test_exclude_name() throws Exception {
  86. SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class);
  87. filter.getExcludes().add("name");
  88. Assert.assertEquals("{\"id\":123}", JSON.toJSONString(vo, filter));
  89. }
  90. public static class VO {
  91. private int id;
  92. private String name;
  93. public int getId() {
  94. return id;
  95. }
  96. public void setId(int id) {
  97. this.id = id;
  98. }
  99. public String getName() {
  100. return name;
  101. }
  102. public void setName(String name) {
  103. this.name = name;
  104. }
  105. }
  106. public static class A {
  107. private int id;
  108. private String name;
  109. public int getId() {
  110. return id;
  111. }
  112. public void setId(int id) {
  113. this.id = id;
  114. }
  115. public String getName() {
  116. return name;
  117. }
  118. public void setName(String name) {
  119. this.name = name;
  120. }
  121. }
  122. }