jjg@508: /* ohair@554: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@508: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@508: * jjg@508: * This code is free software; you can redistribute it and/or modify it jjg@508: * under the terms of the GNU General Public License version 2 only, as jjg@508: * published by the Free Software Foundation. jjg@508: * jjg@508: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@508: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@508: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@508: * version 2 for more details (a copy is included in the LICENSE file that jjg@508: * accompanied this code). jjg@508: * jjg@508: * You should have received a copy of the GNU General Public License version jjg@508: * 2 along with this work; if not, write to the Free Software Foundation, jjg@508: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@508: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@508: */ jjg@508: jjg@508: /* jjg@508: * @test jjg@529: * @bug 6893943 6937318 jjg@508: * @summary exit code from javah with no args is 0 jjg@508: */ jjg@508: jjg@508: import java.io.*; jjg@508: import java.util.*; jjg@508: jjg@508: public class T6893943 { jjg@529: static final String[] NO_ARGS = { }; jjg@529: static final String[] HELP = { "-help" }; jjg@529: static final String NEWLINE = System.getProperty("line.separator"); jjg@529: jjg@508: public static void main(String... args) throws Exception { jjg@508: new T6893943().run(); jjg@508: } jjg@508: jjg@508: void run() throws Exception { jjg@529: testSimpleAPI(NO_ARGS, 1); jjg@529: testSimpleAPI(HELP, 0); jjg@529: testCommand(NO_ARGS, 1); jjg@529: testCommand(HELP, 0); jjg@508: } jjg@508: jjg@529: void testSimpleAPI(String[] args, int expect_rc) throws Exception { jjg@529: System.err.println("Test simple api: " + Arrays.asList(args)); jjg@529: StringWriter sw = new StringWriter(); jjg@529: PrintWriter pw = new PrintWriter(sw); jjg@529: int rc = com.sun.tools.javah.Main.run(args, pw); jjg@529: pw.close(); jjg@529: expect("testSimpleAPI", sw.toString(), rc, expect_rc); jjg@508: } jjg@508: jjg@529: void testCommand(String[] args, int expect_rc) throws Exception { jjg@529: System.err.println("Test command: " + Arrays.asList(args)); jjg@508: File javaHome = new File(System.getProperty("java.home")); jjg@508: if (javaHome.getName().equals("jre")) jjg@508: javaHome = javaHome.getParentFile(); jjg@508: jjg@508: List command = new ArrayList(); jjg@508: command.add(new File(new File(javaHome, "bin"), "javah").getPath()); jjg@508: command.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path")); jjg@529: command.addAll(Arrays.asList(args)); jjg@508: //System.err.println("command: " + command); jjg@508: jjg@508: ProcessBuilder pb = new ProcessBuilder(command); jjg@508: pb.redirectErrorStream(true); jjg@508: Process p = pb.start(); jjg@508: p.getOutputStream().close(); jjg@529: StringWriter sw = new StringWriter(); jjg@508: String line; jjg@508: BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); jjg@508: while ((line = in.readLine()) != null) jjg@529: sw.write(line + NEWLINE); jjg@508: int rc = p.waitFor(); jjg@529: expect("testCommand", sw.toString(), rc, expect_rc); jjg@508: } jjg@508: jjg@529: void expect(String name, String out, int actual_rc, int expect_rc) throws Exception { jjg@529: if (out.isEmpty()) jjg@529: throw new Exception("No output from javah"); jjg@529: jjg@529: if (!out.startsWith("Usage:")) { jjg@529: System.err.println(out); jjg@529: throw new Exception("Unexpected output from javah"); jjg@529: } jjg@529: jjg@529: if (actual_rc != expect_rc) jjg@529: throw new Exception(name + ": unexpected exit: " + actual_rc + ", expected: " + expect_rc); jjg@508: } jjg@508: }