diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java index 3699518..5f613f9 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java @@ -128,9 +128,7 @@ public Object evaluateScript(String script, Map globalVariables, Prin } } - if (classLoader != null) { - engine.setClassLoader(classLoader); - } + engine.setClassLoader(classLoader); if (globalVariables != null) { for (Map.Entry entry : globalVariables.entrySet()) { @@ -159,8 +157,6 @@ public Object evaluateScript(String script, Map globalVariables, Prin @Override public void close() throws IOException { - if (classLoader != null) { - classLoader.close(); - } + classLoader.close(); } } diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java index 2bc737f..1b1773d 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java @@ -111,7 +111,7 @@ public void setClassPath(List classPath) { * default encoding. */ public void setScriptEncoding(String encoding) { - this.encoding = (encoding != null && !encoding.isEmpty()) ? encoding : null; + this.encoding = encoding != null && !encoding.isEmpty() ? encoding : null; } /** diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java index 35f64a5..b00a4cb 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java @@ -32,26 +32,28 @@ * * @author Benjamin Bentmann */ -public class BeanShellScriptInterpreterTest { +class BeanShellScriptInterpreterTest { @Test - public void testEvaluateScript() throws Exception { + void testEvaluateScript() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - ScriptInterpreter interpreter = new BeanShellScriptInterpreter(); - assertEquals( - Boolean.TRUE, - interpreter.evaluateScript("System.out.print(\"Test\"); return true;", null, new PrintStream(out))); + try (ScriptInterpreter interpreter = new BeanShellScriptInterpreter()) { + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript("System.out.print(\"Test\"); return true;", null, new PrintStream(out))); + } assertEquals("Test", out.toString()); } @Test - public void testEvaluateScriptVars() throws Exception { + void testEvaluateScriptVars() throws Exception { Map vars = new HashMap<>(); vars.put("testVar", "data"); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ScriptInterpreter interpreter = new BeanShellScriptInterpreter(); - assertEquals( - Boolean.TRUE, - interpreter.evaluateScript("System.out.print(testVar); return true;", vars, new PrintStream(out))); + try (ScriptInterpreter interpreter = new BeanShellScriptInterpreter()) { + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript("System.out.print(testVar); return true;", vars, new PrintStream(out))); + } assertEquals("data", out.toString()); } } diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java index cdc6158..0e97e2d 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java @@ -35,27 +35,29 @@ * * @author Benjamin Bentmann */ -public class GroovyScriptInterpreterTest { +class GroovyScriptInterpreterTest { @Test - public void testEvaluateScript() throws Exception { + void testEvaluateScript() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - assertEquals( - Boolean.TRUE, interpreter.evaluateScript("print \"Test\"\nreturn true", null, new PrintStream(out))); + try (ScriptInterpreter interpreter = new GroovyScriptInterpreter()) { + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript("print \"Test\"\nreturn true", null, new PrintStream(out))); + } assertEquals("Test", out.toString()); } @Test - public void testEvaluateScriptWithDefaultClassPath() throws Exception { + void testEvaluateScriptWithDefaultClassPath() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - - assertEquals( - Boolean.TRUE, - interpreter.evaluateScript( - "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", - null, - new PrintStream(out))); + try (ScriptInterpreter interpreter = new GroovyScriptInterpreter()) { + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript( + "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", + null, + new PrintStream(out))); + } String testClassPath = new File("target/test-classes/class-path.txt").toURI().getPath(); @@ -63,19 +65,20 @@ public void testEvaluateScriptWithDefaultClassPath() throws Exception { } @Test - public void testEvaluateScriptWithCustomClassPath() throws Exception { + void testEvaluateScriptWithCustomClassPath() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - ScriptInterpreter interpreter = new GroovyScriptInterpreter(); + try (ScriptInterpreter interpreter = new GroovyScriptInterpreter()) { - List classPath = Collections.singletonList(new File("src/test-class-path").getAbsolutePath()); - interpreter.setClassPath(classPath); + List classPath = Collections.singletonList(new File("src/test-class-path").getAbsolutePath()); + interpreter.setClassPath(classPath); - assertEquals( - Boolean.TRUE, - interpreter.evaluateScript( - "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", - null, - new PrintStream(out))); + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript( + "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", + null, + new PrintStream(out))); + } String testClassPath = new File("src/test-class-path/class-path.txt").toURI().getPath(); @@ -83,13 +86,14 @@ public void testEvaluateScriptWithCustomClassPath() throws Exception { } @Test - public void testEvaluateScriptVars() throws Exception { + void testEvaluateScriptVars() throws Exception { Map vars = new HashMap<>(); vars.put("testVar", "data"); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - assertEquals( - Boolean.TRUE, interpreter.evaluateScript("print testVar\nreturn true", vars, new PrintStream(out))); + try (ScriptInterpreter interpreter = new GroovyScriptInterpreter()) { + assertEquals( + Boolean.TRUE, interpreter.evaluateScript("print testVar\nreturn true", vars, new PrintStream(out))); + } assertEquals("data", out.toString()); } }