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