T6893943.java revision 2721:f7ce2cfa4cdb
1/* 2 * Copyright (c) 2010, 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 6893943 6937318 27 * @summary exit code from javah with no args is 0 28 */ 29 30import java.io.*; 31import java.util.*; 32 33public class T6893943 { 34 static final String[] NO_ARGS = { }; 35 static final String[] HELP = { "-help" }; 36 static final String NEWLINE = System.getProperty("line.separator"); 37 38 public static void main(String... args) throws Exception { 39 new T6893943().run(); 40 } 41 42 void run() throws Exception { 43 testSimpleAPI(NO_ARGS, 1); 44 testSimpleAPI(HELP, 0); 45 testCommand(NO_ARGS, 1); 46 testCommand(HELP, 0); 47 } 48 49 void testSimpleAPI(String[] args, int expect_rc) throws Exception { 50 System.err.println("Test simple api: " + Arrays.asList(args)); 51 StringWriter sw = new StringWriter(); 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); 56 } 57 58 void testCommand(String[] args, int expect_rc) throws Exception { 59 System.err.println("Test command: " + Arrays.asList(args)); 60 File javaHome = new File(System.getProperty("java.home")); 61 List<String> command = new ArrayList<String>(); 62 command.add(new File(new File(javaHome, "bin"), "javah").getPath()); 63 command.addAll(Arrays.asList(args)); 64 //System.err.println("command: " + command); 65 66 ProcessBuilder pb = new ProcessBuilder(command); 67 pb.redirectErrorStream(true); 68 Process p = pb.start(); 69 p.getOutputStream().close(); 70 StringWriter sw = new StringWriter(); 71 String line; 72 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); 73 while ((line = in.readLine()) != null) 74 sw.write(line + NEWLINE); 75 int rc = p.waitFor(); 76 expect("testCommand", sw.toString(), rc, expect_rc); 77 } 78 79 void expect(String name, String out, int actual_rc, int expect_rc) throws Exception { 80 if (out.isEmpty()) 81 throw new Exception("No output from javah"); 82 83 if (!out.startsWith("Usage:")) { 84 System.err.println(out); 85 throw new Exception("Unexpected output from javah"); 86 } 87 88 if (actual_rc != expect_rc) 89 throw new Exception(name + ": unexpected exit: " + actual_rc + ", expected: " + expect_rc); 90 } 91} 92