summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java')
-rw-r--r--sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java186
1 files changed, 186 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java b/sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java
new file mode 100644
index 0000000000..19c0d9bcf6
--- /dev/null
+++ b/sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.tuscany.container.rhino.rhino;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.container.rhino.rhino.RhinoScript;
+import org.mozilla.javascript.EcmaError;
+
+/**
+ * Tests for the RhinoScript
+ */
+public class RhinoScriptTestCase extends TestCase {
+
+ private static final String scriptName = "RhinoScriptTestCase.js";
+
+ private String script;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.script = readResource(scriptName);
+ }
+
+ public void testSimpleInvocation() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ Object x = ri.invoke("echo", new Object[] { "petra" }, null);
+ assertEquals("petra", x);
+ }
+
+ public void testCopy() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ Object x = ri.invoke("echo", new Object[] { "petra" }, null);
+ assertEquals("petra", x);
+
+ ri = ri.copy();
+ x = ri.invoke("echo", new Object[] { "sue" }, null);
+ assertEquals("sue", x);
+
+ }
+
+ public void testContexts1() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ Map<String, Object> contexts = new HashMap<String, Object>();
+ contexts.put("a", "petra");
+ Object x = ri.invoke("getA", null, contexts);
+ assertEquals("petra", x);
+ }
+
+ /**
+ * Tests context not accessable across invocations
+ */
+ public void testContexts2() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ Map<String, Object> contexts = new HashMap<String, Object>();
+ contexts.put("a", "petra");
+ Object x = ri.invoke("getA", null, contexts);
+ assertEquals("petra", x);
+
+ try {
+ x = ri.invoke("getA", null, null);
+ assertTrue("expected ReferenceError", false);
+ } catch (EcmaError e) {
+ assertEquals("ReferenceError", e.getName());
+ }
+ }
+
+ /**
+ * Tests shared scope is accessable across invocations
+ */
+ public void testScopes1() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ ri.invoke("setGlobalVarY", new Object[] { "petra" }, null);
+
+ Object x = ri.invoke("getGlobalVarY", null, null);
+ assertEquals("petra", x);
+ }
+
+ /**
+ * Tests local vars are NOT accessable across invocations
+ */
+ public void testScopes2() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ ri.invoke("setLocalVarY", new Object[] { "petra" }, null);
+
+ try {
+ ri.invoke("getGlobalVarY", null, null);
+ assertTrue("expected ReferenceError", false);
+ } catch (EcmaError e) {
+ assertEquals("ReferenceError", e.getName());
+ }
+ }
+
+ /**
+ * Tests shared scope is accessable when using contexts (ie an wire scope)
+ */
+ public void testScopes3() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ ri.invoke("setGlobalVarY", new Object[] { "petra" }, null);
+
+ Map<String, Object> contexts = new HashMap<String, Object>();
+ contexts.put("a", "sue");
+ Object x = ri.invoke("getGlobalVarY", null, contexts);
+ assertEquals("petra", x);
+
+ x = ri.invoke("getA", null, contexts);
+ assertEquals("sue", x);
+
+ }
+
+ /**
+ * Tests a copy only retains the script scope not the shared scope
+ */
+ public void testScopes4() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ ri.invoke("setGlobalVarY", new Object[] { "petra" }, null);
+
+ ri = ri.copy();
+ try {
+ ri.invoke("getGlobalVarY", null, null);
+ assertTrue("expected ReferenceError", false);
+ } catch (EcmaError e) {
+ assertEquals("ReferenceError", e.getName());
+ }
+ try {
+ ri.invoke("getA", null, null);
+ assertTrue("expected ReferenceError", false);
+ } catch (EcmaError e) {
+ assertEquals("ReferenceError", e.getName());
+ }
+
+ }
+
+ public void testGetInt() {
+ RhinoScript ri = new RhinoScript(scriptName, script);
+ Object x = ri.invoke("getInt", null, Integer.TYPE, null);
+ assertEquals(Integer.class, x.getClass());
+ }
+
+ /**
+ * Read a resource into a String
+ */
+ private String readResource(String name) {
+ try {
+ URL url = getClass().getResource(name);
+ if (url == null) {
+ throw new RuntimeException("resource not found: " + name);
+ }
+ InputStream inputStream = url.openStream();
+
+ StringBuffer resource = new StringBuffer();
+ int n = 0;
+
+ while ((n = inputStream.read()) != -1) {
+ resource.append((char) n);
+ }
+
+ inputStream.close();
+
+ String s = resource.toString();
+ return s;
+
+ } catch (IOException e) {
+ throw new RuntimeException("IOException reading resource " + name, e);
+ }
+ }
+
+} \ No newline at end of file