From abefe1a37b0e9ca6bee2543dc1a97e3cf72da85f Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Thu, 2 Sep 2010 23:12:00 +0000 Subject: Add a SOAP Web service binding and a test case for it. Minor cleanup, remove redundant test case and refactor test WSDL namespaces. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@992150 13f79535-47bb-0310-9956-ffa450edef68 --- .../sample/impl/SampleImplementationProcessor.java | 18 ++- .../src/test/java/sample/ClientTest.java | 4 +- .../src/test/java/sample/WelloTest.java | 10 +- .../src/test/java/sample/impl/EmbedTestCase.java | 32 ++++- .../test/java/sample/impl/InControlTestCase.java | 147 --------------------- .../src/test/java/sample/impl/RunWSTestCase.java | 120 +++++++++++++++++ .../src/test/resources/Hello.wsdl | 4 +- .../src/test/resources/Upper.wsdl | 4 +- .../src/test/resources/test.composite | 19 ++- 9 files changed, 188 insertions(+), 170 deletions(-) delete mode 100644 sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java create mode 100644 sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/RunWSTestCase.java diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/SampleImplementationProcessor.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/SampleImplementationProcessor.java index cd7767672f..6017030d35 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/SampleImplementationProcessor.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/main/java/sample/impl/SampleImplementationProcessor.java @@ -140,7 +140,7 @@ public class SampleImplementationProcessor extends BaseStAXArtifactProcessor imp static WSDLInterface resolve(final QName name, final ModelResolver res, final ProcessorContext ctx, final WSDLFactory wif) throws ContributionResolveException { final WSDLInterface wi = res.resolveModel(WSDLInterface.class, interfaze(name, wif), ctx); if(!wi.isUnresolved()) - return wi; + return domBound(wi); final WSDLDefinition wd = res.resolveModel(WSDLDefinition.class, definition(wi.getName(), wif), ctx); if(wd.isUnresolved()) @@ -152,11 +152,23 @@ public class SampleImplementationProcessor extends BaseStAXArtifactProcessor imp try { final WSDLInterface nwi = wif.createWSDLInterface(pt.getElement(), wd, res, ctx.getMonitor()); nwi.setWsdlDefinition(wd); - nwi.resetDataBinding(DOMDataBinding.NAME); res.addModel(nwi, ctx); - return nwi; + return domBound(nwi); } catch(InvalidInterfaceException e) { throw new ContributionResolveException(e); } } + + /** + * Return a WSDL interface configured to use a DOM databinding. + */ + static WSDLInterface domBound(WSDLInterface wi) throws ContributionResolveException { + try { + final WSDLInterface domwi = (WSDLInterface)wi.clone(); + domwi.resetDataBinding(DOMDataBinding.NAME); + return domwi; + } catch(CloneNotSupportedException e) { + throw new ContributionResolveException(e); + } + } } diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/ClientTest.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/ClientTest.java index 3b635708ec..92291c98a1 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/ClientTest.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/ClientTest.java @@ -46,7 +46,7 @@ public class ClientTest { @Java(Hello.class) Hello jello; - @WSDL("http://sample#Hello") + @WSDL("http://sample/hello#Hello") WSDLReference wello; public String jello(String s) { @@ -56,7 +56,7 @@ public class ClientTest { public String wello(String s) { out.println("ClientTest.wello(" + s + ")"); - final Element hreq = xdom("http://sample", "hello", elem("name", text(s))); + final Element hreq = xdom("http://sample/hello", "hello", elem("name", text(s))); final Element hres = wello.call("hello", hreq); diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/WelloTest.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/WelloTest.java index 3b3a6a4566..1bb9fc11ce 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/WelloTest.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/WelloTest.java @@ -40,20 +40,20 @@ import sample.api.WSDLReference; * * @version $Rev$ $Date$ */ -@WSDL("http://sample#Hello") +@WSDL("http://sample/hello#Hello") public class WelloTest { - @WSDL("http://sample#Upper") + @WSDL("http://sample/upper#Upper") WSDLReference upper; public Element call(String op, Element e) { out.println("WelloTest." + op + "(" + xml(e) + ")"); final String name = xreduce(print, "", xfilter(select("name"), elems(e))); - final Element ureq = xdom("http://sample", "upper", elem("s", text("Hello " + name))); + final Element ureq = xdom("http://sample/upper", "upper", elem("s", text("Hello " + name))); final Element ures = upper.call("upper", ureq); - final String s = xreduce(print, "", xfilter(select("result"), elems(ures))); - return xdom("http://sample", "helloResponse", elem("result", text(s))); + final String s = xreduce(print, "", xfilter(select("result"), elems(ures))); + return xdom("http://sample/hello", "helloResponse", elem("result", text(s))); } } diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java index 41f6d95f3c..f30b5c9853 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java @@ -25,8 +25,10 @@ import static sample.impl.EmbedUtil.component; import static sample.impl.EmbedUtil.composite; import static sample.impl.EmbedUtil.contrib; import static sample.impl.EmbedUtil.deploy; +import static sample.impl.EmbedUtil.extensionPoints; import static sample.impl.EmbedUtil.implementation; import static sample.impl.EmbedUtil.node; +import static sample.impl.EmbedUtil.providerFactories; import static sample.impl.EmbedUtil.reference; import static sample.impl.EmbedUtil.service; import static sample.impl.EmbedUtil.wsdli; @@ -36,6 +38,9 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -50,7 +55,8 @@ import sample.WelloTest; /** * Test how to assemble a contribution, a SCDL composite and run it on an embedded - * Tuscany runtime node. + * Tuscany runtime node. Also shows how pass in a ProviderFactory instead of having + * it loaded and constructed by the runtime node. * * @version $Rev$ $Date$ */ @@ -60,12 +66,12 @@ public class EmbedTestCase { @BeforeClass public static void setUp() throws Exception { - // Load the test WSDL definitions (could also construct - // the WSDL and XSD models in code but that'd be quite - // painful, so just load them from XML for now) + // Load the test WSDL definitions (could also construct the WSDL + // and XSD models in code but that'd be quite painful, so just + // load them from XML for now) final Contribution contrib = contrib("test", here()); - WSDLInterface Hello_wsdl = wsdli("Hello.wsdl", "http://sample", "Hello", contrib); - WSDLInterface Upper_wsdl = wsdli("Upper.wsdl", "http://sample", "Upper", contrib); + WSDLInterface Hello_wsdl = wsdli("Hello.wsdl", "http://sample/hello", "Hello", contrib); + WSDLInterface Upper_wsdl = wsdli("Upper.wsdl", "http://sample/upper", "Upper", contrib); // Assemble a test composite model (see EmbedUtil // for the little DSL used here, much more concise @@ -91,12 +97,24 @@ public class EmbedTestCase { reference("upper", "upper-test")), component("upper-test", implementation(UpperTest.class, - service(Upper.class)))); + service(Upper.class)))); + + // Register a test instance of our sample implementation ProviderFactory + providerFactories().addProviderFactory(testProviderFactory()); // Run with it node = node(deploy(contrib, comp)); node.start(); } + + static ProviderFactory testProviderFactory() { + // This shows how to get called when a provider is created + return new SampleProviderFactory(extensionPoints()) { + public ImplementationProvider createImplementationProvider(RuntimeComponent comp, SampleImplementation impl) { + out.println("Creating a provider for component " + comp.getName()); + return super.createImplementationProvider(comp, impl); + }}; + } @AfterClass public static void tearDown() throws Exception { diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java deleted file mode 100644 index 6523e0b233..0000000000 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/InControlTestCase.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * 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 sample.impl; - -import static java.lang.System.out; -import static org.junit.Assert.assertEquals; -import static sample.impl.EmbedUtil.component; -import static sample.impl.EmbedUtil.composite; -import static sample.impl.EmbedUtil.contrib; -import static sample.impl.EmbedUtil.deploy; -import static sample.impl.EmbedUtil.extensionPoints; -import static sample.impl.EmbedUtil.implementation; -import static sample.impl.EmbedUtil.node; -import static sample.impl.EmbedUtil.providerFactories; -import static sample.impl.EmbedUtil.reference; -import static sample.impl.EmbedUtil.service; -import static sample.impl.EmbedUtil.wsdli; -import static sample.impl.TestUtil.here; - -import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; -import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.provider.ImplementationProvider; -import org.apache.tuscany.sca.provider.ProviderFactory; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -import sample.Client; -import sample.ClientTest; -import sample.Hello; -import sample.JelloTest; -import sample.Upper; -import sample.UpperTest; -import sample.WelloTest; - -/** - * Test how to assemble a contribution, a SCDL composite and run it on an embedded - * Tuscany runtime node. Same as EmbedTestCase but shows how stay in control and - * pass in a ProviderFactory instead of having it loaded and constructed by the - * runtime node. - * - * @version $Rev$ $Date$ - */ -public class InControlTestCase { - static Node node; - - @BeforeClass - public static void setUp() throws Exception { - - // Load the test WSDL definitions (could also construct - // the WSDL and XSD models in code but that'd be quite - // painful, so just load them from XML for now) - final Contribution contrib = contrib("test", here()); - WSDLInterface Hello_wsdl = wsdli("Hello.wsdl", "http://sample", "Hello", contrib); - WSDLInterface Upper_wsdl = wsdli("Upper.wsdl", "http://sample", "Upper", contrib); - - // Assemble a test composite model (see EmbedUtil - // for the little DSL used here, much more concise - // than using the assembly model interfaces) - final Composite comp = - composite("http://sample", "test", - component("client-test", - implementation(ClientTest.class, - service(Client.class), - reference("jello", Hello.class), - reference("wello", Hello_wsdl)), - reference("jello", "jello-test"), - reference("wello", "wello-test")), - component("wello-test", - implementation(WelloTest.class, - service(Hello_wsdl), - reference("upper", Upper_wsdl)), - reference("upper", "upper-test")), - component("jello-test", - implementation(JelloTest.class, - service(Hello.class), - reference("upper", Upper.class)), - reference("upper", "upper-test")), - component("upper-test", - implementation(UpperTest.class, - service(Upper.class)))); - - // Register a test instance of our sample implementation ProviderFactory - providerFactories().addProviderFactory(testProviderFactory()); - - // Run with it - node = node(deploy(contrib, comp)); - node.start(); - } - - static ProviderFactory testProviderFactory() { - // This shows that the embedder can take control and get called - // when a provider is created, and return whatever provider, then - // invoker, implementation instance etc - return new SampleProviderFactory(extensionPoints()) { - public ImplementationProvider createImplementationProvider(RuntimeComponent comp, SampleImplementation impl) { - out.println("Creating a provider for component " + comp.getName()); - return super.createImplementationProvider(comp, impl); - } - }; - } - - @AfterClass - public static void tearDown() throws Exception { - node.stop(); - } - - @Test - public void jello() { - out.println("RunTestCase.jello"); - final String r = client().jello("Java"); - out.println(r); - assertEquals("HELLO JAVA", r); - } - - @Test - public void wello() { - out.println("RunTestCase.wello"); - final String r = client().wello("WSDL"); - out.println(r); - assertEquals("HELLO WSDL", r); - } - - static Client client() { - return node.getService(Client.class, "client-test/Client"); - } -} diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/RunWSTestCase.java b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/RunWSTestCase.java new file mode 100644 index 0000000000..61133d0fbf --- /dev/null +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/java/sample/impl/RunWSTestCase.java @@ -0,0 +1,120 @@ +/* + * 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 sample.impl; + +import static java.lang.System.out; +import static org.junit.Assert.assertTrue; +import static sample.impl.TestUtil.here; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.Socket; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.http.jetty.JettyServer; +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test a component that provides and consumes SOAP Web services. + * + * @version $Rev$ $Date$ + */ +public class RunWSTestCase { + static Node node; + static JettyServer jetty; + + @BeforeClass + public static void setUp() throws Exception { + // Start test composite on a Tuscany node + final NodeFactory nf = NodeFactory.newInstance(); + node = nf.createNode(new Contribution("test", here())); + node.start(); + + // Mock up a test Web service on http://localhost:8086/wsupper + jetty = new JettyServer((ExtensionPointRegistry)nf.getExtensionPointRegistry()); + jetty.start(); + jetty.addServletMapping("http://localhost:8086/wsupper", new HttpServlet() { + private static final long serialVersionUID = 1L; + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + assertTrue(read(req.getInputStream()).contains("Hello SOAP")); + final String soapresp = + "" + + "" + + "HELLO SOAP" + + ""; + + write(soapresp, resp.getOutputStream()); + } + }); + } + + @AfterClass + public static void tearDown() throws Exception { + jetty.stop(); + node.stop(); + } + + @Test + public void wsello() throws Exception { + out.println("RunWSTestCase.wsello"); + // Send a SOAP request to the Web service provided by SCA component wsello-test + // on http://localhost:8085/wsello + final Socket s = new Socket("localhost", 8085); + final String soapreq = + "POST /wsello HTTP/1.0\r\n" + + "Content-Type: text/xml; charset=UTF-8\r\n" + + "Content-length: 231\r\n\r\n" + + "" + + "" + + "SOAP" + + ""; + write(soapreq, s.getOutputStream()); + assertTrue(read(s.getInputStream()).contains("HELLO SOAP")); + } + + static void write(final String s, final OutputStream o) throws IOException { + final OutputStreamWriter w = new OutputStreamWriter(o); + w.write(s); + w.flush(); + } + + static String read(final InputStream i) throws IOException { + return read(new BufferedReader(new InputStreamReader(i))); + } + + static String read(final BufferedReader r) throws IOException { + final String s = r.readLine(); + return s == null? "" : s + read(r); + } +} diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Hello.wsdl b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Hello.wsdl index 561e560e7a..360cea5ff3 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Hello.wsdl +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Hello.wsdl @@ -17,13 +17,13 @@ * specific language governing permissions and limitations * under the License. --> - - diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Upper.wsdl b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Upper.wsdl index 60eaec9f27..8a148bb5e8 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Upper.wsdl +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/Upper.wsdl @@ -17,13 +17,13 @@ * specific language governing permissions and limitations * under the License. --> - - diff --git a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/test.composite b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/test.composite index 535488b74e..43a75f50a7 100644 --- a/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/test.composite +++ b/sandbox/sebastien/java/embed/samples/implementation-extension/src/test/resources/test.composite @@ -20,24 +20,39 @@ - + name="wstest"> + + + + + + + + + + + + + + + + -- cgit v1.2.3