Path: blob/master/test/jdk/javax/script/GetInterfaceTest.java
41145 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 696021126* @summary JavaScript engine allows creation of interface although methods not available.27*/2829import javax.script.*;3031public class GetInterfaceTest {32public static void main(String[] args) throws Exception {33ScriptEngineManager manager = new ScriptEngineManager();34ScriptEngine engine = manager.getEngineByName("nashorn");3536if (engine == null) {37System.out.println("Warning: No js engine engine found; test vacuously passes.");38return;39}4041// don't define any function.42engine.eval("");4344Runnable runnable = ((Invocable)engine).getInterface(Runnable.class);45if (runnable != null) {46throw new RuntimeException("runnable is not null!");47}4849// now define "run"50engine.eval("function run() { print('this is run function'); }");51runnable = ((Invocable)engine).getInterface(Runnable.class);52// should not return null now!53runnable.run();5455// define only one method of "Foo2"56engine.eval("function bar() { print('bar function'); }");57Foo2 foo2 = ((Invocable)engine).getInterface(Foo2.class);58if (foo2 != null) {59throw new RuntimeException("foo2 is not null!");60}6162// now define other method of "Foo2"63engine.eval("function bar2() { print('bar2 function'); }");64foo2 = ((Invocable)engine).getInterface(Foo2.class);65foo2.bar();66foo2.bar2();67}6869public interface Foo {70public void bar();71}7273public interface Foo2 extends Foo {74public void bar2();75}76}777879