Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/8172297/Main.java
38853 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8172297 819649126* @summary Test that carriage-return and new-line characters27* are preserved in webservice parameters28* @compile ws/HelloWorld.java ws/HelloWorldImpl.java Main.java29* @run testng/othervm Main30*/3132import java.io.IOException;33import java.net.ServerSocket;34import java.net.URL;35import java.util.Collections;36import java.util.Set;37import java.util.concurrent.CountDownLatch;3839import javax.xml.namespace.QName;40import javax.xml.soap.SOAPMessage;41import javax.xml.ws.Endpoint;42import javax.xml.ws.Service;43import javax.xml.ws.handler.Handler;44import javax.xml.ws.handler.MessageContext;45import javax.xml.ws.handler.soap.SOAPHandler;46import javax.xml.ws.handler.soap.SOAPMessageContext;4748import org.testng.Assert;49import org.testng.annotations.DataProvider;50import org.testng.annotations.Test;5152import ws.HelloWorld;53import ws.HelloWorldImpl;5455public class Main {5657@Test(dataProvider="callHandlerDataProvider")58public void runTest(boolean callGetMessageInHandler) throws Exception {59CountDownLatch serverInitSignal = new CountDownLatch(1);60CountDownLatch testDoneSignal = new CountDownLatch(1);6162WebserviceRunner serverThread = new WebserviceRunner(serverInitSignal, testDoneSignal);63(new Thread(serverThread)).start();6465serverInitSignal.await();6667boolean paramModified = runClientCode(serverThread.getPort(), callGetMessageInHandler);6869testDoneSignal.countDown();7071Assert.assertEquals(callGetMessageInHandler, paramModified,72"WS parameter has not been processed as expected");73}7475@DataProvider76public Object[][] callHandlerDataProvider() {77return new Object[][]{{true}, {false}};78}7980/*81* Connects to launched web service endpoint, sends message with CR/NL symbols and82* checks if it was modified during the round trip client/server communication.83*/84private boolean runClientCode(int port, boolean callGetMessage) throws Exception {85System.out.println("Launching WS client connection on " + port + " port");86URL url = new URL("http://localhost:" + port + "/ws/hello?wsdl");87QName qname = new QName("http://ws/", "HelloWorldImplService");88Service service = Service.create(url, qname);8990registerHandler(service, callGetMessage);9192HelloWorld hello = (HelloWorld) service.getPort(HelloWorld.class);9394logStringContent("Client input parameter", WS_PARAM_VALUE);9596String response = hello.getHelloWorldAsString(WS_PARAM_VALUE);97logStringContent("Client response parameter", response);9899return !WS_PARAM_VALUE.equals(response);100}101102/*103* Register message handler and call SOAPMessageContext.getMessage104* to emulate issue reported in JDK-8196491105*/106private void registerHandler(Service service, final boolean callGetMessage) {107System.out.printf( "Client %s call getMessage inside message handler%n",108callGetMessage ? "will" : "will not" );109// Set custom SOAP message handler resolver110service.setHandlerResolver(portInfo -> {111Handler h = new SOAPHandler<SOAPMessageContext>() {112113@Override114public boolean handleMessage(SOAPMessageContext context) {115if (callGetMessage) {116// Trigger exception from JDK-8196491117SOAPMessage msg = context.getMessage();118}119return true;120}121122@Override123public boolean handleFault(SOAPMessageContext context) {124return true;125}126127@Override128public void close(MessageContext context) {129}130131@Override132public Set<QName> getHeaders() {133return null;134}135136};137return Collections.singletonList(h);138});139}140141/*142* Outputs the parameter value with newline and carriage-return symbols143* replaced with #CR and #NL text abbreviations.144*/145private static void logStringContent(String description, String parameter) {146String readableContent = parameter.replaceAll("\r", "#CR")147.replaceAll("\n", "#NL");148System.out.println(description + ": '" + readableContent + "'");149}150151/* Web service parameter value with newline and carriage-return symbols */152private final static String WS_PARAM_VALUE = "\r\r\n\r\r CarriageReturn and "153+"NewLine \r\n\n Test \r\r\r\r";154155/*156* Web service server thread that publishes WS on vacant port and waits157* for client to finalize testing158*/159class WebserviceRunner implements Runnable {160// Latch used to signalize when WS endpoint is initialized161private final CountDownLatch initSignal;162// Latch used to signalize when client completed testing163private final CountDownLatch doneSignal;164// Port where WS endpoint is published165private volatile int port = 0;166167// Constructor168WebserviceRunner(CountDownLatch initSignal, CountDownLatch doneSignal) {169this.initSignal = initSignal;170this.doneSignal = doneSignal;171}172173// Returns port of the published endpoint174public int getPort() {175return port;176}177178/*179* Publish web service on vacant port and waits for the client to180* complete testing.181*/182public void run() {183try {184// Find vacant port number185ServerSocket ss = new ServerSocket(0);186port = ss.getLocalPort();187ss.close();188189// Publish WebService190System.out.println("Publishing WebService on " + port + " port");191Endpoint ep = Endpoint.publish("http://localhost:" + port + "/ws/hello", new HelloWorldImpl());192193// Notify main thread that WS endpoint is published194initSignal.countDown();195196// Wait for main thread to complete testing197System.out.println("Waiting for done signal from test client.");198doneSignal.await();199200// Terminate WS endpoint201System.out.println("Got done signal from the client. Stopping WS endpoint.");202ep.stop();203} catch (IOException ioe) {204System.out.println("Failed to get vacant port number:" + ioe);205} catch (InterruptedException ie) {206System.out.println("Failed to wait for test completion:" + ie);207}208}209}210}211212213