From 86ea0ac06244eb0d8140efc69f246989c0b350e8 Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Thu, 26 Nov 2015 18:57:12 +0100 Subject: [PATCH] PathFunctions must exist when compiled. 'size' alias for 'length'. --- .../function/PathFunctionFactory.java | 19 ++++++++----------- .../com/jayway/jsonpath/DeepScanTest.java | 6 ++++++ .../function/JSONEntityPathFunctionTest.java | 5 +++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java index 1b716adf..c7e5da70 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java @@ -37,13 +37,13 @@ public class PathFunctionFactory { // JSON Entity Functions map.put("length", Length.class); + map.put("size", Length.class); FUNCTIONS = Collections.unmodifiableMap(map); } /** - * Either provides a pass thru function when the function cannot be properly mapped or otherwise returns the function - * implementation based on the name using the internal FUNCTION map + * Returns the function by name or throws InvalidPathException if function not found. * * @see #FUNCTIONS * @see PathFunction @@ -57,18 +57,15 @@ public class PathFunctionFactory { * @throws InvalidPathException */ public static PathFunction newFunction(String name) throws InvalidPathException { - PathFunction result = new PassthruPathFunction(); - - if (null != name && FUNCTIONS.containsKey(name) && PathFunction.class.isAssignableFrom(FUNCTIONS.get(name))) { + Class functionClazz = FUNCTIONS.get(name); + if(functionClazz == null){ + throw new InvalidPathException("Function with name: " + name + " does not exists."); + } else { try { - result = (PathFunction)FUNCTIONS.get(name).newInstance(); - } catch (InstantiationException e) { - throw new InvalidPathException("Function of name: " + name + " cannot be created", e); - } catch (IllegalAccessException e) { + return (PathFunction)functionClazz.newInstance(); + } catch (Exception e) { throw new InvalidPathException("Function of name: " + name + " cannot be created", e); } } - return result; - } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java b/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java index 59827ac7..e98ee62f 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java @@ -298,4 +298,10 @@ public class DeepScanTest extends BaseTest { assertThat(using(JSON_SMART_CONFIGURATION.addOptions(Option.REQUIRE_PROPERTIES)).parse(animals).read("$..[?(@.mammal == true)].color", List.class)).containsExactly(brown, white); } + @Test + public void scan_with_a_function_filter() { + List result = JsonPath.parse(JSON_DOCUMENT).read("$..*[?(@.length() > 5)]"); + assertThat(result).hasSize(1); + } + } diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java index e1721668..3017cbb6 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java @@ -49,17 +49,22 @@ public class JSONEntityPathFunctionTest extends BaseFunctionTest { " }\n" + "}"; + + + private Configuration conf = Configurations.JSON_SMART_CONFIGURATION; @Test public void testLengthOfTextArray() { // The length of JSONArray is an integer verifyFunction(conf, "$['text'].length()", TEXT_SERIES, 6); + verifyFunction(conf, "$['text'].size()", TEXT_SERIES, 6); } @Test public void testLengthOfNumberArray() { // The length of JSONArray is an integer verifyFunction(conf, "$.numbers.length()", NUMBER_SERIES, 10); + verifyFunction(conf, "$.numbers.size()", NUMBER_SERIES, 10); }