langtools/test/tools/javac/processing/6348193/T6348193.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 5520 86e4b9a9da40
child 27988 d81c4220d579
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6348193
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary AS8.1 UR2 BAT test failure with "javac"
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @compile -proc:none T6348193.java
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * @run main/othervm T6348193
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.net.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.api.JavacTool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
@SupportedAnnotationTypes({"*"})
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
public class T6348193 extends AbstractProcessor
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
{
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    private static final boolean verbose = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    enum NoYes { NO, YES };
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    enum NoGoodBad { NO, GOOD, BAD};
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    public static final String myName = T6348193.class.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    public static void main(String... args) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        if (System.getSecurityManager() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            throw new AssertionError("unexpected security manager");
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        for (NoYes secMgr: EnumSet.allOf(NoYes.class))
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            for (NoGoodBad config: EnumSet.allOf(NoGoodBad.class))
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
                for (NoYes proc: EnumSet.allOf(NoYes.class))
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
                    test(secMgr, config, proc);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private static File processed = new File("processed");
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    public static void test(NoYes secMgr, NoGoodBad config, NoYes proc) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        if (verbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            System.err.println("secMgr:" + secMgr + " config:" + config + " proc:" + proc);
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        if (secMgr == NoYes.YES && System.getSecurityManager() == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            System.setSecurityManager(new NoLoaderSecurityManager());
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        installConfigFile(config);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        processed.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        List<String> args = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        //args.add("-XprintRounds");
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        if (proc == NoYes.YES) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            args.add("-processor");
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            args.add(myName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        args.add("-processorpath");
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        args.add(System.getProperty("java.class.path"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        args.add("-d");
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        args.add(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        JavacTool t = JavacTool.create(); // avoid using class loader
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        MyDiagListener dl = new MyDiagListener();
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        PrintWriter out = new PrintWriter(System.err, true);
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    88
        try (StandardJavaFileManager fm = t.getStandardFileManager(dl, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    89
            File file = new File(System.getProperty("test.src"), myName+".java");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    90
            Iterable<? extends JavaFileObject> files =
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    91
                fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    92
            boolean ok = t.getTask(out, null, dl, args, null, files).call();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    94
            if (config == NoGoodBad.GOOD || proc == NoYes.YES) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    95
                if (secMgr == NoYes.YES) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    96
                    if (dl.last == null)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    97
                        throw new AssertionError("Security manager installed, and processors present, "
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    98
                                                 + " but no diagnostic received");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    99
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   100
                else {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   101
                    if (!processed.exists())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   102
                        throw new AssertionError("No security manager installed, and processors present, "
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   103
                                                 + " but no processing occurred");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   104
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   105
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   106
            else if (config == NoGoodBad.BAD) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   107
                // TODO: should verify that no compiler crash occurred
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   108
                // needs revised JSR199 spec
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            else {
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   111
                if (processed.exists())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   112
                    throw new AssertionError("No processors present, but processing occurred!");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   114
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   115
            if (verbose)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
   116
                System.err.println("OK");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    // set up or remove a service configuration file
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    static void installConfigFile(NoGoodBad type) throws IOException {
1478
1847a080da9a 6759796: test/tools/javac/6348193/T6348193.java fails if there are empty entries on the bootclasspath
jjg
parents: 10
diff changeset
   122
        File f = new File(System.getProperty("test.classes", "."));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        for (String s: new String[] { "META-INF", "services", Processor.class.getName() })
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            f = new File(f, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        BufferedWriter out;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        switch (type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        case GOOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            f.getParentFile().mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            out = new BufferedWriter(new FileWriter(f));
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            out.write(myName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            out.newLine();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        case BAD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            f.getParentFile().mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            out = new BufferedWriter(new FileWriter(f));
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            out.write("This is not a valid line");
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            out.newLine();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        case NO:
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            f.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    // annotation processor method
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv )
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            // touch a file to indicate we have run
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            new FileWriter(processed).close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    static class MyDiagListener implements DiagnosticListener<JavaFileObject>
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        public void report(Diagnostic<? extends JavaFileObject> message) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            if (verbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                System.err.println(message);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            last = message;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        Diagnostic<? extends JavaFileObject> last;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    static class NoLoaderSecurityManager extends SecurityManager
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        public void checkCreateClassLoader() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            throw new SecurityException("Not today, thanks you!");
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        public void checkPropertyAccess(String key) { /*OK*/ }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        public void checkDelete(String file) { /*OK*/ }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        public void checkRead(FileDescriptor fd) { /*OK*/ }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        public void checkRead(String file) { /*OK*/ }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        public void checkRead(String file, Object context) { /*OK*/ }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        public void checkWrite(String file) { /*OK*/ }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
}