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