Skip to content

Commit

Permalink
[MSHARED-1378] Cleanup of test code
Browse files Browse the repository at this point in the history
- remove public classifier
- use try with resources
  • Loading branch information
slawekjaranowski committed Apr 5, 2024
1 parent eef92e6 commit 43123f4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ public Object evaluateScript(String script, Map<String, ?> globalVariables, Prin
}
}

if (classLoader != null) {
engine.setClassLoader(classLoader);
}
engine.setClassLoader(classLoader);

if (globalVariables != null) {
for (Map.Entry<String, ?> entry : globalVariables.entrySet()) {
Expand Down Expand Up @@ -159,8 +157,6 @@ public Object evaluateScript(String script, Map<String, ?> globalVariables, Prin

@Override
public void close() throws IOException {
if (classLoader != null) {
classLoader.close();
}
classLoader.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void setClassPath(List<String> classPath) {
* default encoding.
*/
public void setScriptEncoding(String encoding) {
this.encoding = (encoding != null && !encoding.isEmpty()) ? encoding : null;
this.encoding = encoding != null && !encoding.isEmpty() ? encoding : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,61 +35,65 @@
*
* @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();
assertEquals(testClassPath, out.toString());
}

@Test
public void testEvaluateScriptWithCustomClassPath() throws Exception {
void testEvaluateScriptWithCustomClassPath() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ScriptInterpreter interpreter = new GroovyScriptInterpreter();
try (ScriptInterpreter interpreter = new GroovyScriptInterpreter()) {

List<String> classPath = Collections.singletonList(new File("src/test-class-path").getAbsolutePath());
interpreter.setClassPath(classPath);
List<String> 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();
assertEquals(testClassPath, out.toString());
}

@Test
public void testEvaluateScriptVars() throws Exception {
void testEvaluateScriptVars() throws Exception {
Map<String, Object> 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());
}
}

0 comments on commit 43123f4

Please sign in to comment.