test/tools/javac/nativeHeaders/NativeHeaderTest.java

changeset 1230
b14d9583ce92
child 1407
f14c693a0e48
equal deleted inserted replaced
1226:97bec6ab1227 1230:b14d9583ce92
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 */
23
24 /*
25 * @test
26 * @bug 7150368
27 * @summary javac should include basic ability to generate native headers
28 */
29
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;
43
44 import javax.tools.StandardJavaFileManager;
45 import javax.tools.StandardLocation;
46
47 import com.sun.source.util.JavacTask;
48 import com.sun.tools.javac.api.JavacTool;
49
50 public class NativeHeaderTest {
51 public static void main(String... args) throws Exception {
52 new NativeHeaderTest().run();
53 }
54
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 };
62
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 };
70
71 // ---------- Test cases, invoked reflectively via run. ----------
72
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(); }"));
78
79 Set<String> expect = createSet("C.h");
80
81 test(rk, gk, files, expect);
82 }
83
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(); } }"));
89
90 Set<String> expect = createSet("C_Inner.h");
91 if (gk == GenKind.FULL) expect.add("C.h");
92
93 test(rk, gk, files, expect);
94 }
95
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 { } } }"));
101
102 Set<String> expect = createSet("C.h");
103
104 test(rk, gk, files, expect);
105 }
106
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 + "}"));
115
116 Set<String> expect = createSet("C_Inner.h");
117 if (gk == GenKind.FULL) expect.add("C.h");
118
119 test(rk, gk, files, expect);
120
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 }
126
127 @Test
128 void annoTest(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 { }"));
132
133 Set<String> expect = createSet("C.h");
134
135 test(rk, gk, files, expect);
136 }
137
138 @Test
139 void annoNestedClassTest(RunKind rk, GenKind gk) throws Exception {
140 List<File> files = new ArrayList<File>();
141 files.add(createFile("p/C.java",
142 "class C { @javax.tools.annotation.GenerateNativeHeader class Inner { } }"));
143
144 Set<String> expect = createSet("C_Inner.h");
145 if (gk == GenKind.FULL) expect.add("C.h");
146
147 test(rk, gk, files, expect);
148 }
149
150 /**
151 * The worker method for each test case.
152 * Compile the files and verify that exactly the expected set of header files
153 * is generated.
154 */
155 void test(RunKind rk, GenKind gk, List<File> files, Set<String> expect) throws Exception {
156 List<String> args = new ArrayList<String>();
157 if (gk == GenKind.FULL)
158 args.add("-XDjavah:full");
159
160 switch (rk) {
161 case CMD:
162 args.add("-d");
163 args.add(classesDir.getPath());
164 args.add("-h");
165 args.add(headersDir.getPath());
166 for (File f: files)
167 args.add(f.getPath());
168 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]));
169 if (rc != 0)
170 throw new Exception("compilation failed, rc=" + rc);
171 break;
172
173 case API:
174 fm.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(srcDir));
175 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(classesDir));
176 fm.setLocation(StandardLocation.NATIVE_HEADER_OUTPUT, Arrays.asList(headersDir));
177 JavacTask task = javac.getTask(null, fm, null, args, null,
178 fm.getJavaFileObjectsFromFiles(files));
179 if (!task.call())
180 throw new Exception("compilation failed");
181 break;
182 }
183
184 Set<String> found = createSet(headersDir.list());
185 checkEqual("header files", expect, found);
186 }
187
188 /** Marker annotation for test cases. */
189 @Retention(RetentionPolicy.RUNTIME)
190 @interface Test { }
191
192 /** Combo test to run all test cases in all modes. */
193 void run() throws Exception {
194 javac = JavacTool.create();
195 fm = javac.getStandardFileManager(null, null, null);
196
197 for (RunKind rk: RunKind.values()) {
198 for (GenKind gk: GenKind.values()) {
199 for (Method m: getClass().getDeclaredMethods()) {
200 Annotation a = m.getAnnotation(Test.class);
201 if (a != null) {
202 init(rk, gk, m.getName());
203 try {
204 m.invoke(this, new Object[] { rk, gk });
205 } catch (InvocationTargetException e) {
206 Throwable cause = e.getCause();
207 throw (cause instanceof Exception) ? ((Exception) cause) : e;
208 }
209 System.err.println();
210 }
211 }
212 }
213 }
214 System.err.println(testCount + " tests" + ((errorCount == 0) ? "" : ", " + errorCount + " errors"));
215 if (errorCount > 0)
216 throw new Exception(errorCount + " errors found");
217 }
218
219 /**
220 * Init directories for a test case.
221 */
222 void init(RunKind rk, GenKind gk, String name) throws IOException {
223 System.err.println("Test " + rk + " " + gk + " " + name);
224 testCount++;
225
226 testDir = new File(rk.toString().toLowerCase() + "_" + gk.toString().toLowerCase() + "-" + name);
227 srcDir = new File(testDir, "src");
228 srcDir.mkdirs();
229 classesDir = new File(testDir, "classes");
230 classesDir.mkdirs();
231 headersDir = new File(testDir, "headers");
232 headersDir.mkdirs();
233 }
234
235 /** Create a source file with given body text. */
236 File createFile(String path, final String body) throws IOException {
237 File f = new File(srcDir, path);
238 f.getParentFile().mkdirs();
239 try (FileWriter out = new FileWriter(f)) {
240 out.write(body);
241 }
242 return f;
243 }
244
245 /** Convenience method to create a set of items. */
246 <T> Set<T> createSet(T... items) {
247 return new HashSet<T>(Arrays.asList(items));
248 }
249
250 /** Convenience method to check two values are equal, and report an error if not. */
251 <T> void checkEqual(String label, T expect, T found) {
252 if ((found == null) ? (expect == null) : found.equals(expect))
253 return;
254 System.err.println("Error: mismatch");
255 System.err.println(" expected: " + expect);
256 System.err.println(" found: " + found);
257 errorCount++;
258 }
259
260 // Shared across API test cases
261 JavacTool javac;
262 StandardJavaFileManager fm;
263
264 // Directories set up by init
265 File testDir;
266 File srcDir;
267 File classesDir;
268 File headersDir;
269
270 // Statistics
271 int testCount;
272 int errorCount;
273 }
274

mercurial