summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.rhino/src/test/java/org/apache/tuscany/container/rhino/RhinoFunctionInvokerTestCase.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sandbox/ant/container.rhino/src/test/java/org/apache/tuscany/container/rhino/RhinoFunctionInvokerTestCase.java160
1 files changed, 160 insertions, 0 deletions
diff --git a/sandbox/ant/container.rhino/src/test/java/org/apache/tuscany/container/rhino/RhinoFunctionInvokerTestCase.java b/sandbox/ant/container.rhino/src/test/java/org/apache/tuscany/container/rhino/RhinoFunctionInvokerTestCase.java
new file mode 100644
index 0000000000..132c602028
--- /dev/null
+++ b/sandbox/ant/container.rhino/src/test/java/org/apache/tuscany/container/rhino/RhinoFunctionInvokerTestCase.java
@@ -0,0 +1,160 @@
+/*
+ * 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.tuscany.container.rhino;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.container.rhino.mock.Foo;
+
+/**
+ * Tests for the RhinoFunctionInvoker
+ */
+public class RhinoFunctionInvokerTestCase extends TestCase {
+
+ public RhinoFunctionInvokerTestCase() {
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ public void testNoArgsInvoke() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getPetra() {return 'petra';}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, null);
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getPetra");
+ assertNotNull(invoker);
+ assertEquals("petra", invoker.invoke(null));
+ }
+
+ public void testOneArgInvoke() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getS(s) {return s;}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, null);
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getS");
+ assertNotNull(invoker);
+ assertEquals("petra", invoker.invoke(new Object[] { "petra" }));
+ }
+
+ public void testMultiArgsInvoke() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function concat(x, y) {return x + y}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("concat");
+ assertNotNull(invoker);
+ assertEquals("petrasue", invoker.invoke(new Object[] { "petra", "sue" }));
+ }
+
+ public void testNoResponseInvoke() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getNull() {}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getNull");
+ assertNotNull(invoker);
+ assertEquals(null, invoker.invoke(new Object[0]));
+ }
+
+ public void testNullResponseInvoke() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getNull() {return null;}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getNull");
+ assertNotNull(invoker);
+ assertEquals(null, invoker.invoke(new Object[0]));
+ }
+
+ public void testResponseTypeDefaultString() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getTrue() {return true;}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getTrue");
+ assertNotNull(invoker);
+ Object o = invoker.invoke(new Object[0]);
+ assertTrue(o instanceof String);
+ assertEquals("true", o);
+ }
+
+ public void testResponseTypeBoolean() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getTrue() {return true;}", getClass().getClassLoader());
+ instanceFactory.setResponseClass("getTrue", Boolean.class);
+ List<Class> services = new ArrayList<Class>();
+ services.add(TestTypes.class);
+ RhinoScriptInstance instance = instanceFactory.createInstance(services, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getTrue");
+ assertNotNull(invoker);
+ assertTrue((Boolean) invoker.invoke(new Object[0]));
+ }
+
+ interface TestTypes {
+ Boolean getTrue();
+ String[] getAs();
+ Boolean[] getBs();
+ }
+
+ public void testResponseTypeStringArray() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getAs() {var as = new Array(1);as[0]='petra';return as;}", getClass().getClassLoader());
+ instanceFactory.setResponseClass("getAs", new String[0].getClass());
+ List<Class> services = new ArrayList<Class>();
+ services.add(TestTypes.class);
+ RhinoScriptInstance instance = instanceFactory.createInstance(services, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getAs");
+ assertNotNull(invoker);
+ Object o = invoker.invoke(new Object[0]);
+ assertNotNull(o);
+ assertTrue(o.getClass().isArray());
+ assertEquals("petra", ((Object[]) o)[0]);
+ }
+
+ public void testResponseTypeBooleanArray() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getBs() {var bs = new Array(1);bs[0]=true;return bs;}", getClass().getClassLoader());
+ instanceFactory.setResponseClass("getBs", new Boolean[0].getClass());
+ List<Class> services = new ArrayList<Class>();
+ services.add(TestTypes.class);
+ RhinoScriptInstance instance = instanceFactory.createInstance(services, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getBs");
+ assertNotNull(invoker);
+ Object o = invoker.invoke(new Object[0]);
+ assertNotNull(o);
+ assertTrue(o.getClass().isArray());
+ assertTrue(((Boolean[]) o)[0]);
+ }
+
+ public void testRequestCustomType() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null, "function getFooS(foo) {return foo.getS();}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getFooS");
+ assertNotNull(invoker);
+
+ Foo foo = new Foo();
+ foo.setS("petra");
+ Object o = invoker.invoke(new Object[] { foo });
+ assertEquals(foo.getS(), o);
+ }
+
+ public void testResponseCustomType() {
+ RhinoScriptInstanceFactory instanceFactory = new RhinoScriptInstanceFactory("foo", null,
+ "importClass(Packages.org.apache.tuscany.container.rhino.mock.Foo);function getFoo(s) {var foo = new Foo(); foo.setS(s);return foo;}", getClass().getClassLoader());
+ RhinoScriptInstance instance = instanceFactory.createInstance(null, new HashMap<String, Object>());
+ RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getFoo");
+ assertNotNull(invoker);
+
+ Object o = invoker.invoke(new Object[] { "petra" });
+ assertNotNull(o);
+ assertEquals("petra", ((Foo) o).getS());
+ }
+
+}