test/tools/javac/processing/6348193/T6348193.java

changeset 1
9a66ca7c79fa
child 143
4feda9f0dbe7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/6348193/T6348193.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,187 @@
     1.4 +/*
     1.5 + * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6348193
    1.30 + * @summary AS8.1 UR2 BAT test failure with "javac"
    1.31 + * @compile -proc:none T6348193.java
    1.32 + * @run main/othervm T6348193
    1.33 + */
    1.34 +
    1.35 +import java.io.*;
    1.36 +import java.net.*;
    1.37 +import java.util.*;
    1.38 +import javax.annotation.processing.*;
    1.39 +import javax.lang.model.element.*;
    1.40 +import javax.tools.*;
    1.41 +import com.sun.tools.javac.api.JavacTool;
    1.42 +
    1.43 +@SupportedAnnotationTypes({"*"})
    1.44 +public class T6348193 extends AbstractProcessor
    1.45 +{
    1.46 +    private static final boolean verbose = true;
    1.47 +
    1.48 +    enum NoYes { NO, YES };
    1.49 +    enum NoGoodBad { NO, GOOD, BAD};
    1.50 +
    1.51 +    public static final String myName = T6348193.class.getName();
    1.52 +
    1.53 +    public static void main(String... args) throws IOException {
    1.54 +        if (System.getSecurityManager() != null)
    1.55 +            throw new AssertionError("unexpected security manager");
    1.56 +
    1.57 +        for (NoYes secMgr: EnumSet.allOf(NoYes.class))
    1.58 +            for (NoGoodBad config: EnumSet.allOf(NoGoodBad.class))
    1.59 +                for (NoYes proc: EnumSet.allOf(NoYes.class))
    1.60 +                    test(secMgr, config, proc);
    1.61 +    }
    1.62 +
    1.63 +    private static File processed = new File("processed");
    1.64 +
    1.65 +    public static void test(NoYes secMgr, NoGoodBad config, NoYes proc) throws IOException {
    1.66 +        if (verbose)
    1.67 +            System.err.println("secMgr:" + secMgr + " config:" + config + " proc:" + proc);
    1.68 +
    1.69 +        if (secMgr == NoYes.YES && System.getSecurityManager() == null)
    1.70 +            System.setSecurityManager(new NoLoaderSecurityManager());
    1.71 +
    1.72 +        installConfigFile(config);
    1.73 +
    1.74 +        processed.delete();
    1.75 +
    1.76 +        List<String> args = new ArrayList<String>();
    1.77 +        //args.add("-XprintRounds");
    1.78 +        if (proc == NoYes.YES) {
    1.79 +            args.add("-processor");
    1.80 +            args.add(myName);
    1.81 +        }
    1.82 +        args.add("-processorpath");
    1.83 +        args.add(System.getProperty("java.class.path"));
    1.84 +        args.add("-d");
    1.85 +        args.add(".");
    1.86 +
    1.87 +        JavacTool t = JavacTool.create(); // avoid using class loader
    1.88 +
    1.89 +        MyDiagListener dl = new MyDiagListener();
    1.90 +        PrintWriter out = new PrintWriter(System.err, true);
    1.91 +        StandardJavaFileManager fm = t.getStandardFileManager(dl, null, null);
    1.92 +        File file = new File(System.getProperty("test.src"), myName+".java");
    1.93 +        Iterable<? extends JavaFileObject> files =
    1.94 +            fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    1.95 +        boolean ok = t.getTask(out, null, dl, args, null, files).call();
    1.96 +
    1.97 +        if (config == NoGoodBad.GOOD || proc == NoYes.YES) {
    1.98 +            if (secMgr == NoYes.YES) {
    1.99 +                if (dl.last == null)
   1.100 +                    throw new AssertionError("Security manager installed, and processors present, "
   1.101 +                                             + " but no diagnostic received");
   1.102 +            }
   1.103 +            else {
   1.104 +                if (!processed.exists())
   1.105 +                    throw new AssertionError("No security manager installed, and processors present, "
   1.106 +                                             + " but no processing occurred");
   1.107 +            }
   1.108 +        }
   1.109 +        else if (config == NoGoodBad.BAD) {
   1.110 +            // TODO: should verify that no compiler crash occurred
   1.111 +            // needs revised JSR199 spec
   1.112 +        }
   1.113 +        else {
   1.114 +            if (processed.exists())
   1.115 +                throw new AssertionError("No processors present, but processing occurred!");
   1.116 +        }
   1.117 +
   1.118 +        if (verbose)
   1.119 +            System.err.println("OK");
   1.120 +    }
   1.121 +
   1.122 +    // set up or remove a service configuration file
   1.123 +    static void installConfigFile(NoGoodBad type) throws IOException {
   1.124 +        URL self = T6348193.class.getClassLoader().getResource(myName+".class");
   1.125 +        if (!self.getProtocol().equals("file"))
   1.126 +            throw new AssertionError();
   1.127 +        File f = new File(self.getFile()).getParentFile();
   1.128 +        for (String s: new String[] { "META-INF", "services", Processor.class.getName() })
   1.129 +            f = new File(f, s);
   1.130 +        BufferedWriter out;
   1.131 +        switch (type) {
   1.132 +        case GOOD:
   1.133 +            f.getParentFile().mkdirs();
   1.134 +            out = new BufferedWriter(new FileWriter(f));
   1.135 +            out.write(myName);
   1.136 +            out.newLine();
   1.137 +            out.close();
   1.138 +            break;
   1.139 +        case BAD:
   1.140 +            f.getParentFile().mkdirs();
   1.141 +            out = new BufferedWriter(new FileWriter(f));
   1.142 +            out.write("This is not a valid line");
   1.143 +            out.newLine();
   1.144 +            out.close();
   1.145 +            break;
   1.146 +        case NO:
   1.147 +            f.delete();
   1.148 +        }
   1.149 +
   1.150 +
   1.151 +    }
   1.152 +
   1.153 +    // annotation processor method
   1.154 +    public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv )
   1.155 +    {
   1.156 +        try {
   1.157 +            // touch a file to indicate we have run
   1.158 +            new FileWriter(processed).close();
   1.159 +        } catch (IOException e) {
   1.160 +        }
   1.161 +        return true;
   1.162 +    }
   1.163 +
   1.164 +    static class MyDiagListener implements DiagnosticListener<JavaFileObject>
   1.165 +    {
   1.166 +        public void report(Diagnostic<? extends JavaFileObject> message) {
   1.167 +            if (verbose)
   1.168 +                System.err.println(message);
   1.169 +            last = message;
   1.170 +        }
   1.171 +
   1.172 +        Diagnostic<? extends JavaFileObject> last;
   1.173 +    }
   1.174 +
   1.175 +    static class NoLoaderSecurityManager extends SecurityManager
   1.176 +    {
   1.177 +        public void checkCreateClassLoader() {
   1.178 +            throw new SecurityException("Not today, thanks you!");
   1.179 +        }
   1.180 +
   1.181 +        public void checkPropertyAccess(String key) { /*OK*/ }
   1.182 +
   1.183 +        public void checkDelete(String file) { /*OK*/ }
   1.184 +        public void checkRead(FileDescriptor fd) { /*OK*/ }
   1.185 +        public void checkRead(String file) { /*OK*/ }
   1.186 +        public void checkRead(String file, Object context) { /*OK*/ }
   1.187 +        public void checkWrite(String file) { /*OK*/ }
   1.188 +
   1.189 +    }
   1.190 +}

mercurial