summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/FunctionTestCase.java39
-rw-r--r--sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoaderTestCase.java133
-rw-r--r--sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactoryTestCase.java91
-rw-r--r--sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceTestCase.java65
4 files changed, 328 insertions, 0 deletions
diff --git a/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/FunctionTestCase.java b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/FunctionTestCase.java
new file mode 100644
index 0000000000..d7ce7399a1
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/FunctionTestCase.java
@@ -0,0 +1,39 @@
+/*
+ * 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.jsr223;
+
+import java.lang.reflect.InvocationTargetException;
+
+import junit.framework.TestCase;
+
+public class FunctionTestCase extends TestCase {
+
+ public void testBasicInvoke() {
+ String src = "function foo(s) { return 'foo ' + s; }";
+ ScriptInstanceFactory factory = new ScriptInstanceFactory("test.js", null, src, null);
+ ScriptInstance inst = factory.createInstance(null, null);
+ Object o;
+ try {
+ o = inst.invokeTarget("foo", new Object[] { "petra" });
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ assertEquals("foo petra", o);
+ }
+}
diff --git a/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoaderTestCase.java b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoaderTestCase.java
new file mode 100644
index 0000000000..4ece8956ab
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoaderTestCase.java
@@ -0,0 +1,133 @@
+/*
+ * 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.jsr223;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.createMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+import static org.osoa.sca.Version.XML_NAMESPACE_1_0;
+
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.container.jsr223.ScriptImplementationLoader;
+import org.apache.tuscany.spi.component.CompositeComponent;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.loader.LoaderException;
+import org.apache.tuscany.spi.loader.LoaderRegistry;
+import org.apache.tuscany.spi.loader.MissingResourceException;
+
+/**
+ *
+ */
+public class ScriptImplementationLoaderTestCase extends TestCase {
+ private CompositeComponent parent;
+
+ private XMLStreamReader reader;
+
+ private DeploymentContext deploymentContext;
+
+ private ClassLoader classLoader;
+
+ private LoaderRegistry registry;
+
+ private ScriptImplementationLoader loader;
+
+ public void testLoadNoScriptAttribute() throws LoaderException, XMLStreamException {
+ expect(reader.getAttributeValue(null, "script")).andReturn(null);
+ replay(reader);
+ replay(deploymentContext);
+
+ try {
+ loader.load(parent, reader, deploymentContext);
+ fail();
+ } catch (MissingResourceException e) {
+ // ok
+ }
+ verify(reader);
+ verify(deploymentContext);
+ }
+
+ public void testLoad() throws LoaderException, XMLStreamException {
+ expect(reader.getAttributeValue(null, "script")).andReturn("foo.mock");
+ expect(reader.getAttributeValue(null, "class")).andReturn(null);
+ expect(reader.next()).andReturn(XMLStreamConstants.END_ELEMENT);
+ expect(deploymentContext.getClassLoader()).andReturn(classLoader);
+
+ replay(reader);
+ replay(deploymentContext);
+
+ ScriptImplementationLoader mockLoader = new ScriptImplementationLoader(registry) {
+ protected String loadSource(ClassLoader cl, String resource) throws LoaderException {
+ assertSame(classLoader, cl);
+ assertEquals("foo.mock", resource);
+ return "bar";
+ }
+ };
+ mockLoader.load(parent, reader, deploymentContext);
+ verify(reader);
+ verify(deploymentContext);
+ }
+
+ public void testLoadNoScriptPresent() throws LoaderException, XMLStreamException {
+ expect(reader.getAttributeValue(null, "script")).andReturn("foo.py");
+ expect(reader.getAttributeValue(null, "class")).andReturn(null);
+ expect(reader.next()).andReturn(XMLStreamConstants.END_ELEMENT);
+ expect(deploymentContext.getClassLoader()).andReturn(classLoader);
+
+ replay(reader);
+ replay(deploymentContext);
+
+ ScriptImplementationLoader mockLoader = new ScriptImplementationLoader(registry) {
+ protected String loadSource(ClassLoader cl, String resource) throws LoaderException {
+ assertSame(classLoader, cl);
+ assertEquals("foo.py", resource);
+ throw new MissingResourceException(resource);
+ }
+ };
+ try {
+ mockLoader.load(parent, reader, deploymentContext);
+ fail();
+ } catch (MissingResourceException e) {
+ assertEquals("foo.py", e.getMessage());
+ }
+ verify(reader);
+ verify(deploymentContext);
+ }
+
+ public void testGetXMLType() throws LoaderException {
+ assertEquals(XML_NAMESPACE_1_0, loader.getXMLType().getNamespaceURI());
+ assertEquals("implementation.script", loader.getXMLType().getLocalPart());
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ registry = createMock(LoaderRegistry.class);
+ loader = new ScriptImplementationLoader(registry);
+
+ parent = createMock(CompositeComponent.class);
+ reader = createMock(XMLStreamReader.class);
+ deploymentContext = createMock(DeploymentContext.class);
+ classLoader = createMock(ClassLoader.class);
+ }
+}
diff --git a/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactoryTestCase.java b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactoryTestCase.java
new file mode 100644
index 0000000000..6ec5b54285
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactoryTestCase.java
@@ -0,0 +1,91 @@
+/*
+ * 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.jsr223;
+
+import java.lang.reflect.InvocationTargetException;
+
+import junit.framework.TestCase;
+
+public class ScriptInstanceFactoryTestCase extends TestCase {
+
+// public void testCreateInstance() throws InvocationTargetException {
+// BSFManager.registerScriptingEngine("mock", MockBSFEngine.class.getName(), new String[] {"mock"});
+// ScriptInstanceFactory factory = new ScriptInstanceFactory("foo.mock", "bar", "baz", getClass().getClassLoader());
+// Map<String, Object> context = new HashMap<String, Object>();
+// context.put("foo", "bar");
+// ScriptInstance instance = factory.createInstance(null, context);
+// assertNotNull(instance);
+// assertNotNull(instance.bsfEngine);
+//// assertNotNull(instance.clazz);
+// }
+//
+// public void testCreateInstanceNoClass() throws InvocationTargetException {
+// BSFManager.registerScriptingEngine("mock", MockBSFEngine.class.getName(), new String[] {"mock"});
+// ScriptInstanceFactory factory = new ScriptInstanceFactory("foo.mock", null, "baz", getClass().getClassLoader());
+// Map<String, Object> context = new HashMap<String, Object>();
+// context.put("foo", "bar");
+// ScriptInstance instance = factory.createInstance(null, context);
+// assertNotNull(instance);
+// assertNotNull(instance.bsfEngine);
+// }
+//
+// public void testCreateInstanceRuby() throws InvocationTargetException {
+// BSFManager.registerScriptingEngine("ruby", MockBSFEngine.class.getName(), new String[] {"mock"});
+// ScriptInstanceFactory factory = new ScriptInstanceFactory("foo.mock", "bar", "baz", getClass().getClassLoader());
+// Map<String, Object> context = new HashMap<String, Object>();
+// context.put("foo", "bar");
+// ScriptInstance instance = factory.createInstance(null, context);
+// assertNotNull(instance);
+// assertNotNull(instance.bsfEngine);
+//// assertNotNull(instance.clazz);
+// }
+//
+// public void testBadCreateInstance() throws InvocationTargetException {
+// ScriptInstanceFactory factory = new ScriptInstanceFactory("foo", "bar", "baz", getClass().getClassLoader());
+// Map<String, Object> context = new HashMap<String, Object>();
+// try {
+// factory.createInstance(null, context);
+// fail();
+// } catch (ObjectCreationException e) {
+// // expected
+// }
+// }
+//
+ public void testGetters() throws InvocationTargetException {
+ ScriptInstanceFactory factory = new ScriptInstanceFactory("foo", "bar", "baz", getClass().getClassLoader());
+// assertEquals("foo", factory.getScriptName());
+// assertEquals("bar", factory.getClassName());
+// assertEquals("baz", factory.getScriptSource());
+ assertEquals(getClass().getClassLoader(), factory.getClassLoader());
+ }
+
+ public void testGetScriptExtension() throws InvocationTargetException {
+ ScriptInstanceFactory factory = new ScriptInstanceFactory("x.js", null, null, null);
+ assertEquals("js", factory.getScriptExtension());
+ }
+
+ public void testNullGetScriptExtension() throws InvocationTargetException {
+ ScriptInstanceFactory factory = new ScriptInstanceFactory("x", null, null, null);
+ assertEquals(null, factory.getScriptExtension());
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+}
diff --git a/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceTestCase.java b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceTestCase.java
new file mode 100644
index 0000000000..bd38e02c2f
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceTestCase.java
@@ -0,0 +1,65 @@
+/*
+ * 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.jsr223;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.createMock;
+import static org.easymock.classextension.EasyMock.replay;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.script.Invocable;
+import javax.script.ScriptException;
+
+import junit.framework.TestCase;
+
+import org.easymock.IAnswer;
+
+public class ScriptInstanceTestCase extends TestCase {
+
+ public void testInvokeTarget() throws InvocationTargetException, ScriptException, NoSuchMethodException {
+ Invocable invocable = createMock(Invocable.class);
+ expect(invocable.call("foo", null)).andReturn("foo petra");
+ replay(invocable);
+ ScriptInstance instance = new ScriptInstance(invocable);
+ assertEquals("foo petra", instance.invokeTarget("foo", null));
+ }
+
+ public void testInvokeTargetException() throws InvocationTargetException, ScriptException, NoSuchMethodException {
+ Invocable invocable = createMock(Invocable.class);
+ expect(invocable.call("foo", null)).andStubAnswer(new IAnswer<Object>() {
+ public Object answer() throws Throwable {
+ throw new RuntimeException("bang");
+ }
+ });
+ replay(invocable);
+ ScriptInstance instance = new ScriptInstance(invocable);
+ try {
+ instance.invokeTarget("foo", null);
+ fail();
+ } catch (InvocationTargetException e) {
+ assertEquals("bang", e.getCause().getMessage());
+ }
+
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+}