From ecad2476a1c0aaff3cf9ea0da0fab0b1fba80ee3 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Fri, 5 Apr 2024 19:39:34 +0200 Subject: [PATCH] [MSHARED-1378] Cleanup of test code - remove public classifier - use try with resources --- .../BeanShellScriptInterpreter.java | 8 +-- .../scriptinterpreter/ScriptRunner.java | 2 +- .../BeanShellScriptInterpreterTest.java | 24 ++++---- .../GroovyScriptInterpreterTest.java | 60 ++++++++++--------- 4 files changed, 48 insertions(+), 46 deletions(-) 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()); } }