jjg@416: /* ohair@554: * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. jjg@416: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@416: * jjg@416: * This code is free software; you can redistribute it and/or modify it jjg@416: * under the terms of the GNU General Public License version 2 only, as jjg@416: * published by the Free Software Foundation. jjg@416: * jjg@416: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@416: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@416: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@416: * version 2 for more details (a copy is included in the LICENSE file that jjg@416: * accompanied this code). jjg@416: * jjg@416: * You should have received a copy of the GNU General Public License version jjg@416: * 2 along with this work; if not, write to the Free Software Foundation, jjg@416: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@416: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@416: */ jjg@416: jjg@416: /* jjg@416: * @test jjg@416: * @bug 6572945 jjg@416: * @summary rewrite javah as an annotation processor, instead of as a doclet jjg@416: * @build TestClass1 TestClass2 TestClass3 jjg@416: * @run main T6572945 jjg@416: */ jjg@416: jjg@416: import java.io.*; jjg@416: import java.util.*; jjg@416: import com.sun.tools.javah.Main; jjg@416: jjg@416: public class T6572945 jjg@416: { jjg@416: static File testSrc = new File(System.getProperty("test.src", ".")); jjg@416: static File testClasses = new File(System.getProperty("test.classes", ".")); jjg@416: static boolean isWindows = System.getProperty("os.name").startsWith("Windows"); jjg@416: jjg@416: public static void main(String... args) jjg@416: throws IOException, InterruptedException jjg@416: { jjg@416: boolean ok = new T6572945().run(args); jjg@416: if (!ok) jjg@416: throw new Error("Test Failed"); jjg@416: } jjg@416: jjg@416: public boolean run(String[] args) jjg@416: throws IOException, InterruptedException jjg@416: { jjg@416: if (args.length == 1) jjg@416: jdk = new File(args[0]); jjg@416: jjg@416: test("-o", "jni.file.1", "-jni", "TestClass1"); jjg@416: test("-o", "jni.file.2", "-jni", "TestClass1", "TestClass2"); jjg@416: test("-d", "jni.dir.1", "-jni", "TestClass1", "TestClass2"); jjg@416: test("-o", "jni.file.3", "-jni", "TestClass3"); jjg@416: jjg@416: // The following tests are disabled because llni support has been jjg@416: // discontinued, and because bugs in old javah means that character jjg@416: // for character testing against output from old javah does not work. jjg@416: // In fact, the LLNI impl in new javah is actually better than the jjg@416: // impl in old javah because of a couple of significant bug fixes. jjg@416: jjg@416: // test("-o", "llni.file.1", "-llni", "TestClass1"); jjg@416: // test("-o", "llni.file.2", "-llni", "TestClass1", "TestClass2"); jjg@416: // test("-d", "llni.dir.1", "-llni", "TestClass1", "TestClass2"); jjg@416: // test("-o", "llni.file.3", "-llni", "TestClass3"); jjg@416: jjg@416: return (errors == 0); jjg@416: } jjg@416: jjg@416: void test(String... args) jjg@416: throws IOException, InterruptedException jjg@416: { jjg@416: String[] cp_args = new String[args.length + 2]; jjg@416: cp_args[0] = "-classpath"; jjg@416: cp_args[1] = testClasses.getPath(); jjg@416: System.arraycopy(args, 0, cp_args, 2, args.length); jjg@416: jjg@416: if (jdk != null) jjg@416: init(cp_args); jjg@416: jjg@416: File out = null; jjg@416: for (int i = 0; i < args.length; i++) { jjg@416: if (args[i].equals("-o")) { jjg@416: out = new File(args[++i]); jjg@416: break; jjg@416: } else if (args[i].equals("-d")) { jjg@416: out = new File(args[++i]); jjg@416: out.mkdirs(); jjg@416: break; jjg@416: } jjg@416: } jjg@416: jjg@416: try { jjg@416: System.out.println("test: " + Arrays.asList(cp_args)); jjg@416: jjg@416: // // Uncomment and use the following lines to execute javah via the jjg@416: // // command line -- for example, to run old javah and set up the golden files jjg@416: // List cmd = new ArrayList(); jjg@416: // File javaHome = new File(System.getProperty("java.home")); jjg@416: // if (javaHome.getName().equals("jre")) jjg@416: // javaHome = javaHome.getParentFile(); jjg@416: // File javah = new File(new File(javaHome, "bin"), "javah"); jjg@416: // cmd.add(javah.getPath()); jjg@416: // cmd.addAll(Arrays.asList(cp_args)); jjg@416: // ProcessBuilder pb = new ProcessBuilder(cmd); jjg@416: // pb.redirectErrorStream(true); jjg@416: // pb.start(); jjg@416: // Process p = pb.start(); jjg@416: // String line; jjg@416: // BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); jjg@416: // while ((line = in.readLine()) != null) jjg@416: // System.err.println(line); jjg@416: // in.close(); jjg@416: // int rc = p.waitFor(); jjg@416: jjg@416: // Use new javah jjg@416: PrintWriter err = new PrintWriter(System.err, true); jjg@416: int rc = Main.run(cp_args, err); jjg@416: jjg@416: if (rc != 0) { jjg@416: error("javah failed: rc=" + rc); jjg@416: return; jjg@416: } jjg@416: jjg@416: // The golden files use the LL suffix for long constants, which jjg@416: // is used on Linux and Solaris. On Windows, the suffix is i64, jjg@416: // so compare will update the golden files on the fly before the jjg@416: // final comparison. jjg@416: compare(new File(new File(testSrc, "gold"), out.getName()), out); jjg@416: } catch (Throwable t) { jjg@416: t.printStackTrace(); jjg@416: error("javah threw exception"); jjg@416: } jjg@416: } jjg@416: jjg@416: void init(String[] args) throws IOException, InterruptedException { jjg@416: String[] cmdArgs = new String[args.length + 1]; jjg@416: cmdArgs[0] = new File(new File(jdk, "bin"), "javah").getPath(); jjg@416: System.arraycopy(args, 0, cmdArgs, 1, args.length); jjg@416: jjg@416: System.out.println("init: " + Arrays.asList(cmdArgs)); jjg@416: jjg@416: ProcessBuilder pb = new ProcessBuilder(cmdArgs); jjg@416: pb.directory(new File(testSrc, "gold")); jjg@416: pb.redirectErrorStream(true); jjg@416: Process p = pb.start(); jjg@416: BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); jjg@416: String line; jjg@416: while ((line = in.readLine()) != null) jjg@416: System.out.println("javah: " + line); jjg@416: int rc = p.waitFor(); jjg@416: if (rc != 0) jjg@416: error("javah: exit code " + rc); jjg@416: } jjg@416: jjg@416: /** Compare two directories. jjg@416: * @param f1 The golden directory jjg@416: * @param f2 The directory to be compared jjg@416: */ jjg@416: void compare(File f1, File f2) { jjg@416: compare(f1, f2, null); jjg@416: } jjg@416: jjg@416: /** Compare two files or directories jjg@416: * @param f1 The golden directory jjg@416: * @param f2 The directory to be compared jjg@416: * @param p An optional path identifying a file within the two directories jjg@416: */ jjg@416: void compare(File f1, File f2, String p) { jjg@416: File f1p = (p == null ? f1 : new File(f1, p)); jjg@416: File f2p = (p == null ? f2 : new File(f2, p)); jjg@416: System.out.println("compare " + f1p + " " + f2p); jjg@416: if (f1p.isDirectory() && f2p.isDirectory()) { jjg@416: Set children = new HashSet(); jjg@416: children.addAll(Arrays.asList(f1p.list())); jjg@416: children.addAll(Arrays.asList(f2p.list())); jjg@416: for (String c: children) { jjg@416: compare(f1, f2, new File(p, c).getPath()); // null-safe for p jjg@416: } jjg@416: } jjg@416: else if (f1p.isFile() && f2p.isFile()) { jjg@416: String s1 = read(f1p); jjg@416: if (isWindows) { jjg@416: // f1/s1 is the golden file jjg@416: // on Windows, long constants use the i64 suffix, not LL jjg@416: s1 = s1.replaceAll("( [0-9]+)LL\n", "$1i64\n"); jjg@416: } jjg@416: String s2 = read(f2p); jjg@416: if (!s1.equals(s2)) { jjg@416: System.out.println("File: " + f1p + "\n" + s1); jjg@416: System.out.println("File: " + f2p + "\n" + s2); jjg@416: error("Files differ: " + f1p + " " + f2p); jjg@416: } jjg@416: } jjg@416: else if (f1p.exists() && !f2p.exists()) jjg@416: error("Only in " + f1 + ": " + p); jjg@416: else if (f2p.exists() && !f1p.exists()) jjg@416: error("Only in " + f2 + ": " + p); jjg@416: else jjg@416: error("Files differ: " + f1p + " " + f2p); jjg@416: } jjg@416: jjg@416: private String read(File f) { jjg@416: try { jjg@416: BufferedReader in = new BufferedReader(new FileReader(f)); jjg@416: try { jjg@416: StringBuilder sb = new StringBuilder((int) f.length()); jjg@416: String line; jjg@416: while ((line = in.readLine()) != null) { jjg@416: sb.append(line); jjg@416: sb.append("\n"); jjg@416: } jjg@416: return sb.toString(); jjg@416: } finally { jjg@416: try { jjg@416: in.close(); jjg@416: } catch (IOException e) { jjg@416: } jjg@416: } jjg@416: } catch (IOException e) { jjg@416: error("error reading " + f + ": " + e); jjg@416: return ""; jjg@416: } jjg@416: } jjg@416: jjg@416: jjg@416: private void error(String msg) { jjg@416: System.out.println(msg); jjg@416: errors++; jjg@416: } jjg@416: jjg@416: private int errors; jjg@416: private File jdk; jjg@416: }