Mon, 21 Jan 2013 11:16:28 -0800
Merge
1 /*
2 * Copyright (c) 2012, 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 7150368 8003412
27 * @summary javac should include basic ability to generate native headers
28 */
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.lang.annotation.Annotation;
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
44 import javax.tools.StandardJavaFileManager;
45 import javax.tools.StandardLocation;
47 import com.sun.source.util.JavacTask;
48 import com.sun.tools.javac.api.JavacTool;
50 public class NativeHeaderTest {
51 public static void main(String... args) throws Exception {
52 new NativeHeaderTest().run();
53 }
55 /** How to invoke javac. */
56 enum RunKind {
57 /** Use the command line entry point. */
58 CMD,
59 /** Use the JavaCompiler API. */
60 API
61 };
63 /** Which classes for which to generate headers. */
64 enum GenKind {
65 /** Just classes with native methods or the marker annotation. */
66 SIMPLE,
67 /** All appropriate classes within the top level class. */
68 FULL
69 };
71 // ---------- Test cases, invoked reflectively via run. ----------
73 @Test
74 void simpleTest(RunKind rk, GenKind gk) throws Exception {
75 List<File> files = new ArrayList<File>();
76 files.add(createFile("p/C.java",
77 "class C { native void m(); }"));
79 Set<String> expect = createSet("C.h");
81 test(rk, gk, files, expect);
82 }
84 @Test
85 void nestedClassTest(RunKind rk, GenKind gk) throws Exception {
86 List<File> files = new ArrayList<File>();
87 files.add(createFile("p/C.java",
88 "class C { static class Inner { native void m(); } }"));
90 Set<String> expect = createSet("C_Inner.h");
91 if (gk == GenKind.FULL) expect.add("C.h");
93 test(rk, gk, files, expect);
94 }
96 @Test
97 void localClassTest(RunKind rk, GenKind gk) throws Exception {
98 List<File> files = new ArrayList<File>();
99 files.add(createFile("p/C.java",
100 "class C { native void m(); void m2() { class Local { } } }"));
102 Set<String> expect = createSet("C.h");
104 test(rk, gk, files, expect);
105 }
107 @Test
108 void syntheticClassTest(RunKind rk, GenKind gk) throws Exception {
109 List<File> files = new ArrayList<File>();
110 files.add(createFile("p/C.java",
111 "class C {\n"
112 + " private C() { }\n"
113 + " class Inner extends C { native void m(); }\n"
114 + "}"));
116 Set<String> expect = createSet("C_Inner.h");
117 if (gk == GenKind.FULL) expect.add("C.h");
119 test(rk, gk, files, expect);
121 // double check the synthetic class was generated
122 checkEqual("generatedClasses",
123 createSet("C.class", "C$1.class", "C$Inner.class"),
124 createSet(classesDir.list()));
125 }
127 @Test
128 void oldAnnoTest(RunKind rk, GenKind gk) throws Exception {
129 List<File> files = new ArrayList<File>();
130 files.add(createFile("p/C.java",
131 "@javax.tools.annotation.GenerateNativeHeader class C { }"));
133 Set<String> expect = createSet("C.h");
135 test(rk, gk, files, expect);
136 }
138 @Test
139 void annoTest(RunKind rk, GenKind gk) throws Exception {
140 List<File> files = new ArrayList<File>();
141 files.add(createFile("p/C.java",
142 "class C { @java.lang.annotation.Native public static final int i = 1907; }"));
144 Set<String> expect = createSet("C.h");
146 test(rk, gk, files, expect);
147 }
149 @Test
150 void oldAnnoNestedClassTest(RunKind rk, GenKind gk) throws Exception {
151 List<File> files = new ArrayList<File>();
152 files.add(createFile("p/C.java",
153 "class C { @javax.tools.annotation.GenerateNativeHeader class Inner { } }"));
155 Set<String> expect = createSet("C_Inner.h");
156 if (gk == GenKind.FULL) expect.add("C.h");
158 test(rk, gk, files, expect);
159 }
161 @Test
162 void annoNestedClassTest(RunKind rk, GenKind gk) throws Exception {
163 List<File> files = new ArrayList<File>();
164 files.add(createFile("p/C.java",
165 "class C { class Inner { @java.lang.annotation.Native public static final int i = 1907; } }"));
167 Set<String> expect = createSet("C_Inner.h");
168 if (gk == GenKind.FULL) expect.add("C.h");
170 test(rk, gk, files, expect);
171 }
173 /**
174 * The worker method for each test case.
175 * Compile the files and verify that exactly the expected set of header files
176 * is generated.
177 */
178 void test(RunKind rk, GenKind gk, List<File> files, Set<String> expect) throws Exception {
179 List<String> args = new ArrayList<String>();
180 if (gk == GenKind.FULL)
181 args.add("-XDjavah:full");
183 switch (rk) {
184 case CMD:
185 args.add("-d");
186 args.add(classesDir.getPath());
187 args.add("-h");
188 args.add(headersDir.getPath());
189 for (File f: files)
190 args.add(f.getPath());
191 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]));
192 if (rc != 0)
193 throw new Exception("compilation failed, rc=" + rc);
194 break;
196 case API:
197 fm.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(srcDir));
198 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(classesDir));
199 fm.setLocation(StandardLocation.NATIVE_HEADER_OUTPUT, Arrays.asList(headersDir));
200 JavacTask task = javac.getTask(null, fm, null, args, null,
201 fm.getJavaFileObjectsFromFiles(files));
202 if (!task.call())
203 throw new Exception("compilation failed");
204 break;
205 }
207 Set<String> found = createSet(headersDir.list());
208 checkEqual("header files", expect, found);
209 }
211 /** Marker annotation for test cases. */
212 @Retention(RetentionPolicy.RUNTIME)
213 @interface Test { }
215 /** Combo test to run all test cases in all modes. */
216 void run() throws Exception {
217 javac = JavacTool.create();
218 fm = javac.getStandardFileManager(null, null, null);
220 for (RunKind rk: RunKind.values()) {
221 for (GenKind gk: GenKind.values()) {
222 for (Method m: getClass().getDeclaredMethods()) {
223 Annotation a = m.getAnnotation(Test.class);
224 if (a != null) {
225 init(rk, gk, m.getName());
226 try {
227 m.invoke(this, new Object[] { rk, gk });
228 } catch (InvocationTargetException e) {
229 Throwable cause = e.getCause();
230 throw (cause instanceof Exception) ? ((Exception) cause) : e;
231 }
232 System.err.println();
233 }
234 }
235 }
236 }
237 System.err.println(testCount + " tests" + ((errorCount == 0) ? "" : ", " + errorCount + " errors"));
238 if (errorCount > 0)
239 throw new Exception(errorCount + " errors found");
240 }
242 /**
243 * Init directories for a test case.
244 */
245 void init(RunKind rk, GenKind gk, String name) throws IOException {
246 System.err.println("Test " + rk + " " + gk + " " + name);
247 testCount++;
249 testDir = new File(rk.toString().toLowerCase() + "_" + gk.toString().toLowerCase() + "-" + name);
250 srcDir = new File(testDir, "src");
251 srcDir.mkdirs();
252 classesDir = new File(testDir, "classes");
253 classesDir.mkdirs();
254 headersDir = new File(testDir, "headers");
255 headersDir.mkdirs();
256 }
258 /** Create a source file with given body text. */
259 File createFile(String path, final String body) throws IOException {
260 File f = new File(srcDir, path);
261 f.getParentFile().mkdirs();
262 try (FileWriter out = new FileWriter(f)) {
263 out.write(body);
264 }
265 return f;
266 }
268 /** Convenience method to create a set of items. */
269 <T> Set<T> createSet(T... items) {
270 return new HashSet<T>(Arrays.asList(items));
271 }
273 /** Convenience method to check two values are equal, and report an error if not. */
274 <T> void checkEqual(String label, T expect, T found) {
275 if ((found == null) ? (expect == null) : found.equals(expect))
276 return;
277 System.err.println("Error: mismatch");
278 System.err.println(" expected: " + expect);
279 System.err.println(" found: " + found);
280 errorCount++;
281 }
283 // Shared across API test cases
284 JavacTool javac;
285 StandardJavaFileManager fm;
287 // Directories set up by init
288 File testDir;
289 File srcDir;
290 File classesDir;
291 File headersDir;
293 // Statistics
294 int testCount;
295 int errorCount;
296 }