Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 8004833 |
aoqi@0 | 27 | * @summary Integrate doclint support into javac |
aoqi@0 | 28 | */ |
aoqi@0 | 29 | |
aoqi@0 | 30 | import java.io.File; |
aoqi@0 | 31 | import java.io.PrintWriter; |
aoqi@0 | 32 | import java.io.StringWriter; |
aoqi@0 | 33 | import java.net.URI; |
aoqi@0 | 34 | import java.util.Arrays; |
aoqi@0 | 35 | import java.util.Collections; |
aoqi@0 | 36 | import java.util.List; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import javax.tools.Diagnostic; |
aoqi@0 | 39 | import javax.tools.JavaCompiler; |
aoqi@0 | 40 | import javax.tools.JavaFileObject; |
aoqi@0 | 41 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 42 | import javax.tools.StandardJavaFileManager; |
aoqi@0 | 43 | import javax.tools.StandardLocation; |
aoqi@0 | 44 | import javax.tools.ToolProvider; |
aoqi@0 | 45 | import static javax.tools.Diagnostic.Kind.*; |
aoqi@0 | 46 | |
aoqi@0 | 47 | import com.sun.source.util.JavacTask; |
aoqi@0 | 48 | import com.sun.tools.javac.main.Main; |
aoqi@0 | 49 | import java.util.EnumSet; |
aoqi@0 | 50 | import java.util.Set; |
aoqi@0 | 51 | import java.util.regex.Matcher; |
aoqi@0 | 52 | import java.util.regex.Pattern; |
aoqi@0 | 53 | |
aoqi@0 | 54 | public class DocLintTest { |
aoqi@0 | 55 | public static void main(String... args) throws Exception { |
aoqi@0 | 56 | new DocLintTest().run(); |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | JavaCompiler javac; |
aoqi@0 | 60 | StandardJavaFileManager fm; |
aoqi@0 | 61 | JavaFileObject file; |
aoqi@0 | 62 | |
aoqi@0 | 63 | final String code = |
aoqi@0 | 64 | /* 01 */ "/** Class comment. */\n" + |
aoqi@0 | 65 | /* 02 */ "public class Test {\n" + |
aoqi@0 | 66 | /* 03 */ " /** Method comment. */\n" + |
aoqi@0 | 67 | /* 04 */ " public void method() { }\n" + |
aoqi@0 | 68 | /* 05 */ "\n" + |
aoqi@0 | 69 | /* 06 */ " /** Syntax < error. */\n" + |
aoqi@0 | 70 | /* 07 */ " private void syntaxError() { }\n" + |
aoqi@0 | 71 | /* 08 */ "\n" + |
aoqi@0 | 72 | /* 09 */ " /** @see DoesNotExist */\n" + |
aoqi@0 | 73 | /* 10 */ " protected void referenceError() { }\n" + |
aoqi@0 | 74 | /* 08 */ "\n" + |
aoqi@0 | 75 | /* 09 */ " /** @return */\n" + |
aoqi@0 | 76 | /* 10 */ " public int emptyReturn() { return 0; }\n" + |
aoqi@0 | 77 | /* 11 */ "}\n"; |
aoqi@0 | 78 | |
aoqi@0 | 79 | final String rawDiags = "-XDrawDiagnostics"; |
aoqi@0 | 80 | private enum Message { |
aoqi@0 | 81 | // doclint messages |
aoqi@0 | 82 | DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"), |
aoqi@0 | 83 | DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"), |
aoqi@0 | 84 | DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"), |
aoqi@0 | 85 | |
aoqi@0 | 86 | OPT_BADARG(ERROR, "invalid flag: -Xdoclint:badarg"); |
aoqi@0 | 87 | |
aoqi@0 | 88 | final Diagnostic.Kind kind; |
aoqi@0 | 89 | final String text; |
aoqi@0 | 90 | |
aoqi@0 | 91 | static Message get(String text) { |
aoqi@0 | 92 | for (Message m: values()) { |
aoqi@0 | 93 | if (m.text.equals(text)) |
aoqi@0 | 94 | return m; |
aoqi@0 | 95 | } |
aoqi@0 | 96 | return null; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | Message(Diagnostic.Kind kind, String text) { |
aoqi@0 | 100 | this.kind = kind; |
aoqi@0 | 101 | this.text = text; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | @Override |
aoqi@0 | 105 | public String toString() { |
aoqi@0 | 106 | return "[" + kind + ",\"" + text + "\"]"; |
aoqi@0 | 107 | } |
aoqi@0 | 108 | } |
aoqi@0 | 109 | void run() throws Exception { |
aoqi@0 | 110 | javac = ToolProvider.getSystemJavaCompiler(); |
aoqi@0 | 111 | fm = javac.getStandardFileManager(null, null, null); |
aoqi@0 | 112 | fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); |
aoqi@0 | 113 | file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) { |
aoqi@0 | 114 | @Override |
aoqi@0 | 115 | public CharSequence getCharContent(boolean ignoreEncoding) { |
aoqi@0 | 116 | return code; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | }; |
aoqi@0 | 119 | |
aoqi@0 | 120 | test(Collections.<String>emptyList(), |
aoqi@0 | 121 | Main.Result.OK, |
aoqi@0 | 122 | EnumSet.noneOf(Message.class)); |
aoqi@0 | 123 | |
aoqi@0 | 124 | test(Arrays.asList("-Xdoclint:none"), |
aoqi@0 | 125 | Main.Result.OK, |
aoqi@0 | 126 | EnumSet.noneOf(Message.class)); |
aoqi@0 | 127 | |
aoqi@0 | 128 | test(Arrays.asList(rawDiags, "-Xdoclint"), |
aoqi@0 | 129 | Main.Result.ERROR, |
aoqi@0 | 130 | EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12)); |
aoqi@0 | 131 | |
aoqi@0 | 132 | test(Arrays.asList(rawDiags, "-Xdoclint:all/public"), |
aoqi@0 | 133 | Main.Result.OK, |
aoqi@0 | 134 | EnumSet.of(Message.DL_WRN12)); |
aoqi@0 | 135 | |
aoqi@0 | 136 | test(Arrays.asList(rawDiags, "-Xdoclint:syntax"), |
aoqi@0 | 137 | Main.Result.ERROR, |
aoqi@0 | 138 | EnumSet.of(Message.DL_ERR6, Message.DL_WRN12)); |
aoqi@0 | 139 | |
aoqi@0 | 140 | test(Arrays.asList(rawDiags, "-Xdoclint:reference"), |
aoqi@0 | 141 | Main.Result.ERROR, |
aoqi@0 | 142 | EnumSet.of(Message.DL_ERR9)); |
aoqi@0 | 143 | |
aoqi@0 | 144 | test(Arrays.asList(rawDiags, "-Xdoclint:badarg"), |
aoqi@0 | 145 | Main.Result.CMDERR, |
aoqi@0 | 146 | EnumSet.of(Message.OPT_BADARG)); |
aoqi@0 | 147 | |
aoqi@0 | 148 | if (errors > 0) |
aoqi@0 | 149 | throw new Exception(errors + " errors occurred"); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { |
aoqi@0 | 153 | System.err.println("test: " + opts); |
aoqi@0 | 154 | StringWriter sw = new StringWriter(); |
aoqi@0 | 155 | PrintWriter pw = new PrintWriter(sw); |
aoqi@0 | 156 | List<JavaFileObject> files = Arrays.asList(file); |
aoqi@0 | 157 | try { |
aoqi@0 | 158 | JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files); |
aoqi@0 | 159 | boolean ok = t.call(); |
aoqi@0 | 160 | pw.close(); |
aoqi@0 | 161 | String out = sw.toString().replaceAll("[\r\n]+", "\n"); |
aoqi@0 | 162 | if (!out.isEmpty()) |
aoqi@0 | 163 | System.err.println(out); |
aoqi@0 | 164 | if (ok && expectResult != Main.Result.OK) { |
aoqi@0 | 165 | error("Compilation succeeded unexpectedly"); |
aoqi@0 | 166 | } else if (!ok && expectResult != Main.Result.ERROR) { |
aoqi@0 | 167 | error("Compilation failed unexpectedly"); |
aoqi@0 | 168 | } else |
aoqi@0 | 169 | check(out, expectMessages); |
aoqi@0 | 170 | } catch (IllegalArgumentException e) { |
aoqi@0 | 171 | System.err.println(e); |
aoqi@0 | 172 | String expectOut = expectMessages.iterator().next().text; |
aoqi@0 | 173 | if (expectResult != Main.Result.CMDERR) |
aoqi@0 | 174 | error("unexpected exception caught"); |
aoqi@0 | 175 | else if (!e.getMessage().equals(expectOut)) { |
aoqi@0 | 176 | error("unexpected exception message: " |
aoqi@0 | 177 | + e.getMessage() |
aoqi@0 | 178 | + " expected: " + expectOut); |
aoqi@0 | 179 | } |
aoqi@0 | 180 | } |
aoqi@0 | 181 | |
aoqi@0 | 182 | // if (errors > 0) |
aoqi@0 | 183 | // throw new Error("stop"); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | |
aoqi@0 | 186 | private void check(String out, Set<Message> expect) { |
aoqi@0 | 187 | Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); |
aoqi@0 | 188 | Set<Message> found = EnumSet.noneOf(Message.class); |
aoqi@0 | 189 | int e = 0, w = 0; |
aoqi@0 | 190 | if (!out.isEmpty()) { |
aoqi@0 | 191 | for (String line: out.split("[\r\n]+")) { |
aoqi@0 | 192 | Matcher s = stats.matcher(line); |
aoqi@0 | 193 | if (s.matches()) { |
aoqi@0 | 194 | int i = Integer.valueOf(s.group(1)); |
aoqi@0 | 195 | if (s.group(2).equals("error")) |
aoqi@0 | 196 | e++; |
aoqi@0 | 197 | else |
aoqi@0 | 198 | w++; |
aoqi@0 | 199 | continue; |
aoqi@0 | 200 | } |
aoqi@0 | 201 | |
aoqi@0 | 202 | Message m = Message.get(line); |
aoqi@0 | 203 | if (m == null) |
aoqi@0 | 204 | error("Unexpected line: " + line); |
aoqi@0 | 205 | else |
aoqi@0 | 206 | found.add(m); |
aoqi@0 | 207 | } |
aoqi@0 | 208 | } |
aoqi@0 | 209 | for (Message m: expect) { |
aoqi@0 | 210 | if (!found.contains(m)) |
aoqi@0 | 211 | error("expected message not found: " + m.text); |
aoqi@0 | 212 | } |
aoqi@0 | 213 | for (Message m: found) { |
aoqi@0 | 214 | if (!expect.contains(m)) |
aoqi@0 | 215 | error("unexpected message found: " + m.text); |
aoqi@0 | 216 | } |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | void error(String msg) { |
aoqi@0 | 220 | System.err.println("Error: " + msg); |
aoqi@0 | 221 | errors++; |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | int errors; |
aoqi@0 | 225 | } |