From: Jim Hahn Date: Fri, 15 Feb 2019 14:50:25 +0000 (-0500) Subject: Fix sonar issues with gson code X-Git-Tag: 1.4.0~48 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=8b32784d749d4bcacfa4e8f684501559f999be07;p=policy%2Fcommon.git Fix sonar issues with gson code Fixed issues with GsonTestUtils: - don't set static from within a non-static method - don't throw generic RuntimeException Resolved checkstyle issue. Removed trailing spaces. Change-Id: If0c85733f62a24b56088dc90aadd4b95b3a6c91a Issue-ID: POLICY-1428 Signed-off-by: Jim Hahn --- 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 bfdca97c..d3354e49 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 @@ -61,7 +61,7 @@ public class GsonTestUtils { /** * Engine used to interpolate strings before they're compared. */ - private static volatile ScriptEngine engine = null; + private static volatile ScriptEngine engineInstance = null; /** * Used to encode and decode an object via gson. @@ -198,13 +198,9 @@ public class GsonTestUtils { return text; } - // create the engine and bind the object to the variable, "obj" - if (engine == null) { - // race condition here, but it's ok to overwrite with a new engine - engine = new ScriptEngineManager().getEngineByName("javascript"); - } - - Bindings bindings = engine.createBindings(); + // bind the object to the variable, "obj" + ScriptEngine eng = getEngine(); + Bindings bindings = eng.createBindings(); bindings.put("obj", object); // work our way through the text, interpolating script elements as we go @@ -222,11 +218,15 @@ public class GsonTestUtils { // interpolate the script String script = mat.group(1); try { - Object result = engine.eval(script, bindings); + /* + * 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); bldr.append(result == null ? "null" : result.toString()); } catch (ScriptException e) { - throw new RuntimeException("cannot expand element: " + mat.group(), e); + throw new JsonParseException("cannot expand element: " + mat.group(), e); } } @@ -236,6 +236,20 @@ public class GsonTestUtils { return bldr.toString(); } + /** + * Gets the script engine instance. + * + * @return the script engine + */ + private static ScriptEngine getEngine() { + if (engineInstance == null) { + // race condition here, but it's ok to overwrite with a new engine + engineInstance = new ScriptEngineManager().getEngineByName("javascript"); + } + + return engineInstance; + } + /** * Encodes an object using gson. * 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 39cde7dc..58beb2a6 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 @@ -135,8 +135,10 @@ public class GsonTestUtilsTest { result = utils.applyScripts("use ${obj.text} this", data); assertEquals("use null this", result); - assertThatThrownBy(() -> utils.applyScripts("use ${obj.text} this", null)).isInstanceOf(RuntimeException.class) - .hasCauseInstanceOf(ScriptException.class).hasMessage("cannot expand element: ${obj.text}"); + assertThatThrownBy(() -> utils.applyScripts("use ${obj.text} this", null)) + .isInstanceOf(JsonParseException.class) + .hasCauseInstanceOf(ScriptException.class) + .hasMessage("cannot expand element: ${obj.text}"); } @Test