|
Apache CXF example source code file (ClientServerTest.java)
The Apache CXF ClientServerTest.java source code/** * 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.cxf.systest.jaxws; import java.io.InputStream; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.logging.Logger; import javax.xml.namespace.QName; import javax.xml.ws.AsyncHandler; import javax.xml.ws.BindingProvider; import javax.xml.ws.Endpoint; import javax.xml.ws.Response; import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import javax.xml.xpath.XPathConstants; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.apache.cxf.Bus; import org.apache.cxf.binding.soap.Soap11; import org.apache.cxf.bus.CXFBusFactory; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.configuration.security.AuthorizationPolicy; import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.dynamic.DynamicClientFactory; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.helpers.XMLUtils; import org.apache.cxf.helpers.XPathUtils; import org.apache.cxf.message.Message; import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.hello_world_soap_http.BadRecordLitFault; import org.apache.hello_world_soap_http.DocLitBare; import org.apache.hello_world_soap_http.Greeter; import org.apache.hello_world_soap_http.NoSuchCodeLitFault; import org.apache.hello_world_soap_http.SOAPService; import org.apache.hello_world_soap_http.SOAPServiceBogusAddressTest; import org.apache.hello_world_soap_http.SOAPServiceDocLitBare; import org.apache.hello_world_soap_http.SOAPServiceMultiPortTypeTest; import org.apache.hello_world_soap_http.types.BareDocumentResponse; import org.apache.hello_world_soap_http.types.GreetMeLaterResponse; import org.apache.hello_world_soap_http.types.GreetMeResponse; import org.junit.BeforeClass; import org.junit.Test; public class ClientServerTest extends AbstractBusClientServerTestBase { static final String PORT = allocatePort(Server.class); static final String BARE_PORT = allocatePort(Server.class, 1); static final String BOGUS_REAL_PORT = allocatePort(Server.class, 2); static final String BOGUS_PORT = allocatePort(Server.class, 3); static final String PUB_PORT = allocatePort(Server.class, 4); static final Logger LOG = LogUtils.getLogger(ClientServerTest.class); private final QName serviceName = new QName("http://apache.org/hello_world_soap_http", "SOAPService"); private final QName portName = new QName("http://apache.org/hello_world_soap_http", "SoapPort"); private final QName fakePortName = new QName("http://apache.org/hello_world_soap_http", "FakePort"); private final QName portName1 = new QName("http://apache.org/hello_world_soap_http", "SoapPort2"); @BeforeClass public static void startServers() throws Exception { // set up configuration to enable schema validation URL url = ClientServerTest.class.getResource("fault-stack-trace.xml"); assertNotNull("cannot find test resource", url); defaultConfigFileName = url.toString(); assertTrue("server did not launch correctly", launchServer(Server.class)); } @Test public void testCXF2419() throws Exception { org.apache.cxf.hello_world.elrefs.SOAPService serv = new org.apache.cxf.hello_world.elrefs.SOAPService(); org.apache.cxf.hello_world.elrefs.Greeter g = serv.getSoapPort(); updateAddressPort(g, PORT); assertEquals("Hello CXF", g.greetMe("CXF")); } @Test public void testBase64() throws Exception { URL wsdl = getClass().getResource("/wsdl/others/dynamic_client_base64.wsdl"); assertNotNull(wsdl); String wsdlUrl = null; wsdlUrl = wsdl.toURI().toString(); CXFBusFactory busFactory = new CXFBusFactory(); Bus bus = busFactory.createBus(); DynamicClientFactory dynamicClientFactory = DynamicClientFactory.newInstance(bus); Client client = dynamicClientFactory.createClient(wsdlUrl); assertNotNull(client); } @Test public void testJaxWsDynamicClient() throws Exception { URL wsdl = getClass().getResource("/wsdl/others/dynamic_client_base64.wsdl"); assertNotNull(wsdl); String wsdlUrl = null; wsdlUrl = wsdl.toURI().toString(); CXFBusFactory busFactory = new CXFBusFactory(); Bus bus = busFactory.createBus(); org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory dynamicClientFactory = org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory.newInstance(bus); Client client = dynamicClientFactory.createClient(wsdlUrl); assertNotNull(client); } @Test public void testBasicConnection() throws Exception { SOAPService service = new SOAPService(); assertNotNull(service); Greeter greeter = service.getPort(portName, Greeter.class); updateAddressPort(greeter, PORT); try { greeter.greetMe("test"); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals("Bonjour", reply); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } BindingProvider bp = (BindingProvider)greeter; Map<String, Object> responseContext = bp.getResponseContext(); Integer responseCode = (Integer) responseContext.get(Message.RESPONSE_CODE); assertEquals(200, responseCode.intValue()); } @Test public void testTimeoutConfigutation() throws Exception { SOAPService service = new SOAPService(); assertNotNull(service); Greeter greeter = service.getPort(portName, Greeter.class); updateAddressPort(greeter, PORT); ((javax.xml.ws.BindingProvider)greeter).getRequestContext().put("javax.xml.ws.client.receiveTimeout", "1"); try { greeter.greetMe("test"); // remove fail() check to let this test pass in the powerful machine } catch (Throwable ex) { Object cause = null; if (ex.getCause() != null) { cause = ex.getCause(); } assertTrue("Timeout cause is expected", cause instanceof java.net.SocketTimeoutException); } } @Test public void testNillable() throws Exception { SOAPService service = new SOAPService(); assertNotNull(service); Greeter greeter = service.getPort(portName, Greeter.class); updateAddressPort(greeter, PORT); try { String reply = greeter.testNillable("test", 100); assertEquals("test", reply); reply = greeter.testNillable(null, 100); assertNull(reply); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testAddPortWithSpecifiedSoap12Binding() throws Exception { Service service = Service.create(serviceName); service.addPort(fakePortName, javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING, "http://localhost:" + PORT + "/SoapContext/SoapPort"); Greeter greeter = service.getPort(fakePortName, Greeter.class); String response = new String("Bonjour"); try { greeter.greetMe("test"); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals(response, reply); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testAddPortWithSpecifiedSoap11Binding() throws Exception { Service service = Service.create(serviceName); service.addPort(fakePortName, javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:" + PORT + "/SoapContext/SoapPort"); Greeter greeter = service.getPort(fakePortName, Greeter.class); String response = new String("Bonjour"); try { greeter.greetMe("test"); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals(response, reply); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testAddPort() throws Exception { Service service = Service.create(serviceName); service.addPort(fakePortName, "http://schemas.xmlsoap.org/soap/", "http://localhost:" + PORT + "/SoapContext/SoapPort"); Greeter greeter = service.getPort(fakePortName, Greeter.class); String response = new String("Bonjour"); try { greeter.greetMe("test"); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals(response, reply); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testGetPortOneParam() throws Exception { URL url = getClass().getResource("/wsdl/hello_world.wsdl"); Service service = Service.create(url, serviceName); Greeter greeter = service.getPort(Greeter.class); String response = new String("Bonjour"); try { ((BindingProvider)greeter).getRequestContext() .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + PORT + "/SoapContext/SoapPort"); greeter.greetMe("test"); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals(response, reply); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testDocLitBareConnection() throws Exception { SOAPServiceDocLitBare service = new SOAPServiceDocLitBare(); assertNotNull(service); DocLitBare greeter = service.getPort(portName1, DocLitBare.class); updateAddressPort(greeter, BARE_PORT); try { BareDocumentResponse bareres = greeter.testDocLitBare("MySimpleDocument"); assertNotNull("no response for operation testDocLitBare", bareres); assertEquals("CXF", bareres.getCompany()); assertTrue(bareres.getId() == 1); } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testBasicConnectionAndOneway() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); SOAPService service = new SOAPService(wsdl, serviceName); assertNotNull(service); Greeter greeter = service.getPort(portName, Greeter.class); updateAddressPort(greeter, PORT); String response1 = new String("Hello Milestone-"); String response2 = new String("Bonjour"); try { for (int idx = 0; idx < 1; idx++) { String greeting = greeter.greetMe("Milestone-" + idx); assertNotNull("no response received from service", greeting); String exResponse = response1 + idx; assertEquals(exResponse, greeting); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals(response2, reply); greeter.greetMeOneWay("Milestone-" + idx); } } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testBasicConnection2() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); SOAPService service = new SOAPService(wsdl, serviceName); assertNotNull(service); //getPort only passing in SEI Greeter greeter = service.getPort(Greeter.class); ((BindingProvider)greeter).getRequestContext() .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + PORT + "/SoapContext/SoapPort"); String response1 = new String("Hello Milestone-"); String response2 = new String("Bonjour"); try { for (int idx = 0; idx < 5; idx++) { String greeting = greeter.greetMe("Milestone-" + idx); assertNotNull("no response received from service", greeting); String exResponse = response1 + idx; assertEquals(exResponse, greeting); String reply = greeter.sayHi(); assertNotNull("no response received from service", reply); assertEquals(response2, reply); greeter.greetMeOneWay("Milestone-" + idx); } } catch (UndeclaredThrowableException ex) { throw (Exception)ex.getCause(); } } @Test public void testAsyncPollingCall() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); SOAPService service = new SOAPService(wsdl, serviceName); assertNotNull(service); Greeter greeter = service.getPort(portName, Greeter.class); assertNotNull(service); updateAddressPort(greeter, PORT); long before = System.currentTimeMillis(); long delay = 3000; Response<GreetMeLaterResponse> r1 = greeter.greetMeLaterAsync(delay); Response<GreetMeLaterResponse> r2 = greeter.greetMeLaterAsync(delay); long after = System.currentTimeMillis(); assertTrue("Duration of calls exceeded " + (2 * delay) + " ms", after - before < (2 * delay)); // first time round, responses should not be available yet assertFalse("Response already available.", r1.isDone()); assertFalse("Response already available.", r2.isDone()); // after three seconds responses should be available long waited = 0; while (waited < (delay + 1000)) { try { Thread.sleep(500); } catch (InterruptedException ex) { // ignore } if (r1.isDone() && r2.isDone()) { break; } waited += 500; } assertTrue("Response is not available.", r1.isDone()); assertTrue("Response is not available.", r2.isDone()); } @Test public void testAsyncSynchronousPolling() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); SOAPService service = new SOAPService(wsdl, serviceName); assertNotNull(service); final String expectedString = new String("Hello, finally!"); class Poller extends Thread { Response<GreetMeLaterResponse> response; int tid; Poller(Response<GreetMeLaterResponse> r, int t) { response = r; tid = t; } public void run() { if (tid % 2 > 0) { while (!response.isDone()) { try { Thread.sleep(100); } catch (InterruptedException ex) { // ignore } } } GreetMeLaterResponse reply = null; try { reply = response.get(); } catch (Exception ex) { fail("Poller " + tid + " failed with " + ex); } assertNotNull("Poller " + tid + ": no response received from service", reply); String s = reply.getResponseType(); assertEquals(expectedString, s); } } Greeter greeter = (Greeter)service.getPort(portName, Greeter.class); updateAddressPort(greeter, PORT); long before = System.currentTimeMillis(); long delay = 3000; Response<GreetMeLaterResponse> response = greeter.greetMeLaterAsync(delay); long after = System.currentTimeMillis(); assertTrue("Duration of calls exceeded " + delay + " ms", after - before < delay); // first time round, responses should not be available yet assertFalse("Response already available.", response.isDone()); Poller[] pollers = new Poller[4]; for (int i = 0; i < pollers.length; i++) { pollers[i] = new Poller(response, i); } for (Poller p : pollers) { p.start(); } for (Poller p : pollers) { p.join(); } } static class MyHandler implements AsyncHandler<GreetMeLaterResponse> { static int invocationCount; private String replyBuffer; public void handleResponse(Response<GreetMeLaterResponse> response) { invocationCount++; try { GreetMeLaterResponse reply = response.get(); replyBuffer = reply.getResponseType(); } catch (InterruptedException ex) { ex.printStackTrace(); } catch (ExecutionException ex) { ex.printStackTrace(); } } String getReplyBuffer() { return replyBuffer; } } @Test public void testAsyncCallUseProperAssignedExecutor() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); SOAPService service = new SOAPService(wsdl, serviceName); class TestExecutor implements Executor { private int count; public void execute(Runnable command) { count++; LOG.info("asyn call time " + count); command.run(); } public int getCount() { return count; } } Executor executor = new TestExecutor(); service.setExecutor(executor); assertNotNull(service); assertSame(executor, service.getExecutor()); assertEquals(((TestExecutor)executor).getCount(), 0); Greeter greeter = service.getPort(portName, Greeter.class); updateAddressPort(greeter, PORT); List<Response Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF ClientServerTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.