Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javah/T6893943.java
32285 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6893943 693731826* @summary exit code from javah with no args is 027*/2829import java.io.*;30import java.util.*;3132public class T6893943 {33static final String[] NO_ARGS = { };34static final String[] HELP = { "-help" };35static final String NEWLINE = System.getProperty("line.separator");3637public static void main(String... args) throws Exception {38new T6893943().run();39}4041void run() throws Exception {42testSimpleAPI(NO_ARGS, 1);43testSimpleAPI(HELP, 0);44testCommand(NO_ARGS, 1);45testCommand(HELP, 0);46}4748void testSimpleAPI(String[] args, int expect_rc) throws Exception {49System.err.println("Test simple api: " + Arrays.asList(args));50StringWriter sw = new StringWriter();51PrintWriter pw = new PrintWriter(sw);52int rc = com.sun.tools.javah.Main.run(args, pw);53pw.close();54expect("testSimpleAPI", sw.toString(), rc, expect_rc);55}5657void testCommand(String[] args, int expect_rc) throws Exception {58System.err.println("Test command: " + Arrays.asList(args));59File javaHome = new File(System.getProperty("java.home"));60if (javaHome.getName().equals("jre"))61javaHome = javaHome.getParentFile();6263List<String> command = new ArrayList<String>();64command.add(new File(new File(javaHome, "bin"), "javah").getPath());65command.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"));66command.addAll(Arrays.asList(args));67//System.err.println("command: " + command);6869ProcessBuilder pb = new ProcessBuilder(command);70pb.redirectErrorStream(true);71Process p = pb.start();72p.getOutputStream().close();73StringWriter sw = new StringWriter();74String line;75BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));76while ((line = in.readLine()) != null)77sw.write(line + NEWLINE);78int rc = p.waitFor();79expect("testCommand", sw.toString(), rc, expect_rc);80}8182void expect(String name, String out, int actual_rc, int expect_rc) throws Exception {83if (out.isEmpty())84throw new Exception("No output from javah");8586if (!out.startsWith("Usage:")) {87System.err.println(out);88throw new Exception("Unexpected output from javah");89}9091if (actual_rc != expect_rc)92throw new Exception(name + ": unexpected exit: " + actual_rc + ", expected: " + expect_rc);93}94}959697