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