aboutsummaryrefslogtreecommitdiff
path: root/test/src/jdk/nashorn/api/scripting/test/InvocableTest.java
blob: bba4ce50240db59a64f9bb0e714da1b966c461b8 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.api.scripting.test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import java.util.Objects;
import java.util.function.Function;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * Tests for javax.script.Invocable implementation of nashorn.
 */
@SuppressWarnings("javadoc")
public class InvocableTest {

    private static void log(final String msg) {
        org.testng.Reporter.log(msg, true);
    }

    @Test
    public void invokeMethodTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();");
            final Object obj = e.get("myExample");
            final Object res = ((Invocable) e).invokeMethod(obj, "hello");
            assertEquals(res, "Hello World!");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }

    @Test
    /**
     * Check that we can call invokeMethod on an object that we got by
     * evaluating script with different Context set.
     */
    public void invokeMethodDifferentContextTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            // define an object with method on it
            final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })");

            final ScriptContext ctxt = new SimpleScriptContext();
            ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
            e.setContext(ctxt);

            // invoke 'func' on obj - but with current script context changed
            final Object res = ((Invocable) e).invokeMethod(obj, "hello");
            assertEquals(res, "Hello World!");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }

    @Test
    /**
     * Check that invokeMethod throws NPE on null method name.
     */
    public void invokeMethodNullNameTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            final Object obj = e.eval("({})");
            ((Invocable) e).invokeMethod(obj, null);
            fail("should have thrown NPE");
        } catch (final Exception exp) {
            if (!(exp instanceof NullPointerException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that invokeMethod throws NoSuchMethodException on missing method.
     */
    public void invokeMethodMissingTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            final Object obj = e.eval("({})");
            ((Invocable) e).invokeMethod(obj, "nonExistentMethod");
            fail("should have thrown NoSuchMethodException");
        } catch (final Exception exp) {
            if (!(exp instanceof NoSuchMethodException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that calling method on non-script object 'thiz' results in
     * IllegalArgumentException.
     */
    public void invokeMethodNonScriptObjectThizTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            ((Invocable) e).invokeMethod(new Object(), "toString");
            fail("should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that calling method on null 'thiz' results in
     * IllegalArgumentException.
     */
    public void invokeMethodNullThizTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            ((Invocable) e).invokeMethod(null, "toString");
            fail("should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that calling method on mirror created by another engine results in
     * IllegalArgumentException.
     */
    public void invokeMethodMixEnginesTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine engine1 = m.getEngineByName("nashorn");
        final ScriptEngine engine2 = m.getEngineByName("nashorn");

        try {
            final Object obj = engine1.eval("({ run: function() {} })");
            // pass object from engine1 to engine2 as 'thiz' for invokeMethod
            ((Invocable) engine2).invokeMethod(obj, "run");
            fail("should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    public void getInterfaceTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        final Invocable inv = (Invocable) e;

        // try to get interface from global functions
        try {
            e.eval("function run() { print('run'); };");
            final Runnable runnable = inv.getInterface(Runnable.class);
            runnable.run();
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }

        // try interface on specific script object
        try {
            e.eval("var obj = { run: function() { print('run from obj'); } };");
            final Object obj = e.get("obj");
            final Runnable runnable = inv.getInterface(obj, Runnable.class);
            runnable.run();
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }

    public interface Foo {

        public void bar();
    }

    public interface Foo2 extends Foo {

        public void bar2();
    }

    @Test
    public void getInterfaceMissingTest() {
        final ScriptEngineManager manager = new ScriptEngineManager();
        final ScriptEngine engine = manager.getEngineByName("nashorn");

        // don't define any function.
        try {
            engine.eval("");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }

        Runnable runnable = ((Invocable) engine).getInterface(Runnable.class);
        if (runnable != null) {
            fail("runnable is not null!");
        }

        // now define "run"
        try {
            engine.eval("function run() { print('this is run function'); }");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
        runnable = ((Invocable) engine).getInterface(Runnable.class);
        // should not return null now!
        runnable.run();

        // define only one method of "Foo2"
        try {
            engine.eval("function bar() { print('bar function'); }");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }

        Foo2 foo2 = ((Invocable) engine).getInterface(Foo2.class);
        if (foo2 != null) {
            throw new RuntimeException("foo2 is not null!");
        }

        // now define other method of "Foo2"
        try {
            engine.eval("function bar2() { print('bar2 function'); }");
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
        foo2 = ((Invocable) engine).getInterface(Foo2.class);
        foo2.bar();
        foo2.bar2();
    }

    @Test
    /**
     * Try passing non-interface Class object for interface implementation.
     */
    public void getNonInterfaceGetInterfaceTest() {
        final ScriptEngineManager manager = new ScriptEngineManager();
        final ScriptEngine engine = manager.getEngineByName("nashorn");
        try {
            log(Objects.toString(((Invocable) engine).getInterface(Object.class)));
            fail("Should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                fail("IllegalArgumentException expected, got " + exp);
            }
        }
    }

    @Test
    /**
     * Check that we can get interface out of a script object even after
     * switching to use different ScriptContext.
     */
    public void getInterfaceDifferentContext() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        try {
            final Object obj = e.eval("({ run: function() { } })");

            // change script context
            final ScriptContext ctxt = new SimpleScriptContext();
            ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
            e.setContext(ctxt);

            final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class);
            r.run();
        } catch (final Exception exp) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }

    @Test
    /**
     * Check that getInterface on non-script object 'thiz' results in
     * IllegalArgumentException.
     */
    public void getInterfaceNonScriptObjectThizTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            ((Invocable) e).getInterface(new Object(), Runnable.class);
            fail("should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that getInterface on null 'thiz' results in
     * IllegalArgumentException.
     */
    public void getInterfaceNullThizTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            ((Invocable) e).getInterface(null, Runnable.class);
            fail("should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that calling getInterface on mirror created by another engine
     * results in IllegalArgumentException.
     */
    public void getInterfaceMixEnginesTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine engine1 = m.getEngineByName("nashorn");
        final ScriptEngine engine2 = m.getEngineByName("nashorn");

        try {
            final Object obj = engine1.eval("({ run: function() {} })");
            // pass object from engine1 to engine2 as 'thiz' for getInterface
            ((Invocable) engine2).getInterface(obj, Runnable.class);
            fail("should have thrown IllegalArgumentException");
        } catch (final Exception exp) {
            if (!(exp instanceof IllegalArgumentException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * check that null function name results in NPE.
     */
    public void invokeFunctionNullNameTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            ((Invocable)e).invokeFunction(null);
            fail("should have thrown NPE");
        } catch (final Exception exp) {
            if (!(exp instanceof NullPointerException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that attempt to call missing function results in
     * NoSuchMethodException.
     */
    public void invokeFunctionMissingTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            ((Invocable)e).invokeFunction("NonExistentFunc");
            fail("should have thrown NoSuchMethodException");
        } catch (final Exception exp) {
            if (!(exp instanceof NoSuchMethodException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    /**
     * Check that invokeFunction calls functions only from current context's
     * Bindings.
     */
    public void invokeFunctionDifferentContextTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");

        try {
            // define an object with method on it
            e.eval("function hello() { return 'Hello World!'; }");
            final ScriptContext ctxt = new SimpleScriptContext();
            ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
            // change engine's current context
            e.setContext(ctxt);

            ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context!
            fail("should have thrown NoSuchMethodException");
        } catch (final Exception exp) {
            if (!(exp instanceof NoSuchMethodException)) {
                exp.printStackTrace();
                fail(exp.getMessage());
            }
        }
    }

    @Test
    public void invokeFunctionExceptionTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        try {
            e.eval("function func() { throw new TypeError(); }");
        } catch (final Throwable t) {
            t.printStackTrace();
            fail(t.getMessage());
        }

        try {
            ((Invocable) e).invokeFunction("func");
            fail("should have thrown exception");
        } catch (final ScriptException se) {
            // ECMA TypeError property wrapped as a ScriptException
            log("got " + se + " as expected");
        } catch (final Throwable t) {
            t.printStackTrace();
            fail(t.getMessage());
        }
    }

    @Test
    public void invokeMethodExceptionTest() {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        try {
            e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }");
        } catch (final Throwable t) {
            t.printStackTrace();
            fail(t.getMessage());
        }

        try {
            final Object sobj = e.get("sobj");
            ((Invocable) e).invokeMethod(sobj, "foo");
            fail("should have thrown exception");
        } catch (final ScriptException se) {
            // ECMA TypeError property wrapped as a ScriptException
            log("got " + se + " as expected");
        } catch (final Throwable t) {
            t.printStackTrace();
            fail(t.getMessage());
        }
    }

    @Test
    /**
     * Tests whether invocation of a JavaScript method through a variable arity
     * Java method will pass the vararg array. Both non-vararg and vararg
     * JavaScript methods are tested.
     *
     * @throws ScriptException
     */
    public void variableArityInterfaceTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        e.eval(
                "function test1(i, strings) {"
                + "    return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)"
                + "}"
                + "function test2() {"
                + "    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])"
                + "}");
        final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class);
        Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]");
        Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]");
    }

    @Test
    public void defaultMethodTest() throws ScriptException {
        final ScriptEngineManager m = new ScriptEngineManager();
        final ScriptEngine e = m.getEngineByName("nashorn");
        final Invocable inv = (Invocable) e;

        final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})");
        @SuppressWarnings("unchecked")
        final Function<String, String> func = inv.getInterface(obj, Function.class);
        assertEquals(func.apply("hello"), "HELLO");
    }
}