diff options
author | Jim Hahn <jrh3@att.com> | 2020-01-20 17:39:32 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-01-20 17:48:21 -0500 |
commit | b46e315c8f803e4fb54fc7ba49b94b8052f1c037 (patch) | |
tree | d0284baca6bc31f8830a42a155774a4176912b78 /utils-test | |
parent | c22d70d8f2429747ac7eb991c8d83aa3be633d1b (diff) |
Replace deprecated Nashorn javascript engine
Some test drivers use javascript to evaluate expected results.
In java 8, the standard javascript engine was Nashorn, but that has
been deprecated. As the test scripts use a simple xxx.yyy.zzz notation,
decided to go with a small expression language, Apache JEXL, instead.
Issue-ID: POLICY-2324
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: Iffff468a880c612bdb31da84964dfa4132fa15e3
Diffstat (limited to 'utils-test')
3 files changed, 21 insertions, 15 deletions
diff --git a/utils-test/pom.xml b/utils-test/pom.xml index 028a6f8d..4b6a4c68 100644 --- a/utils-test/pom.xml +++ b/utils-test/pom.xml @@ -37,6 +37,11 @@ <dependencies> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-jexl3</artifactId> + <version>3.0</version> + </dependency> + <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java index f37f32a1..6ae42faf 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-management * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,10 +39,11 @@ import java.util.List; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.script.Bindings; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; +import org.apache.commons.jexl3.JexlBuilder; +import org.apache.commons.jexl3.JexlContext; +import org.apache.commons.jexl3.JexlEngine; +import org.apache.commons.jexl3.JexlException; +import org.apache.commons.jexl3.MapContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,7 +62,7 @@ public class GsonTestUtils { /** * Engine used to interpolate strings before they're compared. */ - private static ScriptEngine engineInstance = null; + private static JexlEngine engineInstance = null; /** * Used to encode and decode an object via gson. @@ -199,9 +200,9 @@ public class GsonTestUtils { } // bind the object to the variable, "obj" - ScriptEngine eng = getEngine(); - Bindings bindings = eng.createBindings(); - bindings.put("obj", object); + JexlEngine eng = getEngine(); + JexlContext context = new MapContext(); + context.set("obj", object); // work our way through the text, interpolating script elements as we go StringBuilder bldr = new StringBuilder(); @@ -222,10 +223,10 @@ public class GsonTestUtils { * Note: must use "eng" instead of "engineInstance" to ensure that we use * the same engine that's associated with the bindings. */ - Object result = eng.eval(script, bindings); + Object result = eng.createExpression(script).evaluate(context); bldr.append(result == null ? "null" : result.toString()); - } catch (ScriptException e) { + } catch (JexlException e) { throw new JsonParseException("cannot expand element: " + mat.group(), e); } } @@ -241,10 +242,10 @@ public class GsonTestUtils { * * @return the script engine */ - private static ScriptEngine getEngine() { + private static JexlEngine getEngine() { if (engineInstance == null) { // race condition here, but it's ok to overwrite with a new engine - engineInstance = new ScriptEngineManager().getEngineByName("javascript"); + engineInstance = new JexlBuilder().create(); } return engineInstance; diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java index 35fea577..11ee63c0 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java @@ -34,7 +34,7 @@ import com.google.gson.JsonParseException; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import javax.script.ScriptException; +import org.apache.commons.jexl3.JexlException; import org.junit.Before; import org.junit.Test; @@ -139,7 +139,7 @@ public class GsonTestUtilsTest { assertThatThrownBy(() -> utils.applyScripts("use ${obj.text} this", null)) .isInstanceOf(JsonParseException.class) - .hasCauseInstanceOf(ScriptException.class) + .hasCauseInstanceOf(JexlException.class) .hasMessage("cannot expand element: ${obj.text}"); } |