test/tools/javah/6572945/T6572945.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javah/6572945/T6572945.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,239 @@
     1.4 +/*
     1.5 + * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6572945
    1.30 + * @summary rewrite javah as an annotation processor, instead of as a doclet
    1.31 + * @build TestClass1 TestClass2 TestClass3
    1.32 + * @run main T6572945
    1.33 + */
    1.34 +
    1.35 +import java.io.*;
    1.36 +import java.util.*;
    1.37 +import com.sun.tools.javah.Main;
    1.38 +
    1.39 +public class T6572945
    1.40 +{
    1.41 +    static File testSrc = new File(System.getProperty("test.src", "."));
    1.42 +    static File testClasses = new File(System.getProperty("test.classes", "."));
    1.43 +    static boolean isWindows = System.getProperty("os.name").startsWith("Windows");
    1.44 +
    1.45 +    public static void main(String... args)
    1.46 +        throws IOException, InterruptedException
    1.47 +    {
    1.48 +        boolean ok = new T6572945().run(args);
    1.49 +        if (!ok)
    1.50 +            throw new Error("Test Failed");
    1.51 +    }
    1.52 +
    1.53 +    public boolean run(String[] args)
    1.54 +        throws IOException, InterruptedException
    1.55 +    {
    1.56 +        if (args.length == 1)
    1.57 +            jdk = new File(args[0]);
    1.58 +
    1.59 +        test("-o", "jni.file.1",  "-jni", "TestClass1");
    1.60 +        test("-o", "jni.file.2",  "-jni", "TestClass1", "TestClass2");
    1.61 +        test("-d", "jni.dir.1",   "-jni", "TestClass1", "TestClass2");
    1.62 +        test("-o", "jni.file.3",  "-jni", "TestClass3");
    1.63 +
    1.64 +        // The following tests are disabled because llni support has been
    1.65 +        // discontinued, and because bugs in old javah means that character
    1.66 +        // for character testing against output from old javah does not work.
    1.67 +        // In fact, the LLNI impl in new javah is actually better than the
    1.68 +        // impl in old javah because of a couple of significant bug fixes.
    1.69 +
    1.70 +//        test("-o", "llni.file.1", "-llni", "TestClass1");
    1.71 +//        test("-o", "llni.file.2", "-llni", "TestClass1", "TestClass2");
    1.72 +//        test("-d", "llni.dir.1",  "-llni", "TestClass1", "TestClass2");
    1.73 +//        test("-o", "llni.file.3", "-llni", "TestClass3");
    1.74 +
    1.75 +        return (errors == 0);
    1.76 +    }
    1.77 +
    1.78 +    void test(String... args)
    1.79 +        throws IOException, InterruptedException
    1.80 +    {
    1.81 +        String[] cp_args = new String[args.length + 2];
    1.82 +        cp_args[0] = "-classpath";
    1.83 +        cp_args[1] = testClasses.getPath();
    1.84 +        System.arraycopy(args, 0, cp_args, 2, args.length);
    1.85 +
    1.86 +        if (jdk != null)
    1.87 +            init(cp_args);
    1.88 +
    1.89 +        File out = null;
    1.90 +        for (int i = 0; i < args.length; i++) {
    1.91 +            if (args[i].equals("-o")) {
    1.92 +                out = new File(args[++i]);
    1.93 +                break;
    1.94 +            } else if (args[i].equals("-d")) {
    1.95 +                out = new File(args[++i]);
    1.96 +                out.mkdirs();
    1.97 +                break;
    1.98 +            }
    1.99 +        }
   1.100 +
   1.101 +        try {
   1.102 +            System.out.println("test: " + Arrays.asList(cp_args));
   1.103 +
   1.104 +//            // Uncomment and use the following lines to execute javah via the
   1.105 +//            // command line -- for example, to run old javah and set up the golden files
   1.106 +//            List<String> cmd = new ArrayList<String>();
   1.107 +//            File javaHome = new File(System.getProperty("java.home"));
   1.108 +//            if (javaHome.getName().equals("jre"))
   1.109 +//                javaHome = javaHome.getParentFile();
   1.110 +//            File javah = new File(new File(javaHome, "bin"), "javah");
   1.111 +//            cmd.add(javah.getPath());
   1.112 +//            cmd.addAll(Arrays.asList(cp_args));
   1.113 +//            ProcessBuilder pb = new ProcessBuilder(cmd);
   1.114 +//            pb.redirectErrorStream(true);
   1.115 +//            pb.start();
   1.116 +//            Process p = pb.start();
   1.117 +//            String line;
   1.118 +//            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   1.119 +//            while ((line = in.readLine()) != null)
   1.120 +//                System.err.println(line);
   1.121 +//            in.close();
   1.122 +//            int rc = p.waitFor();
   1.123 +
   1.124 +            // Use new javah
   1.125 +            PrintWriter err = new PrintWriter(System.err, true);
   1.126 +            int rc = Main.run(cp_args, err);
   1.127 +
   1.128 +            if (rc != 0) {
   1.129 +                error("javah failed: rc=" + rc);
   1.130 +                return;
   1.131 +            }
   1.132 +
   1.133 +            // The golden files use the LL suffix for long constants, which
   1.134 +            // is used on Linux and Solaris.   On Windows, the suffix is i64,
   1.135 +            // so compare will update the golden files on the fly before the
   1.136 +            // final comparison.
   1.137 +            compare(new File(new File(testSrc, "gold"), out.getName()), out);
   1.138 +        } catch (Throwable t) {
   1.139 +            t.printStackTrace();
   1.140 +            error("javah threw exception");
   1.141 +        }
   1.142 +    }
   1.143 +
   1.144 +    void init(String[] args) throws IOException, InterruptedException {
   1.145 +        String[] cmdArgs = new String[args.length + 1];
   1.146 +        cmdArgs[0] = new File(new File(jdk, "bin"), "javah").getPath();
   1.147 +        System.arraycopy(args, 0, cmdArgs, 1, args.length);
   1.148 +
   1.149 +        System.out.println("init: " + Arrays.asList(cmdArgs));
   1.150 +
   1.151 +        ProcessBuilder pb = new ProcessBuilder(cmdArgs);
   1.152 +        pb.directory(new File(testSrc, "gold"));
   1.153 +        pb.redirectErrorStream(true);
   1.154 +        Process p = pb.start();
   1.155 +        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   1.156 +        String line;
   1.157 +        while ((line = in.readLine()) != null)
   1.158 +            System.out.println("javah: " + line);
   1.159 +        int rc = p.waitFor();
   1.160 +        if (rc != 0)
   1.161 +            error("javah: exit code " + rc);
   1.162 +    }
   1.163 +
   1.164 +    /** Compare two directories.
   1.165 +     *  @param f1 The golden directory
   1.166 +     *  @param f2 The directory to be compared
   1.167 +     */
   1.168 +    void compare(File f1, File f2) {
   1.169 +        compare(f1, f2, null);
   1.170 +    }
   1.171 +
   1.172 +    /** Compare two files or directories
   1.173 +     *  @param f1 The golden directory
   1.174 +     *  @param f2 The directory to be compared
   1.175 +     *  @param p An optional path identifying a file within the two directories
   1.176 +     */
   1.177 +    void compare(File f1, File f2, String p) {
   1.178 +        File f1p = (p == null ? f1 : new File(f1, p));
   1.179 +        File f2p = (p == null ? f2 : new File(f2, p));
   1.180 +        System.out.println("compare " + f1p + " " + f2p);
   1.181 +        if (f1p.isDirectory() && f2p.isDirectory()) {
   1.182 +            Set<String> children = new HashSet<String>();
   1.183 +            children.addAll(Arrays.asList(f1p.list()));
   1.184 +            children.addAll(Arrays.asList(f2p.list()));
   1.185 +            for (String c: children) {
   1.186 +                compare(f1, f2, new File(p, c).getPath()); // null-safe for p
   1.187 +            }
   1.188 +        }
   1.189 +        else if (f1p.isFile() && f2p.isFile()) {
   1.190 +            String s1 = read(f1p);
   1.191 +            if (isWindows) {
   1.192 +                // f1/s1 is the golden file
   1.193 +                // on Windows, long constants use the i64 suffix, not LL
   1.194 +                s1 = s1.replaceAll("( [0-9]+)LL\n", "$1i64\n");
   1.195 +            }
   1.196 +            String s2 = read(f2p);
   1.197 +            if (!s1.equals(s2)) {
   1.198 +                System.out.println("File: " + f1p + "\n" + s1);
   1.199 +                System.out.println("File: " + f2p + "\n" + s2);
   1.200 +                error("Files differ: " + f1p + " " + f2p);
   1.201 +            }
   1.202 +        }
   1.203 +        else if (f1p.exists() && !f2p.exists())
   1.204 +            error("Only in " + f1 + ": " + p);
   1.205 +        else if (f2p.exists() && !f1p.exists())
   1.206 +            error("Only in " + f2 + ": " + p);
   1.207 +        else
   1.208 +            error("Files differ: " + f1p + " " + f2p);
   1.209 +    }
   1.210 +
   1.211 +    private String read(File f) {
   1.212 +        try {
   1.213 +            BufferedReader in = new BufferedReader(new FileReader(f));
   1.214 +            try {
   1.215 +                StringBuilder sb = new StringBuilder((int) f.length());
   1.216 +                String line;
   1.217 +                while ((line = in.readLine()) != null) {
   1.218 +                    sb.append(line);
   1.219 +                    sb.append("\n");
   1.220 +                }
   1.221 +                return sb.toString();
   1.222 +            } finally {
   1.223 +                try {
   1.224 +                    in.close();
   1.225 +                } catch (IOException e) {
   1.226 +                }
   1.227 +            }
   1.228 +        } catch (IOException e) {
   1.229 +            error("error reading " + f + ": " + e);
   1.230 +            return "";
   1.231 +        }
   1.232 +    }
   1.233 +
   1.234 +
   1.235 +    private void error(String msg) {
   1.236 +        System.out.println(msg);
   1.237 +        errors++;
   1.238 +    }
   1.239 +
   1.240 +    private int errors;
   1.241 +    private File jdk;
   1.242 +}

mercurial