T7018098.java revision 2675:4be0e35f385a
1/*
2 * Copyright (c) 2011, 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 7018098
27 * @summary CacheFSInfo persists too long
28 * @library /tools/javac/lib
29 * @build JavacTestingAbstractProcessor T7018098
30 * @run main T7018098
31 */
32
33import java.io.*;
34import java.util.*;
35import javax.annotation.processing.RoundEnvironment;
36import javax.annotation.processing.SupportedOptions;
37import javax.lang.model.element.TypeElement;
38import javax.tools.Diagnostic;
39
40import com.sun.tools.javac.file.FSInfo;
41import com.sun.tools.javac.processing.JavacProcessingEnvironment;
42import com.sun.tools.javac.util.Context;
43
44@SupportedOptions("expect")
45public class T7018098 extends JavacTestingAbstractProcessor {
46    public static void main(String... args) throws Exception {
47        new T7018098().run();
48    }
49
50    static File testDir = new File("T7018098.dir");
51
52    void run() throws Exception {
53        String myName = T7018098.class.getSimpleName();
54        File testSrc = new File(System.getProperty("test.src"));
55        File file = new File(testSrc, myName + ".java");
56
57        _assert(!testDir.exists());
58
59        compile(
60            "-proc:only",
61            "-processor", myName,
62            "-Aexpect=false",
63            file.getPath());
64
65        testDir.mkdirs();
66        _assert(testDir.exists());
67
68        compile(
69            "-proc:only",
70            "-processor", myName,
71            "-Aexpect=true",
72            file.getPath());
73    }
74
75    void _assert(boolean cond) {
76        if (!cond)
77            throw new AssertionError();
78    }
79
80    void compile(String... args) throws Exception {
81        StringWriter sw = new StringWriter();
82        PrintWriter pw = new PrintWriter(sw);
83        int rc = com.sun.tools.javac.Main.compile(args, pw);
84        pw.close();
85        String out = sw.toString();
86        if (!out.isEmpty())
87            System.err.println(out);
88        if (rc != 0)
89            throw new Exception("compilation failed unexpectedly: rc=" + rc);
90    }
91
92    //---------------
93
94    @Override
95    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
96        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
97        FSInfo fsInfo = context.get(FSInfo.class);
98
99        round++;
100        if (round == 1) {
101            boolean expect = Boolean.valueOf(options.get("expect"));
102            checkEqual("cache result", fsInfo.isDirectory(testDir), expect);
103            initialFSInfo = fsInfo;
104        } else {
105            checkEqual("fsInfo", fsInfo, initialFSInfo);
106        }
107
108        return true;
109    }
110
111    <T> void checkEqual(String label, T actual, T expected) {
112        if (actual != expected)
113            messager.printMessage(Diagnostic.Kind.ERROR,
114                    "Unexpected value for " + label
115                    + "; expected: " + expected
116                    + "; found: " + actual);
117    }
118
119    int round = 0;
120    FSInfo initialFSInfo;
121}
122