From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- sandbox/ant/container.jsr223/pom.xml | 83 +++++++++++++ .../jsr223/ScriptImplementationLoader.java | 81 +++++++++++++ .../tuscany/container/jsr223/ScriptInstance.java | 50 ++++++++ .../container/jsr223/ScriptInstanceFactory.java | 75 ++++++++++++ .../src/main/resources/META-INF/sca/default.scdl | 40 +++++++ .../main/resources/META-INF/sca/script.system.scdl | 40 +++++++ .../tuscany/container/jsr223/FunctionTestCase.java | 39 ++++++ .../jsr223/ScriptImplementationLoaderTestCase.java | 133 +++++++++++++++++++++ .../jsr223/ScriptInstanceFactoryTestCase.java | 91 ++++++++++++++ .../container/jsr223/ScriptInstanceTestCase.java | 65 ++++++++++ 10 files changed, 697 insertions(+) create mode 100644 sandbox/ant/container.jsr223/pom.xml create mode 100644 sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoader.java create mode 100644 sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstance.java create mode 100644 sandbox/ant/container.jsr223/src/main/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactory.java create mode 100644 sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/default.scdl create mode 100644 sandbox/ant/container.jsr223/src/main/resources/META-INF/sca/script.system.scdl create mode 100644 sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/FunctionTestCase.java create mode 100644 sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptImplementationLoaderTestCase.java create mode 100644 sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceFactoryTestCase.java create mode 100644 sandbox/ant/container.jsr223/src/test/java/org/apache/tuscany/container/jsr223/ScriptInstanceTestCase.java (limited to 'sandbox/ant/container.jsr223') 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 @@ + + + + + + org.apache.tuscany.sca.services.containers + parent + 1.0-incubator-M2-SNAPSHOT + + + 4.0.0 + container-jsr223 + Apache Tuscany Script Container using JSR-223 + Apache Tuscany Script Container using JSR-223 + + + + + jsr223 + http://dist.codehaus.org/mule/dependencies/maven2 + + + + + + + org.apache.tuscany.sca.services.containers + easy-container + ${sca.version} + compile + + + + script + jsr223 + 1.0 + provided + + + + org.apache.tuscany.sca + test + ${sca.version} + test + + + + org.easymock + easymock + + + + org.easymock + easymockclassextension + + + + rhino + js + 1.6R2 + test + + + + + 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. + * + * + * + */ +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{ + + 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 services, Map 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 @@ + + + + + + + + + + + + + + + + + + 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 @@ + + + + + + + + + + + + + + + + + + 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 context = new HashMap(); +// 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 context = new HashMap(); +// 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 context = new HashMap(); +// 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 context = new HashMap(); +// 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() { + 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(); + } +} -- cgit v1.2.3