21 * have any questions. |
21 * have any questions. |
22 */ |
22 */ |
23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @bug 6893943 |
26 * @bug 6893943 6937318 |
27 * @summary exit code from javah with no args is 0 |
27 * @summary exit code from javah with no args is 0 |
28 */ |
28 */ |
29 |
29 |
30 import java.io.*; |
30 import java.io.*; |
31 import java.util.*; |
31 import java.util.*; |
32 |
32 |
33 public class T6893943 { |
33 public class T6893943 { |
|
34 static final String[] NO_ARGS = { }; |
|
35 static final String[] HELP = { "-help" }; |
|
36 static final String NEWLINE = System.getProperty("line.separator"); |
|
37 |
34 public static void main(String... args) throws Exception { |
38 public static void main(String... args) throws Exception { |
35 new T6893943().run(); |
39 new T6893943().run(); |
36 } |
40 } |
37 |
41 |
38 void run() throws Exception { |
42 void run() throws Exception { |
39 testSimpleAPI(); |
43 testSimpleAPI(NO_ARGS, 1); |
40 testCommand(); |
44 testSimpleAPI(HELP, 0); |
|
45 testCommand(NO_ARGS, 1); |
|
46 testCommand(HELP, 0); |
41 } |
47 } |
42 |
48 |
43 void testSimpleAPI() throws Exception { |
49 void testSimpleAPI(String[] args, int expect_rc) throws Exception { |
44 PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.err)); |
50 System.err.println("Test simple api: " + Arrays.asList(args)); |
45 int rc = com.sun.tools.javah.Main.run(new String[] { }, pw); |
51 StringWriter sw = new StringWriter(); |
46 expect("testSimpleAPI", rc, 1); |
52 PrintWriter pw = new PrintWriter(sw); |
|
53 int rc = com.sun.tools.javah.Main.run(args, pw); |
|
54 pw.close(); |
|
55 expect("testSimpleAPI", sw.toString(), rc, expect_rc); |
47 } |
56 } |
48 |
57 |
49 void testCommand() throws Exception { |
58 void testCommand(String[] args, int expect_rc) throws Exception { |
|
59 System.err.println("Test command: " + Arrays.asList(args)); |
50 File javaHome = new File(System.getProperty("java.home")); |
60 File javaHome = new File(System.getProperty("java.home")); |
51 if (javaHome.getName().equals("jre")) |
61 if (javaHome.getName().equals("jre")) |
52 javaHome = javaHome.getParentFile(); |
62 javaHome = javaHome.getParentFile(); |
53 |
63 |
54 List<String> command = new ArrayList<String>(); |
64 List<String> command = new ArrayList<String>(); |
55 command.add(new File(new File(javaHome, "bin"), "javah").getPath()); |
65 command.add(new File(new File(javaHome, "bin"), "javah").getPath()); |
56 command.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path")); |
66 command.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path")); |
|
67 command.addAll(Arrays.asList(args)); |
57 //System.err.println("command: " + command); |
68 //System.err.println("command: " + command); |
58 |
69 |
59 ProcessBuilder pb = new ProcessBuilder(command); |
70 ProcessBuilder pb = new ProcessBuilder(command); |
60 pb.redirectErrorStream(true); |
71 pb.redirectErrorStream(true); |
61 Process p = pb.start(); |
72 Process p = pb.start(); |
62 p.getOutputStream().close(); |
73 p.getOutputStream().close(); |
|
74 StringWriter sw = new StringWriter(); |
63 String line; |
75 String line; |
64 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
76 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
65 while ((line = in.readLine()) != null) |
77 while ((line = in.readLine()) != null) |
66 System.err.println("javah: " + line); |
78 sw.write(line + NEWLINE); |
67 int rc = p.waitFor(); |
79 int rc = p.waitFor(); |
68 expect("testCommand", rc, 1); |
80 expect("testCommand", sw.toString(), rc, expect_rc); |
69 } |
81 } |
70 |
82 |
71 void expect(String name, int actual, int expect) throws Exception { |
83 void expect(String name, String out, int actual_rc, int expect_rc) throws Exception { |
72 if (actual != expect) |
84 if (out.isEmpty()) |
73 throw new Exception(name + ": unexpected exit: " + actual + ", expected: " + expect); |
85 throw new Exception("No output from javah"); |
|
86 |
|
87 if (!out.startsWith("Usage:")) { |
|
88 System.err.println(out); |
|
89 throw new Exception("Unexpected output from javah"); |
|
90 } |
|
91 |
|
92 if (actual_rc != expect_rc) |
|
93 throw new Exception(name + ": unexpected exit: " + actual_rc + ", expected: " + expect_rc); |
74 } |
94 } |
75 } |
95 } |