/
TestEvalWithScriptContext.java
75 lines (57 loc) · 3.27 KB
/
TestEvalWithScriptContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package delight.nashornsandbox;
import java.util.concurrent.Executors;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import org.junit.Assert;
import org.junit.Test;
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
@SuppressWarnings("all")
public class TestEvalWithScriptContext {
@Test
public void test() throws ScriptCPUAbuseException, ScriptException {
final NashornSandbox sandbox = NashornSandboxes.create();
SandboxScriptContext newContext1 = sandbox.createScriptContext();
Bindings engineScope1 = newContext1.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
engineScope1.put("y", 2);
SandboxScriptContext newContext2 = sandbox.createScriptContext();
Bindings engineScope2 = newContext2.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
engineScope2.put("y", 4);
final Object res1 = sandbox.eval("function cal() {var x = y + 1; return x;} cal();", newContext1);
Assert.assertTrue(res1.equals(Double.valueOf("3.0")) || res1.equals(new Integer(3)));
final Object res2 = sandbox.eval("function cal() {var x = y + 1; return x;} cal();", newContext2);
Assert.assertTrue(res2.equals(Double.valueOf("5.0")) || res2.equals(new Integer(5)));
}
@Test
public void test_compiled() throws ScriptCPUAbuseException, ScriptException {
final NashornSandbox sandbox = NashornSandboxes.create();
ScriptContext newContext1 = new SimpleScriptContext();
Bindings engineScope1 = newContext1.getBindings(ScriptContext.ENGINE_SCOPE);
engineScope1.put("y", 2);
ScriptContext newContext2 = new SimpleScriptContext();
Bindings engineScope2 = newContext2.getBindings(ScriptContext.ENGINE_SCOPE);
engineScope2.put("y", 4);
final Object res1 = sandbox.eval(sandbox.compile("function cal() {var x = y + 1; return x;} cal();"), newContext1);
Assert.assertTrue(res1.equals(Double.valueOf("3.0")) || res1.equals(new Integer(3)));
final Object res2 = sandbox.eval(sandbox.compile("function cal() {var x = y + 1; return x;} cal();"), newContext2);
Assert.assertTrue(res2.equals(Double.valueOf("5.0")) || res2.equals(new Integer(5)));
}
@Test
public void testWithCPUAndMemory() throws ScriptCPUAbuseException, ScriptException {
final NashornSandbox sandbox = NashornSandboxes.create();
sandbox.setMaxCPUTime(100);
sandbox.setMaxMemory(1000 * 1024);
sandbox.setExecutor(Executors.newSingleThreadExecutor());
SandboxScriptContext newContext1 = sandbox.createScriptContext();
Bindings engineScope1 = newContext1.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
engineScope1.put("y", 2);
SandboxScriptContext newContext2 = sandbox.createScriptContext();
Bindings engineScope2 = newContext2.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
engineScope2.put("y", 4);
final Object res1 = sandbox.eval("function cal() {var x = y + 1; return x;} cal();", newContext1);
Assert.assertTrue(res1.equals(Double.valueOf("3.0")) || res1.equals(new Integer(3)));
final Object res2 = sandbox.eval("function cal() {var x = y + 1; return x;} cal();", newContext2);
Assert.assertTrue(res2.equals(Double.valueOf("5.0")) || res2.equals(new Integer(5)));
}
}