jdk/test/javax/script/GetInterfaceTest.java
author sundar
Wed, 27 Feb 2013 17:22:44 +0530
changeset 16038 de84354579d3
parent 8772 e088a9afb9cc
child 16719 debc7896d82d
permissions -rw-r--r--
8009115: jtreg tests under jdk/test/javax/script should use nashorn as script engine Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8772
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     1
/*
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     4
 *
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     8
 *
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    13
 * accompanied this code).
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    14
 *
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    18
 *
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    21
 * questions.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    22
 */
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    23
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    24
/*
16038
de84354579d3 8009115: jtreg tests under jdk/test/javax/script should use nashorn as script engine
sundar
parents: 8772
diff changeset
    25
 * @run ignore
8772
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    26
 * @test
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    27
 * @bug 6960211
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    28
 * @summary JavaScript engine allows creation of interface although methods not available.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    29
 */
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    30
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    31
import javax.script.*;
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    32
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    33
public class GetInterfaceTest {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    34
    public static void main(String[] args) throws Exception {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    35
        ScriptEngineManager manager = new ScriptEngineManager();
16038
de84354579d3 8009115: jtreg tests under jdk/test/javax/script should use nashorn as script engine
sundar
parents: 8772
diff changeset
    36
        ScriptEngine engine = manager.getEngineByName("nashorn");
8772
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    37
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    38
        if (engine == null) {
16038
de84354579d3 8009115: jtreg tests under jdk/test/javax/script should use nashorn as script engine
sundar
parents: 8772
diff changeset
    39
            System.out.println("Warning: No js engine engine found; test vacuously passes.");
8772
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    40
            return;
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    41
        }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    42
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    43
        // don't define any function.
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    44
        engine.eval("");
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    45
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    46
        Runnable runnable = ((Invocable)engine).getInterface(Runnable.class);
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    47
        if (runnable != null) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    48
            throw new RuntimeException("runnable is not null!");
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    49
        }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    50
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    51
        // now define "run"
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    52
        engine.eval("function run() { println('this is run function'); }");
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    53
        runnable = ((Invocable)engine).getInterface(Runnable.class);
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    54
        // should not return null now!
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    55
        runnable.run();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    56
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    57
        // define only one method of "Foo2"
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    58
        engine.eval("function bar() { println('bar function'); }");
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    59
        Foo2 foo2 = ((Invocable)engine).getInterface(Foo2.class);
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    60
        if (foo2 != null) {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    61
            throw new RuntimeException("foo2 is not null!");
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    62
        }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    63
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    64
        // now define other method of "Foo2"
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    65
        engine.eval("function bar2() { println('bar2 function'); }");
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    66
        foo2 = ((Invocable)engine).getInterface(Foo2.class);
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    67
        foo2.bar();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    68
        foo2.bar2();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    69
    }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    70
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    71
    interface Foo {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    72
        public void bar();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    73
    }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    74
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    75
    interface Foo2 extends Foo {
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    76
        public void bar2();
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    77
    }
e088a9afb9cc 6960211: JavaScript engine allows creation of interface although methods not available
sundar
parents:
diff changeset
    78
}