aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6920317 aoqi@0: * @summary package-info.java file has to be specified on the javac cmdline, else it will not be avail aoqi@0: * @library /tools/javac/lib aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import javax.annotation.processing.*; aoqi@0: import javax.lang.model.*; aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.lang.model.util.*; aoqi@0: import javax.tools.*; aoqi@0: aoqi@0: /** aoqi@0: * The test exercises different ways of providing annotations for a package. aoqi@0: * Each way provides an annotation with a unique argument. For each test aoqi@0: * case, the test verifies that the annotation with the correct argument is aoqi@0: * found by the compiler. aoqi@0: */ aoqi@0: public class T6920317 { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new T6920317().run(args); aoqi@0: } aoqi@0: aoqi@0: // Used to describe properties of files to be put on command line, source path, class path aoqi@0: enum Kind { aoqi@0: /** File is not used. */ aoqi@0: NONE, aoqi@0: /** File is used. */ aoqi@0: OLD, aoqi@0: /** Only applies to files on classpath/sourcepath, when there is another file on the aoqi@0: * other path of type OLD, in which case, this file must be newer than the other one. */ aoqi@0: NEW, aoqi@0: /** Only applies to files on classpath/sourcepath, when there is no file in any other aoqi@0: * location, in which case, this file will be generated by the annotation processor. */ aoqi@0: GEN aoqi@0: } aoqi@0: aoqi@0: void run(String... args) throws Exception { aoqi@0: // if no args given, all test cases are run aoqi@0: // if args given, they indicate the test cases to be run aoqi@0: for (int i = 0; i < args.length; i++) { aoqi@0: tests.add(Integer.valueOf(args[i])); aoqi@0: } aoqi@0: aoqi@0: setup(); aoqi@0: aoqi@0: // Run tests for all combinations of files on command line, source path and class path. aoqi@0: // Invalid combinations are skipped in the test method aoqi@0: for (Kind cmdLine: EnumSet.of(Kind.NONE, Kind.OLD)) { aoqi@0: for (Kind srcPath: Kind.values()) { aoqi@0: for (Kind clsPath: Kind.values()) { aoqi@0: try { aoqi@0: test(cmdLine, srcPath, clsPath); aoqi@0: } catch (Exception e) { aoqi@0: e.printStackTrace(); aoqi@0: error("Exception " + e); aoqi@0: // uncomment to stop on first failed test case aoqi@0: // throw e; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors occurred"); aoqi@0: } aoqi@0: aoqi@0: /** One time setup for files and directories to be used in the various test cases. */ aoqi@0: void setup() throws Exception { aoqi@0: // Annotation used in test cases to annotate package. This file is aoqi@0: // given on the command line in test cases. aoqi@0: test_java = writeFile("Test.java", "package p; @interface Test { String value(); }"); aoqi@0: // Compile the annotation for use later in setup aoqi@0: File tmpClasses = new File("tmp.classes"); aoqi@0: compile(tmpClasses, new String[] { }, test_java); aoqi@0: aoqi@0: // package-info file to use on the command line when requied aoqi@0: cl_pkgInfo_java = writeFile("cl/p/package-info.java", "@Test(\"CL\") package p;"); aoqi@0: aoqi@0: // source path containing package-info aoqi@0: sp_old = new File("src.old"); aoqi@0: writeFile("src.old/p/package-info.java", "@Test(\"SP_OLD\") package p;"); aoqi@0: aoqi@0: // class path containing package-info aoqi@0: cp_old = new File("classes.old"); aoqi@0: compile(cp_old, new String[] { "-classpath", tmpClasses.getPath() }, aoqi@0: writeFile("tmp.old/p/package-info.java", "@Test(\"CP_OLD\") package p;")); aoqi@0: aoqi@0: // source path containing package-info which is newer than the one in cp-old aoqi@0: sp_new = new File("src.new"); aoqi@0: File old_class = new File(cp_old, "p/package-info.class"); aoqi@0: writeFile("src.new/p/package-info.java", "@Test(\"SP_NEW\") package p;", old_class); aoqi@0: aoqi@0: // class path containing package-info which is newer than the one in sp-old aoqi@0: cp_new = new File("classes.new"); aoqi@0: File old_java = new File(sp_old, "p/package-info.java"); aoqi@0: compile(cp_new, new String[] { "-classpath", tmpClasses.getPath() }, aoqi@0: writeFile("tmp.new/p/package-info.java", "@Test(\"CP_NEW\") package p;", old_java)); aoqi@0: aoqi@0: // directory containing package-info.java to be "generated" later by annotation processor aoqi@0: sp_gen = new File("src.gen"); aoqi@0: writeFile("src.gen/p/package-info.java", "@Test(\"SP_GEN\") package p;"); aoqi@0: aoqi@0: // directory containing package-info.class to be "generated" later by annotation processor aoqi@0: cp_gen = new File("classes.gen"); aoqi@0: compile(cp_gen, new String[] { "-classpath", tmpClasses.getPath() }, aoqi@0: writeFile("tmp.gen/p/package-info.java", "@Test(\"CP_GEN\") package p;")); aoqi@0: } aoqi@0: aoqi@0: void test(Kind cl, Kind sp, Kind cp) throws Exception { aoqi@0: if (skip(cl, sp, cp)) aoqi@0: return; aoqi@0: aoqi@0: ++count; aoqi@0: // if test cases specified, skip this test case if not selected aoqi@0: if (tests.size() > 0 && !tests.contains(count)) aoqi@0: return; aoqi@0: aoqi@0: System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp); aoqi@0: aoqi@0: // test specific tmp directory aoqi@0: File test_tmp = new File("tmp.test" + count); aoqi@0: test_tmp.mkdirs(); aoqi@0: aoqi@0: // build up list of options and files to be compiled aoqi@0: List opts = new ArrayList(); aoqi@0: List files = new ArrayList(); aoqi@0: aoqi@0: // expected value for annotation aoqi@0: String expect = null; aoqi@0: aoqi@0: opts.add("-processorpath"); aoqi@0: String testClasses = System.getProperty("test.classes"); aoqi@0: String testClassPath = System.getProperty("test.class.path", testClasses); aoqi@0: opts.add(testClassPath); aoqi@0: opts.add("-processor"); aoqi@0: opts.add(Processor.class.getName()); aoqi@0: opts.add("-proc:only"); aoqi@0: opts.add("-d"); aoqi@0: opts.add(test_tmp.getPath()); aoqi@0: //opts.add("-verbose"); aoqi@0: files.add(test_java); aoqi@0: aoqi@0: /* aoqi@0: * Analyze each of cl, cp, sp, building up the options and files to aoqi@0: * be compiled, and determining the expected outcome fo the test case. aoqi@0: */ aoqi@0: aoqi@0: // command line file: either omitted or given aoqi@0: if (cl == Kind.OLD) { aoqi@0: files.add(cl_pkgInfo_java); aoqi@0: // command line files always supercede files on paths aoqi@0: expect = "CL"; aoqi@0: } aoqi@0: aoqi@0: // source path: aoqi@0: switch (sp) { aoqi@0: case NONE: aoqi@0: break; aoqi@0: aoqi@0: case OLD: aoqi@0: opts.add("-sourcepath"); aoqi@0: opts.add(sp_old.getPath()); aoqi@0: if (expect == null && cp == Kind.NONE) { aoqi@0: assert cl == Kind.NONE && cp == Kind.NONE; aoqi@0: expect = "SP_OLD"; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case NEW: aoqi@0: opts.add("-sourcepath"); aoqi@0: opts.add(sp_new.getPath()); aoqi@0: if (expect == null) { aoqi@0: assert cl == Kind.NONE && cp == Kind.OLD; aoqi@0: expect = "SP_NEW"; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case GEN: aoqi@0: opts.add("-Agen=" + new File(sp_gen, "p/package-info.java")); aoqi@0: assert cl == Kind.NONE && cp == Kind.NONE; aoqi@0: expect = "SP_GEN"; aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // class path: aoqi@0: switch (cp) { aoqi@0: case NONE: aoqi@0: break; aoqi@0: aoqi@0: case OLD: aoqi@0: opts.add("-classpath"); aoqi@0: opts.add(cp_old.getPath()); aoqi@0: if (expect == null && sp == Kind.NONE) { aoqi@0: assert cl == Kind.NONE && sp == Kind.NONE; aoqi@0: expect = "CP_OLD"; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case NEW: aoqi@0: opts.add("-classpath"); aoqi@0: opts.add(cp_new.getPath()); aoqi@0: if (expect == null) { aoqi@0: assert cl == Kind.NONE && sp == Kind.OLD; aoqi@0: expect = "CP_NEW"; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case GEN: aoqi@0: opts.add("-Agen=" + new File(cp_gen, "p/package-info.class")); aoqi@0: assert cl == Kind.NONE && sp == Kind.NONE; aoqi@0: expect = "CP_GEN"; aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // pass expected value to annotation processor aoqi@0: assert expect != null; aoqi@0: opts.add("-Aexpect=" + expect); aoqi@0: aoqi@0: // compile the files with the options that have been built up aoqi@0: compile(opts, files); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return true if this combination of parameters does not identify a useful test case. aoqi@0: */ aoqi@0: boolean skip(Kind cl, Kind sp, Kind cp) { aoqi@0: // skip if no package files required aoqi@0: if (cl == Kind.NONE && sp == Kind.NONE && cp == Kind.NONE) aoqi@0: return true; aoqi@0: aoqi@0: // skip if both sp and sp are OLD, since results may be indeterminate aoqi@0: if (sp == Kind.OLD && cp == Kind.OLD) aoqi@0: return true; aoqi@0: aoqi@0: // skip if sp or cp is NEW but the other is not OLD aoqi@0: if ((sp == Kind.NEW && cp != Kind.OLD) || (cp == Kind.NEW && sp != Kind.OLD)) aoqi@0: return true; aoqi@0: aoqi@0: // only use GEN if no other package-info files present aoqi@0: if (sp == Kind.GEN && !(cl == Kind.NONE && cp == Kind.NONE) || aoqi@0: cp == Kind.GEN && !(cl == Kind.NONE && sp == Kind.NONE)) { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // remaining combinations are valid aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** Write a file with a given body. */ aoqi@0: File writeFile(String path, String body) throws Exception { aoqi@0: File f = new File(path); aoqi@0: if (f.getParentFile() != null) aoqi@0: f.getParentFile().mkdirs(); aoqi@0: Writer out = new FileWriter(path); aoqi@0: try { aoqi@0: out.write(body); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: return f; aoqi@0: } aoqi@0: aoqi@0: /** Write a file with a given body, ensuring that the file is newer than a reference file. */ aoqi@0: File writeFile(String path, String body, File ref) throws Exception { aoqi@0: for (int i = 0; i < 5; i++) { aoqi@0: File f = writeFile(path, body); aoqi@0: if (f.lastModified() > ref.lastModified()) aoqi@0: return f; aoqi@0: Thread.sleep(2000); aoqi@0: } aoqi@0: throw new Exception("cannot create file " + path + " newer than " + ref); aoqi@0: } aoqi@0: aoqi@0: /** Compile a file to a given directory, with options provided. */ aoqi@0: void compile(File dir, String[] opts, File src) throws Exception { aoqi@0: dir.mkdirs(); aoqi@0: List opts2 = new ArrayList(); aoqi@0: opts2.addAll(Arrays.asList("-d", dir.getPath())); aoqi@0: opts2.addAll(Arrays.asList(opts)); aoqi@0: compile(opts2, Collections.singletonList(src)); aoqi@0: } aoqi@0: aoqi@0: /** Compile files with options provided. */ aoqi@0: void compile(List opts, List files) throws Exception { aoqi@0: System.err.println("javac: " + opts + " " + files); aoqi@0: List args = new ArrayList(); aoqi@0: args.addAll(opts); aoqi@0: for (File f: files) aoqi@0: args.add(f.getPath()); aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); aoqi@0: pw.flush(); aoqi@0: if (sw.getBuffer().length() > 0) aoqi@0: System.err.println(sw.toString()); aoqi@0: if (rc != 0) aoqi@0: throw new Exception("compilation failed: rc=" + rc); aoqi@0: } aoqi@0: aoqi@0: /** Report an error. */ aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: /** Test case counter. */ aoqi@0: int count; aoqi@0: aoqi@0: /** Number of errors found. */ aoqi@0: int errors; aoqi@0: aoqi@0: /** Optional set of test cases to be run; empty implies all test cases. */ aoqi@0: Set tests = new HashSet(); aoqi@0: aoqi@0: /* Files created by setup. */ aoqi@0: File test_java; aoqi@0: File sp_old; aoqi@0: File sp_new; aoqi@0: File sp_gen; aoqi@0: File cp_old; aoqi@0: File cp_new; aoqi@0: File cp_gen; aoqi@0: File cl_pkgInfo_java; aoqi@0: aoqi@0: /** Annotation processor used to verify the expected value for the aoqi@0: package annotations found by javac. */ aoqi@0: @SupportedOptions({ "gen", "expect" }) aoqi@0: public static class Processor extends JavacTestingAbstractProcessor { aoqi@0: public boolean process(Set annots, RoundEnvironment renv) { aoqi@0: round++; aoqi@0: System.err.println("Round " + round + " annots:" + annots + " rootElems:" + renv.getRootElements()); aoqi@0: aoqi@0: // if this is the first round and the gen option is given, use the filer to create aoqi@0: // a copy of the file specified by the gen option. aoqi@0: String gen = getOption("gen"); aoqi@0: if (round == 1 && gen != null) { aoqi@0: try { aoqi@0: Filer filer = processingEnv.getFiler(); aoqi@0: JavaFileObject f; aoqi@0: if (gen.endsWith(".java")) aoqi@0: f = filer.createSourceFile("p.package-info"); aoqi@0: else aoqi@0: f = filer.createClassFile("p.package-info"); aoqi@0: System.err.println("copy " + gen + " to " + f.getName()); aoqi@0: write(f, read(new File(gen))); aoqi@0: } catch (IOException e) { aoqi@0: error("Cannot create package-info file: " + e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // if annotation processing is complete, verify the package annotation aoqi@0: // found by the compiler. aoqi@0: if (renv.processingOver()) { aoqi@0: System.err.println("final round"); aoqi@0: Elements eu = processingEnv.getElementUtils(); aoqi@0: TypeElement te = eu.getTypeElement("p.Test"); aoqi@0: PackageElement pe = eu.getPackageOf(te); aoqi@0: System.err.println("final: te:" + te + " pe:" + pe); aoqi@0: List annos = pe.getAnnotationMirrors(); aoqi@0: System.err.println("final: annos:" + annos); aoqi@0: if (annos.size() == 1) { aoqi@0: String expect = "@" + te + "(\"" + getOption("expect") + "\")"; aoqi@0: String actual = annos.get(0).toString(); aoqi@0: checkEqual("package annotations", actual, expect); aoqi@0: } else { aoqi@0: error("Wrong number of annotations found: (" + annos.size() + ") " + annos); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: /** Get an option given to the annotation processor. */ aoqi@0: String getOption(String name) { aoqi@0: return processingEnv.getOptions().get(name); aoqi@0: } aoqi@0: aoqi@0: /** Read a file. */ aoqi@0: byte[] read(File file) { aoqi@0: byte[] bytes = new byte[(int) file.length()]; aoqi@0: DataInputStream in = null; aoqi@0: try { aoqi@0: in = new DataInputStream(new FileInputStream(file)); aoqi@0: in.readFully(bytes); aoqi@0: } catch (IOException e) { aoqi@0: error("Error reading file: " + e); aoqi@0: } finally { aoqi@0: if (in != null) { aoqi@0: try { aoqi@0: in.close(); aoqi@0: } catch (IOException e) { aoqi@0: error("Error closing file: " + e); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return bytes; aoqi@0: } aoqi@0: aoqi@0: /** Write a file. */ aoqi@0: void write(JavaFileObject file, byte[] bytes) { aoqi@0: OutputStream out = null; aoqi@0: try { aoqi@0: out = file.openOutputStream(); aoqi@0: out.write(bytes, 0, bytes.length); aoqi@0: } catch (IOException e) { aoqi@0: error("Error writing file: " + e); aoqi@0: } finally { aoqi@0: if (out != null) { aoqi@0: try { aoqi@0: out.close(); aoqi@0: } catch (IOException e) { aoqi@0: error("Error closing file: " + e); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Check two strings are equal, and report an error if they are not. */ aoqi@0: private void checkEqual(String label, String actual, String expect) { aoqi@0: if (!actual.equals(expect)) { aoqi@0: error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Report an error to the annotation processing system. */ aoqi@0: void error(String msg) { aoqi@0: Messager messager = processingEnv.getMessager(); aoqi@0: messager.printMessage(Diagnostic.Kind.ERROR, msg); aoqi@0: } aoqi@0: aoqi@0: int round; aoqi@0: } aoqi@0: }