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 --- branches/pre-spec-changes/test/.ruleset | 190 +++++++++++++++++++ branches/pre-spec-changes/test/LICENSE.txt | 202 +++++++++++++++++++++ branches/pre-spec-changes/test/NOTICE.txt | 14 ++ branches/pre-spec-changes/test/pom.xml | 46 +++++ .../org/apache/tuscany/test/ArtifactFactory.java | 154 ++++++++++++++++ .../java/org/apache/tuscany/test/SCATestCase.java | 183 +++++++++++++++++++ .../java/org/apache/tuscany/test/TestLauncher.java | 34 ++++ .../tuscany/test/binding/TestBindingBuilder.java | 53 ++++++ .../test/binding/TestBindingDefinition.java | 32 ++++ .../tuscany/test/binding/TestBindingLoader.java | 57 ++++++ .../test/binding/TestBindingRuntimeException.java | 24 +++ .../test/binding/TestBindingServiceBinding.java | 43 +++++ .../apache/tuscany/test/binding/TestInvoker.java | 49 +++++ .../tuscany/test/binding/TestReferenceBinding.java | 30 +++ .../test/binding/TestSocketBindingBuilder.java | 37 ++++ .../test/binding/TestSocketBindingDefinition.java | 27 +++ .../test/binding/TestSocketBindingLoader.java | 46 +++++ .../binding/TestSocketBindingServiceBinding.java | 129 +++++++++++++ .../tuscany/test/binding/TestSocketInvoker.java | 123 +++++++++++++ .../test/binding/TestSocketReferenceBinding.java | 38 ++++ .../main/resources/META-INF/sca/test.default.scdl | 44 +++++ .../main/resources/META-INF/tuscany/system.scdl | 163 +++++++++++++++++ .../META-INF/tuscany/test.binding.system.scdl | 44 +++++ .../apache/tuscany/test/SCATestCaseTestCase.java | 35 ++++ 24 files changed, 1797 insertions(+) create mode 100644 branches/pre-spec-changes/test/.ruleset create mode 100644 branches/pre-spec-changes/test/LICENSE.txt create mode 100644 branches/pre-spec-changes/test/NOTICE.txt create mode 100644 branches/pre-spec-changes/test/pom.xml create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java create mode 100644 branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketReferenceBinding.java create mode 100644 branches/pre-spec-changes/test/src/main/resources/META-INF/sca/test.default.scdl create mode 100644 branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/system.scdl create mode 100644 branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/test.binding.system.scdl create mode 100644 branches/pre-spec-changes/test/src/test/java/org/apache/tuscany/test/SCATestCaseTestCase.java (limited to 'branches/pre-spec-changes/test') diff --git a/branches/pre-spec-changes/test/.ruleset b/branches/pre-spec-changes/test/.ruleset new file mode 100644 index 0000000000..3886f07f2d --- /dev/null +++ b/branches/pre-spec-changes/test/.ruleset @@ -0,0 +1,190 @@ + + + + PMD Plugin preferences rule set + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/branches/pre-spec-changes/test/LICENSE.txt b/branches/pre-spec-changes/test/LICENSE.txt new file mode 100644 index 0000000000..0084319535 --- /dev/null +++ b/branches/pre-spec-changes/test/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, serviceDefinition marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. diff --git a/branches/pre-spec-changes/test/NOTICE.txt b/branches/pre-spec-changes/test/NOTICE.txt new file mode 100644 index 0000000000..d83ebbe236 --- /dev/null +++ b/branches/pre-spec-changes/test/NOTICE.txt @@ -0,0 +1,14 @@ +${pom.name} +Copyright (c) 2005 - 2006 The Apache Software Foundation + +Apache Tuscany is an effort undergoing incubation at The Apache Software +Foundation (ASF), sponsored by the Apache Web Services PMC. Incubation is +required of all newly accepted projects until a further review indicates that +the infrastructure, communications, and decision making process have stabilized +in a manner consistent with other successful ASF projects. While incubation +status is not necessarily a reflection of the completeness or stability of the +code, it does indicate that the project has yet to be fully endorsed by the ASF. + +This product includes software developed by +The Apache Software Foundation (http://www.apache.org/). + diff --git a/branches/pre-spec-changes/test/pom.xml b/branches/pre-spec-changes/test/pom.xml new file mode 100644 index 0000000000..70ec793727 --- /dev/null +++ b/branches/pre-spec-changes/test/pom.xml @@ -0,0 +1,46 @@ + + + + + org.apache.tuscany.sca + parent + 0.1-pre-spec-SNAPSHOT + + 4.0.0 + test + Apache Tuscany Test Framework + Tuscany Testcase Framework + + + + org.apache.tuscany.sca.kernel + core + + + junit + junit + compile + + + org.easymock + easymock + + + diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java new file mode 100644 index 0000000000..a7dbe0f90f --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/ArtifactFactory.java @@ -0,0 +1,154 @@ +/* + * 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.test; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.builder.Connector; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.WireService; + +import org.apache.tuscany.core.builder.ConnectorImpl; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.idl.java.JavaInterfaceProcessorRegistryImpl; +import org.apache.tuscany.core.implementation.composite.ServiceImpl; +import org.apache.tuscany.core.wire.InboundInvocationChainImpl; +import org.apache.tuscany.core.wire.InboundWireImpl; +import org.apache.tuscany.core.wire.InvokerInterceptor; +import org.apache.tuscany.core.wire.OutboundInvocationChainImpl; +import org.apache.tuscany.core.wire.OutboundWireImpl; +import org.apache.tuscany.core.wire.jdk.JDKWireService; + +/** + * A factory for creating runtime artifacts to facilitate testing without directly instantiating core implementation + * classes + * + * @version $$Rev$$ $$Date$$ + */ +public final class ArtifactFactory { + + private ArtifactFactory() { + } + + public static Connector createConnector() { + return new ConnectorImpl(createWireService(), null, null, null); + } + + public static WireService createWireService() { + return new JDKWireService(new WorkContextImpl(), null); + } + + public static Service createService(String name, CompositeComponent parent, ServiceContract contract) { + return new ServiceImpl(name, parent, contract); + } + + /** + * Creates an inbound wire. After a wire is returned, client code must call {@link + * #terminateWire(org.apache.tuscany.spi.wire.InboundWire)}. These two methods have been separated to allow wires to + * be decorated with interceptors or handlers prior to their completion + * + * @param serviceName the service name associated with the wire + * @param interfaze the interface associated with the wire + */ + public static InboundWire createLocalInboundWire(String serviceName, Class interfaze) + throws InvalidServiceContractException { + InboundWire wire = new InboundWireImpl(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract contract = registry.introspect(interfaze); + wire.setServiceContract(contract); + wire.setServiceName(serviceName); + wire.addInvocationChains(createInboundChains(interfaze)); + return wire; + } + + /** + * Creates an outbound wire. After a wire is returned, client code must call {@link + * #terminateWire(org.apache.tuscany.spi.wire.OutboundWire)}. These two methods have been separated to allow wires + * to be decorated with interceptors or handlers prior to their completion + * + * @param refName the reference name the wire is associated with on the client + * @param interfaze the interface associated with the wire + */ + public static OutboundWire createLocalOutboundWire(String refName, Class interfaze) + throws InvalidServiceContractException { + OutboundWire wire = new OutboundWireImpl(); + wire.setReferenceName(refName); + wire.addInvocationChains(createOutboundChains(interfaze)); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract contract = registry.introspect(interfaze); + wire.setServiceContract(contract); + return wire; + } + + + /** + * Finalizes the target wire + */ + public static void terminateWire(InboundWire wire) { + for (InboundInvocationChain chain : wire.getInvocationChains().values()) { + // add tail interceptor + chain.addInterceptor(new InvokerInterceptor()); + } + } + + public static void terminateWire(OutboundWire wire) { + for (OutboundInvocationChain chain : wire.getInvocationChains().values()) { + // add tail interceptor + chain.addInterceptor(new InvokerInterceptor()); + } + } + + private static Map, OutboundInvocationChain> createOutboundChains(Class interfaze) + throws InvalidServiceContractException { + Map, OutboundInvocationChain> invocations = new HashMap, OutboundInvocationChain>(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract contract = registry.introspect(interfaze); + for (Operation operation : contract.getOperations().values()) { + OutboundInvocationChain chain = new OutboundInvocationChainImpl(operation); + invocations.put(operation, chain); + } + return invocations; + } + + private static Map, InboundInvocationChain> createInboundChains(Class interfaze) + throws InvalidServiceContractException { + Map, InboundInvocationChain> invocations = new HashMap, InboundInvocationChain>(); + JavaInterfaceProcessorRegistry registry = new JavaInterfaceProcessorRegistryImpl(); + ServiceContract contract = registry.introspect(interfaze); + for (Operation operation : contract.getOperations().values()) { + InboundInvocationChain chain = new InboundInvocationChainImpl(operation); + // add tail interceptor + //chain.addInterceptor(new InvokerInterceptor()); + invocations.put(operation, chain); + } + return invocations; + } + + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java new file mode 100644 index 0000000000..829f99b6f9 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/SCATestCase.java @@ -0,0 +1,183 @@ +/* + * 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.test; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.bootstrap.ComponentNames; +import org.apache.tuscany.spi.builder.BuilderException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.ComponentException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.deployer.Deployer; +import org.apache.tuscany.spi.deployer.DeploymentMonitor; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.wire.WireService; + +import junit.framework.TestCase; +import org.apache.tuscany.api.TuscanyException; +import org.apache.tuscany.core.implementation.system.model.SystemCompositeImplementation; +import org.apache.tuscany.core.launcher.CompositeContextImpl; +import org.apache.tuscany.core.launcher.LauncherImpl; +import org.apache.tuscany.core.monitor.JavaLoggingMonitorFactory; +import org.apache.tuscany.host.MonitorFactory; +import org.apache.tuscany.host.runtime.InitializationException; + +import org.osoa.sca.CurrentCompositeContext; + +/** + * Base class for JUnit tests that want to run in an SCA client environment. + * + * @version $Rev$ $Date$ + */ +public abstract class SCATestCase extends TestCase { + protected CompositeComponent component; + private CompositeContextImpl context; + private Map extensions = new HashMap(); + private URL applicationSCDL; + private LauncherImpl launcher; + private MonitorFactory monitorFactory; + + protected void setUp() throws Exception { + super.setUp(); + if (monitorFactory == null) { + monitorFactory = new JavaLoggingMonitorFactory(); + } + ClassLoader cl = getClass().getClassLoader(); + launcher = new LauncherImpl(); + launcher.setApplicationLoader(cl); + URL scdl = cl.getResource(LauncherImpl.METAINF_SYSTEM_SCDL_PATH); + + try { + CompositeComponent composite = launcher.bootRuntime(scdl, monitorFactory); + for (String extensionName : extensions.keySet()) { + deployExtension(composite, extensionName, extensions.get(extensionName)); + } + + SCAObject wireServiceComponent = composite.getSystemChild(ComponentNames.TUSCANY_WIRE_SERVICE); + if (!(wireServiceComponent instanceof AtomicComponent)) { + throw new InitializationException("WireService must be an atomic component"); + } + + WireService wireService = (WireService) ((AtomicComponent) wireServiceComponent).getTargetInstance(); + + if (applicationSCDL == null) { + throw new RuntimeException("application SCDL not found: " + applicationSCDL); + } + component = launcher.bootApplication("application", applicationSCDL); + component.start(); + context = new CompositeContextImpl(component, wireService); + CurrentCompositeContext.setContext(context); + } catch (TuscanyException e) { + DeploymentMonitor monitor = monitorFactory.getMonitor(DeploymentMonitor.class); + monitor.deploymentError(e); + throw e; + } + + } + + /** + * A TestCase can use this to overide the default SCDL location of "META-INF/sca/default.scdl" + */ + protected void setApplicationSCDL(URL applicationSCDL) { + this.applicationSCDL = applicationSCDL; + } + + /** + * Set the application scdl based on the classpath entry for a class. Normally this will be a class in the + * production code associated with this test case. + * + * @param aClass a Class from which to determine the resource base url + * @param path location of the application SCDL relative to the base class + * @throws MalformedURLException if the path is malformed + */ + protected void setApplicationSCDL(Class aClass, String path) throws MalformedURLException { + URL root = getRoot(aClass); + setApplicationSCDL(new URL(root, path)); + } + + /** + * A TestCase can use this to add the SCDL location of an extention to be deployed to the runtime + */ + protected void addExtension(String extensionName, URL extentionSCDL) { + extensions.put(extensionName, extentionSCDL); + } + + + /** + * Sets the monitor factory to use + * + * @param monitorFactory the monitor factory to use + */ + protected void setMonitorFactory(MonitorFactory monitorFactory) { + this.monitorFactory = monitorFactory; + } + + protected void deployExtension(CompositeComponent composite, String extensionName, URL scdlURL) + throws LoaderException, BuilderException, ComponentException, InitializationException { + SystemCompositeImplementation implementation = new SystemCompositeImplementation(); + implementation.setScdlLocation(scdlURL); + implementation.setClassLoader(new URLClassLoader(new URL[]{scdlURL}, getClass().getClassLoader())); + + ComponentDefinition definition = + new ComponentDefinition(extensionName, implementation); + + + SCAObject child = composite.getSystemChild(ComponentNames.TUSCANY_DEPLOYER); + if (!(child instanceof AtomicComponent)) { + throw new InitializationException("Deployer must be an atomic component"); + } + Deployer deployer = (Deployer) ((AtomicComponent) child).getTargetInstance(); + Component component = deployer.deploy(composite, definition); + component.start(); + } + + + protected static URL getRoot(Class aClass) { + String name = aClass.getName(); + String classPath = "/" + name.replace('.', '/') + ".class"; + URL classURL = aClass.getResource(classPath); + assert classURL != null; + StringBuilder prefix = new StringBuilder(); + for (int i = 0; i < name.length(); i++) { + if (name.charAt(i) == '.') { + prefix.append("../"); + } + } + try { + return new URL(classURL, prefix.toString()); + } catch (MalformedURLException e) { + throw new AssertionError(); + } + } + + protected void tearDown() throws Exception { + CurrentCompositeContext.setContext(null); + component.stop(); + launcher.shutdownRuntime(); + super.tearDown(); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java new file mode 100644 index 0000000000..b66716bac0 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/TestLauncher.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.test; + +import java.io.File; + +import org.apache.tuscany.core.launcher.LauncherImpl; + +/** + * @version $Rev$ $Date$ + */ +public class TestLauncher extends LauncherImpl { + + public File getInstallDirectory() { + return new File("."); + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java new file mode 100644 index 0000000000..8354b0710a --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingBuilder.java @@ -0,0 +1,53 @@ +/* + * 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.test.binding; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ReferenceBinding; +import org.apache.tuscany.spi.component.ServiceBinding; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingBuilder extends BindingBuilderExtension { + + @SuppressWarnings("unchecked") + public ServiceBinding build(CompositeComponent parent, + BoundServiceDefinition definition, + TestBindingDefinition bindingDefinition, + DeploymentContext ctx) { + return new TestBindingServiceBinding(definition.getName(), parent); + } + + public ReferenceBinding build(CompositeComponent parent, + BoundReferenceDefinition definition, + TestBindingDefinition bindingDefinition, + DeploymentContext ctx) { + String name = definition.getName(); + return new TestReferenceBinding(name, parent); + } + + protected Class getBindingType() { + return TestBindingDefinition.class; + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java new file mode 100644 index 0000000000..19228fd86a --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingDefinition.java @@ -0,0 +1,32 @@ +/* + * 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.test.binding; + +import org.apache.tuscany.spi.model.BindingDefinition; + + +/** + * A simple binding for test purposes. + * + * @version $$Rev$$ $$Date$$ + */ +public class TestBindingDefinition extends BindingDefinition { + + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java new file mode 100644 index 0000000000..ef7a0ebb6f --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingLoader.java @@ -0,0 +1,57 @@ +/* + * 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.test.binding; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.Version; +import org.osoa.sca.annotations.Constructor; + +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.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingLoader extends LoaderExtension { + + public static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.test"); + + @Constructor + public TestBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return BINDING_TEST; + } + + public TestBindingDefinition load(CompositeComponent parent, + ModelObject object, XMLStreamReader reader, + DeploymentContext context) throws XMLStreamException, LoaderException { + return new TestBindingDefinition(); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java new file mode 100644 index 0000000000..88c62d1420 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingRuntimeException.java @@ -0,0 +1,24 @@ +package org.apache.tuscany.test.binding; + +import org.apache.tuscany.api.TuscanyRuntimeException; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingRuntimeException extends TuscanyRuntimeException { + + public TestBindingRuntimeException() { + } + + public TestBindingRuntimeException(String message) { + super(message); + } + + public TestBindingRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public TestBindingRuntimeException(Throwable cause) { + super(cause); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java new file mode 100644 index 0000000000..341be523a3 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestBindingServiceBinding.java @@ -0,0 +1,43 @@ +/* + * 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.test.binding; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ServiceBindingExtension; + +/** + * @version $Rev$ $Date$ + */ +public class TestBindingServiceBinding extends ServiceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + public TestBindingServiceBinding(String name, CompositeComponent parent) throws CoreRuntimeException { + super(name, parent); + // do nothing, but this could register with the host environment + } + + public QName getBindingType() { + return BINDING_TEST; + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java new file mode 100644 index 0000000000..c3b02024af --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestInvoker.java @@ -0,0 +1,49 @@ +package org.apache.tuscany.test.binding; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class TestInvoker implements TargetInvoker { + + private boolean cacheable; + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + public boolean isOptimizable() { + return isCacheable(); + } + + public Object invokeTarget(final Object payload, final short sequence) throws InvocationTargetException { + // echo back the result, a real binding would invoke some API for flowing the request + return ((Object[]) payload)[0]; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody(), TargetInvoker.NONE); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBodyWithFault(e.getCause()); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java new file mode 100644 index 0000000000..025cfb1e1e --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestReferenceBinding.java @@ -0,0 +1,30 @@ +package org.apache.tuscany.test.binding; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ReferenceBindingExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class TestReferenceBinding extends ReferenceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + public TestReferenceBinding(String name, CompositeComponent parent) { + super(name, parent); + } + + public QName getBindingType() { + return BINDING_TEST; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + return new TestInvoker(); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java new file mode 100644 index 0000000000..020bdd3ef9 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingBuilder.java @@ -0,0 +1,37 @@ +package org.apache.tuscany.test.binding; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ReferenceBinding; +import org.apache.tuscany.spi.component.ServiceBinding; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; + +/** + * @version $Rev$ $Date$ + */ +public class TestSocketBindingBuilder extends BindingBuilderExtension { + + public ServiceBinding build(CompositeComponent parent, + BoundServiceDefinition definition, + TestSocketBindingDefinition bindingDefinition, + DeploymentContext ctx) { + int port = bindingDefinition.getPort(); + return new TestSocketBindingServiceBinding(definition.getName(), port, parent); + } + + public ReferenceBinding build(CompositeComponent parent, + BoundReferenceDefinition definition, + TestSocketBindingDefinition bindingDefinition, + DeploymentContext ctx) { + String name = definition.getName(); + int port = bindingDefinition.getPort(); + String host = bindingDefinition.getHost(); + return new TestSocketReferenceBinding(name, host, port, parent); + } + + protected Class getBindingType() { + return TestSocketBindingDefinition.class; + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java new file mode 100644 index 0000000000..fa5f7b2c57 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingDefinition.java @@ -0,0 +1,27 @@ +package org.apache.tuscany.test.binding; + +import org.apache.tuscany.spi.model.BindingDefinition; + +/** + * A simple socket-based binding. Service operations may onyl take one parameter that is Serializable + * + * @version $$Rev$$ $$Date$$ + */ +public class TestSocketBindingDefinition extends BindingDefinition { + private String host; + private int port; + + public TestSocketBindingDefinition(String host, int port) { + this.host = host; + this.port = port; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java new file mode 100644 index 0000000000..cdec616c35 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingLoader.java @@ -0,0 +1,46 @@ +package org.apache.tuscany.test.binding; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.Version; +import org.osoa.sca.annotations.Constructor; + +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.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * @version $Rev$ $Date$ + */ +public class TestSocketBindingLoader extends LoaderExtension { + + public static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + + @Constructor + public TestSocketBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return TestSocketBindingLoader.BINDING_TEST; + } + + public TestSocketBindingDefinition load(CompositeComponent parent, + ModelObject object, XMLStreamReader reader, + DeploymentContext context) throws XMLStreamException, LoaderException { + String host = reader.getAttributeValue(null, "host"); + int port; + try { + port = Integer.parseInt(reader.getAttributeValue(null, "port")); + } catch (NumberFormatException e) { + throw new LoaderException("Invalid port specified", e); + } + return new TestSocketBindingDefinition(host, port); + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java new file mode 100644 index 0000000000..a1531b6f5f --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketBindingServiceBinding.java @@ -0,0 +1,129 @@ +package org.apache.tuscany.test.binding; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import javax.xml.namespace.QName; + +import org.osoa.sca.Version; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ServiceBindingExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.MessageImpl; + +/** + * Implements a very simple remote, socket-based binding for test purposes. This binding exposes serviceBindings using a socket + * on a given port. Service operations must take only one paramter that is Serializable. + * + * @version $Rev$ $Date$ + */ +public class TestSocketBindingServiceBinding extends ServiceBindingExtension { + private static final QName BINDING_TEST = new QName(Version.XML_NAMESPACE_1_0, "binding.socket"); + private int port; + private ServerSocket socket; + private ExecutorService executor; + private TestSocketBindingServiceBinding.ServerRunnable runnable; + + public TestSocketBindingServiceBinding(String name, int port, CompositeComponent parent) throws CoreRuntimeException { + super(name, parent); + this.port = port; + } + + public QName getBindingType() { + return BINDING_TEST; + } + + public void start() { + executor = Executors.newSingleThreadExecutor(); + // create a listener, note that a work scheduler should normally be used to spawn work in different threads + runnable = new ServerRunnable(); + executor.execute(runnable); + } + + public void stop() { + try { + runnable.setEnd(true); + socket.close(); + executor.shutdownNow(); + } catch (IOException e) { + throw new TestBindingRuntimeException(e); + } + } + + /** + * Creates a socket listener in another thread which handles one client at a time. For a real binding, a work + * scheduler should be used + */ + private class ServerRunnable implements Runnable { + + private boolean end; + + public void setEnd(boolean end) { + this.end = end; + } + + public void run() { + Socket clientSocket; + ObjectInputStream is = null; + ObjectOutputStream os = null; + try { + socket = new ServerSocket(port); + } catch (IOException e) { + throw new TestBindingRuntimeException(e); + } + while (!end) { + try { + clientSocket = socket.accept(); + is = new ObjectInputStream(clientSocket.getInputStream()); + String operation = is.readUTF(); + int argn = is.readInt(); + Object[] args = new Object[argn]; + for (int i = 0; i < argn; i++) { + args[i] = is.readObject(); + } + Map, InboundInvocationChain> chains = getInboundWire().getInvocationChains(); + for (InboundInvocationChain chain : chains.values()) { + if (chain.getOperation().getName().equals(operation)) { + Message message = new MessageImpl(); + message.setTargetInvoker(chain.getTargetInvoker()); + message.setBody(args); + message = chain.getHeadInterceptor().invoke(message); + os = new ObjectOutputStream(clientSocket.getOutputStream()); + os.writeObject(message.getBody()); + os.flush(); + } + } + } catch (IOException e) { + throw new TestBindingRuntimeException(e); + } catch (ClassNotFoundException e) { + throw new TestBindingRuntimeException(e); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + // ingore + } + } + if (is != null) { + try { + is.close(); + } catch (IOException e) { + // ignore + } + } + } + } + } + } +} diff --git a/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java new file mode 100644 index 0000000000..15ebd672df --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/java/org/apache/tuscany/test/binding/TestSocketInvoker.java @@ -0,0 +1,123 @@ +package org.apache.tuscany.test.binding; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.InvocationTargetException; +import java.net.Socket; +import java.net.UnknownHostException; + +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * Responsible for serializing an operation parameter flowing the invocation through the socket + * + * @version $Rev$ $Date$ + */ +public class TestSocketInvoker implements TargetInvoker { + private String host; + private int port; + private String operation; + + private boolean cacheable; + + public TestSocketInvoker(String host, int port, String operation) { + this.host = host; + this.port = port; + this.operation = operation; + } + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + public boolean isOptimizable() { + return isCacheable(); + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object resp = invokeTarget(msg.getBody(), TargetInvoker.NONE); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBodyWithFault(e.getCause()); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + /** + * Sends the payload over a socket + */ + public Object invokeTarget(final Object object, final short sequence) throws InvocationTargetException { + int argn; + if (object == null) { + argn = 0; + } else if (!object.getClass().isArray()) { + argn = 1; + } else { + argn = ((Object[])object).length; + } + + Socket socket = null; + ObjectOutputStream os = null; + ObjectInputStream is = null; + try { + socket = new Socket(host, port); + os = new ObjectOutputStream(socket.getOutputStream()); + os.writeUTF(operation); + os.writeInt(argn); + for (int i=0; i + + + + + + + + + + + + + + + + + + + + + diff --git a/branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/system.scdl b/branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/system.scdl new file mode 100644 index 0000000000..da82b3b0b4 --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/system.scdl @@ -0,0 +1,163 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + .m2/repository + + + + + + 10 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/test.binding.system.scdl b/branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/test.binding.system.scdl new file mode 100644 index 0000000000..e6ffb7300b --- /dev/null +++ b/branches/pre-spec-changes/test/src/main/resources/META-INF/tuscany/test.binding.system.scdl @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/branches/pre-spec-changes/test/src/test/java/org/apache/tuscany/test/SCATestCaseTestCase.java b/branches/pre-spec-changes/test/src/test/java/org/apache/tuscany/test/SCATestCaseTestCase.java new file mode 100644 index 0000000000..fac190b02e --- /dev/null +++ b/branches/pre-spec-changes/test/src/test/java/org/apache/tuscany/test/SCATestCaseTestCase.java @@ -0,0 +1,35 @@ +/* + * 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.test; + +import java.net.URL; +import java.net.MalformedURLException; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class SCATestCaseTestCase extends TestCase { + public void testGetRoot() throws MalformedURLException { + URL classUrl = getClass().getResource("SCATestCaseTestCase.class"); + URL root = SCATestCase.getRoot(getClass()); + assertEquals(classUrl, new URL(root, "org/apache/tuscany/test/SCATestCaseTestCase.class")); + } +} -- cgit v1.2.3