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 c84a6c22..a9f7df5f 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 @@ -1,7 +1,6 @@ package com.jayway.jsonpath.internal.function; import com.jayway.jsonpath.InvalidPathException; -import com.jayway.jsonpath.internal.Path; import com.jayway.jsonpath.internal.function.json.Append; import com.jayway.jsonpath.internal.function.json.KeySetFunction; import com.jayway.jsonpath.internal.function.numeric.Average; @@ -16,7 +15,6 @@ import com.jayway.jsonpath.internal.function.text.Concatenate; import com.jayway.jsonpath.internal.function.text.Length; import java.io.InvalidClassException; -import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -89,6 +87,9 @@ public class PathFunctionFactory { } public static void addCustomFunction(String name,Class function) throws InvalidClassException { + if(FUNCTIONS.containsKey(name)){ + throw new InvalidPathException("Function with name: " + name + " already exists"); + } if (!PathFunction.class.isAssignableFrom(function)){ throw new InvalidClassException("Function with name: " + name + "must be a instance of PathFunction class"); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/CustomFunctionTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/CustomFunctionTest.java index 1d1dd1cc..3c9593ca 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/CustomFunctionTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/CustomFunctionTest.java @@ -2,17 +2,15 @@ package com.jayway.jsonpath.internal.function; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configurations; -import com.jayway.jsonpath.internal.EvaluationContext; -import com.jayway.jsonpath.internal.PathRef; +import com.jayway.jsonpath.InvalidPathException; import org.junit.jupiter.api.Test; import java.io.InvalidClassException; -import java.util.List; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -public class CustomFunctionTest extends BaseFunctionTest { +class CustomFunctionTest extends BaseFunctionTest { private Configuration conf = Configurations.JSON_SMART_CONFIGURATION; @@ -30,4 +28,10 @@ public class CustomFunctionTest extends BaseFunctionTest { assertDoesNotThrow(()->PathFunctionFactory.addCustomFunction("toUpperCase",ToUpperCase.class)); verifyTextFunction(conf,"$['text'][0].toUpperCase()","A"); } + + @Test + void testAddExistentFunction(){ + assertThrows(InvalidPathException.class, () -> PathFunctionFactory.addCustomFunction("avg", + ToUpperCase.class)); + } }