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