1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/doclint/DocLintTest.java Wed Dec 19 11:29:56 2012 +0000 1.3 @@ -0,0 +1,225 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 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 8004833 1.30 + * @summary Integrate doclint support into javac 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.List; 1.40 + 1.41 +import javax.tools.Diagnostic; 1.42 +import javax.tools.JavaCompiler; 1.43 +import javax.tools.JavaFileObject; 1.44 +import javax.tools.SimpleJavaFileObject; 1.45 +import javax.tools.StandardJavaFileManager; 1.46 +import javax.tools.StandardLocation; 1.47 +import javax.tools.ToolProvider; 1.48 +import static javax.tools.Diagnostic.Kind.*; 1.49 + 1.50 +import com.sun.source.util.JavacTask; 1.51 +import com.sun.tools.javac.main.Main; 1.52 +import java.util.EnumSet; 1.53 +import java.util.Set; 1.54 +import java.util.regex.Matcher; 1.55 +import java.util.regex.Pattern; 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 + JavaCompiler javac; 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 + /* 08 */ "\n" + 1.78 + /* 09 */ " /** @return */\n" + 1.79 + /* 10 */ " public int emptyReturn() { return 0; }\n" + 1.80 + /* 11 */ "}\n"; 1.81 + 1.82 + final String rawDiags = "-XDrawDiagnostics"; 1.83 + private enum Message { 1.84 + // doclint messages 1.85 + DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"), 1.86 + DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"), 1.87 + DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"), 1.88 + 1.89 + OPT_BADARG(ERROR, "invalid flag: -Xdoclint:badarg"); 1.90 + 1.91 + final Diagnostic.Kind kind; 1.92 + final String text; 1.93 + 1.94 + static Message get(String text) { 1.95 + for (Message m: values()) { 1.96 + if (m.text.equals(text)) 1.97 + return m; 1.98 + } 1.99 + return null; 1.100 + } 1.101 + 1.102 + Message(Diagnostic.Kind kind, String text) { 1.103 + this.kind = kind; 1.104 + this.text = text; 1.105 + } 1.106 + 1.107 + @Override 1.108 + public String toString() { 1.109 + return "[" + kind + ",\"" + text + "\"]"; 1.110 + } 1.111 + } 1.112 + void run() throws Exception { 1.113 + javac = ToolProvider.getSystemJavaCompiler(); 1.114 + fm = javac.getStandardFileManager(null, null, null); 1.115 + fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); 1.116 + file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) { 1.117 + @Override 1.118 + public CharSequence getCharContent(boolean ignoreEncoding) { 1.119 + return code; 1.120 + } 1.121 + }; 1.122 + 1.123 + test(Collections.<String>emptyList(), 1.124 + Main.Result.OK, 1.125 + EnumSet.noneOf(Message.class)); 1.126 + 1.127 + test(Arrays.asList("-Xdoclint:none"), 1.128 + Main.Result.OK, 1.129 + EnumSet.noneOf(Message.class)); 1.130 + 1.131 + test(Arrays.asList(rawDiags, "-Xdoclint"), 1.132 + Main.Result.ERROR, 1.133 + EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12)); 1.134 + 1.135 + test(Arrays.asList(rawDiags, "-Xdoclint:all/public"), 1.136 + Main.Result.OK, 1.137 + EnumSet.of(Message.DL_WRN12)); 1.138 + 1.139 + test(Arrays.asList(rawDiags, "-Xdoclint:syntax"), 1.140 + Main.Result.ERROR, 1.141 + EnumSet.of(Message.DL_ERR6, Message.DL_WRN12)); 1.142 + 1.143 + test(Arrays.asList(rawDiags, "-Xdoclint:reference"), 1.144 + Main.Result.ERROR, 1.145 + EnumSet.of(Message.DL_ERR9)); 1.146 + 1.147 + test(Arrays.asList(rawDiags, "-Xdoclint:badarg"), 1.148 + Main.Result.CMDERR, 1.149 + EnumSet.of(Message.OPT_BADARG)); 1.150 + 1.151 + if (errors > 0) 1.152 + throw new Exception(errors + " errors occurred"); 1.153 + } 1.154 + 1.155 + void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { 1.156 + System.err.println("test: " + opts); 1.157 + StringWriter sw = new StringWriter(); 1.158 + PrintWriter pw = new PrintWriter(sw); 1.159 + List<JavaFileObject> files = Arrays.asList(file); 1.160 + try { 1.161 + JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files); 1.162 + boolean ok = t.call(); 1.163 + pw.close(); 1.164 + String out = sw.toString().replaceAll("[\r\n]+", "\n"); 1.165 + if (!out.isEmpty()) 1.166 + System.err.println(out); 1.167 + if (ok && expectResult != Main.Result.OK) { 1.168 + error("Compilation succeeded unexpectedly"); 1.169 + } else if (!ok && expectResult != Main.Result.ERROR) { 1.170 + error("Compilation failed unexpectedly"); 1.171 + } else 1.172 + check(out, expectMessages); 1.173 + } catch (IllegalArgumentException e) { 1.174 + System.err.println(e); 1.175 + String expectOut = expectMessages.iterator().next().text; 1.176 + if (expectResult != Main.Result.CMDERR) 1.177 + error("unexpected exception caught"); 1.178 + else if (!e.getMessage().equals(expectOut)) { 1.179 + error("unexpected exception message: " 1.180 + + e.getMessage() 1.181 + + " expected: " + expectOut); 1.182 + } 1.183 + } 1.184 + 1.185 +// if (errors > 0) 1.186 +// throw new Error("stop"); 1.187 + } 1.188 + 1.189 + private void check(String out, Set<Message> expect) { 1.190 + Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); 1.191 + Set<Message> found = EnumSet.noneOf(Message.class); 1.192 + int e = 0, w = 0; 1.193 + if (!out.isEmpty()) { 1.194 + for (String line: out.split("[\r\n]+")) { 1.195 + Matcher s = stats.matcher(line); 1.196 + if (s.matches()) { 1.197 + int i = Integer.valueOf(s.group(1)); 1.198 + if (s.group(2).equals("error")) 1.199 + e++; 1.200 + else 1.201 + w++; 1.202 + continue; 1.203 + } 1.204 + 1.205 + Message m = Message.get(line); 1.206 + if (m == null) 1.207 + error("Unexpected line: " + line); 1.208 + else 1.209 + found.add(m); 1.210 + } 1.211 + } 1.212 + for (Message m: expect) { 1.213 + if (!found.contains(m)) 1.214 + error("expected message not found: " + m.text); 1.215 + } 1.216 + for (Message m: found) { 1.217 + if (!expect.contains(m)) 1.218 + error("unexpected message found: " + m.text); 1.219 + } 1.220 + } 1.221 + 1.222 + void error(String msg) { 1.223 + System.err.println("Error: " + msg); 1.224 + errors++; 1.225 + } 1.226 + 1.227 + int errors; 1.228 +}