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 31 import java.io.File; 32 import java.io.PrintWriter; 33 import java.io.StringWriter; 34 import java.net.URI; 35 import java.util.Arrays; 36 import java.util.Collections; 37 import java.util.EnumSet; 38 import java.util.List; 39 import java.util.Set; 40 import java.util.regex.Matcher; 41 import java.util.regex.Pattern; 42 43 import javax.tools.Diagnostic; 44 import javax.tools.DocumentationTool; 45 import javax.tools.DocumentationTool.DocumentationTask; 46 import javax.tools.JavaFileObject; 47 import javax.tools.SimpleJavaFileObject; 48 import javax.tools.StandardJavaFileManager; 49 import javax.tools.StandardLocation; 50 import javax.tools.ToolProvider; 51 import static javax.tools.Diagnostic.Kind.*; 52 53 import com.sun.tools.javac.main.Main; 54 55 public 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 files = Arrays.asList(new TestJFO("Test.java", code)); 146 147 test(Collections.<String>emptyList(), 148 Main.Result.ERROR, 149 EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A)); 150 151 test(Arrays.asList(rawDiags), 152 Main.Result.ERROR, 153 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); 154 155 // test(Arrays.asList("-Xdoclint:none"), 156 // Main.Result.OK, 157 // EnumSet.of(Message.JD_WRN10, Message.JD_WRN13)); 158 159 test(Arrays.asList(rawDiags, "-Xdoclint"), 160 Main.Result.ERROR, 161 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); 162 163 test(Arrays.asList(rawDiags, "-Xdoclint:all/public"), 164 Main.Result.ERROR, 165 EnumSet.of(Message.OPT_BADQUAL)); 166 167 test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"), 168 Main.Result.OK, 169 EnumSet.of(Message.DL_WRN12)); 170 171 test(Arrays.asList(rawDiags, "-Xdoclint:syntax"), 172 Main.Result.OK, 173 EnumSet.of(Message.DL_WRN12)); 174 175 test(Arrays.asList(rawDiags, "-private"), 176 Main.Result.ERROR, 177 EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12)); 178 179 test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"), 180 Main.Result.ERROR, 181 EnumSet.of(Message.DL_ERR6, Message.DL_WRN12)); 182 183 test(Arrays.asList(rawDiags, "-Xdoclint:reference"), 184 Main.Result.ERROR, 185 EnumSet.of(Message.DL_ERR9)); 186 187 test(Arrays.asList(rawDiags, "-Xdoclint:badarg"), 188 Main.Result.ERROR, 189 EnumSet.of(Message.OPT_BADARG)); 190 191 files = Arrays.asList(new TestJFO("p1/P1Test.java", p1Code), 192 new TestJFO("p2/P2Test.java", p2Code)); 193 194 test(Arrays.asList(rawDiags), 195 Main.Result.ERROR, 196 EnumSet.of(Message.DL_ERR_P1TEST, Message.DL_ERR_P2TEST)); 197 198 test(Arrays.asList(rawDiags, "-Xdoclint/package:p1"), 199 Main.Result.ERROR, 200 EnumSet.of(Message.DL_ERR_P1TEST)); 201 202 test(Arrays.asList(rawDiags, "-Xdoclint/package:*p"), 203 Main.Result.ERROR, 204 EnumSet.of(Message.OPT_BADPACKAGEARG)); 205 206 if (errors > 0) 207 throw new Exception(errors + " errors occurred"); 208 } finally { 209 fm.close(); 210 } 211 } 212 213 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { 214 System.err.println("test: " + opts); 215 StringWriter sw = new StringWriter(); 216 PrintWriter pw = new PrintWriter(sw); 217 try { 218 DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files); 219 boolean ok = t.call(); 220 pw.close(); 221 String out = sw.toString().replaceAll("[\r\n]+", "\n"); 222 if (!out.isEmpty()) 223 System.err.println(out); 224 if (ok && expectResult != Main.Result.OK) { 225 error("Compilation succeeded unexpectedly"); 226 } else if (!ok && expectResult != Main.Result.ERROR) { 227 error("Compilation failed unexpectedly"); 228 } else 229 check(out, expectMessages); 230 } catch (IllegalArgumentException e) { 231 System.err.println(e); 232 String expectOut = expectMessages.iterator().next().text; 233 if (expectResult != Main.Result.CMDERR) 234 error("unexpected exception caught"); 235 else if (!e.getMessage().equals(expectOut)) { 236 error("unexpected exception message: " 237 + e.getMessage() 238 + " expected: " + expectOut); 239 } 240 } 241 242 // if (errors > 0) 243 // throw new Error("stop"); 244 } 245 246 private void check(String out, Set<Message> expect) { 247 Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*"); 248 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); 249 Set<Message> found = EnumSet.noneOf(Message.class); 250 int e = 0, w = 0; 251 for (String line: out.split("[\r\n]+")) { 252 if (ignore.matcher(line).matches()) 253 continue; 254 255 Matcher s = stats.matcher(line); 256 if (s.matches()) { 257 int i = Integer.valueOf(s.group(1)); 258 if (s.group(2).equals("error")) 259 e++; 260 else 261 w++; 262 continue; 263 } 264 265 Message m = Message.get(line); 266 if (m == null) 267 error("Unexpected line: " + line); 268 else 269 found.add(m); 270 } 271 for (Message m: expect) { 272 if (!found.contains(m)) 273 error("expected message not found: " + m.text); 274 } 275 for (Message m: found) { 276 if (!expect.contains(m)) 277 error("unexpected message found: " + m.text); 278 } 279 } 280 281 void error(String msg) { 282 System.err.println("Error: " + msg); 283 errors++; 284 } 285 286 int errors; 287 288 class TestJFO extends SimpleJavaFileObject { 289 290 private final String content; 291 292 public TestJFO(String fileName, String content) { 293 super(URI.create(fileName), JavaFileObject.Kind.SOURCE); 294 this.content = content; 295 } 296 297 @Override 298 public CharSequence getCharContent(boolean ignoreEncoding) { 299 return content; 300 } 301 }; 302 }