Refactor NashornTest
This commit is contained in:
parent
fbb4065024
commit
638b6090f0
@ -4,16 +4,11 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.script.*;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.script.Bindings;
|
|
||||||
import javax.script.Invocable;
|
|
||||||
import javax.script.ScriptEngine;
|
|
||||||
import javax.script.ScriptEngineManager;
|
|
||||||
import javax.script.ScriptException;
|
|
||||||
|
|
||||||
public class NashornTest {
|
public class NashornTest {
|
||||||
|
|
||||||
private ScriptEngine engine;
|
private ScriptEngine engine;
|
||||||
@ -40,9 +35,7 @@ public class NashornTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void magicMethods() throws ScriptException {
|
public void magicMethods() throws ScriptException {
|
||||||
engine.eval("var demo = load('classpath:js/no_such.js');" +
|
engine.eval("var demo = load('classpath:js/no_such.js');" + "var tmp = demo.doesNotExist;" + "var none = demo.callNonExistingMethod()");
|
||||||
"var tmp = demo.doesNotExist;" +
|
|
||||||
"var none = demo.callNonExistingMethod()");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -52,9 +45,7 @@ public class NashornTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void basicUsage() throws ScriptException {
|
public void basicUsage() throws ScriptException {
|
||||||
Object result = engine.eval("var greeting='hello world';" +
|
Object result = engine.eval("var greeting='hello world';" + "print(greeting);" + "greeting");
|
||||||
"print(greeting);" +
|
|
||||||
"greeting");
|
|
||||||
|
|
||||||
Assert.assertEquals("hello world", result);
|
Assert.assertEquals("hello world", result);
|
||||||
}
|
}
|
||||||
@ -65,22 +56,19 @@ public class NashornTest {
|
|||||||
Map<String, Object> map = (Map<String, Object>) obj;
|
Map<String, Object> map = (Map<String, Object>) obj;
|
||||||
|
|
||||||
Assert.assertEquals("hello", map.get("greet"));
|
Assert.assertEquals("hello", map.get("greet"));
|
||||||
Assert.assertTrue(List.class.isAssignableFrom(map.get("primes").getClass()));
|
Assert.assertTrue(List.class.isAssignableFrom(map
|
||||||
|
.get("primes")
|
||||||
|
.getClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tryCatchGuard() throws ScriptException {
|
public void tryCatchGuard() throws ScriptException {
|
||||||
engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" +
|
engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.failFunc();");
|
||||||
"math.failFunc();");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void extensionsExamples() throws ScriptException {
|
public void extensionsExamples() throws ScriptException {
|
||||||
String script = "var list = [1, 2, 3, 4, 5];" + "var result = '';" +
|
String script = "var list = [1, 2, 3, 4, 5];" + "var result = '';" + "for each (var i in list) {" + "result+=i+'-';" + "};" + "print(result);";
|
||||||
"for each (var i in list) {" +
|
|
||||||
"result+=i+'-';" +
|
|
||||||
"};" +
|
|
||||||
"print(result);";
|
|
||||||
engine.eval(script);
|
engine.eval(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,11 +78,7 @@ public class NashornTest {
|
|||||||
bindings.put("count", 3);
|
bindings.put("count", 3);
|
||||||
bindings.put("name", "baeldung");
|
bindings.put("name", "baeldung");
|
||||||
|
|
||||||
String script = "var greeting='Hello ';" +
|
String script = "var greeting='Hello ';" + "for(var i=count;i>0;i--) { " + "greeting+=name + ' '" + "}" + "greeting";
|
||||||
"for(var i=count;i>0;i--) { " +
|
|
||||||
"greeting+=name + ' '" +
|
|
||||||
"}" +
|
|
||||||
"greeting";
|
|
||||||
|
|
||||||
Object bindingsResult = engine.eval(script, bindings);
|
Object bindingsResult = engine.eval(script, bindings);
|
||||||
Assert.assertEquals("Hello baeldung baeldung baeldung ", bindingsResult);
|
Assert.assertEquals("Hello baeldung baeldung baeldung ", bindingsResult);
|
||||||
@ -102,32 +86,25 @@ public class NashornTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void jvmBoundaryExamples() throws ScriptException, NoSuchMethodException {
|
public void jvmBoundaryExamples() throws ScriptException, NoSuchMethodException {
|
||||||
engine.eval("function composeGreeting(name) {" +
|
engine.eval("function composeGreeting(name) {" + "return 'Hello ' + name" + "}");
|
||||||
"return 'Hello ' + name" +
|
|
||||||
"}");
|
|
||||||
|
|
||||||
Invocable invocable = (Invocable) engine;
|
Invocable invocable = (Invocable) engine;
|
||||||
|
|
||||||
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
|
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
|
||||||
Assert.assertEquals("Hello baeldung", funcResult);
|
Assert.assertEquals("Hello baeldung", funcResult);
|
||||||
|
|
||||||
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" +
|
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" + "var map = new HashMap();" + "map.put('hello', 'world');" + "map");
|
||||||
"var map = new HashMap();" +
|
|
||||||
"map.put('hello', 'world');" +
|
|
||||||
"map");
|
|
||||||
|
|
||||||
Assert.assertTrue(Map.class.isAssignableFrom(map.getClass()));
|
Assert.assertTrue(Map.class.isAssignableFrom(map.getClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadExamples() throws ScriptException {
|
public void loadExamples() throws ScriptException {
|
||||||
Object loadResult = engine.eval("load('classpath:js/script.js');" +
|
Object loadResult = engine.eval("load('classpath:js/script.js');" + "increment(5)");
|
||||||
"increment(5)");
|
|
||||||
|
|
||||||
Assert.assertEquals(6.0, loadResult);
|
Assert.assertEquals(6.0, loadResult);
|
||||||
|
|
||||||
Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" +
|
Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.increment(5);");
|
||||||
"math.increment(5);");
|
|
||||||
|
|
||||||
Assert.assertEquals(6.0, math);
|
Assert.assertEquals(6.0, math);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user