test/tools/javah/6572945/T6572945.java

Fri, 27 Sep 2013 11:34:32 -0700

author
mduigou
date
Fri, 27 Sep 2013 11:34:32 -0700
changeset 2073
4ed8565fa536
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8024842: Define ABS_TEST_OUTPUT_DIR via TEST_OUTPUT_DIR
Reviewed-by: ihse, erikj, vromero

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

mercurial