The spec
http://docs.oracle.com/javase/8/docs/api/javax/script/ScriptContext.html
asserts plenty of IAE/NPE exception throwing for getter/setter methods, however this is not completely fulfilled by the state/instance of SimpleScriptContext used by NashornScriptEngine; the following code will produce no exceptions:
new ScriptEngineManager()
.getEngineFactories()
.stream()
.map(ScriptEngineFactory::getScriptEngine)
.map(engine -> {
System.err.println("engine: " + engine);
return engine.getContext();
})
.forEach(c -> {
System.err.println("context: " + c);
c.getAttribute("");
c.getAttribute(null);
c.getAttribute("", ScriptContext.ENGINE_SCOPE);
c.getAttribute(null, ScriptContext.ENGINE_SCOPE);
c.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
c.removeAttribute("", ScriptContext.ENGINE_SCOPE);
c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
c.getAttributesScope("");
c.getAttributesScope(null);
});
However if SimpleScriptContext instance is created manually the expected exceptions are thrown - any of the following lines WILL cause the expected NPE or IAE:
new SimpleScriptContext().getAttribute("");
new SimpleScriptContext().getAttribute(null);
new SimpleScriptContext().getAttribute("", ScriptContext.ENGINE_SCOPE);
new SimpleScriptContext().getAttribute(null, ScriptContext.ENGINE_SCOPE);
new SimpleScriptContext().removeAttribute(null, ScriptContext.ENGINE_SCOPE);
new SimpleScriptContext().removeAttribute("", ScriptContext.ENGINE_SCOPE);
new SimpleScriptContext().setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
new SimpleScriptContext().setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
new SimpleScriptContext().getAttributesScope("");
new SimpleScriptContext().getAttributesScope(null);
---
The following JCK9 tests will fail due to the problem with Nashorn:
api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_NPE]
api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_noScope_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_noScope_NPE]
api/javax_script/ScriptContext/index.html#Exceptions[getAttributesScope_noScope_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[getAttributesScope_noScope_NPE]
api/javax_script/ScriptContext/index.html#Exceptions[removeAttribute_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[removeAttribute_NPE]
api/javax_script/ScriptContext/index.html#Exceptions[setAttribute_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[setAttribute_NPE]
Please note these tests are not targeted or specific for Nashorn - they check any implementation found on a particular JavaSE platform and they fail for Oracle JDK.