Mon, 03 Nov 2014 11:47:41 +0100
8060204: Fix warnings in Joni and tests
Reviewed-by: hannesw, sundar, attila
sundar@518 | 1 | /* |
sundar@518 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
sundar@518 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
sundar@518 | 4 | * |
sundar@518 | 5 | * This code is free software; you can redistribute it and/or modify it |
sundar@518 | 6 | * under the terms of the GNU General Public License version 2 only, as |
sundar@518 | 7 | * published by the Free Software Foundation. Oracle designates this |
sundar@518 | 8 | * particular file as subject to the "Classpath" exception as provided |
sundar@518 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
sundar@518 | 10 | * |
sundar@518 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
sundar@518 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
sundar@518 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
sundar@518 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
sundar@518 | 15 | * accompanied this code). |
sundar@518 | 16 | * |
sundar@518 | 17 | * You should have received a copy of the GNU General Public License version |
sundar@518 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
sundar@518 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
sundar@518 | 20 | * |
sundar@518 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
sundar@518 | 22 | * or visit www.oracle.com if you need additional information or have any |
sundar@518 | 23 | * questions. |
sundar@518 | 24 | */ |
sundar@518 | 25 | |
sundar@518 | 26 | package jdk.nashorn.api.scripting; |
sundar@518 | 27 | |
attila@962 | 28 | import static org.testng.Assert.assertEquals; |
attila@962 | 29 | import static org.testng.Assert.fail; |
sundar@518 | 30 | import java.util.Objects; |
sundar@755 | 31 | import java.util.function.Function; |
sundar@518 | 32 | import javax.script.Invocable; |
sundar@518 | 33 | import javax.script.ScriptContext; |
sundar@518 | 34 | import javax.script.ScriptEngine; |
sundar@518 | 35 | import javax.script.ScriptEngineManager; |
sundar@518 | 36 | import javax.script.ScriptException; |
sundar@518 | 37 | import javax.script.SimpleScriptContext; |
sundar@518 | 38 | import org.testng.Assert; |
sundar@518 | 39 | import org.testng.annotations.Test; |
sundar@518 | 40 | |
sundar@518 | 41 | /** |
sundar@518 | 42 | * Tests for javax.script.Invocable implementation of nashorn. |
sundar@518 | 43 | */ |
lagergren@1082 | 44 | @SuppressWarnings("javadoc") |
sundar@518 | 45 | public class InvocableTest { |
sundar@518 | 46 | |
lagergren@1082 | 47 | private static void log(final String msg) { |
sundar@518 | 48 | org.testng.Reporter.log(msg, true); |
sundar@518 | 49 | } |
sundar@518 | 50 | |
sundar@518 | 51 | @Test |
sundar@518 | 52 | public void invokeMethodTest() { |
sundar@518 | 53 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 54 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 55 | |
sundar@518 | 56 | try { |
sundar@518 | 57 | e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); |
sundar@518 | 58 | final Object obj = e.get("myExample"); |
sundar@518 | 59 | final Object res = ((Invocable) e).invokeMethod(obj, "hello"); |
sundar@518 | 60 | assertEquals(res, "Hello World!"); |
sundar@518 | 61 | } catch (final Exception exp) { |
sundar@518 | 62 | exp.printStackTrace(); |
sundar@518 | 63 | fail(exp.getMessage()); |
sundar@518 | 64 | } |
sundar@518 | 65 | } |
sundar@518 | 66 | |
sundar@518 | 67 | @Test |
sundar@518 | 68 | /** |
sundar@518 | 69 | * Check that we can call invokeMethod on an object that we got by |
sundar@518 | 70 | * evaluating script with different Context set. |
sundar@518 | 71 | */ |
sundar@518 | 72 | public void invokeMethodDifferentContextTest() { |
attila@962 | 73 | final ScriptEngineManager m = new ScriptEngineManager(); |
attila@962 | 74 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 75 | |
sundar@518 | 76 | try { |
sundar@518 | 77 | // define an object with method on it |
attila@962 | 78 | final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); |
sundar@518 | 79 | |
sundar@518 | 80 | final ScriptContext ctxt = new SimpleScriptContext(); |
sundar@518 | 81 | ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); |
sundar@518 | 82 | e.setContext(ctxt); |
sundar@518 | 83 | |
sundar@518 | 84 | // invoke 'func' on obj - but with current script context changed |
sundar@518 | 85 | final Object res = ((Invocable) e).invokeMethod(obj, "hello"); |
sundar@518 | 86 | assertEquals(res, "Hello World!"); |
sundar@518 | 87 | } catch (final Exception exp) { |
sundar@518 | 88 | exp.printStackTrace(); |
sundar@518 | 89 | fail(exp.getMessage()); |
sundar@518 | 90 | } |
sundar@518 | 91 | } |
sundar@518 | 92 | |
sundar@518 | 93 | @Test |
sundar@518 | 94 | /** |
sundar@518 | 95 | * Check that invokeMethod throws NPE on null method name. |
sundar@518 | 96 | */ |
sundar@518 | 97 | public void invokeMethodNullNameTest() { |
sundar@518 | 98 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 99 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 100 | |
sundar@518 | 101 | try { |
sundar@518 | 102 | final Object obj = e.eval("({})"); |
lagergren@1082 | 103 | ((Invocable) e).invokeMethod(obj, null); |
sundar@518 | 104 | fail("should have thrown NPE"); |
sundar@518 | 105 | } catch (final Exception exp) { |
sundar@518 | 106 | if (!(exp instanceof NullPointerException)) { |
sundar@518 | 107 | exp.printStackTrace(); |
sundar@518 | 108 | fail(exp.getMessage()); |
sundar@518 | 109 | } |
sundar@518 | 110 | } |
sundar@518 | 111 | } |
sundar@518 | 112 | |
sundar@518 | 113 | @Test |
sundar@518 | 114 | /** |
sundar@518 | 115 | * Check that invokeMethod throws NoSuchMethodException on missing method. |
sundar@518 | 116 | */ |
sundar@518 | 117 | public void invokeMethodMissingTest() { |
sundar@518 | 118 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 119 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 120 | |
sundar@518 | 121 | try { |
sundar@518 | 122 | final Object obj = e.eval("({})"); |
lagergren@1082 | 123 | ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); |
sundar@518 | 124 | fail("should have thrown NoSuchMethodException"); |
sundar@518 | 125 | } catch (final Exception exp) { |
sundar@518 | 126 | if (!(exp instanceof NoSuchMethodException)) { |
sundar@518 | 127 | exp.printStackTrace(); |
sundar@518 | 128 | fail(exp.getMessage()); |
sundar@518 | 129 | } |
sundar@518 | 130 | } |
sundar@518 | 131 | } |
sundar@518 | 132 | |
sundar@518 | 133 | @Test |
sundar@518 | 134 | /** |
sundar@518 | 135 | * Check that calling method on non-script object 'thiz' results in |
sundar@518 | 136 | * IllegalArgumentException. |
sundar@518 | 137 | */ |
sundar@518 | 138 | public void invokeMethodNonScriptObjectThizTest() { |
sundar@518 | 139 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 140 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 141 | |
sundar@518 | 142 | try { |
sundar@518 | 143 | ((Invocable) e).invokeMethod(new Object(), "toString"); |
sundar@518 | 144 | fail("should have thrown IllegalArgumentException"); |
sundar@518 | 145 | } catch (final Exception exp) { |
sundar@518 | 146 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 147 | exp.printStackTrace(); |
sundar@518 | 148 | fail(exp.getMessage()); |
sundar@518 | 149 | } |
sundar@518 | 150 | } |
sundar@518 | 151 | } |
sundar@518 | 152 | |
sundar@518 | 153 | @Test |
sundar@518 | 154 | /** |
sundar@518 | 155 | * Check that calling method on null 'thiz' results in |
sundar@518 | 156 | * IllegalArgumentException. |
sundar@518 | 157 | */ |
sundar@518 | 158 | public void invokeMethodNullThizTest() { |
sundar@518 | 159 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 160 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 161 | |
sundar@518 | 162 | try { |
sundar@518 | 163 | ((Invocable) e).invokeMethod(null, "toString"); |
sundar@518 | 164 | fail("should have thrown IllegalArgumentException"); |
sundar@518 | 165 | } catch (final Exception exp) { |
sundar@518 | 166 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 167 | exp.printStackTrace(); |
sundar@518 | 168 | fail(exp.getMessage()); |
sundar@518 | 169 | } |
sundar@518 | 170 | } |
sundar@518 | 171 | } |
sundar@518 | 172 | |
sundar@518 | 173 | @Test |
sundar@518 | 174 | /** |
sundar@518 | 175 | * Check that calling method on mirror created by another engine results in |
sundar@518 | 176 | * IllegalArgumentException. |
sundar@518 | 177 | */ |
sundar@518 | 178 | public void invokeMethodMixEnginesTest() { |
sundar@518 | 179 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 180 | final ScriptEngine engine1 = m.getEngineByName("nashorn"); |
sundar@518 | 181 | final ScriptEngine engine2 = m.getEngineByName("nashorn"); |
sundar@518 | 182 | |
sundar@518 | 183 | try { |
attila@962 | 184 | final Object obj = engine1.eval("({ run: function() {} })"); |
sundar@518 | 185 | // pass object from engine1 to engine2 as 'thiz' for invokeMethod |
sundar@518 | 186 | ((Invocable) engine2).invokeMethod(obj, "run"); |
sundar@518 | 187 | fail("should have thrown IllegalArgumentException"); |
sundar@518 | 188 | } catch (final Exception exp) { |
sundar@518 | 189 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 190 | exp.printStackTrace(); |
sundar@518 | 191 | fail(exp.getMessage()); |
sundar@518 | 192 | } |
sundar@518 | 193 | } |
sundar@518 | 194 | } |
sundar@518 | 195 | |
sundar@518 | 196 | @Test |
sundar@518 | 197 | public void getInterfaceTest() { |
sundar@518 | 198 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 199 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 200 | final Invocable inv = (Invocable) e; |
sundar@518 | 201 | |
sundar@518 | 202 | // try to get interface from global functions |
sundar@518 | 203 | try { |
sundar@518 | 204 | e.eval("function run() { print('run'); };"); |
sundar@518 | 205 | final Runnable runnable = inv.getInterface(Runnable.class); |
sundar@518 | 206 | runnable.run(); |
sundar@518 | 207 | } catch (final Exception exp) { |
sundar@518 | 208 | exp.printStackTrace(); |
sundar@518 | 209 | fail(exp.getMessage()); |
sundar@518 | 210 | } |
sundar@518 | 211 | |
sundar@518 | 212 | // try interface on specific script object |
sundar@518 | 213 | try { |
sundar@518 | 214 | e.eval("var obj = { run: function() { print('run from obj'); } };"); |
attila@962 | 215 | final Object obj = e.get("obj"); |
sundar@518 | 216 | final Runnable runnable = inv.getInterface(obj, Runnable.class); |
sundar@518 | 217 | runnable.run(); |
sundar@518 | 218 | } catch (final Exception exp) { |
sundar@518 | 219 | exp.printStackTrace(); |
sundar@518 | 220 | fail(exp.getMessage()); |
sundar@518 | 221 | } |
sundar@518 | 222 | } |
sundar@518 | 223 | |
sundar@518 | 224 | public interface Foo { |
sundar@518 | 225 | |
sundar@518 | 226 | public void bar(); |
sundar@518 | 227 | } |
sundar@518 | 228 | |
sundar@518 | 229 | public interface Foo2 extends Foo { |
sundar@518 | 230 | |
sundar@518 | 231 | public void bar2(); |
sundar@518 | 232 | } |
sundar@518 | 233 | |
sundar@518 | 234 | @Test |
sundar@518 | 235 | public void getInterfaceMissingTest() { |
sundar@518 | 236 | final ScriptEngineManager manager = new ScriptEngineManager(); |
sundar@518 | 237 | final ScriptEngine engine = manager.getEngineByName("nashorn"); |
sundar@518 | 238 | |
sundar@518 | 239 | // don't define any function. |
sundar@518 | 240 | try { |
sundar@518 | 241 | engine.eval(""); |
sundar@518 | 242 | } catch (final Exception exp) { |
sundar@518 | 243 | exp.printStackTrace(); |
sundar@518 | 244 | fail(exp.getMessage()); |
sundar@518 | 245 | } |
sundar@518 | 246 | |
sundar@518 | 247 | Runnable runnable = ((Invocable) engine).getInterface(Runnable.class); |
sundar@518 | 248 | if (runnable != null) { |
sundar@518 | 249 | fail("runnable is not null!"); |
sundar@518 | 250 | } |
sundar@518 | 251 | |
sundar@518 | 252 | // now define "run" |
sundar@518 | 253 | try { |
sundar@518 | 254 | engine.eval("function run() { print('this is run function'); }"); |
sundar@518 | 255 | } catch (final Exception exp) { |
sundar@518 | 256 | exp.printStackTrace(); |
sundar@518 | 257 | fail(exp.getMessage()); |
sundar@518 | 258 | } |
sundar@518 | 259 | runnable = ((Invocable) engine).getInterface(Runnable.class); |
sundar@518 | 260 | // should not return null now! |
sundar@518 | 261 | runnable.run(); |
sundar@518 | 262 | |
sundar@518 | 263 | // define only one method of "Foo2" |
sundar@518 | 264 | try { |
sundar@518 | 265 | engine.eval("function bar() { print('bar function'); }"); |
sundar@518 | 266 | } catch (final Exception exp) { |
sundar@518 | 267 | exp.printStackTrace(); |
sundar@518 | 268 | fail(exp.getMessage()); |
sundar@518 | 269 | } |
sundar@518 | 270 | |
sundar@518 | 271 | Foo2 foo2 = ((Invocable) engine).getInterface(Foo2.class); |
sundar@518 | 272 | if (foo2 != null) { |
sundar@518 | 273 | throw new RuntimeException("foo2 is not null!"); |
sundar@518 | 274 | } |
sundar@518 | 275 | |
sundar@518 | 276 | // now define other method of "Foo2" |
sundar@518 | 277 | try { |
sundar@518 | 278 | engine.eval("function bar2() { print('bar2 function'); }"); |
sundar@518 | 279 | } catch (final Exception exp) { |
sundar@518 | 280 | exp.printStackTrace(); |
sundar@518 | 281 | fail(exp.getMessage()); |
sundar@518 | 282 | } |
sundar@518 | 283 | foo2 = ((Invocable) engine).getInterface(Foo2.class); |
sundar@518 | 284 | foo2.bar(); |
sundar@518 | 285 | foo2.bar2(); |
sundar@518 | 286 | } |
sundar@518 | 287 | |
sundar@518 | 288 | @Test |
sundar@518 | 289 | /** |
sundar@518 | 290 | * Try passing non-interface Class object for interface implementation. |
sundar@518 | 291 | */ |
sundar@518 | 292 | public void getNonInterfaceGetInterfaceTest() { |
sundar@518 | 293 | final ScriptEngineManager manager = new ScriptEngineManager(); |
sundar@518 | 294 | final ScriptEngine engine = manager.getEngineByName("nashorn"); |
sundar@518 | 295 | try { |
sundar@518 | 296 | log(Objects.toString(((Invocable) engine).getInterface(Object.class))); |
sundar@518 | 297 | fail("Should have thrown IllegalArgumentException"); |
sundar@518 | 298 | } catch (final Exception exp) { |
sundar@518 | 299 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 300 | fail("IllegalArgumentException expected, got " + exp); |
sundar@518 | 301 | } |
sundar@518 | 302 | } |
sundar@518 | 303 | } |
sundar@518 | 304 | |
sundar@518 | 305 | @Test |
sundar@518 | 306 | /** |
sundar@518 | 307 | * Check that we can get interface out of a script object even after |
sundar@518 | 308 | * switching to use different ScriptContext. |
sundar@518 | 309 | */ |
sundar@518 | 310 | public void getInterfaceDifferentContext() { |
attila@962 | 311 | final ScriptEngineManager m = new ScriptEngineManager(); |
attila@962 | 312 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 313 | try { |
attila@962 | 314 | final Object obj = e.eval("({ run: function() { } })"); |
sundar@518 | 315 | |
sundar@518 | 316 | // change script context |
attila@962 | 317 | final ScriptContext ctxt = new SimpleScriptContext(); |
sundar@518 | 318 | ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); |
sundar@518 | 319 | e.setContext(ctxt); |
sundar@518 | 320 | |
attila@962 | 321 | final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); |
sundar@518 | 322 | r.run(); |
sundar@518 | 323 | } catch (final Exception exp) { |
sundar@518 | 324 | exp.printStackTrace(); |
sundar@518 | 325 | fail(exp.getMessage()); |
sundar@518 | 326 | } |
sundar@518 | 327 | } |
sundar@518 | 328 | |
sundar@518 | 329 | @Test |
sundar@518 | 330 | /** |
sundar@518 | 331 | * Check that getInterface on non-script object 'thiz' results in |
sundar@518 | 332 | * IllegalArgumentException. |
sundar@518 | 333 | */ |
sundar@518 | 334 | public void getInterfaceNonScriptObjectThizTest() { |
sundar@518 | 335 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 336 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 337 | |
sundar@518 | 338 | try { |
sundar@518 | 339 | ((Invocable) e).getInterface(new Object(), Runnable.class); |
sundar@518 | 340 | fail("should have thrown IllegalArgumentException"); |
sundar@518 | 341 | } catch (final Exception exp) { |
sundar@518 | 342 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 343 | exp.printStackTrace(); |
sundar@518 | 344 | fail(exp.getMessage()); |
sundar@518 | 345 | } |
sundar@518 | 346 | } |
sundar@518 | 347 | } |
sundar@518 | 348 | |
sundar@518 | 349 | @Test |
sundar@518 | 350 | /** |
sundar@518 | 351 | * Check that getInterface on null 'thiz' results in |
sundar@518 | 352 | * IllegalArgumentException. |
sundar@518 | 353 | */ |
sundar@518 | 354 | public void getInterfaceNullThizTest() { |
sundar@518 | 355 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 356 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 357 | |
sundar@518 | 358 | try { |
sundar@518 | 359 | ((Invocable) e).getInterface(null, Runnable.class); |
sundar@518 | 360 | fail("should have thrown IllegalArgumentException"); |
sundar@518 | 361 | } catch (final Exception exp) { |
sundar@518 | 362 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 363 | exp.printStackTrace(); |
sundar@518 | 364 | fail(exp.getMessage()); |
sundar@518 | 365 | } |
sundar@518 | 366 | } |
sundar@518 | 367 | } |
sundar@518 | 368 | |
sundar@518 | 369 | @Test |
sundar@518 | 370 | /** |
sundar@518 | 371 | * Check that calling getInterface on mirror created by another engine |
sundar@518 | 372 | * results in IllegalArgumentException. |
sundar@518 | 373 | */ |
sundar@518 | 374 | public void getInterfaceMixEnginesTest() { |
sundar@518 | 375 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 376 | final ScriptEngine engine1 = m.getEngineByName("nashorn"); |
sundar@518 | 377 | final ScriptEngine engine2 = m.getEngineByName("nashorn"); |
sundar@518 | 378 | |
sundar@518 | 379 | try { |
attila@962 | 380 | final Object obj = engine1.eval("({ run: function() {} })"); |
sundar@518 | 381 | // pass object from engine1 to engine2 as 'thiz' for getInterface |
sundar@518 | 382 | ((Invocable) engine2).getInterface(obj, Runnable.class); |
sundar@518 | 383 | fail("should have thrown IllegalArgumentException"); |
sundar@518 | 384 | } catch (final Exception exp) { |
sundar@518 | 385 | if (!(exp instanceof IllegalArgumentException)) { |
sundar@518 | 386 | exp.printStackTrace(); |
sundar@518 | 387 | fail(exp.getMessage()); |
sundar@518 | 388 | } |
sundar@518 | 389 | } |
sundar@518 | 390 | } |
sundar@518 | 391 | |
sundar@518 | 392 | @Test |
sundar@518 | 393 | /** |
sundar@518 | 394 | * check that null function name results in NPE. |
sundar@518 | 395 | */ |
sundar@518 | 396 | public void invokeFunctionNullNameTest() { |
sundar@518 | 397 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 398 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 399 | |
sundar@518 | 400 | try { |
lagergren@1082 | 401 | ((Invocable)e).invokeFunction(null); |
sundar@518 | 402 | fail("should have thrown NPE"); |
sundar@518 | 403 | } catch (final Exception exp) { |
sundar@518 | 404 | if (!(exp instanceof NullPointerException)) { |
sundar@518 | 405 | exp.printStackTrace(); |
sundar@518 | 406 | fail(exp.getMessage()); |
sundar@518 | 407 | } |
sundar@518 | 408 | } |
sundar@518 | 409 | } |
sundar@518 | 410 | |
sundar@518 | 411 | @Test |
sundar@518 | 412 | /** |
sundar@518 | 413 | * Check that attempt to call missing function results in |
sundar@518 | 414 | * NoSuchMethodException. |
sundar@518 | 415 | */ |
sundar@518 | 416 | public void invokeFunctionMissingTest() { |
sundar@518 | 417 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 418 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 419 | |
sundar@518 | 420 | try { |
lagergren@1082 | 421 | ((Invocable)e).invokeFunction("NonExistentFunc"); |
sundar@518 | 422 | fail("should have thrown NoSuchMethodException"); |
sundar@518 | 423 | } catch (final Exception exp) { |
sundar@518 | 424 | if (!(exp instanceof NoSuchMethodException)) { |
sundar@518 | 425 | exp.printStackTrace(); |
sundar@518 | 426 | fail(exp.getMessage()); |
sundar@518 | 427 | } |
sundar@518 | 428 | } |
sundar@518 | 429 | } |
sundar@518 | 430 | |
sundar@518 | 431 | @Test |
sundar@518 | 432 | /** |
sundar@518 | 433 | * Check that invokeFunction calls functions only from current context's |
sundar@518 | 434 | * Bindings. |
sundar@518 | 435 | */ |
sundar@518 | 436 | public void invokeFunctionDifferentContextTest() { |
attila@962 | 437 | final ScriptEngineManager m = new ScriptEngineManager(); |
attila@962 | 438 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 439 | |
sundar@518 | 440 | try { |
sundar@518 | 441 | // define an object with method on it |
lagergren@1082 | 442 | e.eval("function hello() { return 'Hello World!'; }"); |
sundar@518 | 443 | final ScriptContext ctxt = new SimpleScriptContext(); |
sundar@518 | 444 | ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); |
sundar@518 | 445 | // change engine's current context |
sundar@518 | 446 | e.setContext(ctxt); |
sundar@518 | 447 | |
sundar@518 | 448 | ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! |
sundar@518 | 449 | fail("should have thrown NoSuchMethodException"); |
sundar@518 | 450 | } catch (final Exception exp) { |
sundar@518 | 451 | if (!(exp instanceof NoSuchMethodException)) { |
sundar@518 | 452 | exp.printStackTrace(); |
sundar@518 | 453 | fail(exp.getMessage()); |
sundar@518 | 454 | } |
sundar@518 | 455 | } |
sundar@518 | 456 | } |
sundar@518 | 457 | |
sundar@518 | 458 | @Test |
sundar@518 | 459 | public void invokeFunctionExceptionTest() { |
sundar@518 | 460 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 461 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 462 | try { |
sundar@518 | 463 | e.eval("function func() { throw new TypeError(); }"); |
sundar@518 | 464 | } catch (final Throwable t) { |
sundar@518 | 465 | t.printStackTrace(); |
sundar@518 | 466 | fail(t.getMessage()); |
sundar@518 | 467 | } |
sundar@518 | 468 | |
sundar@518 | 469 | try { |
sundar@518 | 470 | ((Invocable) e).invokeFunction("func"); |
sundar@518 | 471 | fail("should have thrown exception"); |
sundar@518 | 472 | } catch (final ScriptException se) { |
sundar@518 | 473 | // ECMA TypeError property wrapped as a ScriptException |
sundar@518 | 474 | log("got " + se + " as expected"); |
sundar@518 | 475 | } catch (final Throwable t) { |
sundar@518 | 476 | t.printStackTrace(); |
sundar@518 | 477 | fail(t.getMessage()); |
sundar@518 | 478 | } |
sundar@518 | 479 | } |
sundar@518 | 480 | |
sundar@518 | 481 | @Test |
sundar@518 | 482 | public void invokeMethodExceptionTest() { |
sundar@518 | 483 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 484 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 485 | try { |
sundar@518 | 486 | e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); |
sundar@518 | 487 | } catch (final Throwable t) { |
sundar@518 | 488 | t.printStackTrace(); |
sundar@518 | 489 | fail(t.getMessage()); |
sundar@518 | 490 | } |
sundar@518 | 491 | |
sundar@518 | 492 | try { |
sundar@518 | 493 | final Object sobj = e.get("sobj"); |
sundar@518 | 494 | ((Invocable) e).invokeMethod(sobj, "foo"); |
sundar@518 | 495 | fail("should have thrown exception"); |
sundar@518 | 496 | } catch (final ScriptException se) { |
sundar@518 | 497 | // ECMA TypeError property wrapped as a ScriptException |
sundar@518 | 498 | log("got " + se + " as expected"); |
sundar@518 | 499 | } catch (final Throwable t) { |
sundar@518 | 500 | t.printStackTrace(); |
sundar@518 | 501 | fail(t.getMessage()); |
sundar@518 | 502 | } |
sundar@518 | 503 | } |
sundar@518 | 504 | |
sundar@518 | 505 | @Test |
sundar@518 | 506 | /** |
sundar@518 | 507 | * Tests whether invocation of a JavaScript method through a variable arity |
sundar@518 | 508 | * Java method will pass the vararg array. Both non-vararg and vararg |
sundar@518 | 509 | * JavaScript methods are tested. |
sundar@518 | 510 | * |
sundar@518 | 511 | * @throws ScriptException |
sundar@518 | 512 | */ |
sundar@518 | 513 | public void variableArityInterfaceTest() throws ScriptException { |
sundar@518 | 514 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@518 | 515 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@518 | 516 | e.eval( |
sundar@518 | 517 | "function test1(i, strings) {" |
sundar@518 | 518 | + " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" |
sundar@518 | 519 | + "}" |
sundar@518 | 520 | + "function test2() {" |
sundar@518 | 521 | + " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" |
sundar@518 | 522 | + "}"); |
sundar@518 | 523 | final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class); |
sundar@518 | 524 | Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); |
sundar@518 | 525 | Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); |
sundar@518 | 526 | } |
sundar@755 | 527 | |
sundar@755 | 528 | @Test |
sundar@755 | 529 | public void defaultMethodTest() throws ScriptException { |
sundar@755 | 530 | final ScriptEngineManager m = new ScriptEngineManager(); |
sundar@755 | 531 | final ScriptEngine e = m.getEngineByName("nashorn"); |
sundar@755 | 532 | final Invocable inv = (Invocable) e; |
sundar@755 | 533 | |
attila@962 | 534 | final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})"); |
lagergren@1082 | 535 | @SuppressWarnings("unchecked") |
attila@962 | 536 | final Function<String, String> func = inv.getInterface(obj, Function.class); |
sundar@755 | 537 | assertEquals(func.apply("hello"), "HELLO"); |
sundar@755 | 538 | } |
sundar@518 | 539 | } |