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 --- .../container/easy/AsyncInvokerTestCase.java | 207 +++++++++++++++++++++ .../easy/EasyComponentBuilderTestCase.java | 104 +++++++++++ .../container/easy/EasyComponentTestCase.java | 142 ++++++++++++++ .../easy/EasyComponentTypeLoaderTestCase.java | 141 ++++++++++++++ .../container/easy/EasyComponentTypeTestCase.java | 37 ++++ .../easy/EasyImplementationLoaderTestCase.java | 76 ++++++++ .../container/easy/EasyImplementationTestCase.java | 28 +++ .../easy/EasyInstanceFactoryTestCase.java | 55 ++++++ .../container/easy/EasyInvokerTestCase.java | 61 ++++++ .../tuscany/container/easy/mock/AsyncTarget.java | 26 +++ .../container/easy/mock/MockInstanceFactory.java | 30 +++ 11 files changed, 907 insertions(+) create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/AsyncInvokerTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentBuilderTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeLoaderTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationLoaderTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInstanceFactoryTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInvokerTestCase.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/AsyncTarget.java create mode 100644 sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/MockInstanceFactory.java (limited to 'sandbox/ant/container.easy/src/test/java/org/apache') diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/AsyncInvokerTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/AsyncInvokerTestCase.java new file mode 100644 index 0000000000..b49a74ce57 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/AsyncInvokerTestCase.java @@ -0,0 +1,207 @@ +/* + * 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.easy; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.getCurrentArguments; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +import java.lang.reflect.Method; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.easy.AsyncInvoker.ContextBinder; +import org.apache.tuscany.container.easy.AsyncInvoker.ImmutableMessage; +import org.apache.tuscany.container.easy.mock.AsyncTarget; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; +import org.easymock.IAnswer; +import org.easymock.classextension.EasyMock; + +/** + */ +public class AsyncInvokerTestCase extends TestCase { + + @SuppressWarnings("unchecked") + public void testInvoke() throws Exception { + EasyInstance instance = createMock(EasyInstance.class); + expect(instance.invokeTarget("invoke", null)).andReturn(null).once(); + replay(instance); + EasyComponent component = EasyMock.createMock(EasyComponent.class); + expect(component.getTargetInstance()).andReturn(instance); + EasyMock.replay(component); + AsyncMonitor monitor = createMock(AsyncMonitor.class); + replay(monitor); + + WorkScheduler scheduler = createMock(WorkScheduler.class); + scheduler.scheduleWork(isA(Runnable.class)); + expectLastCall().andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + Runnable runnable = (Runnable) getCurrentArguments()[0]; + runnable.run(); + return null; + } + }); + replay(scheduler); + WorkContext context = createMock(WorkContext.class); + Method method = AsyncTarget.class.getMethod("invoke"); + method.setAccessible(true); + InboundWire wire = createMock(InboundWire.class); + AsyncInvoker invoker = new AsyncInvoker("invoke", wire, component, scheduler, monitor, context); + Message msg = new MessageImpl(); + invoker.invoke(msg); + verify(instance); + } + + public void testClone() { + AsyncInvoker invoker = new AsyncInvoker(null, null, null,null,null,null); + assertNotNull(invoker.clone()); + } + + public void testGetInstance() { + EasyComponent component = EasyMock.createMock(EasyComponent.class); + expect(component.getTargetInstance()).andReturn("petra"); + EasyMock.replay(component); + AsyncInvoker invoker = new AsyncInvoker(null, null, component,null,null,null); + assertEquals("petra", invoker.getInstance()); + } + + public void testGetInstanceCacheable() { + EasyComponent component = EasyMock.createMock(EasyComponent.class); + expect(component.getTargetInstance()).andReturn("petra"); + EasyMock.replay(component); + AsyncInvoker invoker = new AsyncInvoker(null, null, component,null,null,null); + invoker.setCacheable(true); + assertEquals("petra", invoker.getInstance()); + } + + public void testGetBody() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + assertNull(message.getBody()); + } + + public void testSetBody() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + try { + message.setBody(null); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + + public void testGetTargetInvoker() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + assertNull(message.getTargetInvoker()); + } + + public void testSetTargetInvoker() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + try { + message.setTargetInvoker(null); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + + public void testGetFromAddress() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + assertNull(message.getFromAddress()); + } + + public void testSetFromAddress() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + try { + message.setFromAddress(null); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + + public void testGetMessageId() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + assertNull(message.getMessageId()); + } + + public void testSetMessageId() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + try { + message.setMessageId(null); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + + public void testGetCorrelationId() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + assertNull(message.getCorrelationId()); + } + + public void testSetCorrelationId() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + try { + message.setCorrelationId(null); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + + public void testIsFault() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + assertFalse(message.isFault()); + } + + public void testSetBodyWithFault() { + ImmutableMessage message = new AsyncInvoker.ImmutableMessage(); + try { + message.setBodyWithFault(null); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + + public void testContextBinder() { + ContextBinder contextBinder = new AsyncInvoker.ContextBinder(); + contextBinder.setContext(null); + try { + contextBinder.start(); + fail(); + } catch (AssertionError e) { + // expected + } + try { + contextBinder.stop(); + fail(); + } catch (AssertionError e) { + // expected + } + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentBuilderTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentBuilderTestCase.java new file mode 100644 index 0000000000..89858da541 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentBuilderTestCase.java @@ -0,0 +1,104 @@ +package org.apache.tuscany.container.easy; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import junit.framework.TestCase; + +import org.apache.tuscany.core.component.scope.ModuleScopeObjectFactory; +import org.apache.tuscany.core.component.scope.ScopeRegistryImpl; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.ScopeRegistry; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.PropertyValue; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.model.ServiceDefinition; +import org.easymock.IAnswer; + +public class EasyComponentBuilderTestCase extends TestCase { + + public void testGetImplementationType() { + EasyComponentBuilder builder = new EasyComponentBuilder(); + assertEquals(EasyImplementation.class, builder.getImplementationType()); + } + + @SuppressWarnings("unchecked") + public void testBuild() { + EasyComponentBuilder builder = new EasyComponentBuilder(); + ScopeRegistry scopeRegistry = new ScopeRegistryImpl(); + scopeRegistry.registerFactory(Scope.COMPOSITE, new ModuleScopeObjectFactory(scopeRegistry)); + builder.setScopeRegistry(scopeRegistry); + DeploymentContext deploymentContext = createMock(DeploymentContext.class); + final ScopeContainer scopeContainer = createMock(ScopeContainer.class); + expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.COMPOSITE; + } + }); + expect(deploymentContext.getModuleScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return scopeContainer; + } + }); + replay(deploymentContext); + ComponentDefinition impl = new ComponentDefinition(new EasyImplementation()); + EasyComponentType componentType = new EasyComponentType(); + componentType.setLifecycleScope(Scope.COMPOSITE); + ServiceDefinition service = new ServiceDefinition(); + ServiceContract serviceContract = new JavaServiceContract(); + service.setServiceContract(serviceContract); + componentType.add(service); + impl.getImplementation().setComponentType(componentType); + + PropertyValue pv = new PropertyValue("foo", "", ""); + ObjectFactory pvFactory = createMock(ObjectFactory.class); + expect(pvFactory.getInstance()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return null; + } + }); + replay(pvFactory); + pv.setValueFactory(pvFactory); + impl.add(pv); + + Component component = builder.build(null, impl, deploymentContext); + assertNotNull(component); + } + + @SuppressWarnings("unchecked") + public void testBuildModuleScope() { + EasyComponentBuilder builder = new EasyComponentBuilder(); + DeploymentContext deploymentContext = createMock(DeploymentContext.class); + final ScopeContainer scopeContainer = createMock(ScopeContainer.class); + expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.MODULE; + } + }); + expect(deploymentContext.getModuleScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return scopeContainer; + } + }); + replay(deploymentContext); + ComponentDefinition impl = new ComponentDefinition(new EasyImplementation()); + EasyComponentType componentType = new EasyComponentType(); + ServiceDefinition service = new ServiceDefinition(); + ServiceContract serviceContract = new JavaServiceContract(); + service.setServiceContract(serviceContract); + componentType.add(service); + impl.getImplementation().setComponentType(componentType); + Component component = builder.build(null, impl, deploymentContext); + assertNotNull(component); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTestCase.java new file mode 100644 index 0000000000..9f5c2a2f14 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTestCase.java @@ -0,0 +1,142 @@ +package org.apache.tuscany.container.easy; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.easy.mock.MockInstanceFactory; +import org.apache.tuscany.core.wire.InboundWireImpl; +import org.apache.tuscany.core.wire.OutboundWireImpl; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.RuntimeWire; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; +import org.easymock.IAnswer; + +public class EasyComponentTestCase extends TestCase { + + private ScopeContainer scopeContainer; + + @SuppressWarnings("unchecked") + public void testCreateTargetInvoker() { + EasyComponent component = new EasyComponent(null,null, null, null, null, scopeContainer, null, null, null); + + Operation operation = new Operation("hashCode", null,null,null,false,null); + ServiceContract contract = new ServiceContract(List.class){}; + operation.setServiceContract(contract); + TargetInvoker invoker = component.createTargetInvoker("hashCode", operation); + + assertNotNull(invoker); + } + + @SuppressWarnings("unchecked") + public void testCreateInstance() throws IOException { + EasyComponent pc = new EasyComponent(null,createBSFEasy(), new HashMap(), null, null, scopeContainer, null, null, null); + Object o = pc.createInstance(); + assertNotNull(o); + assertTrue(o instanceof EasyInstance); + } + + @SuppressWarnings("unchecked") + public void testCreateInstanceWithRef() throws IOException { + WireService wireService = createMock(WireService.class); + expect(wireService.createProxy(isA(RuntimeWire.class))).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.MODULE; + } + }); + + EasyComponent pc = new EasyComponent(null,createBSFEasy(), new HashMap(), null, null, scopeContainer, wireService, null, null); + OutboundWire wire = new OutboundWireImpl(); + wire.setReferenceName("foo"); + pc.addOutboundWire(wire); + Object o = pc.createInstance(); + assertNotNull(o); + assertTrue(o instanceof EasyInstance); + } + + @SuppressWarnings("unchecked") + public void testGetServiceInstance() { + WireService wireService = createMock(WireService.class); + expect(wireService.createProxy(isA(RuntimeWire.class))).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return "foo"; + } + }); + replay(wireService); + EasyComponent pc = new EasyComponent(null,null, null, null, null, scopeContainer, wireService, null, null); + InboundWire wire = new InboundWireImpl(); + pc.addInboundWire(wire); + assertEquals("foo", pc.getServiceInstance()); + } + + @SuppressWarnings("unchecked") + public void testGetServiceInstanceFail() { + EasyComponent pc = new EasyComponent(null,null, null, null, null, scopeContainer, null, null, null); + try { + pc.getServiceInstance(); + fail(); + } catch (TargetException e) { + // expected + } + } + + @SuppressWarnings("unchecked") + public void testGetproperties() { + EasyComponent pc = new EasyComponent(null,null, new HashMap(), null, null, scopeContainer, null, null, null); + assertNotNull(pc.getProperties()); + } + + @SuppressWarnings("unchecked") + public void testGetServiceInterfaces() { + List services = new ArrayList(); + EasyComponent pc = new EasyComponent(null,null,null, services, null, scopeContainer, null, null, null); + assertEquals(services, pc.getServiceInterfaces()); + } + + @SuppressWarnings("unchecked") + public void testCreateAsyncTargetInvoker() { + EasyComponent pc = new EasyComponent(null,null,null, new ArrayList>(), null, scopeContainer, null, null, null); + assertNotNull(pc.createAsyncTargetInvoker(null, new Operation("foo", null,null,null))); + } + + @Override + @SuppressWarnings("unchecked") + protected void setUp() throws Exception { + super.setUp(); + this.scopeContainer = createMock(ScopeContainer.class); + expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.MODULE; + } + }); + } + + public EasyInstanceFactory createBSFEasy() throws IOException { +// URL scriptURL = getClass().getResource("foo.mock"); +// InputStream is = scriptURL.openStream(); +// StringBuilder sb = new StringBuilder(); +// int i = 0; +// while ((i = is.read()) != -1) { +// sb.append((char) i); +// } +// is.close(); +// String script = sb.toString(); + MockInstanceFactory bsfEasy = new MockInstanceFactory("foo.mock", null); + return bsfEasy; + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeLoaderTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeLoaderTestCase.java new file mode 100644 index 0000000000..2a86172a5e --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeLoaderTestCase.java @@ -0,0 +1,141 @@ +/* + * 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.easy; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; + +import java.net.MalformedURLException; +import java.net.URL; + +import javax.xml.stream.XMLStreamException; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.easy.mock.MockInstanceFactory; +import org.apache.tuscany.core.loader.LoaderRegistryImpl; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +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.model.ComponentType; +import org.apache.tuscany.spi.model.ModelObject; +import org.apache.tuscany.spi.model.Scope; +import org.easymock.IAnswer; + +/** + * + */ +public class EasyComponentTypeLoaderTestCase extends TestCase { + + public void testGetSideFileName() { + EasyComponentTypeLoader loader = new EasyComponentTypeLoader(); + assertEquals("BSFEasyTestCase.componentType", loader.getSideFileName("BSFEasyTestCase.mock")); + } + + public void testGetSideFileNameNoDot() { + EasyComponentTypeLoader loader = new EasyComponentTypeLoader(); + assertEquals("BSFEasyTestCase.componentType", loader.getSideFileName("BSFEasyTestCase")); + } + + @SuppressWarnings("unchecked") + public void testLoadFromSideFile() throws MalformedURLException, LoaderException, XMLStreamException { + EasyComponentTypeLoader loader = new EasyComponentTypeLoader(); + LoaderRegistry loaderRegistry = new LoaderRegistryImpl() { + public MO load(CompositeComponent parent, ModelObject mo, URL url, Class type, DeploymentContext ctx) throws LoaderException { + return (MO) new ComponentType(); + } + }; + loader.setLoaderRegistry(loaderRegistry); + loader.loadFromSidefile(null, null); + } + + @SuppressWarnings("unchecked") + public void testLoad() throws LoaderException { + EasyInstanceFactory bsfEasy = new MockInstanceFactory("org/apache/tuscany/container/easy/foo.mock", getClass().getClassLoader()); + EasyComponentTypeLoader loader = new EasyComponentTypeLoader(); + LoaderRegistry loaderRegistry = new LoaderRegistryImpl() { + public MO load(CompositeComponent parent, + ModelObject mo, + URL url, + Class type, + DeploymentContext ctx) throws LoaderException { + return (MO) new ComponentType(); + } + }; + loader.setLoaderRegistry(loaderRegistry); + EasyImplementation implementation = new EasyImplementation(); + implementation.setResourceName("org/apache/tuscany/container/easy/foo.mock"); + implementation.setScriptInstanceFactory(bsfEasy); + DeploymentContext deploymentContext = createMock(DeploymentContext.class); + final ScopeContainer scopeContainer = createMock(ScopeContainer.class); + expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.MODULE; + } + }); + expect(deploymentContext.getModuleScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return scopeContainer; + } + }); + replay(deploymentContext); + loader.load(null, implementation, deploymentContext); + assertNotNull(implementation.getComponentType()); + } + + @SuppressWarnings("unchecked") + public void testLoadMissingSideFile() throws LoaderException { + EasyInstanceFactory bsfEasy = new MockInstanceFactory("org/apche/tuscany/container/easy/foo.mock", getClass().getClassLoader()); + EasyComponentTypeLoader loader = new EasyComponentTypeLoader(); + EasyImplementation implementation = new EasyImplementation(); + implementation.setResourceName("org/apache/tuscany/container/easy/doesntExist"); + implementation.setScriptInstanceFactory(bsfEasy); + DeploymentContext deploymentContext = createMock(DeploymentContext.class); + final ScopeContainer scopeContainer = createMock(ScopeContainer.class); + expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.MODULE; + } + }); + expect(deploymentContext.getModuleScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return scopeContainer; + } + }); + replay(deploymentContext); + try { + loader.load(null, implementation, deploymentContext); + fail(); + } catch (IllegalArgumentException e) { + } + } + + public void testGetImplementationClass() { + EasyComponentTypeLoader loader = new EasyComponentTypeLoader(); + assertEquals(EasyImplementation.class, loader.getImplementationClass()); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeTestCase.java new file mode 100644 index 0000000000..5d9b436bc3 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyComponentTypeTestCase.java @@ -0,0 +1,37 @@ +package org.apache.tuscany.container.easy; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.easy.EasyComponentType; +import org.apache.tuscany.spi.model.ComponentType; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceDefinition; + +public class EasyComponentTypeTestCase extends TestCase { + + public void testLifecycleScope() { + EasyComponentType ct = new EasyComponentType(); + assertEquals(Scope.MODULE, ct.getLifecycleScope()); + ct.setLifecycleScope(Scope.COMPOSITE); + assertEquals(Scope.COMPOSITE, ct.getLifecycleScope()); + } + + @SuppressWarnings("unchecked") + public void testComponentTypeConstructor() { + ComponentType ct = new ComponentType(); + Property property = new Property(); + ct.add(property); + ReferenceDefinition reference = new ReferenceDefinition(); + ct.add(reference); + ServiceDefinition service = new ServiceDefinition(); + ct.add(service); + + EasyComponentType pct = new EasyComponentType(ct); + + assertEquals(property, pct.getProperties().values().iterator().next()); + assertEquals(reference, pct.getReferences().values().iterator().next()); + assertEquals(service, pct.getServices().values().iterator().next()); + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationLoaderTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationLoaderTestCase.java new file mode 100644 index 0000000000..f655015ad4 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationLoaderTestCase.java @@ -0,0 +1,76 @@ +/* + * 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.easy; + +import static org.easymock.classextension.EasyMock.createMock; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +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; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * + */ +public class EasyImplementationLoaderTestCase extends TestCase { + + private LoaderRegistry registry; + + private EasyImplementationLoader loader; + + public void testLoadSource() throws LoaderException { + String script = loader.loadSource(getClass().getClassLoader(), "org/apache/tuscany/container/easy/foo.mock"); + assertTrue(script.startsWith("hello")); + } + + public void testLoadSourceMissingResource() throws LoaderException { + try { + loader.loadSource(getClass().getClassLoader(), "doesnt.exist"); + fail(); + } catch (MissingResourceException e) { + // expected + } + } + + public void testGetXMLType() throws LoaderException { + assertEquals("http://foo", loader.getXMLType().getNamespaceURI()); + assertEquals("bar", loader.getXMLType().getLocalPart()); + } + + protected void setUp() throws Exception { + super.setUp(); + registry = createMock(LoaderRegistry.class); + loader = new EasyImplementationLoader(registry){ + public QName getXMLType() { + return new QName("http://foo", "bar"); + } +// @Override + public EasyImplementation load(CompositeComponent arg0, ModelObject arg1, XMLStreamReader arg2, DeploymentContext arg3) throws XMLStreamException, LoaderException { + return null; + }}; + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationTestCase.java new file mode 100644 index 0000000000..9a7ec25315 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyImplementationTestCase.java @@ -0,0 +1,28 @@ +package org.apache.tuscany.container.easy; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.easy.mock.MockInstanceFactory; + +public class EasyImplementationTestCase extends TestCase { + + private EasyInstanceFactory bsfEasy; + + public void testGetBSFEasy() { + EasyImplementation impl = new EasyImplementation(); + impl.setScriptInstanceFactory(bsfEasy); + assertEquals(bsfEasy, impl.getScriptInstanceFactory()); + } + + public void testGetResourceName() { + EasyImplementation impl = new EasyImplementation(); + impl.setResourceName("foo"); + assertEquals("foo", impl.getResourceName()); + } + + public void setUp() throws Exception { + super.setUp(); + bsfEasy = new MockInstanceFactory("BSFEasyTestCase", null); + } + +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInstanceFactoryTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInstanceFactoryTestCase.java new file mode 100644 index 0000000000..c96d8425eb --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInstanceFactoryTestCase.java @@ -0,0 +1,55 @@ +package org.apache.tuscany.container.easy; + +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.tuscany.container.easy.mock.MockInstanceFactory; + +public class EasyInstanceFactoryTestCase extends TestCase { + + public void testCreateInstance() throws InvocationTargetException { + MockInstanceFactory factory = new MockInstanceFactory("foo.mock", getClass().getClassLoader()); + Map context = new HashMap(); + context.put("foo", "bar"); + EasyInstance instance = factory.createInstance(null, context); + assertNotNull(instance); + } + + public void testCreateInstanceNoClass() throws InvocationTargetException { + MockInstanceFactory factory = new MockInstanceFactory("foo.mock", getClass().getClassLoader()); + Map context = new HashMap(); + context.put("foo", "bar"); + EasyInstance instance = factory.createInstance(null, context); + assertNotNull(instance); + } + + public void testCreateInstanceRuby() throws InvocationTargetException { + MockInstanceFactory factory = new MockInstanceFactory("foo.mock", getClass().getClassLoader()); + Map context = new HashMap(); + context.put("foo", "bar"); + EasyInstance instance = factory.createInstance(null, context); + assertNotNull(instance); + } + + public void testGetters() throws InvocationTargetException { + MockInstanceFactory factory = new MockInstanceFactory("foo", getClass().getClassLoader()); + assertEquals("foo", factory.getResourceName()); + assertEquals(getClass().getClassLoader(), factory.getClassLoader()); + } + + public void testGetResponseClasses() { + MockInstanceFactory factory = new MockInstanceFactory("foo", getClass().getClassLoader()); + Map classes = factory.getResponseClasses(Arrays.asList( new Class[]{ Runnable.class})); + assertEquals(1, classes.size()); + assertEquals("run", classes.keySet().iterator().next()); + assertEquals(void.class, classes.get("run")); + } + + protected void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInvokerTestCase.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInvokerTestCase.java new file mode 100644 index 0000000000..bdd5c4a875 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/EasyInvokerTestCase.java @@ -0,0 +1,61 @@ +package org.apache.tuscany.container.easy; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; + +import java.lang.reflect.InvocationTargetException; + +import junit.framework.TestCase; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.model.Scope; +import org.easymock.IAnswer; + +public class EasyInvokerTestCase extends TestCase { + + private EasyComponent component; + + public void testInvokeTarget() throws InvocationTargetException { + EasyInvoker invoker = new EasyInvoker("hello", component); + assertEquals("hello petra", invoker.invokeTarget(null)); + } + + public void testInvokeTargetException() throws InvocationTargetException, SecurityException, NoSuchMethodException { + EasyInvoker badInvoker = new EasyInvoker("bang", component); + try { + badInvoker.invokeTarget(null); + fail(); + } catch (InvocationTargetException e) { + // expected + } + } + + @SuppressWarnings("unchecked") + protected void setUp() throws Exception { + super.setUp(); + + ScopeContainer scopeContainer = createMock(ScopeContainer.class); + expect(scopeContainer.getScope()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return Scope.MODULE; + } + }); + expect(scopeContainer.getInstance(isA(AtomicComponent.class))).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return new EasyInstance(){ + public Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException { + if ("bang".equals(operationName)) { + throw new RuntimeException("bang"); + } + return "hello petra"; + }}; + } + }); + replay(scopeContainer); + + this.component = new EasyComponent(null, null, null, null, null, scopeContainer, null, null, null); + } +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/AsyncTarget.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/AsyncTarget.java new file mode 100644 index 0000000000..b7317ddcab --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/AsyncTarget.java @@ -0,0 +1,26 @@ +/* + * 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.easy.mock; + +/** + */ +public interface AsyncTarget { + + void invoke(); +} diff --git a/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/MockInstanceFactory.java b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/MockInstanceFactory.java new file mode 100644 index 0000000000..b7c7b3f755 --- /dev/null +++ b/sandbox/ant/container.easy/src/test/java/org/apache/tuscany/container/easy/mock/MockInstanceFactory.java @@ -0,0 +1,30 @@ +package org.apache.tuscany.container.easy.mock; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.container.easy.EasyInstance; +import org.apache.tuscany.container.easy.EasyInstanceFactory; + +public class MockInstanceFactory extends EasyInstanceFactory { + + public MockInstanceFactory(String scriptName, ClassLoader classLoader) { + super(scriptName, classLoader); + } + + @Override + public MockInstance createInstance(List services, Map context) { + return new MockInstance(); + } + +} + +class MockInstance implements EasyInstance { + + public Object invokeTarget(String operationName, Object[] args) throws InvocationTargetException { + // TODO Auto-generated method stub + return null; + } + +} -- cgit v1.2.3