1 /*
   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 
  31 import java.util.List;
  32 import javax.script.*;
  33 import org.testng.annotations.Test;
  34 
  35 public class SimpleScriptContextNameChecksTest {
  36     private List<ScriptEngineFactory> getFactories() {
  37         return new ScriptEngineManager().getEngineFactories();
  38     }
  39 
  40     @Test
  41     public void getAttributeEmptyName() {
  42         for (ScriptEngineFactory fac : getFactories()) {
  43             ScriptContext sc = fac.getScriptEngine().getContext();
  44             String name = fac.getEngineName();
  45             try {
  46                 sc.getAttribute("", ScriptContext.GLOBAL_SCOPE);
  47                 throw new RuntimeException("no exception for " + name);
  48             } catch (IllegalArgumentException iae) {
  49                 System.out.println("got " + iae + " as expected for " + name);
  50             }
  51         }
  52     }
  53 
  54     @Test
  55     public void getAttributeNullName() {
  56         for (ScriptEngineFactory fac : getFactories()) {
  57             ScriptContext sc = fac.getScriptEngine().getContext();
  58             String name = fac.getEngineName();
  59             try {
  60                 sc.getAttribute(null, ScriptContext.GLOBAL_SCOPE);
  61                 throw new RuntimeException("no exception for " + name);
  62             } catch (NullPointerException npe) {
  63                 System.out.println("got " + npe + " as expected for " + name);
  64             }
  65         }
  66     }
  67 
  68     @Test
  69     public void removeAttributeEmptyName() {
  70         for (ScriptEngineFactory fac : getFactories()) {
  71             ScriptContext sc = fac.getScriptEngine().getContext();
  72             String name = fac.getEngineName();
  73             try {
  74                 sc.removeAttribute("", ScriptContext.GLOBAL_SCOPE);
  75                 throw new RuntimeException("no exception for " + name);
  76             } catch (IllegalArgumentException iae) {
  77                 System.out.println("got " + iae + " as expected for " + name);
  78             }
  79         }
  80     }
  81 
  82     @Test
  83     public void removeAttributeNullName() {
  84         for (ScriptEngineFactory fac : getFactories()) {
  85             ScriptContext sc = fac.getScriptEngine().getContext();
  86             String name = fac.getEngineName();
  87             try {
  88                 sc.removeAttribute(null, ScriptContext.GLOBAL_SCOPE);
  89                 throw new RuntimeException("no exception for " + name);
  90             } catch (NullPointerException npe) {
  91                 System.out.println("got " + npe + " as expected for " + name);
  92             }
  93         }
  94     }
  95 
  96     @Test
  97     public void setAttributeEmptyName() {
  98         for (ScriptEngineFactory fac : getFactories()) {
  99             ScriptContext sc = fac.getScriptEngine().getContext();
 100             String name = fac.getEngineName();
 101             try {
 102                 sc.setAttribute("", "value", ScriptContext.GLOBAL_SCOPE);
 103                 throw new RuntimeException("no exception for " + name);
 104             } catch (IllegalArgumentException iae) {
 105                 System.out.println("got " + iae + " as expected for " + name);
 106             }
 107         }
 108     }
 109 
 110     @Test
 111     public void setAttributeNullName() {
 112         for (ScriptEngineFactory fac : getFactories()) {
 113             ScriptContext sc = fac.getScriptEngine().getContext();
 114             String name = fac.getEngineName();
 115             try {
 116                 sc.setAttribute(null, "value", ScriptContext.GLOBAL_SCOPE);
 117                 throw new RuntimeException("no exception for " + name);
 118             } catch (NullPointerException npe) {
 119                 System.out.println("got " + npe + " as expected for " + name);
 120             }
 121         }
 122     }
 123 
 124     @Test
 125     public void getAttributesScopeEmptyName() {
 126         for (ScriptEngineFactory fac : getFactories()) {
 127             ScriptContext sc = fac.getScriptEngine().getContext();
 128             String name = fac.getEngineName();
 129             try {
 130                 sc.getAttributesScope("");
 131                 throw new RuntimeException("no exception for " + name);
 132             } catch (IllegalArgumentException iae) {
 133                 System.out.println("got " + iae + " as expected for " + name);
 134             }
 135         }
 136     }
 137 
 138     @Test
 139     public void getAttributesScopeNullName() {
 140         for (ScriptEngineFactory fac : getFactories()) {
 141             ScriptContext sc = fac.getScriptEngine().getContext();
 142             String name = fac.getEngineName();
 143             try {
 144                 sc.getAttributesScope(null);
 145                 throw new RuntimeException("no exception for " + name);
 146             } catch (NullPointerException npe) {
 147                 System.out.println("got " + npe + " as expected for " + name);
 148             }
 149         }
 150     }
 151 }