Skip to content

Commit fbb4065

Browse files
dkornishevzhendrikse
authored andcommitted
Nashorn second Attempt (eugenp#923)
* Nashorn * Nashorn x2 * Nashorn added trailing newlines * Cleanup * Formatted script lines * Change system outs to asserts * Change Nashorn to be Junit tests * Remove empty test
1 parent 8f4ef41 commit fbb4065

9 files changed

Lines changed: 192 additions & 48 deletions

File tree

‎core-java/src/main/java/com/baeldung/scripting/Nashorn.java‎

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var first = {
2+
name: "Whiskey",
3+
age: 5
4+
};
5+
6+
var second = {
7+
volume: 100
8+
};
9+
10+
Object.bindProperties(first, second);
11+
12+
print(first.volume);
13+
14+
second.volume = 1000;
15+
print(first.volume);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(__FILE__, __LINE__, __DIR__);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var math = {
2+
increment: function (num) {
3+
return ++num;
4+
},
5+
6+
failFunc: function () {
7+
try {
8+
throw "BOOM";
9+
} catch (e if typeof e === 'string') {
10+
print("String thrown: " + e);
11+
}
12+
catch (e) {
13+
print("this shouldn't happen!");
14+
}
15+
}
16+
};
17+
18+
19+
math;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var demo = {
2+
__noSuchProperty__: function (propName) {
3+
print("Accessed non-existing property: " + propName);
4+
},
5+
6+
__noSuchMethod__: function (methodName) {
7+
print("Invoked non-existing method: " + methodName);
8+
}
9+
};
10+
11+
demo;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function increment(num) ++num;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(" hello world".trimLeft());
2+
print("hello world ".trimRight());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function arrays(arr) {
2+
3+
var javaIntArray = Java.to(arr, "int[]");
4+
print(javaIntArray[0]);
5+
print(javaIntArray[1]);
6+
print(javaIntArray[2]);
7+
}
8+
9+
arrays([100, "1654", true]);
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.baeldung.scripting;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.InputStreamReader;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import javax.script.Bindings;
12+
import javax.script.Invocable;
13+
import javax.script.ScriptEngine;
14+
import javax.script.ScriptEngineManager;
15+
import javax.script.ScriptException;
16+
17+
public class NashornTest {
18+
19+
private ScriptEngine engine;
20+
21+
@Before
22+
public void setUp() {
23+
engine = new ScriptEngineManager().getEngineByName("nashorn");
24+
}
25+
26+
@Test
27+
public void trim() throws ScriptException {
28+
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/trim.js")));
29+
}
30+
31+
@Test
32+
public void locations() throws ScriptException {
33+
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/locations.js")));
34+
}
35+
36+
@Test
37+
public void bindProperties() throws ScriptException {
38+
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/bind.js")));
39+
}
40+
41+
@Test
42+
public void magicMethods() throws ScriptException {
43+
engine.eval("var demo = load('classpath:js/no_such.js');" +
44+
"var tmp = demo.doesNotExist;" +
45+
"var none = demo.callNonExistingMethod()");
46+
}
47+
48+
@Test
49+
public void typedArrays() throws ScriptException {
50+
engine.eval(new InputStreamReader(NashornTest.class.getResourceAsStream("/js/typed_arrays.js")));
51+
}
52+
53+
@Test
54+
public void basicUsage() throws ScriptException {
55+
Object result = engine.eval("var greeting='hello world';" +
56+
"print(greeting);" +
57+
"greeting");
58+
59+
Assert.assertEquals("hello world", result);
60+
}
61+
62+
@Test
63+
public void jsonObjectExample() throws ScriptException {
64+
Object obj = engine.eval("Java.asJSONCompatible({ number: 42, greet: 'hello', primes: [2,3,5,7,11,13] })");
65+
Map<String, Object> map = (Map<String, Object>) obj;
66+
67+
Assert.assertEquals("hello", map.get("greet"));
68+
Assert.assertTrue(List.class.isAssignableFrom(map.get("primes").getClass()));
69+
}
70+
71+
@Test
72+
public void tryCatchGuard() throws ScriptException {
73+
engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" +
74+
"math.failFunc();");
75+
}
76+
77+
@Test
78+
public void extensionsExamples() throws ScriptException {
79+
String script = "var list = [1, 2, 3, 4, 5];" + "var result = '';" +
80+
"for each (var i in list) {" +
81+
"result+=i+'-';" +
82+
"};" +
83+
"print(result);";
84+
engine.eval(script);
85+
}
86+
87+
@Test
88+
public void bindingsExamples() throws ScriptException {
89+
Bindings bindings = engine.createBindings();
90+
bindings.put("count", 3);
91+
bindings.put("name", "baeldung");
92+
93+
String script = "var greeting='Hello ';" +
94+
"for(var i=count;i>0;i--) { " +
95+
"greeting+=name + ' '" +
96+
"}" +
97+
"greeting";
98+
99+
Object bindingsResult = engine.eval(script, bindings);
100+
Assert.assertEquals("Hello baeldung baeldung baeldung ", bindingsResult);
101+
}
102+
103+
@Test
104+
public void jvmBoundaryExamples() throws ScriptException, NoSuchMethodException {
105+
engine.eval("function composeGreeting(name) {" +
106+
"return 'Hello ' + name" +
107+
"}");
108+
109+
Invocable invocable = (Invocable) engine;
110+
111+
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
112+
Assert.assertEquals("Hello baeldung", funcResult);
113+
114+
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" +
115+
"var map = new HashMap();" +
116+
"map.put('hello', 'world');" +
117+
"map");
118+
119+
Assert.assertTrue(Map.class.isAssignableFrom(map.getClass()));
120+
}
121+
122+
@Test
123+
public void loadExamples() throws ScriptException {
124+
Object loadResult = engine.eval("load('classpath:js/script.js');" +
125+
"increment(5)");
126+
127+
Assert.assertEquals(6.0, loadResult);
128+
129+
Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" +
130+
"math.increment(5);");
131+
132+
Assert.assertEquals(6.0, math);
133+
}
134+
}

0 commit comments

Comments
 (0)