diff options
Diffstat (limited to 'sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino')
5 files changed, 457 insertions, 0 deletions
diff --git a/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/Foo.java b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/Foo.java new file mode 100644 index 0000000000..397501a23b --- /dev/null +++ b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/Foo.java @@ -0,0 +1,34 @@ +/* + * 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.javascript.rhino; + +class Foo { + private String s; + + public Foo() { + } + + public String getS() { + return s; + } + + public void setS(String s) { + this.s = s; + } +} diff --git a/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoFunctionInvokerTestCase.java b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoFunctionInvokerTestCase.java new file mode 100644 index 0000000000..dad61659f3 --- /dev/null +++ b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoFunctionInvokerTestCase.java @@ -0,0 +1,174 @@ +/* + * 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.javascript.rhino; + +import java.io.IOException; + +import junit.framework.TestCase; + +import org.apache.axiom.om.OMElement; +import org.apache.xmlbeans.XmlException; +import org.apache.xmlbeans.XmlObject; + +/** + * Tests for the RhinoFunctionInvoker + */ +public class RhinoFunctionInvokerTestCase extends TestCase { + + public RhinoFunctionInvokerTestCase() { + } + + protected void setUp() throws Exception { + super.setUp(); + } + + public void testNoArgsInvoke() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getPetra() {return 'petra';}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getPetra"); + assertNotNull(invoker); + assertEquals("petra", invoker.invoke(null)); + } + + public void testOneArgInvoke() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getS(s) {return s;}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getS"); + assertNotNull(invoker); + assertEquals("petra", invoker.invoke(new Object[] { "petra" })); + } + + public void testMultiArgsInvoke() { + RhinoScript rhinoScript = new RhinoScript("foo", "function concat(x, y) {return x + y}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("concat"); + assertNotNull(invoker); + assertEquals("petrasue", invoker.invoke(new Object[] { "petra", "sue" })); + } + + public void testNoResponseInvoke() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getNull() {}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getNull"); + assertNotNull(invoker); + assertEquals(null, invoker.invoke(new Object[0])); + } + + public void testNullResponseInvoke() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getNull() {return null;}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getNull"); + assertNotNull(invoker); + assertEquals(null, invoker.invoke(new Object[0])); + } + + public void testResponseTypeDefaultString() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getTrue() {return true;}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getTrue"); + assertNotNull(invoker); + Object o = invoker.invoke(new Object[0]); + assertTrue(o instanceof String); + assertEquals("true", o); + } + + public void testResponseTypeBoolean() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getTrue() {return true;}"); + //rhinoScript.setResponseClass("getTrue", Boolean.class); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getTrue", Boolean.class); + assertNotNull(invoker); + assertTrue((Boolean) invoker.invoke(new Object[0])); + } + + public void testResponseTypeStringArray() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getAs() {var as = new Array(1);as[0]='petra';return as;}"); + //rhinoScript.setResponseClass("getAs", new String[0].getClass()); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getAs", new String[0].getClass()); + assertNotNull(invoker); + Object o = invoker.invoke(new Object[0]); + assertNotNull(o); + assertTrue(o.getClass().isArray()); + assertEquals("petra", ((Object[]) o)[0]); + } + + public void testResponseTypeBooleanArray() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getBs() {var bs = new Array(1);bs[0]=true;return bs;}"); + rhinoScript.setResponseClass("getBs", new Boolean[0].getClass()); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getBs", new Boolean[0].getClass()); + assertNotNull(invoker); + Object o = invoker.invoke(new Object[0]); + assertNotNull(o); + assertTrue(o.getClass().isArray()); + assertTrue(((Boolean[]) o)[0]); + } + + public void testRequestCustomType() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getFooS(foo) {return foo.getS();}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + 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() { + RhinoScript rhinoScript = new RhinoScript("foo", + "importClass(Packages.org.apache.tuscany.container.javascript.rhino.Foo);function getFoo(s) {var foo = new Foo(); foo.setS(s);return foo;}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getFoo"); + assertNotNull(invoker); + + Object o = invoker.invoke(new Object[] { "petra" }); + assertNotNull(o); + assertEquals("petra", ((Foo) o).getS()); + } + + public void testXMLRequest() throws XmlException, IOException { + RhinoScript rhinoScript = new RhinoScript("foo", "function isXML(x) {return 'xml' == (typeof x);}"); + //rhinoScript.setResponseClass("isXML", Boolean.class); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("isXML", Boolean.class); + assertNotNull(invoker); + + Object xml = XmlObject.Factory.parse("<a><b/></a>"); + assertTrue((Boolean) invoker.invoke(new Object[] { xml })); + + Object notXML = "notXML"; + assertFalse((Boolean) invoker.invoke(new Object[] { notXML })); + } + + public void testXMLResponse() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getXML(s) {return <a> { s } </a>;}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getXML"); + assertNotNull(invoker); + + Object xml = invoker.invoke(new Object[] { "petra" }); + assertNotNull(xml); + assertTrue(xml instanceof OMElement); + assertEquals("<a>petra</a>", ((OMElement) xml).toString()); + } + +} diff --git a/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoSCAConfigTestCase.java b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoSCAConfigTestCase.java new file mode 100644 index 0000000000..8975dbaeac --- /dev/null +++ b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoSCAConfigTestCase.java @@ -0,0 +1,116 @@ +/* + * 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.javascript.rhino; + +import junit.framework.TestCase; + +import org.apache.tuscany.spi.model.Scope; + +public class RhinoSCAConfigTestCase extends TestCase { + + public void testJavaInteface() { + RhinoScript rs = new RhinoScript("javaInterface", "SCA = { javaInterface : 'Test' }"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals("Test", scaConfig.getJavaInterface()); + } + + public void testWSDLLocation() { + RhinoScript rs = new RhinoScript("wsdlLocation", "SCA = { wsdlLocation : 'Test' }"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals("Test", scaConfig.getWSDLLocation()); + } + + public void testWSDLNamespace() { + RhinoScript rs = new RhinoScript("wsdlNamespace", "SCA = { wsdlNamespace : 'Test' }"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals("Test", scaConfig.getWSDLNamespace()); + } + + public void testWSDLPortType() { + RhinoScript rs = new RhinoScript("wsdlPortType", "SCA = { wsdlPortType : 'Test' }"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals("Test", scaConfig.getWSDLPortType()); + } + + public void testScopeStateless() { + RhinoScript rs = new RhinoScript("testScopeStateless", "SCA = { scope : 'stateless', javaInterface : 'helloworld.HelloWorldService'}"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals(Scope.STATELESS, scaConfig.getScope()); + } + + public void testScopeComposite() { + RhinoScript rs = new RhinoScript("testScopeComposite", "SCA = { scope : 'composite', javaInterface : 'helloworld.HelloWorldService'}"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals(Scope.COMPOSITE, scaConfig.getScope()); // TODO + } + + public void testScopeConversational() { + RhinoScript rs = new RhinoScript("testScopeConversational", "SCA = { scope : 'conversational', javaInterface : 'helloworld.HelloWorldService'}"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals(Scope.SESSION, scaConfig.getScope()); + } + + public void testScopeRequest() { + RhinoScript rs = new RhinoScript("testScopeRequest", "SCA = { scope : 'request', javaInterface : 'helloworld.HelloWorldService'}"); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(scaConfig.hasSCAConfig()); + assertEquals(Scope.REQUEST, scaConfig.getScope()); + } + + public void testBadScope() { + RhinoScript rs = new RhinoScript("testScopeStateless", "SCA = { scope : 'rubbish', javaInterface : 'helloworld.HelloWorldService'}"); + try { + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + fail("expecting bad scope exception: " + scaConfig); + } catch (IllegalArgumentException e) { + } + } + + public void testNoConfig() { + RhinoScript rs = new RhinoScript("javaInterface", ""); + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + assertTrue(!scaConfig.hasSCAConfig()); + } + + public void testNoService() { + RhinoScript rs = new RhinoScript("javaInterface", "SCA = {}"); + try { + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + fail("expecting no service exception: " + scaConfig); + } catch (IllegalArgumentException e) { + } + } + + public void testDupicateInteface() { + RhinoScript rs = new RhinoScript("javaInterface", "SCA = { javaInterface : 'Test', wsdlLocation : 'Test' }"); + try { + RhinoSCAConfig scaConfig = rs.getSCAConfig(); + fail("expecting multiple service exception: " + scaConfig); + } catch (IllegalArgumentException e) { + } + } + +} diff --git a/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoScriptInstanceTestCase.java b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoScriptInstanceTestCase.java new file mode 100644 index 0000000000..76d6c7c49d --- /dev/null +++ b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoScriptInstanceTestCase.java @@ -0,0 +1,50 @@ +/* + * 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.javascript.rhino; + +import junit.framework.TestCase; + +/** + * Tests for the RhinoScriptInstance + */ +public class RhinoScriptInstanceTestCase extends TestCase { + + public RhinoScriptInstanceTestCase() { + + } + + protected void setUp() throws Exception { + super.setUp(); + } + + public void testInvokeFunction() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getPetra() {return 'petra';}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + assertEquals("petra", instance.invokeFunction("getPetra", new Object[0])); + } + + public void testCreateRhinoFunctionInvoker() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getPetra() {return 'petra';}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + RhinoFunctionInvoker invoker = instance.createRhinoFunctionInvoker("getPetra"); + assertNotNull(invoker); + assertEquals("petra", invoker.invoke(new Object[0])); + } + +} diff --git a/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoScriptTestCase.java b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoScriptTestCase.java new file mode 100644 index 0000000000..ab684c39ce --- /dev/null +++ b/sandbox/old/contrib/implementation-javascript/container/src/test/java/org/apache/tuscany/container/javascript/rhino/RhinoScriptTestCase.java @@ -0,0 +1,83 @@ +/* + * 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.javascript.rhino; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +/** + * Tests for the RhinoScript + */ +public class RhinoScriptTestCase extends TestCase { + + public RhinoScriptTestCase() { + + } + + protected void setUp() throws Exception { + super.setUp(); + } + + public void testSimpleConstructor() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getPetra() {return 'petra';}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + assertEquals("petra", instance.invokeFunction("getPetra", new Object[0])); + } + + public void testFullConstructor() { + ClassLoader cl = getClass().getClassLoader(); + Map<String, Object> contexts = new HashMap<String, Object>(); + contexts.put("name", "petra"); + RhinoScript rhinoScript = new RhinoScript("foo", "function getName() {return name;}", contexts, cl); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + assertEquals("petra", instance.invokeFunction("getName", new Object[0])); + } + + public void testCreateInstance() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getPetra() {return 'petra';}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + assertNotNull(instance); + } + + public void testCreateInstanceWithContext() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getName() {return name;}"); + Map<String, Object> contexts = new HashMap<String, Object>(); + contexts.put("name", "petra"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(contexts); + assertEquals("petra", instance.invokeFunction("getName", new Object[0])); + } + + public void testDefaultResponseType() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getX() {return 42;}"); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + assertEquals("42", instance.invokeFunction("getX", new Object[0])); + } + + public void testSetResponseType() { + RhinoScript rhinoScript = new RhinoScript("foo", "function getX() {return 42;}"); + rhinoScript.setResponseClass("getX", Integer.class); + RhinoScriptInstance instance = rhinoScript.createRhinoScriptInstance(); + Object x = instance.invokeFunction("getX", new Object[0], Integer.class); + assertTrue(x instanceof Integer); + assertEquals(new Integer(42), x); + } + +} |