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