test/tools/javah/T6893943.java

Fri, 23 Aug 2013 14:17:49 -0700

author
lana
date
Fri, 23 Aug 2013 14:17:49 -0700
changeset 1967
375834b5cf08
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

jjg@508 1 /*
ohair@554 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
jjg@508 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@508 4 *
jjg@508 5 * This code is free software; you can redistribute it and/or modify it
jjg@508 6 * under the terms of the GNU General Public License version 2 only, as
jjg@508 7 * published by the Free Software Foundation.
jjg@508 8 *
jjg@508 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@508 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@508 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@508 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@508 13 * accompanied this code).
jjg@508 14 *
jjg@508 15 * You should have received a copy of the GNU General Public License version
jjg@508 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@508 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@508 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@508 22 */
jjg@508 23
jjg@508 24 /*
jjg@508 25 * @test
jjg@529 26 * @bug 6893943 6937318
jjg@508 27 * @summary exit code from javah with no args is 0
jjg@508 28 */
jjg@508 29
jjg@508 30 import java.io.*;
jjg@508 31 import java.util.*;
jjg@508 32
jjg@508 33 public class T6893943 {
jjg@529 34 static final String[] NO_ARGS = { };
jjg@529 35 static final String[] HELP = { "-help" };
jjg@529 36 static final String NEWLINE = System.getProperty("line.separator");
jjg@529 37
jjg@508 38 public static void main(String... args) throws Exception {
jjg@508 39 new T6893943().run();
jjg@508 40 }
jjg@508 41
jjg@508 42 void run() throws Exception {
jjg@529 43 testSimpleAPI(NO_ARGS, 1);
jjg@529 44 testSimpleAPI(HELP, 0);
jjg@529 45 testCommand(NO_ARGS, 1);
jjg@529 46 testCommand(HELP, 0);
jjg@508 47 }
jjg@508 48
jjg@529 49 void testSimpleAPI(String[] args, int expect_rc) throws Exception {
jjg@529 50 System.err.println("Test simple api: " + Arrays.asList(args));
jjg@529 51 StringWriter sw = new StringWriter();
jjg@529 52 PrintWriter pw = new PrintWriter(sw);
jjg@529 53 int rc = com.sun.tools.javah.Main.run(args, pw);
jjg@529 54 pw.close();
jjg@529 55 expect("testSimpleAPI", sw.toString(), rc, expect_rc);
jjg@508 56 }
jjg@508 57
jjg@529 58 void testCommand(String[] args, int expect_rc) throws Exception {
jjg@529 59 System.err.println("Test command: " + Arrays.asList(args));
jjg@508 60 File javaHome = new File(System.getProperty("java.home"));
jjg@508 61 if (javaHome.getName().equals("jre"))
jjg@508 62 javaHome = javaHome.getParentFile();
jjg@508 63
jjg@508 64 List<String> command = new ArrayList<String>();
jjg@508 65 command.add(new File(new File(javaHome, "bin"), "javah").getPath());
jjg@508 66 command.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"));
jjg@529 67 command.addAll(Arrays.asList(args));
jjg@508 68 //System.err.println("command: " + command);
jjg@508 69
jjg@508 70 ProcessBuilder pb = new ProcessBuilder(command);
jjg@508 71 pb.redirectErrorStream(true);
jjg@508 72 Process p = pb.start();
jjg@508 73 p.getOutputStream().close();
jjg@529 74 StringWriter sw = new StringWriter();
jjg@508 75 String line;
jjg@508 76 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
jjg@508 77 while ((line = in.readLine()) != null)
jjg@529 78 sw.write(line + NEWLINE);
jjg@508 79 int rc = p.waitFor();
jjg@529 80 expect("testCommand", sw.toString(), rc, expect_rc);
jjg@508 81 }
jjg@508 82
jjg@529 83 void expect(String name, String out, int actual_rc, int expect_rc) throws Exception {
jjg@529 84 if (out.isEmpty())
jjg@529 85 throw new Exception("No output from javah");
jjg@529 86
jjg@529 87 if (!out.startsWith("Usage:")) {
jjg@529 88 System.err.println(out);
jjg@529 89 throw new Exception("Unexpected output from javah");
jjg@529 90 }
jjg@529 91
jjg@529 92 if (actual_rc != expect_rc)
jjg@529 93 throw new Exception(name + ": unexpected exit: " + actual_rc + ", expected: " + expect_rc);
jjg@508 94 }
jjg@508 95 }

mercurial