T7018098.java revision 3376:4c740bddc648
149973Scsgr/*
249973Scsgr * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
349973Scsgr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
449973Scsgr *
549973Scsgr * This code is free software; you can redistribute it and/or modify it
649973Scsgr * under the terms of the GNU General Public License version 2 only, as
749973Scsgr * published by the Free Software Foundation.
849973Scsgr *
949973Scsgr * This code is distributed in the hope that it will be useful, but WITHOUT
1049973Scsgr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1149973Scsgr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1249973Scsgr * version 2 for more details (a copy is included in the LICENSE file that
1349973Scsgr * accompanied this code).
1450476Speter *
15165721Sdanger * You should have received a copy of the GNU General Public License version
1649973Scsgr * 2 along with this work; if not, write to the Free Software Foundation,
1779538Sru * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1849973Scsgr *
1979727Sschweikh * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2049973Scsgr * or visit www.oracle.com if you need additional information or have any
2149973Scsgr * questions.
2249973Scsgr */
2381625Sru
2449973Scsgr/*
2584979Sdd * @test
2684979Sdd * @bug 7018098
2749973Scsgr * @summary CacheFSInfo persists too long
2849973Scsgr * @library /tools/javac/lib
2949973Scsgr * @modules jdk.compiler/com.sun.tools.javac.file
3049973Scsgr *          jdk.compiler/com.sun.tools.javac.processing
3149973Scsgr *          jdk.compiler/com.sun.tools.javac.util
3249973Scsgr * @build JavacTestingAbstractProcessor T7018098
3349973Scsgr * @run main T7018098
3449973Scsgr */
3549973Scsgr
36117011Sruimport java.io.*;
37117011Sruimport java.util.*;
38117011Sruimport javax.annotation.processing.RoundEnvironment;
39117011Sruimport javax.annotation.processing.SupportedOptions;
40117011Sruimport javax.lang.model.element.TypeElement;
4184979Sddimport javax.tools.Diagnostic;
4250074Scsgr
43117011Sruimport com.sun.tools.javac.file.FSInfo;
44117011Sruimport com.sun.tools.javac.processing.JavacProcessingEnvironment;
45117011Sruimport com.sun.tools.javac.util.Context;
46117011Sru
4749973Scsgr@SupportedOptions("expect")
4849973Scsgrpublic class T7018098 extends JavacTestingAbstractProcessor {
4949973Scsgr    public static void main(String... args) throws Exception {
50117011Sru        new T7018098().run();
51117011Sru    }
5249973Scsgr
5384979Sdd    static File testDir = new File("T7018098.dir");
5449973Scsgr
5549973Scsgr    void run() throws Exception {
5684979Sdd        String myName = T7018098.class.getSimpleName();
5749973Scsgr        File testSrc = new File(System.getProperty("test.src"));
5884979Sdd        File file = new File(testSrc, myName + ".java");
5949973Scsgr
6049973Scsgr        _assert(!testDir.exists());
61165721Sdanger
62165721Sdanger        compile(
63165721Sdanger            "-XaddExports:jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
64165721Sdanger            "-XaddExports:jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
65210676Sjoel            "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
6649973Scsgr            "-XDaccessInternalAPI",
6784979Sdd            "-proc:only",
6884979Sdd            "-processor", myName,
6950531Smpp            "-Aexpect=false",
7050531Smpp            file.getPath());
7150531Smpp
7250531Smpp        testDir.mkdirs();
73165721Sdanger        _assert(testDir.exists());
7450531Smpp
75165721Sdanger        compile(
7649973Scsgr            "-XaddExports:jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
7749973Scsgr            "-XaddExports:jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
7849973Scsgr            "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
7979727Sschweikh            "-XDaccessInternalAPI",
8049973Scsgr            "-proc:only",
8149973Scsgr            "-processor", myName,
8250531Smpp            "-Aexpect=true",
83140561Sru            file.getPath());
84140561Sru    }
85
86    void _assert(boolean cond) {
87        if (!cond)
88            throw new AssertionError();
89    }
90
91    void compile(String... args) throws Exception {
92        StringWriter sw = new StringWriter();
93        PrintWriter pw = new PrintWriter(sw);
94        int rc = com.sun.tools.javac.Main.compile(args, pw);
95        pw.close();
96        String out = sw.toString();
97        if (!out.isEmpty())
98            System.err.println(out);
99        if (rc != 0)
100            throw new Exception("compilation failed unexpectedly: rc=" + rc);
101    }
102
103    //---------------
104
105    @Override
106    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
107        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
108        FSInfo fsInfo = context.get(FSInfo.class);
109
110        round++;
111        if (round == 1) {
112            boolean expect = Boolean.valueOf(options.get("expect"));
113            checkEqual("cache result", fsInfo.isDirectory(testDir.toPath()), expect);
114            initialFSInfo = fsInfo;
115        } else {
116            checkEqual("fsInfo", fsInfo, initialFSInfo);
117        }
118
119        return true;
120    }
121
122    <T> void checkEqual(String label, T actual, T expected) {
123        if (actual != expected)
124            messager.printMessage(Diagnostic.Kind.ERROR,
125                    "Unexpected value for " + label
126                    + "; expected: " + expected
127                    + "; found: " + actual);
128    }
129
130    int round = 0;
131    FSInfo initialFSInfo;
132}
133