test/tools/javac/nativeHeaders/NativeHeaderTest.java

changeset 1230
b14d9583ce92
child 1407
f14c693a0e48
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/nativeHeaders/NativeHeaderTest.java	Tue Mar 13 15:43:40 2012 -0700
     1.3 @@ -0,0 +1,274 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 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 7150368
    1.30 + * @summary javac should include basic ability to generate native headers
    1.31 + */
    1.32 +
    1.33 +import java.io.File;
    1.34 +import java.io.FileWriter;
    1.35 +import java.io.IOException;
    1.36 +import java.lang.annotation.Annotation;
    1.37 +import java.lang.annotation.Retention;
    1.38 +import java.lang.annotation.RetentionPolicy;
    1.39 +import java.lang.reflect.InvocationTargetException;
    1.40 +import java.lang.reflect.Method;
    1.41 +import java.util.ArrayList;
    1.42 +import java.util.Arrays;
    1.43 +import java.util.HashSet;
    1.44 +import java.util.List;
    1.45 +import java.util.Set;
    1.46 +
    1.47 +import javax.tools.StandardJavaFileManager;
    1.48 +import javax.tools.StandardLocation;
    1.49 +
    1.50 +import com.sun.source.util.JavacTask;
    1.51 +import com.sun.tools.javac.api.JavacTool;
    1.52 +
    1.53 +public class NativeHeaderTest {
    1.54 +    public static void main(String... args) throws Exception {
    1.55 +        new NativeHeaderTest().run();
    1.56 +    }
    1.57 +
    1.58 +    /** How to invoke javac. */
    1.59 +    enum RunKind {
    1.60 +        /** Use the command line entry point. */
    1.61 +        CMD,
    1.62 +        /** Use the JavaCompiler API. */
    1.63 +        API
    1.64 +    };
    1.65 +
    1.66 +    /** Which classes for which to generate headers. */
    1.67 +    enum GenKind {
    1.68 +        /** Just classes with native methods or the marker annotation. */
    1.69 +        SIMPLE,
    1.70 +        /** All appropriate classes within the top level class. */
    1.71 +        FULL
    1.72 +    };
    1.73 +
    1.74 +    // ---------- Test cases, invoked reflectively via run. ----------
    1.75 +
    1.76 +    @Test
    1.77 +    void simpleTest(RunKind rk, GenKind gk) throws Exception {
    1.78 +        List<File> files = new ArrayList<File>();
    1.79 +        files.add(createFile("p/C.java",
    1.80 +                "class C { native void m(); }"));
    1.81 +
    1.82 +        Set<String> expect = createSet("C.h");
    1.83 +
    1.84 +        test(rk, gk, files, expect);
    1.85 +    }
    1.86 +
    1.87 +    @Test
    1.88 +    void nestedClassTest(RunKind rk, GenKind gk) throws Exception {
    1.89 +        List<File> files = new ArrayList<File>();
    1.90 +        files.add(createFile("p/C.java",
    1.91 +                "class C { static class Inner { native void m(); } }"));
    1.92 +
    1.93 +        Set<String> expect = createSet("C_Inner.h");
    1.94 +        if (gk == GenKind.FULL) expect.add("C.h");
    1.95 +
    1.96 +        test(rk, gk, files, expect);
    1.97 +    }
    1.98 +
    1.99 +    @Test
   1.100 +    void localClassTest(RunKind rk, GenKind gk) throws Exception {
   1.101 +        List<File> files = new ArrayList<File>();
   1.102 +        files.add(createFile("p/C.java",
   1.103 +                "class C { native void m(); void m2() { class Local { } } }"));
   1.104 +
   1.105 +        Set<String> expect = createSet("C.h");
   1.106 +
   1.107 +        test(rk, gk, files, expect);
   1.108 +    }
   1.109 +
   1.110 +    @Test
   1.111 +    void syntheticClassTest(RunKind rk, GenKind gk) throws Exception {
   1.112 +        List<File> files = new ArrayList<File>();
   1.113 +        files.add(createFile("p/C.java",
   1.114 +                "class C {\n"
   1.115 +                + "    private C() { }\n"
   1.116 +                + "    class Inner extends C { native void m(); }\n"
   1.117 +                + "}"));
   1.118 +
   1.119 +        Set<String> expect = createSet("C_Inner.h");
   1.120 +        if (gk == GenKind.FULL) expect.add("C.h");
   1.121 +
   1.122 +        test(rk, gk, files, expect);
   1.123 +
   1.124 +        // double check the synthetic class was generated
   1.125 +        checkEqual("generatedClasses",
   1.126 +                createSet("C.class", "C$1.class", "C$Inner.class"),
   1.127 +                createSet(classesDir.list()));
   1.128 +    }
   1.129 +
   1.130 +    @Test
   1.131 +    void annoTest(RunKind rk, GenKind gk) throws Exception {
   1.132 +        List<File> files = new ArrayList<File>();
   1.133 +        files.add(createFile("p/C.java",
   1.134 +                "@javax.tools.annotation.GenerateNativeHeader class C { }"));
   1.135 +
   1.136 +        Set<String> expect = createSet("C.h");
   1.137 +
   1.138 +        test(rk, gk, files, expect);
   1.139 +    }
   1.140 +
   1.141 +    @Test
   1.142 +    void annoNestedClassTest(RunKind rk, GenKind gk) throws Exception {
   1.143 +        List<File> files = new ArrayList<File>();
   1.144 +        files.add(createFile("p/C.java",
   1.145 +                "class C { @javax.tools.annotation.GenerateNativeHeader class Inner { } }"));
   1.146 +
   1.147 +        Set<String> expect = createSet("C_Inner.h");
   1.148 +        if (gk == GenKind.FULL) expect.add("C.h");
   1.149 +
   1.150 +        test(rk, gk, files, expect);
   1.151 +    }
   1.152 +
   1.153 +    /**
   1.154 +     * The worker method for each test case.
   1.155 +     * Compile the files and verify that exactly the expected set of header files
   1.156 +     * is generated.
   1.157 +     */
   1.158 +    void test(RunKind rk, GenKind gk, List<File> files, Set<String> expect) throws Exception {
   1.159 +        List<String> args = new ArrayList<String>();
   1.160 +        if (gk == GenKind.FULL)
   1.161 +            args.add("-XDjavah:full");
   1.162 +
   1.163 +        switch (rk) {
   1.164 +            case CMD:
   1.165 +                args.add("-d");
   1.166 +                args.add(classesDir.getPath());
   1.167 +                args.add("-h");
   1.168 +                args.add(headersDir.getPath());
   1.169 +                for (File f: files)
   1.170 +                    args.add(f.getPath());
   1.171 +                int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]));
   1.172 +                if (rc != 0)
   1.173 +                    throw new Exception("compilation failed, rc=" + rc);
   1.174 +                break;
   1.175 +
   1.176 +            case API:
   1.177 +                fm.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(srcDir));
   1.178 +                fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(classesDir));
   1.179 +                fm.setLocation(StandardLocation.NATIVE_HEADER_OUTPUT, Arrays.asList(headersDir));
   1.180 +                JavacTask task = javac.getTask(null, fm, null, args, null,
   1.181 +                        fm.getJavaFileObjectsFromFiles(files));
   1.182 +                if (!task.call())
   1.183 +                    throw new Exception("compilation failed");
   1.184 +                break;
   1.185 +        }
   1.186 +
   1.187 +        Set<String> found = createSet(headersDir.list());
   1.188 +        checkEqual("header files", expect, found);
   1.189 +    }
   1.190 +
   1.191 +    /** Marker annotation for test cases. */
   1.192 +    @Retention(RetentionPolicy.RUNTIME)
   1.193 +    @interface Test { }
   1.194 +
   1.195 +    /** Combo test to run all test cases in all modes. */
   1.196 +    void run() throws Exception {
   1.197 +        javac = JavacTool.create();
   1.198 +        fm = javac.getStandardFileManager(null, null, null);
   1.199 +
   1.200 +        for (RunKind rk: RunKind.values()) {
   1.201 +            for (GenKind gk: GenKind.values()) {
   1.202 +                for (Method m: getClass().getDeclaredMethods()) {
   1.203 +                    Annotation a = m.getAnnotation(Test.class);
   1.204 +                    if (a != null) {
   1.205 +                        init(rk, gk, m.getName());
   1.206 +                        try {
   1.207 +                            m.invoke(this, new Object[] { rk, gk });
   1.208 +                        } catch (InvocationTargetException e) {
   1.209 +                            Throwable cause = e.getCause();
   1.210 +                            throw (cause instanceof Exception) ? ((Exception) cause) : e;
   1.211 +                        }
   1.212 +                        System.err.println();
   1.213 +                    }
   1.214 +                }
   1.215 +            }
   1.216 +        }
   1.217 +        System.err.println(testCount + " tests" + ((errorCount == 0) ? "" : ", " + errorCount + " errors"));
   1.218 +        if (errorCount > 0)
   1.219 +            throw new Exception(errorCount + " errors found");
   1.220 +    }
   1.221 +
   1.222 +    /**
   1.223 +     * Init directories for a test case.
   1.224 +     */
   1.225 +    void init(RunKind rk, GenKind gk, String name) throws IOException {
   1.226 +        System.err.println("Test " + rk + " " + gk + " " + name);
   1.227 +        testCount++;
   1.228 +
   1.229 +        testDir = new File(rk.toString().toLowerCase() + "_" + gk.toString().toLowerCase() + "-" + name);
   1.230 +        srcDir = new File(testDir, "src");
   1.231 +        srcDir.mkdirs();
   1.232 +        classesDir = new File(testDir, "classes");
   1.233 +        classesDir.mkdirs();
   1.234 +        headersDir = new File(testDir, "headers");
   1.235 +        headersDir.mkdirs();
   1.236 +    }
   1.237 +
   1.238 +    /** Create a source file with given body text. */
   1.239 +    File createFile(String path, final String body) throws IOException {
   1.240 +        File f = new File(srcDir, path);
   1.241 +        f.getParentFile().mkdirs();
   1.242 +        try (FileWriter out = new FileWriter(f)) {
   1.243 +            out.write(body);
   1.244 +        }
   1.245 +        return f;
   1.246 +    }
   1.247 +
   1.248 +    /** Convenience method to create a set of items. */
   1.249 +    <T> Set<T> createSet(T... items) {
   1.250 +        return new HashSet<T>(Arrays.asList(items));
   1.251 +    }
   1.252 +
   1.253 +    /** Convenience method to check two values are equal, and report an error if not. */
   1.254 +    <T> void checkEqual(String label, T expect, T found) {
   1.255 +        if ((found == null) ? (expect == null) : found.equals(expect))
   1.256 +            return;
   1.257 +        System.err.println("Error: mismatch");
   1.258 +        System.err.println("  expected: " + expect);
   1.259 +        System.err.println("     found: " + found);
   1.260 +        errorCount++;
   1.261 +    }
   1.262 +
   1.263 +    // Shared across API test cases
   1.264 +    JavacTool javac;
   1.265 +    StandardJavaFileManager fm;
   1.266 +
   1.267 +    // Directories set up by init
   1.268 +    File testDir;
   1.269 +    File srcDir;
   1.270 +    File classesDir;
   1.271 +    File headersDir;
   1.272 +
   1.273 +    // Statistics
   1.274 +    int testCount;
   1.275 +    int errorCount;
   1.276 +}
   1.277 +

mercurial