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