Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1378] Cleanup of test code #117

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}