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