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