1 /*
   2  * Copyright (c) 2014, 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 8038307
  27  * @summary JAX-WS conformance tests fail when running JCK-devtools-8 suite against RI in EBCDIC emulation mode
  28  * @run main/othervm WsImportTest
  29  */
  30 
  31 import java.io.InputStreamReader;
  32 import java.io.IOException;
  33 import java.io.BufferedReader;
  34 import java.io.File;
  35 import java.nio.file.Files;
  36 import java.nio.file.FileVisitResult;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.nio.file.SimpleFileVisitor;
  40 import java.nio.file.attribute.BasicFileAttributes;
  41 import static java.nio.file.FileVisitResult.*;
  42 
  43 public class WsImportTest {
  44 
  45     public static void main(String[] args) throws IOException {
  46 
  47         String wsimport = getWsImport();
  48         String wsdl = getWSDLFilePath("test-service.wsdl");
  49 
  50         try {
  51             log("Importing wsdl: " + wsdl);
  52             String[] wsargs = {
  53                 wsimport,
  54                 "-p",
  55                 "generated",
  56                 "-J-Dfile.encoding=Cp037",
  57                 wsdl
  58             };
  59 
  60             ProcessBuilder pb = new ProcessBuilder(wsargs);
  61             pb.redirectErrorStream(true);
  62             Process p = pb.start();
  63             logOutput(p);
  64             int result = p.waitFor();
  65             p.destroy();
  66 
  67             if (result != 0) {
  68                 fail("WsImport failed. TEST FAILED.");
  69             } else {
  70                 log("Test PASSED.");
  71             }
  72 
  73         } catch (Exception e) {
  74             e.printStackTrace();
  75             fail(e.getMessage());
  76         } finally {
  77             deleteGeneratedFiles();
  78         }
  79     }
  80 
  81     private static void fail(String message) {
  82         throw new RuntimeException(message);
  83     }
  84 
  85     private static void log(String msg) {
  86         System.out.println(msg);
  87     }
  88 
  89     private static void logOutput(Process p) throws IOException {
  90         BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
  91         String s = r.readLine();
  92         while (s != null) {
  93             log(s.trim());
  94             s = r.readLine();
  95         }
  96     }
  97 
  98     private static void deleteGeneratedFiles() {
  99         Path p = Paths.get("generated");
 100         if (Files.exists(p)) {
 101             try {
 102                 Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
 103                     @Override
 104                     public FileVisitResult visitFile(Path file,
 105                         BasicFileAttributes attrs) throws IOException {
 106 
 107                         Files.delete(file);
 108                         return CONTINUE;
 109                     }
 110                     @Override
 111                     public FileVisitResult postVisitDirectory(Path dir,
 112                         IOException exc) throws IOException {
 113 
 114                         if (exc == null) {
 115                             Files.delete(dir);
 116                             return CONTINUE;
 117                         } else {
 118                             throw exc;
 119                         }
 120                     }
 121                 });
 122             } catch (IOException ioe) {
 123                 ioe.printStackTrace();
 124             }
 125         }
 126     }
 127 
 128     private static String getWSDLFilePath(String filename) {
 129         String testSrc = System.getProperty("test.src");
 130         if (testSrc == null) testSrc = ".";
 131         return Paths.get(testSrc).resolve(filename).toString();
 132     }
 133 
 134     private static String getWsImport() {
 135         String javaHome = System.getProperty("java.home");
 136         String wsimport = javaHome + File.separator + "bin" + File.separator + "wsimport";
 137         if (System.getProperty("os.name").startsWith("Windows")) {
 138             wsimport = wsimport.concat(".exe");
 139         }
 140         return wsimport;
 141     }
 142 }