test/tools/javac/nativeHeaders/javahComparison/CompareTest.java

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 1723
a2889739cf21
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

jjg@1230 1 /*
jjg@1723 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1230 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1230 4 *
jjg@1230 5 * This code is free software; you can redistribute it and/or modify it
jjg@1230 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1230 7 * published by the Free Software Foundation.
jjg@1230 8 *
jjg@1230 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1230 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1230 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1230 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1230 13 * accompanied this code).
jjg@1230 14 *
jjg@1230 15 * You should have received a copy of the GNU General Public License version
jjg@1230 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1230 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1230 18 *
jjg@1230 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1230 20 * or visit www.oracle.com if you need additional information or have any
jjg@1230 21 * questions.
jjg@1230 22 */
jjg@1230 23
jjg@1230 24 /*
jjg@1230 25 * @test
jjg@1723 26 * @bug 7150368 8003412 8000407
jjg@1230 27 * @summary javac should include basic ability to generate native headers
jjg@1230 28 */
jjg@1230 29
jjg@1230 30 import java.io.File;
jjg@1230 31 import java.io.IOException;
jjg@1230 32 import java.io.PrintWriter;
jjg@1230 33 import java.nio.file.Files;
jjg@1230 34 import java.util.ArrayList;
jjg@1230 35 import java.util.Arrays;
jjg@1230 36 import java.util.HashSet;
jjg@1230 37 import java.util.List;
jjg@1230 38 import java.util.Set;
jjg@1230 39
jjg@1230 40 public class CompareTest {
jjg@1230 41 public static void main(String... args) throws Exception {
jjg@1230 42 new CompareTest().run();
jjg@1230 43 }
jjg@1230 44
jjg@1230 45 void run() throws Exception {
jjg@1230 46 File srcDir = new File(System.getProperty("test.src"));
jjg@1230 47 File classesDir = new File("classes");
jjg@1230 48 classesDir.mkdirs();
jjg@1230 49 File javacHeaders = new File("headers.javac");
jjg@1230 50 javacHeaders.mkdirs();
jjg@1230 51 File javahHeaders = new File("headers.javah");
jjg@1230 52 javahHeaders.mkdirs();
jjg@1230 53
jjg@1230 54 List<String> javacArgs = new ArrayList<String>();
jjg@1230 55 javacArgs.add("-d");
jjg@1230 56 javacArgs.add(classesDir.getPath());
jjg@1230 57 javacArgs.add("-h");
jjg@1230 58 javacArgs.add(javacHeaders.getPath());
jjg@1230 59 javacArgs.add("-XDjavah:full");
jjg@1230 60
jjg@1230 61 for (File f: srcDir.listFiles()) {
jjg@1230 62 if (f.getName().matches("TestClass[0-9]+\\.java")) {
jjg@1230 63 sourceFileCount++;
jjg@1230 64 javacArgs.add(f.getPath());
jjg@1230 65 }
jjg@1230 66 }
jjg@1230 67
jjg@1230 68 int rc = com.sun.tools.javac.Main.compile(javacArgs.toArray(new String[javacArgs.size()]));
jjg@1230 69 if (rc != 0)
jjg@1230 70 throw new Exception("javac failed; rc=" + rc);
jjg@1230 71
jjg@1230 72 List<String> javahArgs = new ArrayList<String>();
jjg@1230 73 javahArgs.add("-d");
jjg@1230 74 javahArgs.add(javahHeaders.getPath());
jjg@1230 75
jjg@1230 76 for (File f: classesDir.listFiles()) {
jjg@1230 77 if (f.getName().endsWith(".class")) {
jjg@1230 78 javahArgs.add(inferBinaryName(f));
jjg@1230 79 }
jjg@1230 80 }
jjg@1230 81
jjg@1230 82 PrintWriter pw = new PrintWriter(System.out, true);
jjg@1230 83 rc = com.sun.tools.javah.Main.run(javahArgs.toArray(new String[javahArgs.size()]), pw);
jjg@1230 84 if (rc != 0)
jjg@1230 85 throw new Exception("javah failed; rc=" + rc);
jjg@1230 86
jjg@1230 87 compare(javahHeaders, javacHeaders);
jjg@1230 88
jjg@1230 89 int javahHeaderCount = javahHeaders.list().length;
jjg@1230 90 int javacHeaderCount = javacHeaders.list().length;
jjg@1230 91
jjg@1230 92 System.out.println(sourceFileCount + " .java files found");
jjg@1230 93 System.out.println(javacHeaderCount + " .h files generated by javac");
jjg@1230 94 System.out.println(javahHeaderCount + " .h files generated by javah");
jjg@1230 95 System.out.println(compareCount + " header files compared");
jjg@1230 96
jjg@1230 97 if (javacHeaderCount != javahHeaderCount || javacHeaderCount != compareCount)
jjg@1230 98 error("inconsistent counts");
jjg@1230 99
jjg@1230 100 if (errors > 0)
jjg@1230 101 throw new Exception(errors + " errors occurred");
jjg@1230 102 }
jjg@1230 103
jjg@1230 104 String inferBinaryName(File file) {
jjg@1230 105 String name = file.getName();
jjg@1230 106 return name.substring(0, name.length() - ".class".length()).replace("$", ".");
jjg@1230 107 }
jjg@1230 108
jjg@1230 109 /** Compare two directories.
jjg@1230 110 * @param f1 The golden directory
jjg@1230 111 * @param f2 The directory to be compared
jjg@1230 112 */
jjg@1230 113 void compare(File f1, File f2) {
jjg@1230 114 compare(f1, f2, null);
jjg@1230 115 }
jjg@1230 116
jjg@1230 117 /** Compare two files or directories
jjg@1230 118 * @param f1 The golden directory
jjg@1230 119 * @param f2 The directory to be compared
jjg@1230 120 * @param p An optional path identifying a file within the two directories
jjg@1230 121 */
jjg@1230 122 void compare(File f1, File f2, String p) {
jjg@1230 123 File f1p = (p == null ? f1 : new File(f1, p));
jjg@1230 124 File f2p = (p == null ? f2 : new File(f2, p));
jjg@1230 125 if (f1p.isDirectory() && f2p.isDirectory()) {
jjg@1230 126 Set<String> children = new HashSet<String>();
jjg@1230 127 children.addAll(Arrays.asList(f1p.list()));
jjg@1230 128 children.addAll(Arrays.asList(f2p.list()));
jjg@1230 129 for (String c: children) {
jjg@1230 130 compare(f1, f2, new File(p, c).getPath()); // null-safe for p
jjg@1230 131 }
jjg@1230 132 }
jjg@1230 133 else if (f1p.isFile() && f2p.isFile()) {
jjg@1230 134 System.out.println("checking " + p);
jjg@1230 135 compareCount++;
jjg@1230 136 String s1 = read(f1p);
jjg@1230 137 String s2 = read(f2p);
jjg@1230 138 if (!s1.equals(s2)) {
jjg@1230 139 System.out.println("File: " + f1p + "\n" + s1);
jjg@1230 140 System.out.println("File: " + f2p + "\n" + s2);
jjg@1230 141 error("Files differ: " + f1p + " " + f2p);
jjg@1230 142 }
jjg@1230 143 }
jjg@1230 144 else if (f1p.exists() && !f2p.exists())
jjg@1230 145 error("Only in " + f1 + ": " + p);
jjg@1230 146 else if (f2p.exists() && !f1p.exists())
jjg@1230 147 error("Only in " + f2 + ": " + p);
jjg@1230 148 else
jjg@1230 149 error("Files differ: " + f1p + " " + f2p);
jjg@1230 150 }
jjg@1230 151
jjg@1230 152 private String read(File f) {
jjg@1230 153 try {
jjg@1230 154 return new String(Files.readAllBytes(f.toPath()));
jjg@1230 155 } catch (IOException e) {
jjg@1230 156 error("error reading " + f + ": " + e);
jjg@1230 157 return "";
jjg@1230 158 }
jjg@1230 159 }
jjg@1230 160
jjg@1230 161 private void error(String msg) {
jjg@1230 162 System.out.println(msg);
jjg@1230 163 errors++;
jjg@1230 164 }
jjg@1230 165
jjg@1230 166 private int errors;
jjg@1230 167 private int compareCount;
jjg@1230 168 private int sourceFileCount;
jjg@1230 169 }

mercurial