|
| 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