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