langtools/test/tools/javah/4942232/Test.java
author jwilhelm
Thu, 07 Feb 2013 15:51:25 +0100
changeset 15605 bf7de87bbe3a
parent 6930 b6fea484cbb2
child 30730 d3ce7619db2c
permissions -rw-r--r--
8006432: Ratio flags should be unsigned Summary: Flags changed to be of uintx type Reviewed-by: johnc, tamao
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6930
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     1
/*
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     4
 *
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     8
 *
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    13
 * accompanied this code).
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    14
 *
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    18
 *
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    21
 * questions.
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    22
 */
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    23
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    24
/*
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    25
 * @test
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    26
 * @bug 4942232
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    27
 * @summary missing param class processes without error
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    28
 * @build ParamClassTest Test
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    29
 * @run main Test
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    30
 */
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    31
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    32
import java.io.*;
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    33
import java.util.*;
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    34
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    35
public class Test {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    36
    public static void main(String... args) throws Exception {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    37
        new Test().run();
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    38
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    39
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    40
    void run() throws Exception {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    41
        File testSrc = new File(System.getProperty("test.src"));
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    42
        File testClasses = new File(System.getProperty("test.classes"));
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    43
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    44
        // standard use of javah on valid class file
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    45
        String[] test1Args = {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    46
            "-d", mkdir("test1/out").getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    47
            "-classpath", testClasses.getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    48
            "ParamClassTest"
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    49
        };
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    50
        test(test1Args, 0);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    51
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    52
        // extended use of javah on valid source file
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    53
        String[] test2Args = {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    54
            "-d", mkdir("test2/out").getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    55
            "-classpath", testSrc.getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    56
            "ParamClassTest"
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    57
        };
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    58
        test(test2Args, 0);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    59
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    60
        // javah on class file with missing referents
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    61
        File test3Classes = mkdir("test3/classes");
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    62
        copy(new File(testClasses, "ParamClassTest.class"), test3Classes);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    63
        String[] test3Args = {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    64
            "-d", mkdir("test3/out").getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    65
            "-classpath", test3Classes.getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    66
            "ParamClassTest"
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    67
        };
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    68
        test(test3Args, 1);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    69
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    70
        // javah on source file with missing referents
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    71
        File test4Src = mkdir("test4/src");
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    72
        String paramClassTestSrc = readFile(new File(testSrc, "ParamClassTest.java"));
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    73
        writeFile(new File(test4Src, "ParamClassTest.java"),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    74
                paramClassTestSrc.replaceAll("class Param \\{\\s+\\}", ""));
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    75
        String[] test4Args = {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    76
            "-d", mkdir("test4/out").getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    77
            "-classpath", test4Src.getPath(),
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    78
            "ParamClassTest"
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    79
        };
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    80
        test(test4Args, 15);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    81
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    82
        if (errors > 0)
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    83
            throw new Exception(errors + " errors occurred");
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    84
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    85
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    86
    void test(String[] args, int expect) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    87
        System.err.println("test: " + Arrays.asList(args));
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    88
        int rc = javah(args);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    89
        if (rc != expect)
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    90
            error("Unexpected return code: " + rc + "; expected: " + expect);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    91
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    92
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    93
    int javah(String... args) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    94
        StringWriter sw = new StringWriter();
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    95
        PrintWriter pw = new PrintWriter(sw);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    96
        int rc = com.sun.tools.javah.Main.run(args, pw);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    97
        pw.close();
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    98
        String out = sw.toString();
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
    99
        if (!out.isEmpty())
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   100
            System.err.println(out);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   101
        return rc;
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   102
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   103
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   104
    File mkdir(String path) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   105
        File f = new File(path);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   106
        f.mkdirs();
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   107
        return f;
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   108
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   109
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   110
    void copy(File from, File to) throws IOException {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   111
        if (to.isDirectory())
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   112
            to = new File(to, from.getName());
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   113
        try (DataInputStream in = new DataInputStream(new FileInputStream(from));
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   114
                FileOutputStream out = new FileOutputStream(to)) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   115
            byte[] buf = new byte[(int) from.length()];
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   116
            in.readFully(buf);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   117
            out.write(buf);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   118
        }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   119
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   120
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   121
    String readFile(File f) throws IOException {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   122
        try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   123
            byte[] buf = new byte[(int) f.length()];
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   124
            in.readFully(buf);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   125
            return new String(buf);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   126
        }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   127
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   128
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   129
    void writeFile(File f, String body) throws IOException {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   130
        try (FileWriter out = new FileWriter(f)) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   131
            out.write(body);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   132
        }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   133
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   134
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   135
    void error(String msg) {
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   136
        System.err.println(msg);
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   137
        errors++;
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   138
    }
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   139
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   140
    int errors;
b6fea484cbb2 4942232: missing param class processes without error
jjg
parents:
diff changeset
   141
}