author | sundar |
Tue, 27 Aug 2013 15:54:45 +0530 | |
changeset 19636 | ce19ba858512 |
parent 19624 | 976b3bfbd657 |
child 20210 | 80d652da38ac |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.api.scripting; |
|
27 |
||
28 |
import static org.testng.Assert.assertEquals; |
|
29 |
import static org.testng.Assert.assertNotNull; |
|
30 |
import static org.testng.Assert.assertTrue; |
|
31 |
import static org.testng.Assert.fail; |
|
32 |
||
19636 | 33 |
import java.io.PrintWriter; |
16147 | 34 |
import java.io.StringReader; |
35 |
import java.io.StringWriter; |
|
36 |
import java.lang.reflect.Method; |
|
37 |
import java.util.concurrent.Callable; |
|
38 |
import javax.script.Compilable; |
|
39 |
import javax.script.CompiledScript; |
|
40 |
import javax.script.ScriptEngine; |
|
41 |
import javax.script.ScriptEngineFactory; |
|
42 |
import javax.script.ScriptEngineManager; |
|
43 |
import javax.script.ScriptException; |
|
44 |
import org.testng.annotations.Test; |
|
45 |
||
46 |
/** |
|
47 |
* Tests for JSR-223 script engine for Nashorn. |
|
16260
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
48 |
* |
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
49 |
* @test |
17248
60ef2a001ec2
8013295: ScriptEngineTest.java fails with compilation error when running under jtreg
sundar
parents:
17239
diff
changeset
|
50 |
* @build jdk.nashorn.api.scripting.Window jdk.nashorn.api.scripting.WindowEventHandler jdk.nashorn.api.scripting.VariableArityTestInterface jdk.nashorn.api.scripting.ScriptEngineTest |
16260
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
51 |
* @run testng jdk.nashorn.api.scripting.ScriptEngineTest |
16147 | 52 |
*/ |
53 |
public class ScriptEngineTest { |
|
54 |
||
55 |
private void log(String msg) { |
|
56 |
org.testng.Reporter.log(msg, true); |
|
57 |
} |
|
58 |
||
59 |
@Test |
|
60 |
public void argumentsTest() { |
|
61 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
62 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
63 |
||
64 |
String[] args = new String[] { "hello", "world" }; |
|
65 |
try { |
|
66 |
e.put("arguments", args); |
|
67 |
Object arg0 = e.eval("arguments[0]"); |
|
68 |
Object arg1 = e.eval("arguments[1]"); |
|
69 |
assertEquals(args[0], arg0); |
|
70 |
assertEquals(args[1], arg1); |
|
71 |
} catch (final Exception exp) { |
|
72 |
exp.printStackTrace(); |
|
73 |
fail(exp.getMessage()); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
@Test |
|
78 |
public void argumentsWithTest() { |
|
79 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
80 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
81 |
||
82 |
String[] args = new String[] { "hello", "world" }; |
|
83 |
try { |
|
84 |
e.put("arguments", args); |
|
85 |
Object arg0 = e.eval("var imports = new JavaImporter(java.io); " + |
|
86 |
" with(imports) { arguments[0] }"); |
|
87 |
Object arg1 = e.eval("var imports = new JavaImporter(java.util, java.io); " + |
|
88 |
" with(imports) { arguments[1] }"); |
|
89 |
assertEquals(args[0], arg0); |
|
90 |
assertEquals(args[1], arg1); |
|
91 |
} catch (final Exception exp) { |
|
92 |
exp.printStackTrace(); |
|
93 |
fail(exp.getMessage()); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
@Test |
|
98 |
public void argumentsEmptyTest() { |
|
99 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
100 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
101 |
||
102 |
try { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
103 |
assertEquals(e.eval("arguments instanceof Array"), true); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
104 |
assertEquals(e.eval("arguments.length == 0"), true); |
16147 | 105 |
} catch (final Exception exp) { |
106 |
exp.printStackTrace(); |
|
107 |
fail(exp.getMessage()); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
@Test |
|
112 |
public void factoryTests() { |
|
113 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
114 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
115 |
assertNotNull(e); |
|
116 |
||
117 |
final ScriptEngineFactory fac = e.getFactory(); |
|
118 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
119 |
assertEquals(fac.getLanguageName(), "ECMAScript"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
120 |
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
121 |
assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
122 |
assertEquals(fac.getEngineName(), "Oracle Nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
123 |
assertEquals(fac.getOutputStatement("context"), "print(context)"); |
16241
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
124 |
assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
125 |
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); |
16147 | 126 |
|
127 |
boolean seenJS = false; |
|
128 |
for (String ext : fac.getExtensions()) { |
|
129 |
if (ext.equals("js")) { |
|
130 |
seenJS = true; |
|
131 |
} |
|
132 |
} |
|
133 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
134 |
assertEquals(seenJS, true); |
16147 | 135 |
String str = fac.getMethodCallSyntax("obj", "foo", "x"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
136 |
assertEquals(str, "obj.foo(x)"); |
16147 | 137 |
|
138 |
boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false; |
|
139 |
for (String name : fac.getNames()) { |
|
140 |
switch (name) { |
|
141 |
case "nashorn": seenNashorn = true; break; |
|
142 |
case "javascript": seenJavaScript = true; break; |
|
143 |
case "ECMAScript": seenECMAScript = true; break; |
|
144 |
} |
|
145 |
} |
|
146 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
147 |
assertTrue(seenNashorn); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
148 |
assertTrue(seenJavaScript); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
149 |
assertTrue(seenECMAScript); |
16147 | 150 |
|
151 |
boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false; |
|
152 |
for (String mime : fac.getMimeTypes()) { |
|
153 |
switch (mime) { |
|
154 |
case "application/javascript": seenAppJS = true; break; |
|
155 |
case "application/ecmascript": seenAppECMA = true; break; |
|
156 |
case "text/javascript": seenTextJS = true; break; |
|
157 |
case "text/ecmascript": seenTextECMA = true; break; |
|
158 |
} |
|
159 |
} |
|
160 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
161 |
assertTrue(seenAppJS); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
162 |
assertTrue(seenAppECMA); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
163 |
assertTrue(seenTextJS); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
164 |
assertTrue(seenTextECMA); |
16147 | 165 |
} |
166 |
||
167 |
@Test |
|
168 |
public void evalTests() { |
|
169 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
170 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
171 |
e.put(ScriptEngine.FILENAME, "myfile.js"); |
|
172 |
||
173 |
try { |
|
174 |
e.eval("print('hello')"); |
|
175 |
} catch (final ScriptException se) { |
|
176 |
fail(se.getMessage()); |
|
177 |
} |
|
178 |
try { |
|
179 |
e.eval("print('hello)"); |
|
180 |
fail("script exception expected"); |
|
181 |
} catch (final ScriptException se) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
182 |
assertEquals(se.getLineNumber(), 1); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
183 |
assertEquals(se.getColumnNumber(), 13); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
184 |
assertEquals(se.getFileName(), "myfile.js"); |
16147 | 185 |
// se.printStackTrace(); |
186 |
} |
|
187 |
||
188 |
try { |
|
189 |
Object obj = e.eval("34 + 41"); |
|
190 |
assertTrue(34.0 + 41.0 == ((Number)obj).doubleValue()); |
|
191 |
obj = e.eval("x = 5"); |
|
192 |
assertTrue(5.0 == ((Number)obj).doubleValue()); |
|
193 |
} catch (final ScriptException se) { |
|
194 |
se.printStackTrace(); |
|
195 |
fail(se.getMessage()); |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
@Test |
|
200 |
public void compileTests() { |
|
201 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
202 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
203 |
CompiledScript script = null; |
|
204 |
||
205 |
try { |
|
206 |
script = ((Compilable)e).compile("print('hello')"); |
|
207 |
} catch (final ScriptException se) { |
|
208 |
fail(se.getMessage()); |
|
209 |
} |
|
210 |
||
211 |
try { |
|
212 |
script.eval(); |
|
213 |
} catch (final ScriptException | NullPointerException se) { |
|
214 |
se.printStackTrace(); |
|
215 |
fail(se.getMessage()); |
|
216 |
} |
|
217 |
||
218 |
// try to compile from a Reader |
|
219 |
try { |
|
220 |
script = ((Compilable)e).compile(new StringReader("print('world')")); |
|
221 |
} catch (final ScriptException se) { |
|
222 |
fail(se.getMessage()); |
|
223 |
} |
|
224 |
||
225 |
try { |
|
226 |
script.eval(); |
|
227 |
} catch (final ScriptException | NullPointerException se) { |
|
228 |
se.printStackTrace(); |
|
229 |
fail(se.getMessage()); |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
@Test |
|
234 |
public void accessGlobalTest() { |
|
235 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
236 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
237 |
||
238 |
try { |
|
239 |
e.eval("var x = 'hello'"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
240 |
assertEquals(e.get("x"), "hello"); |
16147 | 241 |
} catch (final ScriptException exp) { |
242 |
exp.printStackTrace(); |
|
243 |
fail(exp.getMessage()); |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
@Test |
|
248 |
public void exposeGlobalTest() { |
|
249 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
250 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
251 |
||
252 |
try { |
|
253 |
e.put("y", "foo"); |
|
254 |
e.eval("print(y)"); |
|
255 |
} catch (final ScriptException exp) { |
|
256 |
exp.printStackTrace(); |
|
257 |
fail(exp.getMessage()); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
@Test |
|
262 |
public void putGlobalFunctionTest() { |
|
263 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
264 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
265 |
||
266 |
e.put("callable", new Callable<String>() { |
|
267 |
@Override |
|
268 |
public String call() throws Exception { |
|
269 |
return "callable was called"; |
|
270 |
} |
|
271 |
}); |
|
272 |
||
273 |
try { |
|
274 |
e.eval("print(callable.call())"); |
|
275 |
} catch (final ScriptException exp) { |
|
276 |
exp.printStackTrace(); |
|
277 |
fail(exp.getMessage()); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
@Test |
|
282 |
public void windowAlertTest() { |
|
283 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
284 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
285 |
final Window window = new Window(); |
|
286 |
||
287 |
try { |
|
288 |
e.put("window", window); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
289 |
e.eval("print(window.alert)"); |
16147 | 290 |
e.eval("window.alert('calling window.alert...')"); |
291 |
} catch (final Exception exp) { |
|
292 |
exp.printStackTrace(); |
|
293 |
fail(exp.getMessage()); |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
@Test |
|
298 |
public void windowLocationTest() { |
|
299 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
300 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
301 |
final Window window = new Window(); |
|
302 |
||
303 |
try { |
|
304 |
e.put("window", window); |
|
305 |
e.eval("print(window.location)"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
306 |
final Object locationValue = e.eval("window.getLocation()"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
307 |
assertEquals(locationValue, "http://localhost:8080/window"); |
16147 | 308 |
} catch (final Exception exp) { |
309 |
exp.printStackTrace(); |
|
310 |
fail(exp.getMessage()); |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
@Test |
|
315 |
public void windowItemTest() { |
|
316 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
317 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
318 |
final Window window = new Window(); |
|
319 |
||
320 |
try { |
|
321 |
e.put("window", window); |
|
322 |
final String item1 = (String)e.eval("window.item(65535)"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
323 |
assertEquals(item1, "ffff"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
324 |
final String item2 = (String)e.eval("window.item(255)"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
325 |
assertEquals(item2, "ff"); |
16147 | 326 |
} catch (final Exception exp) { |
327 |
exp.printStackTrace(); |
|
328 |
fail(exp.getMessage()); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
@Test |
|
333 |
public void windowEventTest() { |
|
334 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
335 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
336 |
final Window window = new Window(); |
|
337 |
||
338 |
try { |
|
339 |
e.put("window", window); |
|
340 |
e.eval("window.onload = function() { print('window load event fired'); return true }"); |
|
341 |
assertTrue((Boolean)e.eval("window.onload.loaded()")); |
|
342 |
final WindowEventHandler handler = window.getOnload(); |
|
343 |
assertNotNull(handler); |
|
344 |
assertTrue(handler.loaded()); |
|
345 |
} catch (final Exception exp) { |
|
346 |
exp.printStackTrace(); |
|
347 |
fail(exp.getMessage()); |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
@Test |
|
352 |
public void throwTest() { |
|
353 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
354 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
355 |
e.put(ScriptEngine.FILENAME, "throwtest.js"); |
|
356 |
||
357 |
try { |
|
358 |
e.eval("throw 'foo'"); |
|
359 |
} catch (final ScriptException exp) { |
|
360 |
log(exp.getMessage()); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
361 |
assertEquals(exp.getMessage(), "foo in throwtest.js at line number 1 at column number 0"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
362 |
assertEquals(exp.getFileName(), "throwtest.js"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
363 |
assertEquals(exp.getLineNumber(), 1); |
16147 | 364 |
} |
365 |
} |
|
366 |
||
367 |
@Test |
|
368 |
public void setTimeoutTest() { |
|
369 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
370 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
371 |
final Window window = new Window(); |
|
372 |
||
373 |
try { |
|
374 |
final Class<?> setTimeoutParamTypes[] = { Window.class, String.class, int.class }; |
|
375 |
final Method setTimeout = Window.class.getDeclaredMethod("setTimeout", setTimeoutParamTypes); |
|
376 |
assertNotNull(setTimeout); |
|
377 |
e.put("window", window); |
|
378 |
e.eval("window.setTimeout('foo()', 100)"); |
|
379 |
||
380 |
// try to make setTimeout global |
|
381 |
e.put("setTimeout", setTimeout); |
|
382 |
// TODO: java.lang.ClassCastException: required class |
|
383 |
// java.lang.Integer but encountered class java.lang.Double |
|
384 |
// e.eval("setTimeout('foo2()', 200)"); |
|
385 |
} catch (final Exception exp) { |
|
386 |
exp.printStackTrace(); |
|
387 |
fail(exp.getMessage()); |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
@Test |
|
392 |
public void setWriterTest() { |
|
393 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
394 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
395 |
final StringWriter sw = new StringWriter(); |
|
396 |
e.getContext().setWriter(sw); |
|
397 |
||
398 |
try { |
|
399 |
e.eval("print('hello world')"); |
|
400 |
} catch (final Exception exp) { |
|
401 |
exp.printStackTrace(); |
|
402 |
fail(exp.getMessage()); |
|
403 |
} |
|
19636 | 404 |
assertEquals(sw.toString(), println("hello world")); |
16147 | 405 |
} |
406 |
||
407 |
@Test |
|
408 |
public void redefineEchoTest() { |
|
409 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
410 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
411 |
||
412 |
try { |
|
413 |
e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }"); |
|
414 |
} catch (final Exception exp) { |
|
415 |
exp.printStackTrace(); |
|
416 |
fail(exp.getMessage()); |
|
417 |
} |
|
418 |
} |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
419 |
@Test |
16147 | 420 |
public void noEnumerablePropertiesTest() { |
421 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
422 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
423 |
try { |
|
424 |
e.eval("for (i in this) { throw 'found property: ' + i }"); |
|
425 |
} catch (final Exception exp) { |
|
426 |
exp.printStackTrace(); |
|
427 |
fail(exp.getMessage()); |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
@Test |
|
432 |
public void noRefErrorForGlobalThisAccessTest() { |
|
433 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
434 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
435 |
try { |
|
436 |
e.eval("this.foo"); |
|
437 |
} catch (final Exception exp) { |
|
438 |
exp.printStackTrace(); |
|
439 |
fail(exp.getMessage()); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
@Test |
|
444 |
public void refErrorForUndeclaredAccessTest() { |
|
445 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
446 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
447 |
try { |
|
448 |
e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }"); |
|
449 |
} catch (final Exception exp) { |
|
450 |
exp.printStackTrace(); |
|
451 |
fail(exp.getMessage()); |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
@Test |
|
456 |
public void typeErrorForGlobalThisCallTest() { |
|
457 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
458 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
459 |
try { |
|
460 |
e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }"); |
|
461 |
} catch (final Exception exp) { |
|
462 |
exp.printStackTrace(); |
|
463 |
fail(exp.getMessage()); |
|
464 |
} |
|
465 |
} |
|
466 |
||
467 |
@Test |
|
468 |
public void refErrorForUndeclaredCallTest() { |
|
469 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
470 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
471 |
try { |
|
472 |
e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }"); |
|
473 |
} catch (final Exception exp) { |
|
474 |
exp.printStackTrace(); |
|
475 |
fail(exp.getMessage()); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
@Test |
|
19107
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
480 |
// check that print function prints arg followed by newline char |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
481 |
public void printTest() { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
482 |
final ScriptEngineManager m = new ScriptEngineManager(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
483 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
484 |
final StringWriter sw = new StringWriter(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
485 |
e.getContext().setWriter(sw); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
486 |
try { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
487 |
e.eval("print('hello')"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
488 |
} catch (final Throwable t) { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
489 |
t.printStackTrace(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
490 |
fail(t.getMessage()); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
491 |
} |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
492 |
|
19636 | 493 |
assertEquals(sw.toString(), println("hello")); |
19107
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
494 |
} |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
495 |
|
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
496 |
@Test |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
497 |
// check that print prints all arguments (more than one) |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
498 |
public void printManyTest() { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
499 |
final ScriptEngineManager m = new ScriptEngineManager(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
500 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
501 |
final StringWriter sw = new StringWriter(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
502 |
e.getContext().setWriter(sw); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
503 |
try { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
504 |
e.eval("print(34, true, 'hello')"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
505 |
} catch (final Throwable t) { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
506 |
t.printStackTrace(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
507 |
fail(t.getMessage()); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
508 |
} |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
509 |
|
19636 | 510 |
assertEquals(sw.toString(), println("34 true hello")); |
511 |
} |
|
512 |
||
513 |
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
|
514 |
||
515 |
// Returns String that would be the result of calling PrintWriter.println |
|
516 |
// of the given String. (This is to handle platform specific newline). |
|
517 |
private static String println(final String str) { |
|
518 |
return str + LINE_SEPARATOR; |
|
19107
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
519 |
} |
16147 | 520 |
} |