1 /*
   2  * Copyright (c) 2010, 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 6920317
  27  * @summary package-info.java file has to be specified on the javac cmdline, else it will not be avail
  28  * @library ../lib
  29  */
  30 
  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.*;
  38 
  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     }
  49 
  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     }
  63 
  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         }
  70 
  71         setup();
  72 
  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         }
  89 
  90         if (errors > 0)
  91             throw new Exception(errors + " errors occurred");
  92     }
  93 
  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);
 102 
 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;");
 105 
 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;");
 109 
 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;"));
 114 
 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);
 119 
 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));
 125 
 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;");
 129 
 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     }
 135 
 136     void test(Kind cl, Kind sp, Kind cp) throws Exception {
 137         if (skip(cl, sp, cp))
 138             return;
 139 
 140         ++count;
 141         // if test cases specified, skip this test case if not selected
 142         if (tests.size() > 0 && !tests.contains(count))
 143             return;
 144 
 145         System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp);
 146 
 147         // test specific tmp directory
 148         File test_tmp = new File("tmp.test" + count);
 149         test_tmp.mkdirs();
 150 
 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>();
 154 
 155         // expected value for annotation
 156         String expect = null;
 157 
 158         opts.add("-processorpath");
 159         opts.add(System.getProperty("test.classes"));
 160         opts.add("-processor");
 161         opts.add(Processor.class.getName());
 162         opts.add("-proc:only");
 163         opts.add("-d");
 164         opts.add(test_tmp.getPath());
 165         //opts.add("-verbose");
 166         files.add(test_java);
 167 
 168         /*
 169          * Analyze each of cl, cp, sp, building up the options and files to
 170          * be compiled, and determining the expected outcome fo the test case.
 171          */
 172 
 173         // command line file: either omitted or given
 174         if (cl == Kind.OLD) {
 175             files.add(cl_pkgInfo_java);
 176             // command line files always supercede files on paths
 177             expect = "CL";
 178         }
 179 
 180         // source path:
 181         switch (sp) {
 182         case NONE:
 183             break;
 184 
 185         case OLD:
 186             opts.add("-sourcepath");
 187             opts.add(sp_old.getPath());
 188             if (expect == null && cp == Kind.NONE) {
 189                 assert cl == Kind.NONE && cp == Kind.NONE;
 190                 expect = "SP_OLD";
 191             }
 192             break;
 193 
 194         case NEW:
 195             opts.add("-sourcepath");
 196             opts.add(sp_new.getPath());
 197             if (expect == null) {
 198                 assert cl == Kind.NONE && cp == Kind.OLD;
 199                 expect = "SP_NEW";
 200             }
 201             break;
 202 
 203         case GEN:
 204             opts.add("-Agen=" + new File(sp_gen, "p/package-info.java"));
 205             assert cl == Kind.NONE && cp == Kind.NONE;
 206             expect = "SP_GEN";
 207             break;
 208         }
 209 
 210         // class path:
 211         switch (cp) {
 212         case NONE:
 213             break;
 214 
 215         case OLD:
 216             opts.add("-classpath");
 217             opts.add(cp_old.getPath());
 218             if (expect == null && sp == Kind.NONE) {
 219                 assert cl == Kind.NONE && sp == Kind.NONE;
 220                 expect = "CP_OLD";
 221             }
 222             break;
 223 
 224         case NEW:
 225             opts.add("-classpath");
 226             opts.add(cp_new.getPath());
 227             if (expect == null) {
 228                 assert cl == Kind.NONE && sp == Kind.OLD;
 229                 expect = "CP_NEW";
 230             }
 231             break;
 232 
 233         case GEN:
 234             opts.add("-Agen=" + new File(cp_gen, "p/package-info.class"));
 235             assert cl == Kind.NONE && sp == Kind.NONE;
 236             expect = "CP_GEN";
 237             break;
 238         }
 239 
 240         // pass expected value to annotation processor
 241         assert expect != null;
 242         opts.add("-Aexpect=" + expect);
 243 
 244         // compile the files with the options that have been built up
 245         compile(opts, files);
 246     }
 247 
 248     /**
 249      * Return true if this combination of parameters does not identify a useful test case.
 250      */
 251     boolean skip(Kind cl, Kind sp, Kind cp) {
 252         // skip if no package files required
 253         if (cl == Kind.NONE && sp == Kind.NONE && cp == Kind.NONE)
 254             return true;
 255 
 256         // skip if both sp and sp are OLD, since results may be indeterminate
 257         if (sp == Kind.OLD && cp == Kind.OLD)
 258             return true;
 259 
 260         // skip if sp or cp is NEW but the other is not OLD
 261         if ((sp == Kind.NEW && cp != Kind.OLD) || (cp == Kind.NEW && sp != Kind.OLD))
 262             return true;
 263 
 264         // only use GEN if no other package-info files present
 265         if (sp == Kind.GEN && !(cl == Kind.NONE && cp == Kind.NONE) ||
 266             cp == Kind.GEN && !(cl == Kind.NONE && sp == Kind.NONE)) {
 267             return true;
 268         }
 269 
 270         // remaining combinations are valid
 271         return false;
 272     }
 273 
 274     /** Write a file with a given body. */
 275     File writeFile(String path, String body) throws Exception {
 276         File f = new File(path);
 277         if (f.getParentFile() != null)
 278             f.getParentFile().mkdirs();
 279         Writer out = new FileWriter(path);
 280         try {
 281             out.write(body);
 282         } finally {
 283             out.close();
 284         }
 285         return f;
 286     }
 287 
 288     /** Write a file with a given body, ensuring that the file is newer than a reference file. */
 289     File writeFile(String path, String body, File ref) throws Exception {
 290         for (int i = 0; i < 5; i++) {
 291             File f = writeFile(path, body);
 292             if (f.lastModified() > ref.lastModified())
 293                 return f;
 294             Thread.sleep(2000);
 295         }
 296         throw new Exception("cannot create file " + path + " newer than " + ref);
 297     }
 298 
 299     /** Compile a file to a given directory, with options provided. */
 300     void compile(File dir, String[] opts, File src) throws Exception {
 301         dir.mkdirs();
 302         List<String> opts2 = new ArrayList<String>();
 303         opts2.addAll(Arrays.asList("-d", dir.getPath()));
 304         opts2.addAll(Arrays.asList(opts));
 305         compile(opts2, Collections.singletonList(src));
 306     }
 307 
 308     /** Compile files with options provided. */
 309     void compile(List<String> opts, List<File> files) throws Exception {
 310         System.err.println("javac: " + opts + " " + files);
 311         List<String> args = new ArrayList<String>();
 312         args.addAll(opts);
 313         for (File f: files)
 314             args.add(f.getPath());
 315         StringWriter sw = new StringWriter();
 316         PrintWriter pw = new PrintWriter(sw);
 317         int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
 318         pw.flush();
 319         if (sw.getBuffer().length() > 0)
 320             System.err.println(sw.toString());
 321         if (rc != 0)
 322             throw new Exception("compilation failed: rc=" + rc);
 323     }
 324 
 325     /** Report an error. */
 326     void error(String msg) {
 327         System.err.println("Error: " + msg);
 328         errors++;
 329     }
 330 
 331     /** Test case counter. */
 332     int count;
 333 
 334     /** Number of errors found. */
 335     int errors;
 336 
 337     /** Optional set of test cases to be run; empty implies all test cases. */
 338     Set<Integer> tests = new HashSet<Integer>();
 339 
 340     /*  Files created by setup. */
 341     File test_java;
 342     File sp_old;
 343     File sp_new;
 344     File sp_gen;
 345     File cp_old;
 346     File cp_new;
 347     File cp_gen;
 348     File cl_pkgInfo_java;
 349 
 350     /** Annotation processor used to verify the expected value for the
 351         package annotations found by javac. */
 352     @SupportedOptions({ "gen", "expect" })
 353     public static class Processor extends JavacTestingAbstractProcessor {
 354         public boolean process(Set<? extends TypeElement> annots, RoundEnvironment renv) {
 355             round++;
 356             System.err.println("Round " + round + " annots:" + annots + " rootElems:" + renv.getRootElements());
 357 
 358             // if this is the first round and the gen option is given, use the filer to create
 359             // a copy of the file specified by the gen option.
 360             String gen = getOption("gen");
 361             if (round == 1 && gen != null) {
 362                 try {
 363                     Filer filer = processingEnv.getFiler();
 364                     JavaFileObject f;
 365                     if (gen.endsWith(".java"))
 366                         f = filer.createSourceFile("p.package-info");
 367                     else
 368                         f = filer.createClassFile("p.package-info");
 369                     System.err.println("copy " + gen + " to " + f.getName());
 370                     write(f, read(new File(gen)));
 371                 } catch (IOException e) {
 372                     error("Cannot create package-info file: " + e);
 373                 }
 374             }
 375 
 376             // if annotation processing is complete, verify the package annotation
 377             // found by the compiler.
 378             if (renv.processingOver()) {
 379                 System.err.println("final round");
 380                 Elements eu = processingEnv.getElementUtils();
 381                 TypeElement te = eu.getTypeElement("p.Test");
 382                 PackageElement pe = eu.getPackageOf(te);
 383                 System.err.println("final: te:" + te + " pe:" + pe);
 384                 List<? extends AnnotationMirror> annos = pe.getAnnotationMirrors();
 385                 System.err.println("final: annos:" + annos);
 386                 if (annos.size() == 1) {
 387                     String expect = "@" + te + "(\"" + getOption("expect") + "\")";
 388                     String actual = annos.get(0).toString();
 389                     checkEqual("package annotations", actual, expect);
 390                 } else {
 391                     error("Wrong number of annotations found: (" + annos.size() + ") " + annos);
 392                 }
 393             }
 394 
 395             return true;
 396         }
 397 
 398         /** Get an option given to the annotation processor. */
 399         String getOption(String name) {
 400             return processingEnv.getOptions().get(name);
 401         }
 402 
 403         /** Read a file. */
 404         byte[] read(File file) {
 405             byte[] bytes = new byte[(int) file.length()];
 406             DataInputStream in = null;
 407             try {
 408                 in = new DataInputStream(new FileInputStream(file));
 409                 in.readFully(bytes);
 410             } catch (IOException e) {
 411                 error("Error reading file: " + e);
 412             } finally {
 413                 if (in != null) {
 414                     try {
 415                         in.close();
 416                     } catch (IOException e) {
 417                         error("Error closing file: " + e);
 418                     }
 419                 }
 420             }
 421             return  bytes;
 422         }
 423 
 424         /** Write a file. */
 425         void write(JavaFileObject file, byte[] bytes) {
 426             OutputStream out = null;
 427             try {
 428                 out = file.openOutputStream();
 429                 out.write(bytes, 0, bytes.length);
 430             } catch (IOException e) {
 431                 error("Error writing file: " + e);
 432             } finally {
 433                 if (out != null) {
 434                     try {
 435                         out.close();
 436                     } catch (IOException e) {
 437                         error("Error closing file: " + e);
 438                     }
 439                 }
 440             }
 441         }
 442 
 443         /** Check two strings are equal, and report an error if they are not. */
 444         private void checkEqual(String label, String actual, String expect) {
 445             if (!actual.equals(expect)) {
 446                 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
 447             }
 448         }
 449 
 450         /** Report an error to the annotation processing system. */
 451         void error(String msg) {
 452             Messager messager = processingEnv.getMessager();
 453             messager.printMessage(Diagnostic.Kind.ERROR, msg);
 454         }
 455 
 456         int round;
 457     }
 458 }