test/tools/javah/6572945/T6572945.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 416
c287d51c57da
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

jjg@416 1 /*
ohair@554 2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
jjg@416 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@416 4 *
jjg@416 5 * This code is free software; you can redistribute it and/or modify it
jjg@416 6 * under the terms of the GNU General Public License version 2 only, as
jjg@416 7 * published by the Free Software Foundation.
jjg@416 8 *
jjg@416 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@416 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@416 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@416 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@416 13 * accompanied this code).
jjg@416 14 *
jjg@416 15 * You should have received a copy of the GNU General Public License version
jjg@416 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@416 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@416 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@416 22 */
jjg@416 23
jjg@416 24 /*
jjg@416 25 * @test
jjg@416 26 * @bug 6572945
jjg@416 27 * @summary rewrite javah as an annotation processor, instead of as a doclet
jjg@416 28 * @build TestClass1 TestClass2 TestClass3
jjg@416 29 * @run main T6572945
jjg@416 30 */
jjg@416 31
jjg@416 32 import java.io.*;
jjg@416 33 import java.util.*;
jjg@416 34 import com.sun.tools.javah.Main;
jjg@416 35
jjg@416 36 public class T6572945
jjg@416 37 {
jjg@416 38 static File testSrc = new File(System.getProperty("test.src", "."));
jjg@416 39 static File testClasses = new File(System.getProperty("test.classes", "."));
jjg@416 40 static boolean isWindows = System.getProperty("os.name").startsWith("Windows");
jjg@416 41
jjg@416 42 public static void main(String... args)
jjg@416 43 throws IOException, InterruptedException
jjg@416 44 {
jjg@416 45 boolean ok = new T6572945().run(args);
jjg@416 46 if (!ok)
jjg@416 47 throw new Error("Test Failed");
jjg@416 48 }
jjg@416 49
jjg@416 50 public boolean run(String[] args)
jjg@416 51 throws IOException, InterruptedException
jjg@416 52 {
jjg@416 53 if (args.length == 1)
jjg@416 54 jdk = new File(args[0]);
jjg@416 55
jjg@416 56 test("-o", "jni.file.1", "-jni", "TestClass1");
jjg@416 57 test("-o", "jni.file.2", "-jni", "TestClass1", "TestClass2");
jjg@416 58 test("-d", "jni.dir.1", "-jni", "TestClass1", "TestClass2");
jjg@416 59 test("-o", "jni.file.3", "-jni", "TestClass3");
jjg@416 60
jjg@416 61 // The following tests are disabled because llni support has been
jjg@416 62 // discontinued, and because bugs in old javah means that character
jjg@416 63 // for character testing against output from old javah does not work.
jjg@416 64 // In fact, the LLNI impl in new javah is actually better than the
jjg@416 65 // impl in old javah because of a couple of significant bug fixes.
jjg@416 66
jjg@416 67 // test("-o", "llni.file.1", "-llni", "TestClass1");
jjg@416 68 // test("-o", "llni.file.2", "-llni", "TestClass1", "TestClass2");
jjg@416 69 // test("-d", "llni.dir.1", "-llni", "TestClass1", "TestClass2");
jjg@416 70 // test("-o", "llni.file.3", "-llni", "TestClass3");
jjg@416 71
jjg@416 72 return (errors == 0);
jjg@416 73 }
jjg@416 74
jjg@416 75 void test(String... args)
jjg@416 76 throws IOException, InterruptedException
jjg@416 77 {
jjg@416 78 String[] cp_args = new String[args.length + 2];
jjg@416 79 cp_args[0] = "-classpath";
jjg@416 80 cp_args[1] = testClasses.getPath();
jjg@416 81 System.arraycopy(args, 0, cp_args, 2, args.length);
jjg@416 82
jjg@416 83 if (jdk != null)
jjg@416 84 init(cp_args);
jjg@416 85
jjg@416 86 File out = null;
jjg@416 87 for (int i = 0; i < args.length; i++) {
jjg@416 88 if (args[i].equals("-o")) {
jjg@416 89 out = new File(args[++i]);
jjg@416 90 break;
jjg@416 91 } else if (args[i].equals("-d")) {
jjg@416 92 out = new File(args[++i]);
jjg@416 93 out.mkdirs();
jjg@416 94 break;
jjg@416 95 }
jjg@416 96 }
jjg@416 97
jjg@416 98 try {
jjg@416 99 System.out.println("test: " + Arrays.asList(cp_args));
jjg@416 100
jjg@416 101 // // Uncomment and use the following lines to execute javah via the
jjg@416 102 // // command line -- for example, to run old javah and set up the golden files
jjg@416 103 // List<String> cmd = new ArrayList<String>();
jjg@416 104 // File javaHome = new File(System.getProperty("java.home"));
jjg@416 105 // if (javaHome.getName().equals("jre"))
jjg@416 106 // javaHome = javaHome.getParentFile();
jjg@416 107 // File javah = new File(new File(javaHome, "bin"), "javah");
jjg@416 108 // cmd.add(javah.getPath());
jjg@416 109 // cmd.addAll(Arrays.asList(cp_args));
jjg@416 110 // ProcessBuilder pb = new ProcessBuilder(cmd);
jjg@416 111 // pb.redirectErrorStream(true);
jjg@416 112 // pb.start();
jjg@416 113 // Process p = pb.start();
jjg@416 114 // String line;
jjg@416 115 // BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
jjg@416 116 // while ((line = in.readLine()) != null)
jjg@416 117 // System.err.println(line);
jjg@416 118 // in.close();
jjg@416 119 // int rc = p.waitFor();
jjg@416 120
jjg@416 121 // Use new javah
jjg@416 122 PrintWriter err = new PrintWriter(System.err, true);
jjg@416 123 int rc = Main.run(cp_args, err);
jjg@416 124
jjg@416 125 if (rc != 0) {
jjg@416 126 error("javah failed: rc=" + rc);
jjg@416 127 return;
jjg@416 128 }
jjg@416 129
jjg@416 130 // The golden files use the LL suffix for long constants, which
jjg@416 131 // is used on Linux and Solaris. On Windows, the suffix is i64,
jjg@416 132 // so compare will update the golden files on the fly before the
jjg@416 133 // final comparison.
jjg@416 134 compare(new File(new File(testSrc, "gold"), out.getName()), out);
jjg@416 135 } catch (Throwable t) {
jjg@416 136 t.printStackTrace();
jjg@416 137 error("javah threw exception");
jjg@416 138 }
jjg@416 139 }
jjg@416 140
jjg@416 141 void init(String[] args) throws IOException, InterruptedException {
jjg@416 142 String[] cmdArgs = new String[args.length + 1];
jjg@416 143 cmdArgs[0] = new File(new File(jdk, "bin"), "javah").getPath();
jjg@416 144 System.arraycopy(args, 0, cmdArgs, 1, args.length);
jjg@416 145
jjg@416 146 System.out.println("init: " + Arrays.asList(cmdArgs));
jjg@416 147
jjg@416 148 ProcessBuilder pb = new ProcessBuilder(cmdArgs);
jjg@416 149 pb.directory(new File(testSrc, "gold"));
jjg@416 150 pb.redirectErrorStream(true);
jjg@416 151 Process p = pb.start();
jjg@416 152 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
jjg@416 153 String line;
jjg@416 154 while ((line = in.readLine()) != null)
jjg@416 155 System.out.println("javah: " + line);
jjg@416 156 int rc = p.waitFor();
jjg@416 157 if (rc != 0)
jjg@416 158 error("javah: exit code " + rc);
jjg@416 159 }
jjg@416 160
jjg@416 161 /** Compare two directories.
jjg@416 162 * @param f1 The golden directory
jjg@416 163 * @param f2 The directory to be compared
jjg@416 164 */
jjg@416 165 void compare(File f1, File f2) {
jjg@416 166 compare(f1, f2, null);
jjg@416 167 }
jjg@416 168
jjg@416 169 /** Compare two files or directories
jjg@416 170 * @param f1 The golden directory
jjg@416 171 * @param f2 The directory to be compared
jjg@416 172 * @param p An optional path identifying a file within the two directories
jjg@416 173 */
jjg@416 174 void compare(File f1, File f2, String p) {
jjg@416 175 File f1p = (p == null ? f1 : new File(f1, p));
jjg@416 176 File f2p = (p == null ? f2 : new File(f2, p));
jjg@416 177 System.out.println("compare " + f1p + " " + f2p);
jjg@416 178 if (f1p.isDirectory() && f2p.isDirectory()) {
jjg@416 179 Set<String> children = new HashSet<String>();
jjg@416 180 children.addAll(Arrays.asList(f1p.list()));
jjg@416 181 children.addAll(Arrays.asList(f2p.list()));
jjg@416 182 for (String c: children) {
jjg@416 183 compare(f1, f2, new File(p, c).getPath()); // null-safe for p
jjg@416 184 }
jjg@416 185 }
jjg@416 186 else if (f1p.isFile() && f2p.isFile()) {
jjg@416 187 String s1 = read(f1p);
jjg@416 188 if (isWindows) {
jjg@416 189 // f1/s1 is the golden file
jjg@416 190 // on Windows, long constants use the i64 suffix, not LL
jjg@416 191 s1 = s1.replaceAll("( [0-9]+)LL\n", "$1i64\n");
jjg@416 192 }
jjg@416 193 String s2 = read(f2p);
jjg@416 194 if (!s1.equals(s2)) {
jjg@416 195 System.out.println("File: " + f1p + "\n" + s1);
jjg@416 196 System.out.println("File: " + f2p + "\n" + s2);
jjg@416 197 error("Files differ: " + f1p + " " + f2p);
jjg@416 198 }
jjg@416 199 }
jjg@416 200 else if (f1p.exists() && !f2p.exists())
jjg@416 201 error("Only in " + f1 + ": " + p);
jjg@416 202 else if (f2p.exists() && !f1p.exists())
jjg@416 203 error("Only in " + f2 + ": " + p);
jjg@416 204 else
jjg@416 205 error("Files differ: " + f1p + " " + f2p);
jjg@416 206 }
jjg@416 207
jjg@416 208 private String read(File f) {
jjg@416 209 try {
jjg@416 210 BufferedReader in = new BufferedReader(new FileReader(f));
jjg@416 211 try {
jjg@416 212 StringBuilder sb = new StringBuilder((int) f.length());
jjg@416 213 String line;
jjg@416 214 while ((line = in.readLine()) != null) {
jjg@416 215 sb.append(line);
jjg@416 216 sb.append("\n");
jjg@416 217 }
jjg@416 218 return sb.toString();
jjg@416 219 } finally {
jjg@416 220 try {
jjg@416 221 in.close();
jjg@416 222 } catch (IOException e) {
jjg@416 223 }
jjg@416 224 }
jjg@416 225 } catch (IOException e) {
jjg@416 226 error("error reading " + f + ": " + e);
jjg@416 227 return "";
jjg@416 228 }
jjg@416 229 }
jjg@416 230
jjg@416 231
jjg@416 232 private void error(String msg) {
jjg@416 233 System.out.println(msg);
jjg@416 234 errors++;
jjg@416 235 }
jjg@416 236
jjg@416 237 private int errors;
jjg@416 238 private File jdk;
jjg@416 239 }

mercurial