Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/xsanymixed/Test.java
38853 views
/*1* Copyright (c) 2014, 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 8036981 8038966 805144126* @summary the content of xs:any content:mixed should remain as is,27* no white space changes and no changes to namespace prefixes28* @run shell compile-wsdl.sh29* @run main/othervm Test30*/3132import com.sun.net.httpserver.HttpServer;3334import javax.xml.transform.Source;35import javax.xml.transform.Transformer;36import javax.xml.transform.TransformerException;37import javax.xml.transform.TransformerFactory;38import javax.xml.transform.stream.StreamResult;39import javax.xml.transform.stream.StreamSource;40import javax.xml.ws.Dispatch;41import javax.xml.ws.Endpoint;42import javax.xml.ws.Service;43import java.io.ByteArrayOutputStream;44import java.io.IOException;45import java.io.StringReader;46import java.net.InetSocketAddress;47import java.net.URL;48import java.nio.file.FileVisitResult;49import java.nio.file.Files;50import java.nio.file.Path;51import java.nio.file.Paths;52import java.nio.file.SimpleFileVisitor;53import java.nio.file.attribute.BasicFileAttributes;5455import static java.nio.file.FileVisitResult.CONTINUE;5657public class Test {5859private static HttpServer httpServer;60private static Endpoint endpoint;61private static final String NL = System.getProperty("line.separator");6263private static final String XS_ANY_MIXED_PART =64"<AppHdr xmlns=\"urn:head.001\">" + NL +65" <Fr>" + NL + NL +66"<FIId xmlns=\"urn:head.009\">" + NL + NL +67" any" + NL +68" white" + NL +69" space" + NL + NL +70" <FinInstnId>... and" + NL + NL +71" NO namespace prefixes!!!" + NL + NL +72" </FinInstnId>" + NL + NL +73" </FIId>" + NL +74"</Fr>" + NL +75"</AppHdr>";7677private static final String XML_REQUEST = "<soap:Envelope " +78"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +79"xmlns:ws=\"http://ws.somewhere.org/\">" +80"<soap:Header/><soap:Body>" +81"<ws:echoRequest>" + NL +82XS_ANY_MIXED_PART + NL +83"</ws:echoRequest>" +84"</soap:Body></soap:Envelope>";8586private static String deployWebservice() throws IOException {87// Manually create HttpServer here using ephemeral address for port88// so as to not end up with attempt to bind to an in-use port89httpServer = HttpServer.create(new InetSocketAddress(0), 0);90httpServer.start();91endpoint = Endpoint.create(new ServiceImpl());92endpoint.publish(httpServer.createContext("/wservice"));9394String wsdlAddress = "http://localhost:" + httpServer.getAddress().getPort() + "/wservice?wsdl";95log("address = " + wsdlAddress);96return wsdlAddress;97}9899private static void stopWebservice() {100if (endpoint != null && endpoint.isPublished()) {101endpoint.stop();102}103if (httpServer != null) {104httpServer.stop(0);105}106}107108public static void main(String[] args) throws IOException, TransformerException {109110try {111String address = deployWebservice();112Service service = Service.create(new URL(address), ServiceImpl.SERVICE_NAME);113114Dispatch<Source> d = service.createDispatch(ServiceImpl.PORT_NAME, Source.class, Service.Mode.MESSAGE);115Source response = d.invoke(new StreamSource(new StringReader(XML_REQUEST)));116117String resultXml = toString(response);118119log("= request ======== \n");120log(XML_REQUEST);121log("= result ========= \n");122log(resultXml);123log("\n==================");124125boolean xsAnyMixedPartSame = resultXml.contains(XS_ANY_MIXED_PART);126log("resultXml.contains(XS_ANY_PART) = " + xsAnyMixedPartSame);127if (!xsAnyMixedPartSame) {128fail("The xs:any content=mixed part is supposed to be same in request and response.");129throw new RuntimeException();130}131132log("TEST PASSED");133} finally {134stopWebservice();135136// if you need to debug or explore wsdl generation result137// comment this line out:138deleteGeneratedFiles();139}140}141142private static String toString(Source response) throws TransformerException, IOException {143ByteArrayOutputStream bos = new ByteArrayOutputStream();144TransformerFactory transformerFactory = TransformerFactory.newInstance();145Transformer transformer = transformerFactory.newTransformer();146transformer.transform(response, new StreamResult(bos));147bos.close();148return new String(bos.toByteArray());149}150151private static void fail(String message) {152log("TEST FAILED.");153throw new RuntimeException(message);154}155156private static void log(String msg) {157System.out.println(msg);158}159160private static void deleteGeneratedFiles() {161Path p = Paths.get("..", "classes", "javax", "xml", "ws", "xsanymixed", "org");162System.out.println("performing cleanup, deleting wsdl compilation result: " + p.toFile().getAbsolutePath());163if (Files.exists(p)) {164try {165Files.walkFileTree(p, new SimpleFileVisitor<Path>() {166@Override167public FileVisitResult visitFile(168Path file,169BasicFileAttributes attrs) throws IOException {170171System.out.println("deleting file [" + file.toFile().getAbsoluteFile() + "]");172Files.delete(file);173return CONTINUE;174}175176@Override177public FileVisitResult postVisitDirectory(178Path dir,179IOException exc) throws IOException {180181System.out.println("deleting dir [" + dir.toFile().getAbsoluteFile() + "]");182if (exc == null) {183Files.delete(dir);184return CONTINUE;185} else {186throw exc;187}188}189});190} catch (IOException ioe) {191ioe.printStackTrace();192}193}194}195196}197198199