blob: 02b33553a261be82208ab5af71a48558fe56b6b6 [file] [log] [blame]
sundar9275b2a2015-05-18 18:57:35 +05301/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8072853
27 * @summary SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
28 * @run testng SimpleScriptContextNameChecksTest
29 */
30
31import java.util.List;
32import java.util.function.Consumer;
33import javax.script.*;
34import org.testng.annotations.Test;
35
36public class SimpleScriptContextNameChecksTest {
37 private List<ScriptEngineFactory> getFactories() {
38 return new ScriptEngineManager().getEngineFactories();
39 }
40
41 private void testAndExpect(Consumer<ScriptContext> c, Class<? extends RuntimeException> clazz) {
42 for (ScriptEngineFactory fac : getFactories()) {
43 ScriptContext sc = fac.getScriptEngine().getContext();
44 String name = fac.getEngineName();
45 try {
46 c.accept(sc);
47 throw new RuntimeException("no exception for " + name);
48 } catch (NullPointerException | IllegalArgumentException e) {
49 if (e.getClass() == clazz) {
50 System.out.println("got " + e + " as expected for " + name);
51 } else {
52 throw e;
53 }
54 }
55 }
56 }
57
58 private void testAndExpectIAE(Consumer<ScriptContext> c) {
59 testAndExpect(c, IllegalArgumentException.class);
60 }
61
62 private void testAndExpectNPE(Consumer<ScriptContext> c) {
63 testAndExpect(c, NullPointerException.class);
64 }
65
66 @Test
67 public void getAttributeEmptyName() {
68 testAndExpectIAE(sc -> sc.getAttribute("", ScriptContext.GLOBAL_SCOPE));
69 }
70
71 @Test
72 public void getAttributeNullName() {
73 testAndExpectNPE(sc -> sc.getAttribute(null, ScriptContext.GLOBAL_SCOPE));
74 }
75
76 @Test
77 public void removeAttributeEmptyName() {
78 testAndExpectIAE(sc -> sc.removeAttribute("", ScriptContext.GLOBAL_SCOPE));
79 }
80
81 @Test
82 public void removeAttributeNullName() {
83 testAndExpectNPE(sc -> sc.removeAttribute(null, ScriptContext.GLOBAL_SCOPE));
84 }
85
86 @Test
87 public void setAttributeEmptyName() {
88 testAndExpectIAE(sc -> sc.setAttribute("", "value", ScriptContext.GLOBAL_SCOPE));
89 }
90
91 @Test
92 public void setAttributeNullName() {
93 testAndExpectNPE(sc -> sc.setAttribute(null, "value", ScriptContext.GLOBAL_SCOPE));
94 }
95
96 @Test
97 public void getAttributesScopeEmptyName() {
98 testAndExpectIAE(sc -> sc.getAttributesScope(""));
99 }
100
101 @Test
102 public void getAttributesScopeNullName() {
103 testAndExpectNPE(sc -> sc.getAttributesScope(null));
104 }
105}