test/tools/javah/compareTest/CompareTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 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 import java.io.DataInputStream;
aoqi@0 25 import java.io.File;
aoqi@0 26 import java.io.IOException;
aoqi@0 27 import java.io.InputStream;
aoqi@0 28 import java.io.PrintWriter;
aoqi@0 29 import java.io.StringWriter;
aoqi@0 30 import java.util.ArrayList;
aoqi@0 31 import java.util.Arrays;
aoqi@0 32 import java.util.Enumeration;
aoqi@0 33 import java.util.List;
aoqi@0 34 import java.util.Set;
aoqi@0 35 import java.util.TreeSet;
aoqi@0 36 import java.util.jar.JarEntry;
aoqi@0 37 import java.util.jar.JarFile;
aoqi@0 38
aoqi@0 39 import com.sun.tools.classfile.AccessFlags;
aoqi@0 40 import com.sun.tools.classfile.ClassFile;
aoqi@0 41 import com.sun.tools.classfile.ConstantPoolException;
aoqi@0 42 import com.sun.tools.classfile.Method;
aoqi@0 43 import java.io.BufferedReader;
aoqi@0 44 import java.io.FileInputStream;
aoqi@0 45 import java.io.InputStreamReader;
aoqi@0 46 import java.util.LinkedHashSet;
aoqi@0 47
aoqi@0 48 public class CompareTest {
aoqi@0 49 String[][] testCases = {
aoqi@0 50 { },
aoqi@0 51 { "-jni" },
aoqi@0 52 // { "-llni" },
aoqi@0 53 };
aoqi@0 54
aoqi@0 55 public static void main(String... args) throws Exception {
aoqi@0 56 new CompareTest().run(args);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 public void run(String... args) throws Exception {
aoqi@0 60 old_javah_cmd = new File(args[0]);
aoqi@0 61 rt_jar = new File(args[1]);
aoqi@0 62
aoqi@0 63 Set<String> testClasses;
aoqi@0 64 if (args.length > 2) {
aoqi@0 65 testClasses = new LinkedHashSet<String>(Arrays.asList(Arrays.copyOfRange(args, 2, args.length)));
aoqi@0 66 } else
aoqi@0 67 testClasses = getNativeClasses(new JarFile(rt_jar));
aoqi@0 68
aoqi@0 69 for (String[] options: testCases) {
aoqi@0 70 for (String name: testClasses) {
aoqi@0 71 test(Arrays.asList(options), rt_jar, name);
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 if (errors == 0)
aoqi@0 76 System.out.println(count + " tests passed");
aoqi@0 77 else
aoqi@0 78 throw new Exception(errors + "/" + count + " tests failed");
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 public void test(List<String> options, File bootclasspath, String className)
aoqi@0 82 throws IOException, InterruptedException {
aoqi@0 83 System.err.println("test: " + options + " " + className);
aoqi@0 84 count++;
aoqi@0 85
aoqi@0 86 testOptions = options;
aoqi@0 87 testClassName = className;
aoqi@0 88
aoqi@0 89 File oldOutDir = initDir(file(new File("old"), className));
aoqi@0 90 int old_rc = old_javah(options, oldOutDir, bootclasspath, className);
aoqi@0 91
aoqi@0 92 File newOutDir = initDir(file(new File("new"), className));
aoqi@0 93 int new_rc = new_javah(options, newOutDir, bootclasspath, className);
aoqi@0 94
aoqi@0 95 if (old_rc != new_rc)
aoqi@0 96 error("return codes differ; old: " + old_rc + ", new: " + new_rc);
aoqi@0 97
aoqi@0 98 compare(oldOutDir, newOutDir);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 int old_javah(List<String> options, File outDir, File bootclasspath, String className)
aoqi@0 102 throws IOException, InterruptedException {
aoqi@0 103 List<String> cmd = new ArrayList<String>();
aoqi@0 104 cmd.add(old_javah_cmd.getPath());
aoqi@0 105 cmd.addAll(options);
aoqi@0 106 cmd.add("-d");
aoqi@0 107 cmd.add(outDir.getPath());
aoqi@0 108 cmd.add("-bootclasspath");
aoqi@0 109 cmd.add(bootclasspath.getPath());
aoqi@0 110 cmd.add(className);
aoqi@0 111 System.err.println("old_javah: " + cmd);
aoqi@0 112 ProcessBuilder pb = new ProcessBuilder(cmd);
aoqi@0 113 pb.redirectErrorStream(true);
aoqi@0 114 Process p = pb.start();
aoqi@0 115 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
aoqi@0 116 String line;
aoqi@0 117 StringBuilder sb = new StringBuilder();
aoqi@0 118 while ((line = in.readLine()) != null) {
aoqi@0 119 sb.append(line);
aoqi@0 120 sb.append("\n");
aoqi@0 121 }
aoqi@0 122 System.err.println("old javah out: " + sb.toString());
aoqi@0 123 return p.waitFor();
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 int new_javah(List<String> options, File outDir, File bootclasspath, String className) {
aoqi@0 127 List<String> args = new ArrayList<String>();
aoqi@0 128 args.addAll(options);
aoqi@0 129 args.add("-d");
aoqi@0 130 args.add(outDir.getPath());
aoqi@0 131 args.add("-bootclasspath");
aoqi@0 132 args.add(bootclasspath.getPath());
aoqi@0 133 args.add(className);
aoqi@0 134 StringWriter sw = new StringWriter();
aoqi@0 135 PrintWriter pw = new PrintWriter(sw);
aoqi@0 136 int rc = com.sun.tools.javah.Main.run(args.toArray(new String[args.size()]), pw);
aoqi@0 137 pw.close();
aoqi@0 138 System.err.println("new javah out: " + sw.toString());
aoqi@0 139 return rc;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 Set<String> getNativeClasses(JarFile jar) throws IOException, ConstantPoolException {
aoqi@0 143 System.err.println("getNativeClasses: " + jar.getName());
aoqi@0 144 Set<String> results = new TreeSet<String>();
aoqi@0 145 Enumeration<JarEntry> e = jar.entries();
aoqi@0 146 while (e.hasMoreElements()) {
aoqi@0 147 JarEntry je = e.nextElement();
aoqi@0 148 if (isNativeClass(jar, je)) {
aoqi@0 149 String name = je.getName();
aoqi@0 150 results.add(name.substring(0, name.length() - 6).replace("/", "."));
aoqi@0 151 }
aoqi@0 152 }
aoqi@0 153 return results;
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException {
aoqi@0 157 String name = entry.getName();
aoqi@0 158 if (name.startsWith("META-INF") || !name.endsWith(".class"))
aoqi@0 159 return false;
aoqi@0 160 //String className = name.substring(0, name.length() - 6).replace("/", ".");
aoqi@0 161 //System.err.println("check " + className);
aoqi@0 162 InputStream in = jar.getInputStream(entry);
aoqi@0 163 ClassFile cf = ClassFile.read(in);
aoqi@0 164 for (int i = 0; i < cf.methods.length; i++) {
aoqi@0 165 Method m = cf.methods[i];
aoqi@0 166 if (m.access_flags.is(AccessFlags.ACC_NATIVE)) {
aoqi@0 167 // System.err.println(className);
aoqi@0 168 return true;
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171 return false;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 void compare(File f1, File f2) throws IOException {
aoqi@0 175 if (f1.isFile() && f2.isFile())
aoqi@0 176 compareFiles(f1, f2);
aoqi@0 177 else if (f1.isDirectory() && f2.isDirectory())
aoqi@0 178 compareDirectories(f1, f2);
aoqi@0 179 else
aoqi@0 180 error("files differ: "
aoqi@0 181 + f1 + " (" + getFileType(f1) + "), "
aoqi@0 182 + f2 + " (" + getFileType(f2) + ")");
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 void compareDirectories(File d1, File d2) throws IOException {
aoqi@0 186 Set<String> list = new TreeSet<String>();
aoqi@0 187 list.addAll(Arrays.asList(d1.list()));
aoqi@0 188 list.addAll(Arrays.asList(d2.list()));
aoqi@0 189 for (String c: list)
aoqi@0 190 compare(new File(d1, c), new File(d2, c));
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 void compareFiles(File f1, File f2) throws IOException {
aoqi@0 194 byte[] c1 = readFile(f1);
aoqi@0 195 byte[] c2 = readFile(f2);
aoqi@0 196 if (!Arrays.equals(c1, c2))
aoqi@0 197 error("files differ: " + f1 + ", " + f2);
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 byte[] readFile(File file) throws IOException {
aoqi@0 201 int size = (int) file.length();
aoqi@0 202 byte[] data = new byte[size];
aoqi@0 203 DataInputStream in = new DataInputStream(new FileInputStream(file));
aoqi@0 204 try {
aoqi@0 205 in.readFully(data);
aoqi@0 206 } finally {
aoqi@0 207 in.close();
aoqi@0 208 }
aoqi@0 209 return data;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 String getFileType(File f) {
aoqi@0 213 return f.isDirectory() ? "directory"
aoqi@0 214 : f.isFile() ? "file"
aoqi@0 215 : f.exists() ? "other"
aoqi@0 216 : "not found";
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 /**
aoqi@0 220 * Set up an empty directory.
aoqi@0 221 */
aoqi@0 222 public File initDir(File dir) {
aoqi@0 223 if (dir.exists())
aoqi@0 224 deleteAll(dir);
aoqi@0 225 dir.mkdirs();
aoqi@0 226 return dir;
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 /**
aoqi@0 230 * Delete a file or a directory (including all its contents).
aoqi@0 231 */
aoqi@0 232 boolean deleteAll(File file) {
aoqi@0 233 if (file.isDirectory()) {
aoqi@0 234 for (File f: file.listFiles())
aoqi@0 235 deleteAll(f);
aoqi@0 236 }
aoqi@0 237 return file.delete();
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 File file(File dir, String... path) {
aoqi@0 241 File f = dir;
aoqi@0 242 for (String p: path)
aoqi@0 243 f = new File(f, p);
aoqi@0 244 return f;
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 /**
aoqi@0 248 * Report an error.
aoqi@0 249 */
aoqi@0 250 void error(String msg, String... more) {
aoqi@0 251 System.err.println("test: " + testOptions + " " + testClassName);
aoqi@0 252 System.err.println("error: " + msg);
aoqi@0 253 for (String s: more)
aoqi@0 254 System.err.println(s);
aoqi@0 255 errors++;
aoqi@0 256 System.exit(1);
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 File old_javah_cmd;
aoqi@0 260 File rt_jar;
aoqi@0 261 List<String> testOptions;
aoqi@0 262 String testClassName;
aoqi@0 263 int count;
aoqi@0 264 int errors;
aoqi@0 265 }

mercurial