Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
1 /*
2 * Copyright (c) 2012, 2013, 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 8000407
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 annoTest(RunKind rk, GenKind gk) throws Exception {
129 List<File> files = new ArrayList<File>();
130 files.add(createFile("p/C.java",
131 "class C { @java.lang.annotation.Native public static final int i = 1907; }"));
133 Set<String> expect = createSet("C.h");
135 test(rk, gk, files, expect);
136 }
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 { class Inner { @java.lang.annotation.Native public static final int i = 1907; } }"));
144 Set<String> expect = createSet("C_Inner.h");
145 if (gk == GenKind.FULL) expect.add("C.h");
147 test(rk, gk, files, expect);
148 }
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");
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;
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 }
184 Set<String> found = createSet(headersDir.list());
185 checkEqual("header files", expect, found);
186 }
188 /** Marker annotation for test cases. */
189 @Retention(RetentionPolicy.RUNTIME)
190 @interface Test { }
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);
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 }
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++;
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 }
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 }
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 }
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 }
260 // Shared across API test cases
261 JavacTool javac;
262 StandardJavaFileManager fm;
264 // Directories set up by init
265 File testDir;
266 File srcDir;
267 File classesDir;
268 File headersDir;
270 // Statistics
271 int testCount;
272 int errorCount;
273 }