test/tools/javac/file/T7018098.java

changeset 872
a19b1f4f23c9
child 1466
b52a38d4536c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/file/T7018098.java	Thu Feb 10 14:24:26 2011 -0800
     1.3 @@ -0,0 +1,121 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug 7018098
    1.30 + * @summary CacheFSInfo persists too long
    1.31 + * @library ../lib
    1.32 + * @build JavacTestingAbstractProcessor T7018098
    1.33 + * @run main T7018098
    1.34 + */
    1.35 +
    1.36 +import java.io.*;
    1.37 +import java.util.*;
    1.38 +import javax.annotation.processing.RoundEnvironment;
    1.39 +import javax.annotation.processing.SupportedOptions;
    1.40 +import javax.lang.model.element.TypeElement;
    1.41 +import javax.tools.Diagnostic;
    1.42 +
    1.43 +import com.sun.tools.javac.file.FSInfo;
    1.44 +import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    1.45 +import com.sun.tools.javac.util.Context;
    1.46 +
    1.47 +@SupportedOptions("expect")
    1.48 +public class T7018098 extends JavacTestingAbstractProcessor {
    1.49 +    public static void main(String... args) throws Exception {
    1.50 +        new T7018098().run();
    1.51 +    }
    1.52 +
    1.53 +    static File testDir = new File("T7018098.dir");
    1.54 +
    1.55 +    void run() throws Exception {
    1.56 +        String myName = T7018098.class.getSimpleName();
    1.57 +        File testSrc = new File(System.getProperty("test.src"));
    1.58 +        File file = new File(testSrc, myName + ".java");
    1.59 +
    1.60 +        _assert(!testDir.exists());
    1.61 +
    1.62 +        compile(
    1.63 +            "-proc:only",
    1.64 +            "-processor", myName,
    1.65 +            "-Aexpect=false",
    1.66 +            file.getPath());
    1.67 +
    1.68 +        testDir.mkdirs();
    1.69 +        _assert(testDir.exists());
    1.70 +
    1.71 +        compile(
    1.72 +            "-proc:only",
    1.73 +            "-processor", myName,
    1.74 +            "-Aexpect=true",
    1.75 +            file.getPath());
    1.76 +    }
    1.77 +
    1.78 +    void _assert(boolean cond) {
    1.79 +        if (!cond)
    1.80 +            throw new AssertionError();
    1.81 +    }
    1.82 +
    1.83 +    void compile(String... args) throws Exception {
    1.84 +        StringWriter sw = new StringWriter();
    1.85 +        PrintWriter pw = new PrintWriter(sw);
    1.86 +        int rc = com.sun.tools.javac.Main.compile(args, pw);
    1.87 +        pw.close();
    1.88 +        String out = sw.toString();
    1.89 +        if (!out.isEmpty())
    1.90 +            System.err.println(out);
    1.91 +        if (rc != 0)
    1.92 +            throw new Exception("compilation failed unexpectedly: rc=" + rc);
    1.93 +    }
    1.94 +
    1.95 +    //---------------
    1.96 +
    1.97 +    @Override
    1.98 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.99 +        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
   1.100 +        FSInfo fsInfo = context.get(FSInfo.class);
   1.101 +
   1.102 +        round++;
   1.103 +        if (round == 1) {
   1.104 +            boolean expect = Boolean.valueOf(options.get("expect"));
   1.105 +            checkEqual("cache result", fsInfo.isDirectory(testDir), expect);
   1.106 +            initialFSInfo = fsInfo;
   1.107 +        } else {
   1.108 +            checkEqual("fsInfo", fsInfo, initialFSInfo);
   1.109 +        }
   1.110 +
   1.111 +        return true;
   1.112 +    }
   1.113 +
   1.114 +    <T> void checkEqual(String label, T actual, T expected) {
   1.115 +        if (actual != expected)
   1.116 +            messager.printMessage(Diagnostic.Kind.ERROR,
   1.117 +                    "Unexpected value for " + label
   1.118 +                    + "; expected: " + expected
   1.119 +                    + "; found: " + actual);
   1.120 +    }
   1.121 +
   1.122 +    int round = 0;
   1.123 +    FSInfo initialFSInfo;
   1.124 +}

mercurial