/src/test/java/com/alibaba/json/bvt/path/JSONPath_field_access_filter_in_int.java

https://github.com/alibaba/fastjson · Java · 151 lines · 117 code · 34 blank · 0 comment · 0 complexity · 889f758de80bb99b4a2a93e513352fbd MD5 · raw file

  1. package com.alibaba.json.bvt.path;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import junit.framework.TestCase;
  5. import org.junit.Assert;
  6. import com.alibaba.fastjson.JSONPath;
  7. public class JSONPath_field_access_filter_in_int extends TestCase {
  8. public void test_list_in() throws Exception {
  9. JSONPath path = new JSONPath("[id in (1001)]");
  10. List<Entity> entities = new ArrayList<Entity>();
  11. entities.add(new Entity(1001, "ljw2083"));
  12. entities.add(new Entity(1002, "wenshao"));
  13. entities.add(new Entity(1003, "yakolee"));
  14. entities.add(new Entity(1004, null));
  15. List<Object> result = (List<Object>) path.eval(entities);
  16. Assert.assertEquals(1, result.size());
  17. Assert.assertSame(entities.get(0), result.get(0));
  18. }
  19. public void test_list_not_in() throws Exception {
  20. JSONPath path = new JSONPath("[id not in (1001)]");
  21. List<Entity> entities = new ArrayList<Entity>();
  22. entities.add(new Entity(1001, "ljw2083"));
  23. entities.add(new Entity(1002, "wenshao"));
  24. entities.add(new Entity(1003, "yakolee"));
  25. entities.add(new Entity(1004, null));
  26. List<Object> result = (List<Object>) path.eval(entities);
  27. Assert.assertEquals(3, result.size());
  28. Assert.assertSame(entities.get(1), result.get(0));
  29. Assert.assertSame(entities.get(2), result.get(1));
  30. Assert.assertSame(entities.get(3), result.get(2));
  31. }
  32. public void test_list_nin() throws Exception {
  33. JSONPath path = new JSONPath("[id nin (1001)]");
  34. List<Entity> entities = new ArrayList<Entity>();
  35. entities.add(new Entity(1001, "ljw2083"));
  36. entities.add(new Entity(1002, "wenshao"));
  37. entities.add(new Entity(1003, "yakolee"));
  38. entities.add(new Entity(1004, null));
  39. List<Object> result = (List<Object>) path.eval(entities);
  40. Assert.assertEquals(3, result.size());
  41. Assert.assertSame(entities.get(1), result.get(0));
  42. Assert.assertSame(entities.get(2), result.get(1));
  43. Assert.assertSame(entities.get(3), result.get(2));
  44. }
  45. public void test_list_not_in_null() throws Exception {
  46. JSONPath path = new JSONPath("[id not in (null)]");
  47. List<Entity> entities = new ArrayList<Entity>();
  48. entities.add(new Entity(1001, "ljw2083"));
  49. entities.add(new Entity(1002, "wenshao"));
  50. entities.add(new Entity(1003, "yakolee"));
  51. entities.add(new Entity(1004, null));
  52. List<Object> result = (List<Object>) path.eval(entities);
  53. Assert.assertEquals(4, result.size());
  54. Assert.assertSame(entities.get(0), result.get(0));
  55. Assert.assertSame(entities.get(1), result.get(1));
  56. Assert.assertSame(entities.get(2), result.get(2));
  57. Assert.assertSame(entities.get(3), result.get(3));
  58. }
  59. public void test_list_in_2() throws Exception {
  60. JSONPath path = new JSONPath("[id in (1001, 1003)]");
  61. List<Entity> entities = new ArrayList<Entity>();
  62. entities.add(new Entity(1001, "ljw2083"));
  63. entities.add(new Entity(1002, "wenshao"));
  64. entities.add(new Entity(1003, "yakolee"));
  65. entities.add(new Entity(1004, null));
  66. List<Object> result = (List<Object>) path.eval(entities);
  67. Assert.assertEquals(2, result.size());
  68. Assert.assertSame(entities.get(0), result.get(0));
  69. Assert.assertSame(entities.get(2), result.get(1));
  70. }
  71. public void test_list_in_3() throws Exception {
  72. JSONPath path = new JSONPath("[id in (1001, 1003, 1004)]");
  73. List<Entity> entities = new ArrayList<Entity>();
  74. entities.add(new Entity(1001, "ljw2083"));
  75. entities.add(new Entity(1002, "wenshao"));
  76. entities.add(new Entity(1003, "yakolee"));
  77. entities.add(new Entity(1004, null));
  78. List<Object> result = (List<Object>) path.eval(entities);
  79. Assert.assertEquals(3, result.size());
  80. Assert.assertSame(entities.get(0), result.get(0));
  81. Assert.assertSame(entities.get(2), result.get(1));
  82. Assert.assertSame(entities.get(3), result.get(2));
  83. }
  84. public void test_list_in_3_null() throws Exception {
  85. JSONPath path = new JSONPath("[id in (1001, 1003, null)]");
  86. List<Entity> entities = new ArrayList<Entity>();
  87. entities.add(new Entity(1001, "ljw2083"));
  88. entities.add(new Entity(1002, "wenshao"));
  89. entities.add(new Entity(1003, "yakolee"));
  90. entities.add(new Entity(null, null));
  91. List<Object> result = (List<Object>) path.eval(entities);
  92. Assert.assertEquals(3, result.size());
  93. Assert.assertSame(entities.get(0), result.get(0));
  94. Assert.assertSame(entities.get(2), result.get(1));
  95. Assert.assertSame(entities.get(3), result.get(2));
  96. }
  97. public static class Entity {
  98. private Integer id;
  99. private String name;
  100. public Entity(Integer id, String name){
  101. this.id = id;
  102. this.name = name;
  103. }
  104. public Integer getId() {
  105. return id;
  106. }
  107. public void setId(Integer id) {
  108. this.id = id;
  109. }
  110. public String getName() {
  111. return name;
  112. }
  113. public void setName(String name) {
  114. this.name = name;
  115. }
  116. }
  117. }