Path: blob/master/test/jdk/javax/script/SimpleScriptContextNameChecksTest.java
41145 views
/*1* Copyright (c) 2015, 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 807285326* @summary SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing27* @run testng SimpleScriptContextNameChecksTest28*/2930import java.util.List;31import java.util.function.Consumer;32import javax.script.*;33import org.testng.annotations.Test;3435public class SimpleScriptContextNameChecksTest {36private List<ScriptEngineFactory> getFactories() {37return new ScriptEngineManager().getEngineFactories();38}3940private void testAndExpect(Consumer<ScriptContext> c, Class<? extends RuntimeException> clazz) {41for (ScriptEngineFactory fac : getFactories()) {42ScriptContext sc = fac.getScriptEngine().getContext();43String name = fac.getEngineName();44try {45c.accept(sc);46throw new RuntimeException("no exception for " + name);47} catch (NullPointerException | IllegalArgumentException e) {48if (e.getClass() == clazz) {49System.out.println("got " + e + " as expected for " + name);50} else {51throw e;52}53}54}55}5657private void testAndExpectIAE(Consumer<ScriptContext> c) {58testAndExpect(c, IllegalArgumentException.class);59}6061private void testAndExpectNPE(Consumer<ScriptContext> c) {62testAndExpect(c, NullPointerException.class);63}6465@Test66public void getAttributeEmptyName() {67testAndExpectIAE(sc -> sc.getAttribute("", ScriptContext.GLOBAL_SCOPE));68}6970@Test71public void getAttributeNullName() {72testAndExpectNPE(sc -> sc.getAttribute(null, ScriptContext.GLOBAL_SCOPE));73}7475@Test76public void removeAttributeEmptyName() {77testAndExpectIAE(sc -> sc.removeAttribute("", ScriptContext.GLOBAL_SCOPE));78}7980@Test81public void removeAttributeNullName() {82testAndExpectNPE(sc -> sc.removeAttribute(null, ScriptContext.GLOBAL_SCOPE));83}8485@Test86public void setAttributeEmptyName() {87testAndExpectIAE(sc -> sc.setAttribute("", "value", ScriptContext.GLOBAL_SCOPE));88}8990@Test91public void setAttributeNullName() {92testAndExpectNPE(sc -> sc.setAttribute(null, "value", ScriptContext.GLOBAL_SCOPE));93}9495@Test96public void getAttributesScopeEmptyName() {97testAndExpectIAE(sc -> sc.getAttributesScope(""));98}99100@Test101public void getAttributesScopeNullName() {102testAndExpectNPE(sc -> sc.getAttributesScope(null));103}104}105106107