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