test/tools/javac/processing/T6920317.java

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 1308
37008b4cd97a
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
Reviewed-by: jjg

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

mercurial