6937318: jdk7 b86: javah and javah -help is no output for these commands
authorjjg
Wed, 24 Mar 2010 12:18:17 -0700
changeset 5217 e20e20050405
parent 5216 b7f7018fa96a
child 5218 066372a81ca9
6937318: jdk7 b86: javah and javah -help is no output for these commands Reviewed-by: darcy
langtools/src/share/classes/com/sun/tools/javah/JavahTask.java
langtools/test/tools/javah/T6893943.java
--- a/langtools/src/share/classes/com/sun/tools/javah/JavahTask.java	Tue Mar 23 18:05:54 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javah/JavahTask.java	Wed Mar 24 12:18:17 2010 -0700
@@ -318,12 +318,6 @@
     int run(String[] args) {
         try {
             handleOptions(args);
-            if (classes == null || classes.size() == 0) {
-                if (help || version || fullVersion)
-                    return 0;
-                else
-                    return 1;
-            }
             boolean ok = run();
             return ok ? 0 : 1;
         } catch (BadArgs e) {
@@ -355,7 +349,7 @@
             fileManager = getDefaultFileManager(diagnosticListener, log);
 
         Iterator<String> iter = args.iterator();
-        boolean noArgs = !iter.hasNext();
+        noArgs = !iter.hasNext();
 
         while (iter.hasNext()) {
             String arg = iter.next();
@@ -416,9 +410,9 @@
 
         Util util = new Util(log, diagnosticListener);
 
-        if (help) {
+        if (noArgs || help) {
             showHelp();
-            return true;
+            return help; // treat noArgs as an error for purposes of exit code
         }
 
         if (version || fullVersion) {
@@ -636,6 +630,7 @@
     String usercp;
     List<String> classes;
     boolean verbose;
+    boolean noArgs;
     boolean help;
     boolean trace;
     boolean version;
--- a/langtools/test/tools/javah/T6893943.java	Tue Mar 23 18:05:54 2010 -0700
+++ b/langtools/test/tools/javah/T6893943.java	Wed Mar 24 12:18:17 2010 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6893943
+ * @bug 6893943 6937318
  * @summary exit code from javah with no args is 0
  */
 
@@ -31,22 +31,32 @@
 import java.util.*;
 
 public class T6893943 {
+    static final String[] NO_ARGS = { };
+    static final String[] HELP = { "-help" };
+    static final String NEWLINE = System.getProperty("line.separator");
+
     public static void main(String... args) throws Exception {
         new T6893943().run();
     }
 
     void run() throws Exception {
-        testSimpleAPI();
-        testCommand();
+        testSimpleAPI(NO_ARGS, 1);
+        testSimpleAPI(HELP, 0);
+        testCommand(NO_ARGS, 1);
+        testCommand(HELP, 0);
     }
 
-    void testSimpleAPI() throws Exception {
-        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.err));
-        int rc = com.sun.tools.javah.Main.run(new String[] { }, pw);
-        expect("testSimpleAPI", rc, 1);
+    void testSimpleAPI(String[] args, int expect_rc) throws Exception {
+        System.err.println("Test simple api: " + Arrays.asList(args));
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javah.Main.run(args, pw);
+        pw.close();
+        expect("testSimpleAPI", sw.toString(), rc, expect_rc);
     }
 
-    void testCommand() throws Exception {
+    void testCommand(String[] args, int expect_rc) throws Exception {
+        System.err.println("Test command: " + Arrays.asList(args));
         File javaHome = new File(System.getProperty("java.home"));
         if (javaHome.getName().equals("jre"))
             javaHome = javaHome.getParentFile();
@@ -54,22 +64,32 @@
         List<String> command = new ArrayList<String>();
         command.add(new File(new File(javaHome, "bin"), "javah").getPath());
         command.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"));
+        command.addAll(Arrays.asList(args));
         //System.err.println("command: " + command);
 
         ProcessBuilder pb = new ProcessBuilder(command);
         pb.redirectErrorStream(true);
         Process p = pb.start();
         p.getOutputStream().close();
+        StringWriter sw = new StringWriter();
         String line;
         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
         while ((line = in.readLine()) != null)
-            System.err.println("javah: " + line);
+            sw.write(line + NEWLINE);
         int rc = p.waitFor();
-        expect("testCommand", rc, 1);
+        expect("testCommand", sw.toString(), rc, expect_rc);
     }
 
-    void expect(String name, int actual, int expect) throws Exception {
-        if (actual != expect)
-            throw new Exception(name + ": unexpected exit: " + actual + ", expected: " + expect);
+    void expect(String name, String out, int actual_rc, int expect_rc) throws Exception {
+        if (out.isEmpty())
+            throw new Exception("No output from javah");
+
+        if (!out.startsWith("Usage:")) {
+            System.err.println(out);
+            throw new Exception("Unexpected output from javah");
+        }
+
+        if (actual_rc != expect_rc)
+            throw new Exception(name + ": unexpected exit: " + actual_rc + ", expected: " + expect_rc);
     }
 }