summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.jsr223
diff options
context:
space:
mode:
authordims <dims@13f79535-47bb-0310-9956-ffa450edef68>2008-06-17 00:23:01 +0000
committerdims <dims@13f79535-47bb-0310-9956-ffa450edef68>2008-06-17 00:23:01 +0000
commitbdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch)
tree38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/ant/container.jsr223
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/ant/container.jsr223')
-rw-r--r--sandbox/ant/container.jsr223/pom.xml83
-rw-r--r--sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java81
-rw-r--r--sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.java50
-rw-r--r--sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java75
-rw-r--r--sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/default.scdl40
-rw-r--r--sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl40
-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
10 files changed, 697 insertions, 0 deletions
diff --git a/sandbox/ant/container.jsr223/pom.xml b/sandbox/ant/container.jsr223/pom.xml
new file mode 100644
index 0000000000..89450eee2e
--- /dev/null
+++ b/sandbox/ant/container.jsr223/pom.xml
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<project>
+
+ <parent>
+ <groupId>org.apache.tuscany.sca.services.containers</groupId>
+ <artifactId>parent</artifactId>
+ <version>1.0-incubator-M2-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>container-jsr223</artifactId>
+ <name>Apache Tuscany Script Container using JSR-223</name>
+ <description>Apache Tuscany Script Container using JSR-223</description>
+
+ <repositories>
+ <repository>
+ <!-- Hmm, there must be a better way to get this -->
+ <id>jsr223</id>
+ <url>http://dist.codehaus.org/mule/dependencies/maven2</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca.services.containers</groupId>
+ <artifactId>easy-container</artifactId>
+ <version>${sca.version}</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>script</groupId>
+ <artifactId>jsr223</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>test</artifactId>
+ <version>${sca.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymockclassextension</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>rhino</groupId>
+ <artifactId>js</artifactId>
+ <version>1.6R2</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
diff --git a/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java
new file mode 100644
index 0000000000..950820fbcd
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java
@@ -0,0 +1,81 @@
+/*
+ * 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.osoa.sca.Version.XML_NAMESPACE_1_0;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.container.easy.EasyImplementation;
+import org.apache.tuscany.container.easy.EasyImplementationLoader;
+import org.apache.tuscany.spi.annotation.Autowire;
+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.LoaderUtil;
+import org.apache.tuscany.spi.loader.MissingResourceException;
+import org.osoa.sca.annotations.Constructor;
+
+/**
+ * Loader for handling implementation.script elements.
+ *
+ * <implementation.script script="path/foo.py" class="myclass">
+ *
+ */
+public class ScriptImplementationLoader extends EasyImplementationLoader {
+
+ private static final QName IMPLEMENTATION_SCRIPT = new QName(XML_NAMESPACE_1_0, "implementation.script");
+
+ @Constructor( { "registry" })
+ public ScriptImplementationLoader(@Autowire LoaderRegistry registry) {
+ super(registry);
+ }
+
+ public QName getXMLType() {
+ return IMPLEMENTATION_SCRIPT;
+ }
+
+ @Override
+ public EasyImplementation load(CompositeComponent parent, XMLStreamReader reader, DeploymentContext deploymentContext) throws XMLStreamException, LoaderException {
+ String scriptName = reader.getAttributeValue(null, "script");
+ if (scriptName == null) {
+ throw new MissingResourceException("implementation element has no 'script' attribute");
+ }
+
+ String className = reader.getAttributeValue(null, "class");
+
+ LoaderUtil.skipToEndElement(reader);
+
+ ClassLoader cl = deploymentContext.getClassLoader();
+ String scriptSource = loadSource(cl, scriptName);
+
+ ScriptInstanceFactory instanceFactory = new ScriptInstanceFactory(scriptName, className, scriptSource, cl);
+
+ EasyImplementation implementation = new EasyImplementation();
+ implementation.setResourceName(scriptName);
+ implementation.setScriptInstanceFactory(instanceFactory);
+
+ registry.loadComponentType(parent, implementation, deploymentContext);
+
+ return implementation;
+ }
+}
diff --git a/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.java b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.java
new file mode 100644
index 0000000000..67a380f0d8
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.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.jsr223;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.script.Invocable;
+
+import org.apache.tuscany.container.easy.EasyInstance;
+
+/**
+ * An invokable instance of a script
+ *
+ * Basically just a wrapper around a BSF engine with an optional script class object.
+ */
+public class ScriptInstance implements EasyInstance {
+
+ protected Invocable invocableEngine;
+
+ public ScriptInstance(Invocable invocableEngine) {
+ this.invocableEngine = invocableEngine;
+ }
+
+ public Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException {
+ try {
+
+ return invocableEngine.call(operationName, args);
+
+ } catch (Exception e) {
+ throw new InvocationTargetException(e);
+ }
+ }
+
+}
diff --git a/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java
new file mode 100644
index 0000000000..11e2728fd3
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java
@@ -0,0 +1,75 @@
+/*
+ * 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.util.List;
+import java.util.Map;
+
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.tuscany.container.easy.EasyInstanceFactory;
+import org.apache.tuscany.spi.ObjectCreationException;
+
+/**
+ * ScriptInstanceFactory creates ScriptInstances for a script
+ */
+public class ScriptInstanceFactory extends EasyInstanceFactory<ScriptInstance>{
+
+ private String className;
+
+ private String scriptSource;
+
+ public ScriptInstanceFactory(String resourceName, String className, String scriptSource, ClassLoader classLoader) {
+ super(resourceName, classLoader);
+ this.className = className;
+ this.scriptSource = scriptSource;
+ }
+
+ /**
+ * Create a new invokeable instance of the script
+ *
+ * @param context
+ * objects to add to scope of the script instance
+ * @return a RhinoScriptInstance
+ */
+ public ScriptInstance createInstance(List<Class> services, Map<String, Object> context) {
+ try {
+
+ ScriptEngineManager engineMgr = new ScriptEngineManager();
+ ScriptEngine engine = engineMgr.getEngineByExtension(getScriptExtension());
+ engine.eval(scriptSource);
+ return new ScriptInstance((Invocable) engine);
+
+ } catch (ScriptException e) {
+ throw new ObjectCreationException(e);
+ }
+ }
+
+ protected String getScriptExtension() {
+ int lastDot = resourceName.lastIndexOf('.');
+ if (lastDot == -1) {
+ return null;
+ }
+ return resourceName.substring(lastDot+1);
+ }
+
+}
diff --git a/sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/default.scdl b/sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/default.scdl
new file mode 100644
index 0000000000..7d9ef5a64e
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/default.scdl
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<!--
+ JavaScript configuration for the launcher environment.
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
+ xmlns:system="http://tuscany.apache.org/xmlns/system/1.0-SNAPSHOT"
+
+ name="org.apache.tuscany.container.jsr223.ScriptImplementation">
+
+ <component name="jsr223.implementationLoader">
+ <system:implementation.system class="org.apache.tuscany.container.jsr223.ScriptImplementationLoader"/>
+ </component>
+
+ <component name="jsr223.componentTypeLoader">
+ <system:implementation.system class="org.apache.tuscany.container.easy.EasyComponentTypeLoader"/>
+ </component>
+
+ <component name="jsr223.componentBuilder">
+ <system:implementation.system class="org.apache.tuscany.container.easy.EasyComponentBuilder"/>
+ </component>
+
+</composite>
diff --git a/sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl b/sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl
new file mode 100644
index 0000000000..7d9ef5a64e
--- /dev/null
+++ b/sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<!--
+ JavaScript configuration for the launcher environment.
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
+ xmlns:system="http://tuscany.apache.org/xmlns/system/1.0-SNAPSHOT"
+
+ name="org.apache.tuscany.container.jsr223.ScriptImplementation">
+
+ <component name="jsr223.implementationLoader">
+ <system:implementation.system class="org.apache.tuscany.container.jsr223.ScriptImplementationLoader"/>
+ </component>
+
+ <component name="jsr223.componentTypeLoader">
+ <system:implementation.system class="org.apache.tuscany.container.easy.EasyComponentTypeLoader"/>
+ </component>
+
+ <component name="jsr223.componentBuilder">
+ <system:implementation.system class="org.apache.tuscany.container.easy.EasyComponentBuilder"/>
+ </component>
+
+</composite>
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();
+ }
+}