1/* 2 * Copyright (c) 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 8071851 27 * @summary Test the -Xdoclint/package option 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.List; 37 38import javax.tools.Diagnostic; 39import javax.tools.JavaCompiler; 40import javax.tools.JavaFileObject; 41import javax.tools.SimpleJavaFileObject; 42import javax.tools.StandardJavaFileManager; 43import javax.tools.StandardLocation; 44import javax.tools.ToolProvider; 45import static javax.tools.Diagnostic.Kind.*; 46 47import com.sun.source.util.JavacTask; 48import com.sun.tools.javac.main.Main; 49import java.util.ArrayList; 50import java.util.EnumSet; 51import java.util.Set; 52import java.util.regex.Matcher; 53import java.util.regex.Pattern; 54 55public class IncludePackagesTest { 56 public static void main(String... args) throws Exception { 57 new IncludePackagesTest().run(); 58 } 59 60 JavaCompiler javac; 61 StandardJavaFileManager fm; 62 List<JavaFileObject> files; 63 64 final String[] sources = new String[] { 65 "p1/p1T.java", 66 "package p1;\n" + 67 "/** Syntax < error. */\n" + 68 "public class p1T {\n" + 69 "}\n", 70 "p1/sp1/p1sp1T.java", 71 "package p1.sp1;\n" + 72 "/** Syntax < error. */\n" + 73 "public class p1sp1T {\n" + 74 "}\n", 75 "p1/sp1/sp2/p1sp1sp2T.java", 76 "package p1.sp1.sp2;\n" + 77 "/** Syntax < error. */\n" + 78 "public class p1sp1sp2T {\n" + 79 "}\n", 80 "p2/p2T.java", 81 "package p2;\n" + 82 "/** Syntax < error. */\n" + 83 "public class p2T {\n" + 84 "}\n", 85 "Default.java", 86 "/** Syntax < error. */\n" + 87 "public class Default {\n" + 88 "}\n", 89 }; 90 91 final String rawDiags = "-XDrawDiagnostics"; 92 private enum Message { 93 // doclint messages 94 p1T(ERROR, "p1T.java:2:12: compiler.err.proc.messager: malformed HTML"), 95 p1sp1T(ERROR, "p1sp1T.java:2:12: compiler.err.proc.messager: malformed HTML"), 96 p1sp1sp2T(ERROR, "p1sp1sp2T.java:2:12: compiler.err.proc.messager: malformed HTML"), 97 p2T(ERROR, "p2T.java:2:12: compiler.err.proc.messager: malformed HTML"), 98 Default(ERROR, "Default.java:1:12: compiler.err.proc.messager: malformed HTML"), 99 INVALID_PACKAGE_ERROR(ERROR, "invalid flag: -Xdoclint/package:wrong+package"); 100 101 final Diagnostic.Kind kind; 102 final String text; 103 104 static Message get(String text) { 105 for (Message m: values()) { 106 if (m.text.equals(text)) 107 return m; 108 } 109 return null; 110 } 111 112 Message(Diagnostic.Kind kind, String text) { 113 this.kind = kind; 114 this.text = text; 115 } 116 117 @Override 118 public String toString() { 119 return "[" + kind + ",\"" + text + "\"]"; 120 } 121 } 122 void run() throws Exception { 123 javac = ToolProvider.getSystemJavaCompiler(); 124 fm = javac.getStandardFileManager(null, null, null); 125 try { 126 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); 127 files = new ArrayList<>(); 128 129 for (int si = 0; si < sources.length; si += 2) { 130 files.add(new JFOImpl(sources[si], sources[si + 1])); 131 } 132 133 test(Arrays.asList(rawDiags, "-Xdoclint"), 134 Main.Result.ERROR, 135 EnumSet.of(Message.p1T, Message.p1sp1T, Message.p1sp1sp2T, 136 Message.p2T, Message.Default)); 137 138 test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:p1"), 139 Main.Result.ERROR, 140 EnumSet.of(Message.p1T)); 141 142 test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:p1.*"), 143 Main.Result.ERROR, 144 EnumSet.of(Message.p1sp1T, Message.p1sp1sp2T)); 145 146 test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:p1.*,-p1.sp1"), 147 Main.Result.ERROR, 148 EnumSet.of(Message.p1sp1sp2T)); 149 150 test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:-p1.sp1"), 151 Main.Result.ERROR, 152 EnumSet.of(Message.p1T, Message.p1sp1sp2T, Message.p2T, Message.Default)); 153 154 test(Arrays.asList(rawDiags, "-Xdoclint", "-Xdoclint/package:wrong+package"), 155 Main.Result.CMDERR, 156 EnumSet.of(Message.INVALID_PACKAGE_ERROR)); 157 158 if (errors > 0) 159 throw new Exception(errors + " errors occurred"); 160 } finally { 161 fm.close(); 162 } 163 } 164 165 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { 166 System.err.println("test: " + opts); 167 StringWriter sw = new StringWriter(); 168 PrintWriter pw = new PrintWriter(sw); 169 try { 170 JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files); 171 boolean ok = t.call(); 172 pw.close(); 173 String out = sw.toString().replaceAll("[\r\n]+", "\n"); 174 if (!out.isEmpty()) 175 System.err.println(out); 176 if (ok && expectResult != Main.Result.OK) { 177 error("Compilation succeeded unexpectedly"); 178 } else if (!ok && expectResult != Main.Result.ERROR) { 179 error("Compilation failed unexpectedly"); 180 } else 181 check(out, expectMessages); 182 } catch (IllegalArgumentException e) { 183 System.err.println(e); 184 String expectOut = expectMessages.iterator().next().text; 185 if (expectResult != Main.Result.CMDERR) 186 error("unexpected exception caught"); 187 else if (!e.getMessage().equals(expectOut)) { 188 error("unexpected exception message: " 189 + e.getMessage() 190 + " expected: " + expectOut); 191 } 192 } 193 } 194 195 private void check(String out, Set<Message> expect) { 196 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); 197 Set<Message> found = EnumSet.noneOf(Message.class); 198 int e = 0, w = 0; 199 if (!out.isEmpty()) { 200 for (String line: out.split("[\r\n]+")) { 201 Matcher s = stats.matcher(line); 202 if (s.matches()) { 203 int i = Integer.valueOf(s.group(1)); 204 if (s.group(2).equals("error")) 205 e++; 206 else 207 w++; 208 continue; 209 } 210 211 Message m = Message.get(line); 212 if (m == null) 213 error("Unexpected line: " + line); 214 else 215 found.add(m); 216 } 217 } 218 for (Message m: expect) { 219 if (!found.contains(m)) 220 error("expected message not found: " + m.text); 221 } 222 for (Message m: found) { 223 if (!expect.contains(m)) 224 error("unexpected message found: " + m.text); 225 } 226 } 227 228 void error(String msg) { 229 System.err.println("Error: " + msg); 230 errors++; 231 } 232 233 int errors; 234 235 class JFOImpl extends SimpleJavaFileObject { 236 237 private final String code; 238 239 public JFOImpl(String fileName, String code) { 240 super(URI.create(fileName), JavaFileObject.Kind.SOURCE); 241 this.code = code; 242 } 243 244 @Override 245 public CharSequence getCharContent(boolean ignoreEncoding) { 246 return code; 247 } 248 } 249} 250