Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/clientjar/TestWsImport.java
38853 views
/*1* Copyright (c) 2013, 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 8016271 802640526* @summary wsimport -clientjar does not create portable jar on windows due to hardcoded '\'27* @run main/othervm TestWsImport28*/2930import javax.xml.namespace.QName;31import javax.xml.ws.Endpoint;32import javax.xml.ws.Service;33import java.io.InputStreamReader;34import java.io.IOException;35import java.io.BufferedReader;36import java.io.File;37import java.net.InetSocketAddress;38import java.net.URL;39import java.nio.file.Files;40import java.nio.file.FileVisitResult;41import java.nio.file.Path;42import java.nio.file.Paths;43import java.nio.file.SimpleFileVisitor;44import java.nio.file.attribute.BasicFileAttributes;45import static java.nio.file.FileVisitResult.*;46import java.util.Enumeration;47import java.util.jar.JarFile;4849import com.sun.net.httpserver.HttpContext;50import com.sun.net.httpserver.HttpServer;5152public class TestWsImport {5354public static void main(String[] args) throws IOException {5556String javaHome = System.getProperty("java.home");57if (javaHome.endsWith("jre")) {58javaHome = new File(javaHome).getParent();59}60String wsimport = javaHome + File.separator + "bin" + File.separator + "wsimport";61if (System.getProperty("os.name").startsWith("Windows")) {62wsimport = wsimport.concat(".exe");63}6465Endpoint endpoint = Endpoint.create(new TestService());66HttpServer httpServer = null;67try {68// Manually create HttpServer here using ephemeral address for port69// so as to not end up with attempt to bind to an in-use port70httpServer = HttpServer.create(new InetSocketAddress(0), 0);71HttpContext httpContext = httpServer.createContext("/hello");72int port = httpServer.getAddress().getPort();73System.out.println("port = " + port);74httpServer.start();75endpoint.publish(httpContext);76String address = "http://localhost:" + port + "/hello";7778Service service = Service.create(new URL(address + "?wsdl"),79new QName("http://test/jaxws/sample/", "TestService"));8081String[] wsargs = {82wsimport,83"-p",84"wstest",85"-J-Djavax.xml.accessExternalSchema=all",86"-J-Dcom.sun.tools.internal.ws.Invoker.noSystemProxies=true",87address + "?wsdl",88"-clientjar",89"wsjar.jar"90};91ProcessBuilder pb = new ProcessBuilder(wsargs);92pb.redirectErrorStream(true);93Process p = pb.start();94BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));95String s = r.readLine();96while (s != null) {97System.out.println(s.trim());98s = r.readLine();99}100p.waitFor();101p.destroy();102103try (JarFile jarFile = new JarFile("wsjar.jar")) {104for (Enumeration em = jarFile.entries(); em.hasMoreElements();) {105String fileName = em.nextElement().toString();106if (fileName.contains("\\")) {107throw new RuntimeException("\"\\\" character detected in jar file: " + fileName);108}109}110}111} catch (Exception e) {112e.printStackTrace();113throw new RuntimeException(e.getMessage());114} finally {115endpoint.stop();116if (httpServer != null) {117httpServer.stop(0);118}119Path p = Paths.get("wsjar.jar");120Files.deleteIfExists(p);121p = Paths.get("wstest");122if (Files.exists(p)) {123try {124Files.walkFileTree(p, new SimpleFileVisitor<Path>() {125@Override126public FileVisitResult visitFile(Path file,127BasicFileAttributes attrs) throws IOException {128129Files.delete(file);130return CONTINUE;131}132@Override133public FileVisitResult postVisitDirectory(Path dir,134IOException exc) throws IOException {135136if (exc == null) {137Files.delete(dir);138return CONTINUE;139} else {140throw exc;141}142}143});144} catch (IOException ioe) {145ioe.printStackTrace();146}147}148}149}150}151152153