Added more unit tests
diff --git a/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptContext.java b/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptContext.java
index bc83af8..ac1184d 100644
--- a/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptContext.java
+++ b/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptContext.java
@@ -32,22 +32,26 @@
private static final Integer[] SCOPES = {SlingScriptConstants.SLING_SCOPE, GLOBAL_SCOPE, ENGINE_SCOPE};
- private Bindings globalScope;
- private Bindings engineScope;
-
+ private Bindings globalScope = new SimpleBindings();
+ private Bindings engineScope = new SimpleBindings();
private Bindings slingScope = new SimpleBindings();
@Override
public void setBindings(final Bindings bindings, final int scope) {
+ if (bindings == null) {
+ throw new NullPointerException("None of the ScriptContext scopes accepts null bindings.");
+ }
switch (scope) {
- case SlingScriptConstants.SLING_SCOPE : this.slingScope = bindings;
+ case SlingScriptConstants.SLING_SCOPE :
+ this.slingScope = bindings;
break;
- case 100: if (bindings == null) throw new NullPointerException("Bindings for ENGINE scope is null");
+ case 100:
this.engineScope = bindings;
break;
- case 200: this.globalScope = bindings;
+ case 200:
+ this.globalScope = bindings;
break;
- default: throw new IllegalArgumentException("Invalid scope");
+ default: throw new IllegalArgumentException("Invalid scope.");
}
}
@@ -58,12 +62,12 @@
case 100: return this.engineScope;
case 200: return this.globalScope;
}
- throw new IllegalArgumentException("Invalid scope");
+ throw new IllegalArgumentException("Invalid scope.");
}
@Override
public void setAttribute(final String name, final Object value, final int scope) {
- if (name == null) throw new IllegalArgumentException("Name is null");
+ if (name == null) throw new IllegalArgumentException("Name is null.");
final Bindings bindings = getBindings(scope);
if (bindings != null) {
bindings.put(name, value);
@@ -71,26 +75,6 @@
}
@Override
- public Object getAttribute(final String name, final int scope) {
- if (name == null) throw new IllegalArgumentException("Name is null");
- final Bindings bindings = getBindings(scope);
- if (bindings != null) {
- return bindings.get(name);
- }
- return null;
- }
-
- @Override
- public Object removeAttribute(final String name, final int scope) {
- if (name == null) throw new IllegalArgumentException("Name is null");
- final Bindings bindings = getBindings(scope);
- if (bindings != null) {
- return bindings.remove(name);
- }
- return null;
- }
-
- @Override
public Object getAttribute(String name) {
if (name == null) throw new IllegalArgumentException("Name is null");
for (final int scope : SCOPES) {
@@ -106,8 +90,28 @@
}
@Override
+ public Object getAttribute(final String name, final int scope) {
+ if (name == null) throw new IllegalArgumentException("Name is null.");
+ final Bindings bindings = getBindings(scope);
+ if (bindings != null) {
+ return bindings.get(name);
+ }
+ return null;
+ }
+
+ @Override
+ public Object removeAttribute(final String name, final int scope) {
+ if (name == null) throw new IllegalArgumentException("Name is null.");
+ final Bindings bindings = getBindings(scope);
+ if (bindings != null) {
+ return bindings.remove(name);
+ }
+ return null;
+ }
+
+ @Override
public int getAttributesScope(String name) {
- if (name == null) throw new IllegalArgumentException("Name is null");
+ if (name == null) throw new IllegalArgumentException("Name is null.");
for (final int scope : SCOPES) {
if ((getBindings(scope) != null) && (getBindings(scope).containsKey(name))) {
return scope;
diff --git a/test/java/org/apache/sling/scripting/resolver/internal/BundledScriptContextTest.java b/test/java/org/apache/sling/scripting/resolver/internal/BundledScriptContextTest.java
new file mode 100644
index 0000000..04ebdef
--- /dev/null
+++ b/test/java/org/apache/sling/scripting/resolver/internal/BundledScriptContextTest.java
@@ -0,0 +1,151 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.resolver.internal;
+
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.SimpleBindings;
+
+import org.apache.sling.api.scripting.SlingScriptConstants;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class BundledScriptContextTest {
+
+ @Test
+ public void testSetAndGetBindings() {
+ BundledScriptContext bundledScriptContext = new BundledScriptContext();
+ Bindings engineScope = new SimpleBindings();
+ Bindings globalScope = new SimpleBindings();
+ Bindings slingScope = new SimpleBindings();
+
+ bundledScriptContext.setBindings(slingScope, SlingScriptConstants.SLING_SCOPE);
+ bundledScriptContext.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
+ bundledScriptContext.setBindings(engineScope, ScriptContext.ENGINE_SCOPE);
+
+ assertEquals(engineScope, bundledScriptContext.getBindings(ScriptContext.ENGINE_SCOPE));
+ assertEquals(globalScope, bundledScriptContext.getBindings(ScriptContext.GLOBAL_SCOPE));
+ assertEquals(slingScope, bundledScriptContext.getBindings(SlingScriptConstants.SLING_SCOPE));
+
+ boolean invalidScopeThrowsException;
+ try {
+ bundledScriptContext.getBindings(Integer.MIN_VALUE);
+ invalidScopeThrowsException = false;
+ } catch (Exception e) {
+ invalidScopeThrowsException = true;
+ }
+ assertTrue(BundledScriptContext.class.getName() + " should throw exceptions for invalid scopes.", invalidScopeThrowsException);
+
+ boolean acceptsAnyScope;
+ try {
+ bundledScriptContext.setBindings(new SimpleBindings(), Integer.MIN_VALUE);
+ acceptsAnyScope = true;
+ } catch (Exception e) {
+ acceptsAnyScope = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept bindings for arbitrary scopes.", acceptsAnyScope);
+
+ boolean acceptsNullBindings;
+ try {
+ bundledScriptContext.setBindings(null, ScriptContext.ENGINE_SCOPE);
+ acceptsNullBindings = true;
+ } catch (Exception e) {
+ acceptsNullBindings = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept null bindings.", acceptsNullBindings);
+ }
+
+ @Test
+ public void testSetAndGetAttribute() {
+ BundledScriptContext bundledScriptContext = new BundledScriptContext();
+ boolean acceptsSettingNullAttributeNames;
+ try {
+ bundledScriptContext.setAttribute(null, 0, ScriptContext.ENGINE_SCOPE);
+ acceptsSettingNullAttributeNames = true;
+ } catch (Exception e) {
+ acceptsSettingNullAttributeNames = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept null attribute names.", acceptsSettingNullAttributeNames);
+
+ boolean acceptsRetrievingNullAttributeNamesFromScope;
+ try {
+ bundledScriptContext.getAttribute(null, ScriptContext.ENGINE_SCOPE);
+ acceptsRetrievingNullAttributeNamesFromScope = true;
+ } catch (Exception e) {
+ acceptsRetrievingNullAttributeNamesFromScope = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept null attribute names.", acceptsRetrievingNullAttributeNamesFromScope);
+
+ boolean acceptsRetrievingNullAttributeNames;
+ try {
+ bundledScriptContext.getAttribute(null);
+ acceptsRetrievingNullAttributeNames = true;
+ } catch (Exception e) {
+ acceptsRetrievingNullAttributeNames = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept null attribute names.", acceptsRetrievingNullAttributeNames);
+
+ bundledScriptContext.setAttribute("nothing", 0, ScriptContext.ENGINE_SCOPE);
+ assertEquals(0, bundledScriptContext.getAttribute("nothing"));
+ assertEquals(0, bundledScriptContext.getAttribute("nothing", ScriptContext.ENGINE_SCOPE));
+ assertNull(bundledScriptContext.getAttribute("nothing", ScriptContext.GLOBAL_SCOPE));
+ }
+
+ @Test
+ public void testRemoveAttribute() {
+ BundledScriptContext bundledScriptContext = new BundledScriptContext();
+ boolean acceptsNullAttributeNames;
+ try {
+ bundledScriptContext.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
+ acceptsNullAttributeNames = true;
+ } catch (Exception e) {
+ acceptsNullAttributeNames = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept null attribute names.", acceptsNullAttributeNames);
+
+ bundledScriptContext.setAttribute("nothing", 0, ScriptContext.ENGINE_SCOPE);
+ assertEquals(0, bundledScriptContext.removeAttribute("nothing", ScriptContext.ENGINE_SCOPE));
+ Bindings engineScope = bundledScriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
+ assertTrue( engineScope != null && engineScope.size() == 0);
+ assertNull(bundledScriptContext.removeAttribute("nothing", ScriptContext.ENGINE_SCOPE));
+ assertNull(bundledScriptContext.removeAttribute("nothing", ScriptContext.GLOBAL_SCOPE));
+ }
+
+ @Test
+ public void testGetAttributesScope() {
+ BundledScriptContext bundledScriptContext = new BundledScriptContext();
+ boolean acceptsNullAttributeNames;
+ try {
+ bundledScriptContext.getAttributesScope(null);
+ acceptsNullAttributeNames = true;
+ } catch (Exception e) {
+ acceptsNullAttributeNames = false;
+ }
+ assertFalse(BundledScriptContext.class.getName() + " should not accept null attribute names.", acceptsNullAttributeNames);
+
+ bundledScriptContext.setAttribute("nothing", 0, SlingScriptConstants.SLING_SCOPE);
+ assertEquals(SlingScriptConstants.SLING_SCOPE, bundledScriptContext.getAttributesScope("nothing"));
+ assertEquals(-1, bundledScriptContext.getAttributesScope("nothing here"));
+ }
+
+}