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