1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javadoc/doclint/DocLintTest.java Thu Jan 10 19:38:57 2013 -0800 1.3 @@ -0,0 +1,251 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8004834 1.30 + * @summary Add doclint support into javadoc 1.31 + */ 1.32 + 1.33 +import java.io.File; 1.34 +import java.io.PrintWriter; 1.35 +import java.io.StringWriter; 1.36 +import java.net.URI; 1.37 +import java.util.Arrays; 1.38 +import java.util.Collections; 1.39 +import java.util.EnumSet; 1.40 +import java.util.List; 1.41 +import java.util.Set; 1.42 +import java.util.regex.Matcher; 1.43 +import java.util.regex.Pattern; 1.44 + 1.45 +import javax.tools.Diagnostic; 1.46 +import javax.tools.DocumentationTool; 1.47 +import javax.tools.DocumentationTool.DocumentationTask; 1.48 +import javax.tools.JavaFileObject; 1.49 +import javax.tools.SimpleJavaFileObject; 1.50 +import javax.tools.StandardJavaFileManager; 1.51 +import javax.tools.StandardLocation; 1.52 +import javax.tools.ToolProvider; 1.53 +import static javax.tools.Diagnostic.Kind.*; 1.54 + 1.55 +import com.sun.tools.javac.main.Main; 1.56 + 1.57 +public class DocLintTest { 1.58 + public static void main(String... args) throws Exception { 1.59 + new DocLintTest().run(); 1.60 + } 1.61 + 1.62 + DocumentationTool javadoc; 1.63 + StandardJavaFileManager fm; 1.64 + JavaFileObject file; 1.65 + 1.66 + final String code = 1.67 + /* 01 */ "/** Class comment. */\n" + 1.68 + /* 02 */ "public class Test {\n" + 1.69 + /* 03 */ " /** Method comment. */\n" + 1.70 + /* 04 */ " public void method() { }\n" + 1.71 + /* 05 */ "\n" + 1.72 + /* 06 */ " /** Syntax < error. */\n" + 1.73 + /* 07 */ " private void syntaxError() { }\n" + 1.74 + /* 08 */ "\n" + 1.75 + /* 09 */ " /** @see DoesNotExist */\n" + 1.76 + /* 10 */ " protected void referenceError() { }\n" + 1.77 + /* 11 */ "\n" + 1.78 + /* 12 */ " /** @return */\n" + 1.79 + /* 13 */ " public int emptyReturn() { return 0; }\n" + 1.80 + /* 14 */ "}\n"; 1.81 + 1.82 + private final String rawDiags = "-XDrawDiagnostics"; 1.83 + 1.84 + private enum Message { 1.85 + // doclint messages 1.86 + DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"), 1.87 + DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"), 1.88 + DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"), 1.89 + 1.90 + // doclint messages when -XDrawDiagnostics is not in effect 1.91 + DL_ERR9A(ERROR, "Test.java:9: error: reference not found"), 1.92 + DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"), 1.93 + 1.94 + // javadoc messages about bad content: these should only appear when doclint is disabled 1.95 + JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"), 1.96 + JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."), 1.97 + 1.98 + // javadoc messages for bad options 1.99 + OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"), 1.100 + OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments"); 1.101 + 1.102 + final Diagnostic.Kind kind; 1.103 + final String text; 1.104 + 1.105 + static Message get(String text) { 1.106 + for (Message m: values()) { 1.107 + if (m.text.equals(text)) 1.108 + return m; 1.109 + } 1.110 + return null; 1.111 + } 1.112 + 1.113 + Message(Diagnostic.Kind kind, String text) { 1.114 + this.kind = kind; 1.115 + this.text = text; 1.116 + } 1.117 + 1.118 + @Override 1.119 + public String toString() { 1.120 + return "[" + kind + ",\"" + text + "\"]"; 1.121 + } 1.122 + } 1.123 + 1.124 + void run() throws Exception { 1.125 + javadoc = ToolProvider.getSystemDocumentationTool(); 1.126 + fm = javadoc.getStandardFileManager(null, null, null); 1.127 + fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); 1.128 + file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) { 1.129 + @Override 1.130 + public CharSequence getCharContent(boolean ignoreEncoding) { 1.131 + return code; 1.132 + } 1.133 + }; 1.134 + 1.135 + test(Collections.<String>emptyList(), 1.136 + Main.Result.ERROR, 1.137 + EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A)); 1.138 + 1.139 + test(Arrays.asList(rawDiags), 1.140 + Main.Result.ERROR, 1.141 + EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); 1.142 + 1.143 + test(Arrays.asList("-Xdoclint:none"), 1.144 + Main.Result.OK, 1.145 + EnumSet.of(Message.JD_WRN10, Message.JD_WRN13)); 1.146 + 1.147 + test(Arrays.asList(rawDiags, "-Xdoclint"), 1.148 + Main.Result.ERROR, 1.149 + EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); 1.150 + 1.151 + test(Arrays.asList(rawDiags, "-Xdoclint:all/public"), 1.152 + Main.Result.ERROR, 1.153 + EnumSet.of(Message.OPT_BADQUAL)); 1.154 + 1.155 + test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"), 1.156 + Main.Result.OK, 1.157 + EnumSet.of(Message.DL_WRN12)); 1.158 + 1.159 + test(Arrays.asList(rawDiags, "-Xdoclint:syntax"), 1.160 + Main.Result.OK, 1.161 + EnumSet.of(Message.DL_WRN12)); 1.162 + 1.163 + test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"), 1.164 + Main.Result.ERROR, 1.165 + EnumSet.of(Message.DL_ERR6, Message.DL_WRN12)); 1.166 + 1.167 + test(Arrays.asList(rawDiags, "-Xdoclint:reference"), 1.168 + Main.Result.ERROR, 1.169 + EnumSet.of(Message.DL_ERR9)); 1.170 + 1.171 + test(Arrays.asList(rawDiags, "-Xdoclint:badarg"), 1.172 + Main.Result.ERROR, 1.173 + EnumSet.of(Message.OPT_BADARG)); 1.174 + 1.175 + if (errors > 0) 1.176 + throw new Exception(errors + " errors occurred"); 1.177 + } 1.178 + 1.179 + void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { 1.180 + System.err.println("test: " + opts); 1.181 + StringWriter sw = new StringWriter(); 1.182 + PrintWriter pw = new PrintWriter(sw); 1.183 + List<JavaFileObject> files = Arrays.asList(file); 1.184 + try { 1.185 + DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files); 1.186 + boolean ok = t.call(); 1.187 + pw.close(); 1.188 + String out = sw.toString().replaceAll("[\r\n]+", "\n"); 1.189 + if (!out.isEmpty()) 1.190 + System.err.println(out); 1.191 + if (ok && expectResult != Main.Result.OK) { 1.192 + error("Compilation succeeded unexpectedly"); 1.193 + } else if (!ok && expectResult != Main.Result.ERROR) { 1.194 + error("Compilation failed unexpectedly"); 1.195 + } else 1.196 + check(out, expectMessages); 1.197 + } catch (IllegalArgumentException e) { 1.198 + System.err.println(e); 1.199 + String expectOut = expectMessages.iterator().next().text; 1.200 + if (expectResult != Main.Result.CMDERR) 1.201 + error("unexpected exception caught"); 1.202 + else if (!e.getMessage().equals(expectOut)) { 1.203 + error("unexpected exception message: " 1.204 + + e.getMessage() 1.205 + + " expected: " + expectOut); 1.206 + } 1.207 + } 1.208 + 1.209 +// if (errors > 0) 1.210 +// throw new Error("stop"); 1.211 + } 1.212 + 1.213 + private void check(String out, Set<Message> expect) { 1.214 + Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*"); 1.215 + Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); 1.216 + Set<Message> found = EnumSet.noneOf(Message.class); 1.217 + int e = 0, w = 0; 1.218 + for (String line: out.split("[\r\n]+")) { 1.219 + if (ignore.matcher(line).matches()) 1.220 + continue; 1.221 + 1.222 + Matcher s = stats.matcher(line); 1.223 + if (s.matches()) { 1.224 + int i = Integer.valueOf(s.group(1)); 1.225 + if (s.group(2).equals("error")) 1.226 + e++; 1.227 + else 1.228 + w++; 1.229 + continue; 1.230 + } 1.231 + 1.232 + Message m = Message.get(line); 1.233 + if (m == null) 1.234 + error("Unexpected line: " + line); 1.235 + else 1.236 + found.add(m); 1.237 + } 1.238 + for (Message m: expect) { 1.239 + if (!found.contains(m)) 1.240 + error("expected message not found: " + m.text); 1.241 + } 1.242 + for (Message m: found) { 1.243 + if (!expect.contains(m)) 1.244 + error("unexpected message found: " + m.text); 1.245 + } 1.246 + } 1.247 + 1.248 + void error(String msg) { 1.249 + System.err.println("Error: " + msg); 1.250 + errors++; 1.251 + } 1.252 + 1.253 + int errors; 1.254 +}