DocLintTest.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2012, 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 8004834 8007610 8129909 27 * @summary Add doclint support into javadoc 28 * @modules jdk.compiler/com.sun.tools.javac.main 29 */ 30 31import java.io.File; 32import java.io.PrintWriter; 33import java.io.StringWriter; 34import java.net.URI; 35import java.util.Arrays; 36import java.util.Collections; 37import java.util.EnumSet; 38import java.util.List; 39import java.util.Set; 40import java.util.regex.Matcher; 41import java.util.regex.Pattern; 42 43import javax.tools.Diagnostic; 44import javax.tools.DocumentationTool; 45import javax.tools.DocumentationTool.DocumentationTask; 46import javax.tools.JavaFileObject; 47import javax.tools.SimpleJavaFileObject; 48import javax.tools.StandardJavaFileManager; 49import javax.tools.StandardLocation; 50import javax.tools.ToolProvider; 51import static javax.tools.Diagnostic.Kind.*; 52 53import com.sun.tools.javac.main.Main; 54 55public class DocLintTest { 56 public static void main(String... args) throws Exception { 57 new DocLintTest().run(); 58 } 59 60 DocumentationTool javadoc; 61 StandardJavaFileManager fm; 62 Iterable<? extends JavaFileObject> files; 63 64 final String code = 65 /* 01 */ "/** Class comment. */\n" + 66 /* 02 */ "public class Test {\n" + 67 /* 03 */ " /** Method comment. */\n" + 68 /* 04 */ " public void method() { }\n" + 69 /* 05 */ "\n" + 70 /* 06 */ " /** Syntax < error. */\n" + 71 /* 07 */ " private void syntaxError() { }\n" + 72 /* 08 */ "\n" + 73 /* 09 */ " /** @see DoesNotExist */\n" + 74 /* 10 */ " protected void referenceError() { }\n" + 75 /* 11 */ "\n" + 76 /* 12 */ " /** @return */\n" + 77 /* 13 */ " public int emptyReturn() { return 0; }\n" + 78 /* 14 */ "}\n"; 79 80 final String p1Code = 81 /* 01 */ "package p1;\n" + 82 /* 02 */ "public class P1Test {\n" + 83 /* 03 */ " /** Syntax < error. */\n" + 84 /* 04 */ " public void method() { }\n" + 85 /* 05 */ "}\n"; 86 87 final String p2Code = 88 /* 01 */ "package p2;\n" + 89 /* 02 */ "public class P2Test {\n" + 90 /* 03 */ " /** Syntax < error. */\n" + 91 /* 04 */ " public void method() { }\n" + 92 /* 05 */ "}\n"; 93 94 private final String rawDiags = "-XDrawDiagnostics"; 95 96 private enum Message { 97 // doclint messages 98 DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"), 99 DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"), 100 DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"), 101 102 DL_ERR_P1TEST(ERROR, "P1Test.java:3:16: compiler.err.proc.messager: malformed HTML"), 103 DL_ERR_P2TEST(ERROR, "P2Test.java:3:16: compiler.err.proc.messager: malformed HTML"), 104 105 // doclint messages when -XDrawDiagnostics is not in effect 106 DL_ERR9A(ERROR, "Test.java:9: error: reference not found"), 107 DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"), 108 109 // javadoc messages about bad content: these should only appear when doclint is disabled 110 JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"), 111 JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."), 112 113 // javadoc messages for bad options 114 OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"), 115 OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments"), 116 OPT_BADPACKAGEARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint/package option"); 117 118 final Diagnostic.Kind kind; 119 final String text; 120 121 static Message get(String text) { 122 for (Message m: values()) { 123 if (m.text.equals(text)) 124 return m; 125 } 126 return null; 127 } 128 129 Message(Diagnostic.Kind kind, String text) { 130 this.kind = kind; 131 this.text = text; 132 } 133 134 @Override 135 public String toString() { 136 return "[" + kind + ",\"" + text + "\"]"; 137 } 138 } 139 140 void run() throws Exception { 141 javadoc = ToolProvider.getSystemDocumentationTool(); 142 fm = javadoc.getStandardFileManager(null, null, null); 143 try { 144 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); 145 fm.setLocation(StandardLocation.CLASS_PATH, Collections.<File>emptyList()); 146 files = Arrays.asList(new TestJFO("Test.java", code)); 147 148 test(Collections.<String>emptyList(), 149 Main.Result.ERROR, 150 EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A)); 151 152 test(Arrays.asList(rawDiags), 153 Main.Result.ERROR, 154 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); 155 156// test(Arrays.asList("-Xdoclint:none"), 157// Main.Result.OK, 158// EnumSet.of(Message.JD_WRN10, Message.JD_WRN13)); 159 160 test(Arrays.asList(rawDiags, "-Xdoclint"), 161 Main.Result.ERROR, 162 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); 163 164 test(Arrays.asList(rawDiags, "-Xdoclint:all/public"), 165 Main.Result.ERROR, 166 EnumSet.of(Message.OPT_BADQUAL)); 167 168 test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"), 169 Main.Result.OK, 170 EnumSet.of(Message.DL_WRN12)); 171 172 test(Arrays.asList(rawDiags, "-Xdoclint:syntax"), 173 Main.Result.OK, 174 EnumSet.of(Message.DL_WRN12)); 175 176 test(Arrays.asList(rawDiags, "-private"), 177 Main.Result.ERROR, 178 EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12)); 179 180 test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"), 181 Main.Result.ERROR, 182 EnumSet.of(Message.DL_ERR6, Message.DL_WRN12)); 183 184 test(Arrays.asList(rawDiags, "-Xdoclint:reference"), 185 Main.Result.ERROR, 186 EnumSet.of(Message.DL_ERR9)); 187 188 test(Arrays.asList(rawDiags, "-Xdoclint:badarg"), 189 Main.Result.ERROR, 190 EnumSet.of(Message.OPT_BADARG)); 191 192 files = Arrays.asList(new TestJFO("p1/P1Test.java", p1Code), 193 new TestJFO("p2/P2Test.java", p2Code)); 194 195 test(Arrays.asList(rawDiags), 196 Main.Result.ERROR, 197 EnumSet.of(Message.DL_ERR_P1TEST, Message.DL_ERR_P2TEST)); 198 199 test(Arrays.asList(rawDiags, "-Xdoclint/package:p1"), 200 Main.Result.ERROR, 201 EnumSet.of(Message.DL_ERR_P1TEST)); 202 203 test(Arrays.asList(rawDiags, "-Xdoclint/package:*p"), 204 Main.Result.ERROR, 205 EnumSet.of(Message.OPT_BADPACKAGEARG)); 206 207 if (errors > 0) 208 throw new Exception(errors + " errors occurred"); 209 } finally { 210 fm.close(); 211 } 212 } 213 214 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { 215 System.err.println("test: " + opts); 216 StringWriter sw = new StringWriter(); 217 PrintWriter pw = new PrintWriter(sw); 218 try { 219 DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files); 220 boolean ok = t.call(); 221 pw.close(); 222 String out = sw.toString().replaceAll("[\r\n]+", "\n"); 223 if (!out.isEmpty()) 224 System.err.println(out); 225 if (ok && expectResult != Main.Result.OK) { 226 error("Compilation succeeded unexpectedly"); 227 } else if (!ok && expectResult != Main.Result.ERROR) { 228 error("Compilation failed unexpectedly"); 229 } else 230 check(out, expectMessages); 231 } catch (IllegalArgumentException e) { 232 System.err.println(e); 233 String expectOut = expectMessages.iterator().next().text; 234 if (expectResult != Main.Result.CMDERR) 235 error("unexpected exception caught"); 236 else if (!e.getMessage().equals(expectOut)) { 237 error("unexpected exception message: " 238 + e.getMessage() 239 + " expected: " + expectOut); 240 } 241 } 242 243// if (errors > 0) 244// throw new Error("stop"); 245 } 246 247 private void check(String out, Set<Message> expect) { 248 Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*"); 249 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); 250 Set<Message> found = EnumSet.noneOf(Message.class); 251 int e = 0, w = 0; 252 for (String line: out.split("[\r\n]+")) { 253 if (ignore.matcher(line).matches()) 254 continue; 255 256 Matcher s = stats.matcher(line); 257 if (s.matches()) { 258 int i = Integer.valueOf(s.group(1)); 259 if (s.group(2).equals("error")) 260 e++; 261 else 262 w++; 263 continue; 264 } 265 266 Message m = Message.get(line); 267 if (m == null) 268 error("Unexpected line: " + line); 269 else 270 found.add(m); 271 } 272 for (Message m: expect) { 273 if (!found.contains(m)) 274 error("expected message not found: " + m.text); 275 } 276 for (Message m: found) { 277 if (!expect.contains(m)) 278 error("unexpected message found: " + m.text); 279 } 280 } 281 282 void error(String msg) { 283 System.err.println("Error: " + msg); 284 errors++; 285 } 286 287 int errors; 288 289 class TestJFO extends SimpleJavaFileObject { 290 291 private final String content; 292 293 public TestJFO(String fileName, String content) { 294 super(URI.create(fileName), JavaFileObject.Kind.SOURCE); 295 this.content = content; 296 } 297 298 @Override 299 public CharSequence getCharContent(boolean ignoreEncoding) { 300 return content; 301 } 302 }; 303} 304