WinCommand.java revision 0:37a05a11f281
1/*
2 * Copyright 2004 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 5006520
26 * @summary Check many different ways to run Windows programs
27 * @author Martin Buchholz
28 *
29 * @compile -source 1.5 WinCommand.java
30 * @run main WinCommand
31 */
32
33import java.io.*;
34import java.util.*;
35import static java.lang.System.*;
36
37class StreamDrainer extends Thread {
38    private final InputStream is;
39    private final ByteArrayOutputStream os = new ByteArrayOutputStream();
40    public StreamDrainer(InputStream is) { this.is = is; }
41    public void run() {
42        try {
43            int i;
44            while ((i = is.read()) >= 0)
45                os.write(i);
46        } catch (Exception e) {}
47    }
48    public String toString() { return os.toString(); }
49}
50
51class CommandRunner {
52    private static Random generator = new Random();
53    public final int exitValue;
54    public final String out;
55    public final String err;
56    CommandRunner(String... args) throws Exception {
57        Process p = (generator.nextInt(2) == 0)
58            ? new ProcessBuilder(args).start()
59            : Runtime.getRuntime().exec(args);
60        StreamDrainer d1 = new StreamDrainer(p.getInputStream());
61        StreamDrainer d2 = new StreamDrainer(p.getErrorStream());
62        d1.start();
63        d2.start();
64        p.waitFor();
65        d1.join();
66        d2.join();
67        this.exitValue = p.exitValue();
68        this.out = d1.toString();
69        this.err = d2.toString();
70    }
71}
72
73public class WinCommand {
74    private static int failed = 0;
75
76    private static void fail(String msg) {
77        err.printf("FAIL: %s%n", msg);
78        failed++;
79    }
80
81    private static String outputOf(String... args) {
82        try {
83            CommandRunner cr = new CommandRunner(args);
84            if (cr.exitValue != 0)
85                fail("exitValue != 0");
86            if (! cr.err.equals(""))
87                fail("stderr: " + cr.err);
88            return cr.out.replaceFirst("[\r\n]+$", "");
89        } catch (Exception e) {
90            fail(e.toString());
91            return "";
92        }
93    }
94
95    private static void checkCD(String... filespecs) {
96        String firstCD = null;
97        for (String filespec : filespecs) {
98            String CD = outputOf(filespec, "/C", "CD");
99            out.printf("%s CD ==> %s%n", filespec, CD);
100            if (firstCD == null) {
101                firstCD = CD;
102                checkDir(CD);
103            }
104            if (! CD.equals(firstCD)) {
105                fail("Inconsistent result from CD subcommand");
106                checkDir(CD);
107            }
108        }
109    }
110
111    private static void checkDir(String dirname) {
112        if (! new File(dirname).isDirectory())
113            fail(String.format("Not a directory: %s%n", dirname));
114    }
115
116    private static void writeFile(String filename, String contents) {
117        try {
118            FileOutputStream fos = new FileOutputStream(filename);
119            fos.write(contents.getBytes());
120            fos.close();
121        } catch (Exception e) {
122            fail("Unexpected exception" + e.toString());
123        }
124    }
125
126    public static void main(String[] args) throws Exception {
127        File systemRoot =
128            getenv("SystemRoot") != null ? new File(getenv("SystemRoot")) :
129            getenv("WINDIR")     != null ? new File(getenv ("WINDIR")) :
130            null;
131        if (systemRoot == null || ! systemRoot.isDirectory())
132            return; // Not Windows as we know it
133
134        String systemDirW = new File(systemRoot, "System32").getPath();
135        String systemDirM = systemDirW.replace('\\', '/');
136        out.printf("systemDirW=%s%n", systemDirW);
137        out.printf("systemDirM=%s%n", systemDirM);
138
139        // Win9x systems don't have a cmd.exe
140        if (new File(systemDirW, "cmd.exe").exists()) {
141            try {
142                out.println("Running cmd.exe tests...");
143                writeFile("cdcmd.cmd", "@echo off\r\nCD\r\n");
144                writeFile("cdbat.bat", "@echo off\r\nCD\r\n");
145                checkCD("cmd",
146                        "cmd.exe",
147                        systemDirW + "\\cmd.exe",
148                        // Only the ".exe" extension can be omitted
149                        systemDirW + "\\cmd",
150                        systemDirM + "/cmd.exe",
151                        systemDirM + "/cmd",
152                        "/" + systemDirM + "/cmd",
153                        "cdcmd.cmd", "./cdcmd.cmd", ".\\cdcmd.cmd",
154                        "cdbat.bat", "./cdbat.bat", ".\\cdbat.bat");
155            } finally {
156                new File("cdcmd.cmd").delete();
157                new File("cdbat.bat").delete();
158            }
159        }
160
161        // 16-bit apps like command.com must have a console;
162        // fix this someday...
163
164//      // Win64 systems don't have a command.com
165//      if (new File(systemDirW, "command.com").exists()
166//          // no output if running without a console;
167//          // fix this in Mustang
168//          && ! outputOf("command.com", "/C", "CD").equals("")) {
169//          out.println("Running command.com tests...");
170//          checkCD("command.com",
171//                  systemDirM + "/command.com",
172//                  systemDirW + "\\command.com");
173//      }
174
175        // Win9x systems have a %SYSTEMDRIVE%\command.com
176//      if (new File("C:\\COMMAND.COM").exists()
177//          && ! outputOf("COMMAND.COM", "/C", "CD").equals("")) {
178//          out.println("Running COMMAND.COM tests...");
179//          checkCD("C:/command.com",
180//                  "C:\\command.com");
181//      }
182
183        if (failed > 0)
184            throw new Exception(failed + " tests failed");
185    }
186}
187