Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/script/JavaScriptScopeTest.java
38833 views
/*1* Copyright (c) 2005, 2008, 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 6346733 670589326* @summary Verify that independent Bindings instances don't27* get affected by default scope assignments. Also, verify28* that script globals can be created and accessed from Java29* as well as JavaScript.30*/3132import javax.script.*;3334public class JavaScriptScopeTest {3536public static void main(String[] args) throws Exception {37ScriptEngineManager manager = new ScriptEngineManager();38ScriptEngine jsengine = Helper.getJsEngine(manager);39if (jsengine == null) {40System.out.println("Warning: No js engine found; test vacuously passes.");41return;42}43jsengine.eval("var v = 'hello';");44// Create a new scope45Bindings b = jsengine.createBindings();46// b is newly created scope. We don't expect 'v' there.47// we expect b to be empty...48if (b.keySet().size() != 0) {49throw new RuntimeException("no variables expected in new scope");50}5152// verify that we can create new variable from Java53jsengine.put("fromJava", "hello world");54// below should execute without problems..55jsengine.eval(" if (fromJava != 'hello world') throw 'unexpected'");5657// verify that script globals are exposed to Java58// we have created 'v' and 'fromJava' already.59if (! jsengine.get("v").equals("hello")) {60throw new RuntimeException("unexpected value of 'v'");61}6263if (! jsengine.get("fromJava").equals("hello world")) {64throw new RuntimeException("unexpected value of 'fromJava'");65}66}67}686970