T7018098.java revision 2734:b96d74fa60aa
143045Sdillon/*
243045Sdillon * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
343045Sdillon * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
443045Sdillon *
543045Sdillon * This code is free software; you can redistribute it and/or modify it
643045Sdillon * under the terms of the GNU General Public License version 2 only, as
743045Sdillon * published by the Free Software Foundation.
843045Sdillon *
943045Sdillon * This code is distributed in the hope that it will be useful, but WITHOUT
1043045Sdillon * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1143045Sdillon * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1243045Sdillon * version 2 for more details (a copy is included in the LICENSE file that
1343045Sdillon * accompanied this code).
1443045Sdillon *
1543045Sdillon * You should have received a copy of the GNU General Public License version
1643045Sdillon * 2 along with this work; if not, write to the Free Software Foundation,
1750476Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1843045Sdillon *
1943045Sdillon * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2043045Sdillon * or visit www.oracle.com if you need additional information or have any
2143045Sdillon * questions.
2243045Sdillon */
2343045Sdillon
2443045Sdillon/**
2543045Sdillon * @test
2643045Sdillon * @bug 7018098
2743045Sdillon * @summary CacheFSInfo persists too long
2843045Sdillon * @library /tools/javac/lib
2943045Sdillon * @build JavacTestingAbstractProcessor T7018098
3069793Sobrien * @run main T7018098
3143045Sdillon */
3243045Sdillon
3343045Sdillonimport java.io.*;
3443045Sdillonimport java.util.*;
3543045Sdillonimport javax.annotation.processing.RoundEnvironment;
3643045Sdillonimport javax.annotation.processing.SupportedOptions;
3743045Sdillonimport javax.lang.model.element.TypeElement;
3843045Sdillonimport javax.tools.Diagnostic;
3943045Sdillon
4043045Sdillonimport com.sun.tools.javac.file.FSInfo;
4143045Sdillonimport com.sun.tools.javac.processing.JavacProcessingEnvironment;
4243045Sdillonimport com.sun.tools.javac.util.Context;
4343045Sdillon
4447018Speter@SupportedOptions("expect")
4547018Speterpublic class T7018098 extends JavacTestingAbstractProcessor {
4647018Speter    public static void main(String... args) throws Exception {
4747018Speter        new T7018098().run();
4843045Sdillon    }
4943045Sdillon
5043045Sdillon    static File testDir = new File("T7018098.dir");
5143045Sdillon
5243045Sdillon    void run() throws Exception {
5343045Sdillon        String myName = T7018098.class.getSimpleName();
5443282Sbde        File testSrc = new File(System.getProperty("test.src"));
5543282Sbde        File file = new File(testSrc, myName + ".java");
5643045Sdillon
5743045Sdillon        _assert(!testDir.exists());
5843045Sdillon
5943045Sdillon        compile(
6043045Sdillon            "-proc:only",
6143045Sdillon            "-processor", myName,
6243045Sdillon            "-Aexpect=false",
6343045Sdillon            file.getPath());
6443045Sdillon
6543045Sdillon        testDir.mkdirs();
6643045Sdillon        _assert(testDir.exists());
6743045Sdillon
6843045Sdillon        compile(
6943045Sdillon            "-proc:only",
7043045Sdillon            "-processor", myName,
7143045Sdillon            "-Aexpect=true",
7243045Sdillon            file.getPath());
7343045Sdillon    }
7443045Sdillon
7543045Sdillon    void _assert(boolean cond) {
7643045Sdillon        if (!cond)
7743045Sdillon            throw new AssertionError();
7843045Sdillon    }
7943045Sdillon
8043045Sdillon    void compile(String... args) throws Exception {
8143045Sdillon        StringWriter sw = new StringWriter();
8243045Sdillon        PrintWriter pw = new PrintWriter(sw);
8343045Sdillon        int rc = com.sun.tools.javac.Main.compile(args, pw);
8443045Sdillon        pw.close();
8543045Sdillon        String out = sw.toString();
8643045Sdillon        if (!out.isEmpty())
8743045Sdillon            System.err.println(out);
8843045Sdillon        if (rc != 0)
8943045Sdillon            throw new Exception("compilation failed unexpectedly: rc=" + rc);
9043045Sdillon    }
9143045Sdillon
9243045Sdillon    //---------------
9343045Sdillon
9443045Sdillon    @Override
9543045Sdillon    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
9643045Sdillon        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
9743045Sdillon        FSInfo fsInfo = context.get(FSInfo.class);
9843045Sdillon
9943045Sdillon        round++;
10043045Sdillon        if (round == 1) {
10143045Sdillon            boolean expect = Boolean.valueOf(options.get("expect"));
10243045Sdillon            checkEqual("cache result", fsInfo.isDirectory(testDir.toPath()), expect);
10343045Sdillon            initialFSInfo = fsInfo;
10443045Sdillon        } else {
10543045Sdillon            checkEqual("fsInfo", fsInfo, initialFSInfo);
10643045Sdillon        }
10743045Sdillon
10843045Sdillon        return true;
10943045Sdillon    }
11047018Speter
11147018Speter    <T> void checkEqual(String label, T actual, T expected) {
11243045Sdillon        if (actual != expected)
11343045Sdillon            messager.printMessage(Diagnostic.Kind.ERROR,
11443045Sdillon                    "Unexpected value for " + label
11543045Sdillon                    + "; expected: " + expected
11643045Sdillon                    + "; found: " + actual);
11743045Sdillon    }
11843045Sdillon
11943045Sdillon    int round = 0;
12043045Sdillon    FSInfo initialFSInfo;
12143045Sdillon}
12243045Sdillon