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