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

https://github.com/alibaba/fastjson · Java · 105 lines · 82 code · 23 blank · 0 comment · 0 complexity · 045f19662fa56197deac6d9b47275f8f MD5 · raw file

  1. package com.alibaba.json.bvt.path;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.JSONPath;
  5. import com.alibaba.fastjson.parser.Feature;
  6. import com.alibaba.fastjson.util.IOUtils;
  7. import junit.framework.TestCase;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. public class BookEvalTest extends TestCase {
  11. private JSONObject root;
  12. protected void setUp() throws Exception {
  13. InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("json/book.json");
  14. InputStreamReader reader = new InputStreamReader(is);
  15. String json = IOUtils.readAll(reader);
  16. IOUtils.close(reader);
  17. root = (JSONObject) JSON.parse(json, Feature.OrderedField);
  18. }
  19. public void test_0() throws Exception {
  20. assertEquals(4, JSONPath.eval(root, "$..book.length()"));
  21. }
  22. public void test_1() throws Exception {
  23. assertEquals("[\"reference\",\"Nigel Rees\",\"Sayings of the Century\",8.95,\"fiction\",\"Evelyn Waugh\",\"Sword of Honour\",12.99,\"fiction\",\"Herman Melville\",\"Moby Dick\",\"0-553-21311-3\",8.99,\"fiction\",\"J. R. R. Tolkien\",\"The Lord of the Rings\",\"0-395-19395-8\",22.99,\"red\",19.95,10]"
  24. , JSON.toJSONString(JSONPath.eval(root, "$..*")));
  25. }
  26. public void test_2() throws Exception {
  27. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", JSON.toJSONString(JSONPath.eval(root, "$.store.book[*].author")));
  28. }
  29. public void test_3() throws Exception {
  30. assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", JSON.toJSONString(JSONPath.eval(root, "$..author")));
  31. }
  32. public void test_4() throws Exception {
  33. assertEquals("[8.95,12.99,8.99,22.99,19.95]", JSON.toJSONString(JSONPath.eval(root, "$..price")));
  34. }
  35. public void test_5() throws Exception {
  36. assertEquals("[8.95,12.99,8.99,22.99]", JSON.toJSONString(JSONPath.eval(root, "$..book.price")));
  37. }
  38. public void test_6() throws Exception {
  39. assertEquals("[[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],{\"color\":\"red\",\"price\":19.95}]"
  40. , JSON.toJSONString(JSONPath.eval(root, "$.store.*")));
  41. }
  42. public void test_7() throws Exception {
  43. assertEquals("[8.95,12.99,8.99,22.99,19.95]"
  44. , JSON.toJSONString(JSONPath.eval(root, "$.store..price")));
  45. }
  46. public void test_8() throws Exception {
  47. assertEquals("{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}"
  48. , JSON.toJSONString(JSONPath.eval(root, "$..book[2]")));
  49. }
  50. public void test_9() throws Exception {
  51. assertEquals("{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}"
  52. , JSON.toJSONString(JSONPath.eval(root, "$..book[-1]")));
  53. }
  54. public void test_10() throws Exception {
  55. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}]"
  56. , JSON.toJSONString(JSONPath.eval(root, "$..book[0,1]")));
  57. }
  58. public void test_11() throws Exception {
  59. assertEquals("[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}]"
  60. , JSON.toJSONString(JSONPath.eval(root, "$..book[?(@.isbn)]")));
  61. }
  62. public void test_12() throws Exception {
  63. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}]"
  64. , JSON.toJSONString(JSONPath.eval(root, "$.store.book[?(@.price < 10)]")));
  65. }
  66. public void test_13() throws Exception {
  67. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}]"
  68. , JSON.toJSONString(JSONPath.eval(root, "$..book[?(@.price <= $['expensive'])]")));
  69. }
  70. public void test_14() throws Exception {
  71. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}]"
  72. , JSON.toJSONString(JSONPath.eval(root, "$..book[?(@.author =~ /.*REES/i)]")));
  73. }
  74. public void test_15() throws Exception {
  75. assertEquals("[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}]"
  76. , JSON.toJSONString(JSONPath.eval(root, "$..book[?(@.author =~ /.*REES/i)]")));
  77. }
  78. public void test_16() throws Exception {
  79. assertEquals("[{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}]"
  80. , JSON.toJSONString(JSONPath.eval(root, "$.store.book[?(@.price < 10 && @.category == 'fiction')]")));
  81. }
  82. }