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