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