jdk/test/javax/xml/ws/clientjar/TestWsImport.java
changeset 20526 ca6b9674ee4e
child 21351 3509dd1d1cab
equal deleted inserted replaced
20525:2d957e35a52f 20526:ca6b9674ee4e
       
     1 /*
       
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8016271
       
    27  * @summary wsimport -clientjar does not create portable jar on windows due to hardcoded '\'
       
    28  * @run main/othervm TestWsImport
       
    29  */
       
    30 
       
    31 import javax.xml.namespace.QName;
       
    32 import javax.xml.ws.Endpoint;
       
    33 import javax.xml.ws.Service;
       
    34 import java.io.InputStreamReader;
       
    35 import java.io.IOException;
       
    36 import java.io.BufferedReader;
       
    37 import java.io.File;
       
    38 import java.net.InetSocketAddress;
       
    39 import java.net.URL;
       
    40 import java.nio.file.Files;
       
    41 import java.nio.file.FileVisitResult;
       
    42 import java.nio.file.Path;
       
    43 import java.nio.file.Paths;
       
    44 import java.nio.file.SimpleFileVisitor;
       
    45 import java.nio.file.attribute.BasicFileAttributes;
       
    46 import static java.nio.file.FileVisitResult.*;
       
    47 import java.util.Enumeration;
       
    48 import java.util.jar.JarFile;
       
    49 
       
    50 import com.sun.net.httpserver.HttpContext;
       
    51 import com.sun.net.httpserver.HttpServer;
       
    52 
       
    53 public class TestWsImport {
       
    54 
       
    55     public static void main(String[] args) throws IOException {
       
    56 
       
    57         String javaHome = System.getProperty("java.home");
       
    58         if (javaHome.endsWith("jre")) {
       
    59             javaHome = new File(javaHome).getParent();
       
    60         }
       
    61         String wsimport = javaHome + File.separator + "bin" + File.separator + "wsimport";
       
    62         if (System.getProperty("os.name").startsWith("Windows")) {
       
    63             wsimport = wsimport.concat(".exe");
       
    64         }
       
    65 
       
    66         Endpoint endpoint = Endpoint.create(new TestService());
       
    67         HttpServer httpServer = null;
       
    68         try {
       
    69             // Manually create HttpServer here using ephemeral address for port
       
    70             // so as to not end up with attempt to bind to an in-use port
       
    71             httpServer = HttpServer.create(new InetSocketAddress(0), 0);
       
    72             HttpContext httpContext = httpServer.createContext("/hello");
       
    73             int port = httpServer.getAddress().getPort();
       
    74             System.out.println("port = " + port);
       
    75             httpServer.start();
       
    76             endpoint.publish(httpContext);
       
    77             String address = "http://localhost:" + port + "/hello";
       
    78 
       
    79             Service service = Service.create(new URL(address + "?wsdl"),
       
    80                 new QName("http://test/jaxws/sample/",
       
    81                 "TestService"));
       
    82 
       
    83             String[] wsargs = {
       
    84                 wsimport,
       
    85                 "-p",
       
    86                 "wstest",
       
    87                 "-J-Djavax.xml.accessExternalSchema=all",
       
    88                 address + "?wsdl",
       
    89                 "-clientjar",
       
    90                 "wsjar.jar"
       
    91             };
       
    92             ProcessBuilder pb = new ProcessBuilder(wsargs);
       
    93             pb.redirectErrorStream(true);
       
    94             Process p = pb.start();
       
    95             BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
    96             String s = r.readLine();
       
    97             while (s != null) {
       
    98                 System.out.println(s.trim());
       
    99                 s = r.readLine();
       
   100             }
       
   101             p.waitFor();
       
   102             p.destroy();
       
   103 
       
   104             try (JarFile jarFile = new JarFile("wsjar.jar")) {
       
   105                 for (Enumeration em = jarFile.entries(); em.hasMoreElements();) {
       
   106                     String fileName = em.nextElement().toString();
       
   107                     if (fileName.contains("\\")) {
       
   108                         throw new RuntimeException("\"\\\" character detected in jar file: " + fileName);
       
   109                     }
       
   110                 }
       
   111             }
       
   112         } catch (Exception e) {
       
   113             e.printStackTrace();
       
   114             throw new RuntimeException(e.getMessage());
       
   115         } finally {
       
   116             endpoint.stop();
       
   117             if (httpServer != null) {
       
   118                 httpServer.stop(0);
       
   119             }
       
   120             Path p = Paths.get("wsjar.jar");
       
   121             Files.deleteIfExists(p);
       
   122             p = Paths.get("wstest");
       
   123             if (Files.exists(p)) {
       
   124                 try {
       
   125                     Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
       
   126                         @Override
       
   127                         public FileVisitResult visitFile(Path file,
       
   128                             BasicFileAttributes attrs) throws IOException {
       
   129 
       
   130                             Files.delete(file);
       
   131                             return CONTINUE;
       
   132                         }
       
   133                         @Override
       
   134                         public FileVisitResult postVisitDirectory(Path dir,
       
   135                             IOException exc) throws IOException {
       
   136 
       
   137                             if (exc == null) {
       
   138                                 Files.delete(dir);
       
   139                                 return CONTINUE;
       
   140                             } else {
       
   141                                 throw exc;
       
   142                             }
       
   143                         }
       
   144                     });
       
   145                 } catch (IOException ioe) {
       
   146                     ioe.printStackTrace();
       
   147                 }
       
   148             }
       
   149         }
       
   150     }
       
   151 }