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