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