IncludePackagesTest.java revision 2846:072008f47620
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8071851
27 * @summary Test the -Xdoclint/package option
28 */
29
30import java.io.File;
31import java.io.PrintWriter;
32import java.io.StringWriter;
33import java.net.URI;
34import java.util.Arrays;
35import java.util.List;
36
37import javax.tools.Diagnostic;
38import javax.tools.JavaCompiler;
39import javax.tools.JavaFileObject;
40import javax.tools.SimpleJavaFileObject;
41import javax.tools.StandardJavaFileManager;
42import javax.tools.StandardLocation;
43import javax.tools.ToolProvider;
44import static javax.tools.Diagnostic.Kind.*;
45
46import com.sun.source.util.JavacTask;
47import com.sun.tools.javac.main.Main;
48import java.util.ArrayList;
49import java.util.EnumSet;
50import java.util.Set;
51import java.util.regex.Matcher;
52import java.util.regex.Pattern;
53
54public class IncludePackagesTest {
55    public static void main(String... args) throws Exception {
56        new IncludePackagesTest().run();
57    }
58
59    JavaCompiler javac;
60    StandardJavaFileManager fm;
61    List<JavaFileObject> files;
62
63    final String[] sources = new String[] {
64        "p1/p1T.java",
65        "package p1;\n" +
66        "/** Syntax < error. */\n" +
67        "public class p1T {\n" +
68        "}\n",
69        "p1/sp1/p1sp1T.java",
70        "package p1.sp1;\n" +
71        "/** Syntax < error. */\n" +
72        "public class p1sp1T {\n" +
73        "}\n",
74        "p1/sp1/sp2/p1sp1sp2T.java",
75        "package p1.sp1.sp2;\n" +
76        "/** Syntax < error. */\n" +
77        "public class p1sp1sp2T {\n" +
78        "}\n",
79        "p2/p2T.java",
80        "package p2;\n" +
81        "/** Syntax < error. */\n" +
82        "public class p2T {\n" +
83        "}\n",
84        "Default.java",
85        "/** Syntax < error. */\n" +
86        "public class Default {\n" +
87        "}\n",
88    };
89
90    final String rawDiags = "-XDrawDiagnostics";
91    private enum Message {
92        // doclint messages
93        p1T(ERROR, "p1T.java:2:12: compiler.err.proc.messager: malformed HTML"),
94        p1sp1T(ERROR, "p1sp1T.java:2:12: compiler.err.proc.messager: malformed HTML"),
95        p1sp1sp2T(ERROR, "p1sp1sp2T.java:2:12: compiler.err.proc.messager: malformed HTML"),
96        p2T(ERROR, "p2T.java:2:12: compiler.err.proc.messager: malformed HTML"),
97        Default(ERROR, "Default.java:1:12: compiler.err.proc.messager: malformed HTML"),
98        INVALID_PACKAGE_ERROR(ERROR, "invalid flag: -Xdoclint/package:wrong+package");
99
100        final Diagnostic.Kind kind;
101        final String text;
102
103        static Message get(String text) {
104            for (Message m: values()) {
105                if (m.text.equals(text))
106                    return m;
107            }
108            return null;
109        }
110
111        Message(Diagnostic.Kind kind, String text) {
112            this.kind = kind;
113            this.text = text;
114        }
115
116        @Override
117        public String toString() {
118            return "[" + kind + ",\"" + text + "\"]";
119        }
120    }
121    void run() throws Exception {
122        javac = ToolProvider.getSystemJavaCompiler();
123        fm = javac.getStandardFileManager(null, null, null);
124        try {
125            fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
126            files = new ArrayList<>();
127
128            for (int si = 0; si < sources.length; si += 2) {
129                files.add(new JFOImpl(sources[si], sources[si + 1]));
130            }
131
132            test(Arrays.asList(rawDiags, "-Xdoclint"),
133                    Main.Result.ERROR,
134                    EnumSet.of(Message.p1T, Message.p1sp1T, Message.p1sp1sp2T,
135                               Message.p2T, Message.Default));
136
137            test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:p1"),
138                    Main.Result.ERROR,
139                    EnumSet.of(Message.p1T));
140
141            test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:p1.*"),
142                    Main.Result.ERROR,
143                    EnumSet.of(Message.p1sp1T, Message.p1sp1sp2T));
144
145            test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:p1.*,-p1.sp1"),
146                    Main.Result.ERROR,
147                    EnumSet.of(Message.p1sp1sp2T));
148
149            test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:-p1.sp1"),
150                    Main.Result.ERROR,
151                    EnumSet.of(Message.p1T, Message.p1sp1sp2T, Message.p2T, Message.Default));
152
153            test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:wrong+package"),
154                    Main.Result.CMDERR,
155                    EnumSet.of(Message.INVALID_PACKAGE_ERROR));
156
157            if (errors > 0)
158                throw new Exception(errors + " errors occurred");
159        } finally {
160            fm.close();
161        }
162    }
163
164    void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
165        System.err.println("test: " + opts);
166        StringWriter sw = new StringWriter();
167        PrintWriter pw = new PrintWriter(sw);
168        try {
169            JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files);
170            boolean ok = t.call();
171            pw.close();
172            String out = sw.toString().replaceAll("[\r\n]+", "\n");
173            if (!out.isEmpty())
174                System.err.println(out);
175            if (ok && expectResult != Main.Result.OK) {
176                error("Compilation succeeded unexpectedly");
177            } else if (!ok && expectResult != Main.Result.ERROR) {
178                error("Compilation failed unexpectedly");
179            } else
180                check(out, expectMessages);
181        } catch (IllegalArgumentException e) {
182            System.err.println(e);
183            String expectOut = expectMessages.iterator().next().text;
184            if (expectResult != Main.Result.CMDERR)
185                error("unexpected exception caught");
186            else if (!e.getMessage().equals(expectOut)) {
187                error("unexpected exception message: "
188                        + e.getMessage()
189                        + " expected: " + expectOut);
190            }
191        }
192    }
193
194    private void check(String out, Set<Message> expect) {
195        Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");
196        Set<Message> found = EnumSet.noneOf(Message.class);
197        int e = 0, w = 0;
198        if (!out.isEmpty()) {
199            for (String line: out.split("[\r\n]+")) {
200                Matcher s = stats.matcher(line);
201                if (s.matches()) {
202                    int i = Integer.valueOf(s.group(1));
203                    if (s.group(2).equals("error"))
204                        e++;
205                    else
206                        w++;
207                    continue;
208                }
209
210                Message m = Message.get(line);
211                if (m == null)
212                    error("Unexpected line: " + line);
213                else
214                    found.add(m);
215            }
216        }
217        for (Message m: expect) {
218            if (!found.contains(m))
219                error("expected message not found: " + m.text);
220        }
221        for (Message m: found) {
222            if (!expect.contains(m))
223                error("unexpected message found: " + m.text);
224        }
225    }
226
227    void error(String msg) {
228        System.err.println("Error: " + msg);
229        errors++;
230    }
231
232    int errors;
233
234    class JFOImpl extends SimpleJavaFileObject {
235
236        private final String code;
237
238        public JFOImpl(String fileName, String code) {
239            super(URI.create(fileName), JavaFileObject.Kind.SOURCE);
240            this.code = code;
241        }
242
243        @Override
244        public CharSequence getCharContent(boolean ignoreEncoding) {
245            return code;
246        }
247    }
248}
249