|
1 /* |
|
2 * Copyright 2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6920317 |
|
27 * @summary package-info.java file has to be specified on the javac cmdline, else it will not be avail |
|
28 */ |
|
29 |
|
30 import java.io.*; |
|
31 import java.util.*; |
|
32 import javax.annotation.processing.*; |
|
33 import javax.lang.model.*; |
|
34 import javax.lang.model.element.*; |
|
35 import javax.lang.model.util.*; |
|
36 import javax.tools.*; |
|
37 |
|
38 /** |
|
39 * The test exercises different ways of providing annotations for a package. |
|
40 * Each way provides an annotation with a unique argument. For each test |
|
41 * case, the test verifies that the annotation with the correct argument is |
|
42 * found by the compiler. |
|
43 */ |
|
44 public class T6920317 { |
|
45 public static void main(String... args) throws Exception { |
|
46 new T6920317().run(args); |
|
47 } |
|
48 |
|
49 // Used to describe properties of files to be put on command line, source path, class path |
|
50 enum Kind { |
|
51 /** File is not used. */ |
|
52 NONE, |
|
53 /** File is used. */ |
|
54 OLD, |
|
55 /** Only applies to files on classpath/sourcepath, when there is another file on the |
|
56 * other path of type OLD, in which case, this file must be newer than the other one. */ |
|
57 NEW, |
|
58 /** Only applies to files on classpath/sourcepath, when there is no file in any other |
|
59 * location, in which case, this file will be generated by the annotation processor. */ |
|
60 GEN |
|
61 } |
|
62 |
|
63 void run(String... args) throws Exception { |
|
64 // if no args given, all test cases are run |
|
65 // if args given, they indicate the test cases to be run |
|
66 for (int i = 0; i < args.length; i++) { |
|
67 tests.add(Integer.valueOf(args[i])); |
|
68 } |
|
69 |
|
70 setup(); |
|
71 |
|
72 // Run tests for all combinations of files on command line, source path and class path. |
|
73 // Invalid combinations are skipped in the test method |
|
74 for (Kind cmdLine: EnumSet.of(Kind.NONE, Kind.OLD)) { |
|
75 for (Kind srcPath: Kind.values()) { |
|
76 for (Kind clsPath: Kind.values()) { |
|
77 try { |
|
78 test(cmdLine, srcPath, clsPath); |
|
79 } catch (Exception e) { |
|
80 e.printStackTrace(); |
|
81 error("Exception " + e); |
|
82 // uncomment to stop on first failed test case |
|
83 // throw e; |
|
84 } |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 if (errors > 0) |
|
90 throw new Exception(errors + " errors occurred"); |
|
91 } |
|
92 |
|
93 /** One time setup for files and directories to be used in the various test cases. */ |
|
94 void setup() throws Exception { |
|
95 // Annotation used in test cases to annotate package. This file is |
|
96 // given on the command line in test cases. |
|
97 test_java = writeFile("Test.java", "package p; @interface Test { String value(); }"); |
|
98 // Compile the annotation for use later in setup |
|
99 File tmpClasses = new File("tmp.classes"); |
|
100 compile(tmpClasses, new String[] { }, test_java); |
|
101 |
|
102 // package-info file to use on the command line when requied |
|
103 cl_pkgInfo_java = writeFile("cl/p/package-info.java", "@Test(\"CL\") package p;"); |
|
104 |
|
105 // source path containing package-info |
|
106 sp_old = new File("src.old"); |
|
107 writeFile("src.old/p/package-info.java", "@Test(\"SP_OLD\") package p;"); |
|
108 |
|
109 // class path containing package-info |
|
110 cp_old = new File("classes.old"); |
|
111 compile(cp_old, new String[] { "-classpath", tmpClasses.getPath() }, |
|
112 writeFile("tmp.old/p/package-info.java", "@Test(\"CP_OLD\") package p;")); |
|
113 |
|
114 // source path containing package-info which is newer than the one in cp-old |
|
115 sp_new = new File("src.new"); |
|
116 File old_class = new File(cp_old, "p/package-info.class"); |
|
117 writeFile("src.new/p/package-info.java", "@Test(\"SP_NEW\") package p;", old_class); |
|
118 |
|
119 // class path containing package-info which is newer than the one in sp-old |
|
120 cp_new = new File("classes.new"); |
|
121 File old_java = new File(sp_old, "p/package-info.java"); |
|
122 compile(cp_new, new String[] { "-classpath", tmpClasses.getPath() }, |
|
123 writeFile("tmp.new/p/package-info.java", "@Test(\"CP_NEW\") package p;", old_java)); |
|
124 |
|
125 // directory containing package-info.java to be "generated" later by annotation processor |
|
126 sp_gen = new File("src.gen"); |
|
127 writeFile("src.gen/p/package-info.java", "@Test(\"SP_GEN\") package p;"); |
|
128 |
|
129 // directory containing package-info.class to be "generated" later by annotation processor |
|
130 cp_gen = new File("classes.gen"); |
|
131 compile(cp_gen, new String[] { "-classpath", tmpClasses.getPath() }, |
|
132 writeFile("tmp.gen/p/package-info.java", "@Test(\"CP_GEN\") package p;")); |
|
133 } |
|
134 |
|
135 void test(Kind cl, Kind sp, Kind cp) throws Exception { |
|
136 if (skip(cl, sp, cp)) |
|
137 return; |
|
138 |
|
139 ++count; |
|
140 // if test cases specified, skip this test case if not selected |
|
141 if (tests.size() > 0 && !tests.contains(count)) |
|
142 return; |
|
143 |
|
144 System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp); |
|
145 |
|
146 // test specific tmp directory |
|
147 File test_tmp = new File("tmp.test" + count); |
|
148 test_tmp.mkdirs(); |
|
149 |
|
150 // build up list of options and files to be compiled |
|
151 List<String> opts = new ArrayList<String>(); |
|
152 List<File> files = new ArrayList<File>(); |
|
153 |
|
154 // expected value for annotation |
|
155 String expect = null; |
|
156 |
|
157 opts.add("-processorpath"); |
|
158 opts.add(System.getProperty("test.classes")); |
|
159 opts.add("-processor"); |
|
160 opts.add(Processor.class.getName()); |
|
161 opts.add("-proc:only"); |
|
162 opts.add("-d"); |
|
163 opts.add(test_tmp.getPath()); |
|
164 //opts.add("-verbose"); |
|
165 files.add(test_java); |
|
166 |
|
167 /* |
|
168 * Analyze each of cl, cp, sp, building up the options and files to |
|
169 * be compiled, and determining the expected outcome fo the test case. |
|
170 */ |
|
171 |
|
172 // command line file: either omitted or given |
|
173 if (cl == Kind.OLD) { |
|
174 files.add(cl_pkgInfo_java); |
|
175 // command line files always supercede files on paths |
|
176 expect = "CL"; |
|
177 } |
|
178 |
|
179 // source path: |
|
180 switch (sp) { |
|
181 case NONE: |
|
182 break; |
|
183 |
|
184 case OLD: |
|
185 opts.add("-sourcepath"); |
|
186 opts.add(sp_old.getPath()); |
|
187 if (expect == null && cp == Kind.NONE) { |
|
188 assert cl == Kind.NONE && cp == Kind.NONE; |
|
189 expect = "SP_OLD"; |
|
190 } |
|
191 break; |
|
192 |
|
193 case NEW: |
|
194 opts.add("-sourcepath"); |
|
195 opts.add(sp_new.getPath()); |
|
196 if (expect == null) { |
|
197 assert cl == Kind.NONE && cp == Kind.OLD; |
|
198 expect = "SP_NEW"; |
|
199 } |
|
200 break; |
|
201 |
|
202 case GEN: |
|
203 opts.add("-Agen=" + new File(sp_gen, "p/package-info.java")); |
|
204 assert cl == Kind.NONE && cp == Kind.NONE; |
|
205 expect = "SP_GEN"; |
|
206 break; |
|
207 } |
|
208 |
|
209 // class path: |
|
210 switch (cp) { |
|
211 case NONE: |
|
212 break; |
|
213 |
|
214 case OLD: |
|
215 opts.add("-classpath"); |
|
216 opts.add(cp_old.getPath()); |
|
217 if (expect == null && sp == Kind.NONE) { |
|
218 assert cl == Kind.NONE && sp == Kind.NONE; |
|
219 expect = "CP_OLD"; |
|
220 } |
|
221 break; |
|
222 |
|
223 case NEW: |
|
224 opts.add("-classpath"); |
|
225 opts.add(cp_new.getPath()); |
|
226 if (expect == null) { |
|
227 assert cl == Kind.NONE && sp == Kind.OLD; |
|
228 expect = "CP_NEW"; |
|
229 } |
|
230 break; |
|
231 |
|
232 case GEN: |
|
233 opts.add("-Agen=" + new File(cp_gen, "p/package-info.class")); |
|
234 assert cl == Kind.NONE && sp == Kind.NONE; |
|
235 expect = "CP_GEN"; |
|
236 break; |
|
237 } |
|
238 |
|
239 // pass expected value to annotation processor |
|
240 assert expect != null; |
|
241 opts.add("-Aexpect=" + expect); |
|
242 |
|
243 // compile the files with the options that have been built up |
|
244 compile(opts, files); |
|
245 } |
|
246 |
|
247 /** |
|
248 * Return true if this combination of parameters does not identify a useful test case. |
|
249 */ |
|
250 boolean skip(Kind cl, Kind sp, Kind cp) { |
|
251 // skip if no package files required |
|
252 if (cl == Kind.NONE && sp == Kind.NONE && cp == Kind.NONE) |
|
253 return true; |
|
254 |
|
255 // skip if both sp and sp are OLD, since results may be indeterminate |
|
256 if (sp == Kind.OLD && cp == Kind.OLD) |
|
257 return true; |
|
258 |
|
259 // skip if sp or cp is NEW but the other is not OLD |
|
260 if ((sp == Kind.NEW && cp != Kind.OLD) || (cp == Kind.NEW && sp != Kind.OLD)) |
|
261 return true; |
|
262 |
|
263 // only use GEN if no other package-info files present |
|
264 if (sp == Kind.GEN && !(cl == Kind.NONE && cp == Kind.NONE) || |
|
265 cp == Kind.GEN && !(cl == Kind.NONE && sp == Kind.NONE)) { |
|
266 return true; |
|
267 } |
|
268 |
|
269 // remaining combinations are valid |
|
270 return false; |
|
271 } |
|
272 |
|
273 /** Write a file with a given body. */ |
|
274 File writeFile(String path, String body) throws Exception { |
|
275 File f = new File(path); |
|
276 if (f.getParentFile() != null) |
|
277 f.getParentFile().mkdirs(); |
|
278 Writer out = new FileWriter(path); |
|
279 try { |
|
280 out.write(body); |
|
281 } finally { |
|
282 out.close(); |
|
283 } |
|
284 return f; |
|
285 } |
|
286 |
|
287 /** Write a file with a given body, ensuring that the file is newer than a reference file. */ |
|
288 File writeFile(String path, String body, File ref) throws Exception { |
|
289 for (int i = 0; i < 5; i++) { |
|
290 File f = writeFile(path, body); |
|
291 if (f.lastModified() > ref.lastModified()) |
|
292 return f; |
|
293 Thread.sleep(2000); |
|
294 } |
|
295 throw new Exception("cannot create file " + path + " newer than " + ref); |
|
296 } |
|
297 |
|
298 /** Compile a file to a given directory, with options provided. */ |
|
299 void compile(File dir, String[] opts, File src) throws Exception { |
|
300 dir.mkdirs(); |
|
301 List<String> opts2 = new ArrayList<String>(); |
|
302 opts2.addAll(Arrays.asList("-d", dir.getPath())); |
|
303 opts2.addAll(Arrays.asList(opts)); |
|
304 compile(opts2, Collections.singletonList(src)); |
|
305 } |
|
306 |
|
307 /** Compile files with options provided. */ |
|
308 void compile(List<String> opts, List<File> files) throws Exception { |
|
309 System.err.println("javac: " + opts + " " + files); |
|
310 List<String> args = new ArrayList<String>(); |
|
311 args.addAll(opts); |
|
312 for (File f: files) |
|
313 args.add(f.getPath()); |
|
314 StringWriter sw = new StringWriter(); |
|
315 PrintWriter pw = new PrintWriter(sw); |
|
316 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); |
|
317 pw.flush(); |
|
318 if (sw.getBuffer().length() > 0) |
|
319 System.err.println(sw.toString()); |
|
320 if (rc != 0) |
|
321 throw new Exception("compilation failed: rc=" + rc); |
|
322 } |
|
323 |
|
324 /** Report an error. */ |
|
325 void error(String msg) { |
|
326 System.err.println("Error: " + msg); |
|
327 errors++; |
|
328 } |
|
329 |
|
330 /** Test case counter. */ |
|
331 int count; |
|
332 |
|
333 /** Number of errors found. */ |
|
334 int errors; |
|
335 |
|
336 /** Optional set of test cases to be run; empty implies all test cases. */ |
|
337 Set<Integer> tests = new HashSet<Integer>(); |
|
338 |
|
339 /* Files created by setup. */ |
|
340 File test_java; |
|
341 File sp_old; |
|
342 File sp_new; |
|
343 File sp_gen; |
|
344 File cp_old; |
|
345 File cp_new; |
|
346 File cp_gen; |
|
347 File cl_pkgInfo_java; |
|
348 |
|
349 /** Annotation processor used to verify the expected value for the |
|
350 package annotations found by javac. */ |
|
351 @SupportedOptions({ "gen", "expect" }) |
|
352 @SupportedAnnotationTypes({"*"}) |
|
353 public static class Processor extends AbstractProcessor { |
|
354 public SourceVersion getSupportedSourceVersion() { |
|
355 return SourceVersion.latest(); |
|
356 } |
|
357 |
|
358 public boolean process(Set<? extends TypeElement> annots, RoundEnvironment renv) { |
|
359 round++; |
|
360 System.err.println("Round " + round + " annots:" + annots + " rootElems:" + renv.getRootElements()); |
|
361 |
|
362 // if this is the first round and the gen option is given, use the filer to create |
|
363 // a copy of the file specified by the gen option. |
|
364 String gen = getOption("gen"); |
|
365 if (round == 1 && gen != null) { |
|
366 try { |
|
367 Filer filer = processingEnv.getFiler(); |
|
368 JavaFileObject f; |
|
369 if (gen.endsWith(".java")) |
|
370 f = filer.createSourceFile("p.package-info"); |
|
371 else |
|
372 f = filer.createClassFile("p.package-info"); |
|
373 System.err.println("copy " + gen + " to " + f.getName()); |
|
374 write(f, read(new File(gen))); |
|
375 } catch (IOException e) { |
|
376 error("Cannot create package-info file: " + e); |
|
377 } |
|
378 } |
|
379 |
|
380 // if annotation processing is complete, verify the package annotation |
|
381 // found by the compiler. |
|
382 if (renv.processingOver()) { |
|
383 System.err.println("final round"); |
|
384 Elements eu = processingEnv.getElementUtils(); |
|
385 TypeElement te = eu.getTypeElement("p.Test"); |
|
386 PackageElement pe = eu.getPackageOf(te); |
|
387 System.err.println("final: te:" + te + " pe:" + pe); |
|
388 List<? extends AnnotationMirror> annos = pe.getAnnotationMirrors(); |
|
389 System.err.println("final: annos:" + annos); |
|
390 if (annos.size() == 1) { |
|
391 String expect = "@" + te + "(\"" + getOption("expect") + "\")"; |
|
392 String actual = annos.get(0).toString(); |
|
393 checkEqual("package annotations", actual, expect); |
|
394 } else { |
|
395 error("Wrong number of annotations found: (" + annos.size() + ") " + annos); |
|
396 } |
|
397 } |
|
398 |
|
399 return true; |
|
400 } |
|
401 |
|
402 /** Get an option given to the annotation processor. */ |
|
403 String getOption(String name) { |
|
404 return processingEnv.getOptions().get(name); |
|
405 } |
|
406 |
|
407 /** Read a file. */ |
|
408 byte[] read(File file) { |
|
409 byte[] bytes = new byte[(int) file.length()]; |
|
410 DataInputStream in = null; |
|
411 try { |
|
412 in = new DataInputStream(new FileInputStream(file)); |
|
413 in.readFully(bytes); |
|
414 } catch (IOException e) { |
|
415 error("Error reading file: " + e); |
|
416 } finally { |
|
417 if (in != null) { |
|
418 try { |
|
419 in.close(); |
|
420 } catch (IOException e) { |
|
421 error("Error closing file: " + e); |
|
422 } |
|
423 } |
|
424 } |
|
425 return bytes; |
|
426 } |
|
427 |
|
428 /** Write a file. */ |
|
429 void write(JavaFileObject file, byte[] bytes) { |
|
430 OutputStream out = null; |
|
431 try { |
|
432 out = file.openOutputStream(); |
|
433 out.write(bytes, 0, bytes.length); |
|
434 } catch (IOException e) { |
|
435 error("Error writing file: " + e); |
|
436 } finally { |
|
437 if (out != null) { |
|
438 try { |
|
439 out.close(); |
|
440 } catch (IOException e) { |
|
441 error("Error closing file: " + e); |
|
442 } |
|
443 } |
|
444 } |
|
445 } |
|
446 |
|
447 /** Check two strings are equal, and report an error if they are not. */ |
|
448 private void checkEqual(String label, String actual, String expect) { |
|
449 if (!actual.equals(expect)) { |
|
450 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect); |
|
451 } |
|
452 } |
|
453 |
|
454 /** Report an error to the annotation processing system. */ |
|
455 void error(String msg) { |
|
456 Messager messager = processingEnv.getMessager(); |
|
457 messager.printMessage(Diagnostic.Kind.ERROR, msg); |
|
458 } |
|
459 |
|
460 int round; |
|
461 } |
|
462 } |