1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8132096 8157611
* @summary test the APIs in the DocTree interface
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.file
* jdk.compiler/com.sun.tools.javac.tree
* jdk.compiler/com.sun.tools.javac.util
* @compile ../DocCommentTester.java DocCommentTreeApiTester.java
* @run main DocCommentTreeApiTester
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.util.Elements;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTrees;
import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.tree.DocPretty;
public class DocCommentTreeApiTester {
private static final String MARKER_START = "<!-- EXPECT_START";
private static final String MARKER_END = "EXPECT_END -->";
private static final String testSrc = System.getProperty("test.src", ".");
private static final JavacTool javac = JavacTool.create();
private static final DocCommentTester.ASTChecker.Printer printer =
new DocCommentTester.ASTChecker.Printer();
int pass;
int fail;
public DocCommentTreeApiTester() {
pass = 0;
fail = 0;
}
public static void main(String... args) throws Exception {
DocCommentTreeApiTester test = new DocCommentTreeApiTester();
try {
// test getting a DocTree from an element
test.runElementAndBreakIteratorTests("OverviewTest.java", "OverviewTest test.");
// test relative paths in a class within a package
test.runRelativePathTest("pkg/Anchor.java", "package.html");
// tests files relative path in an unnamed package
test.runRelativePathTest("OverviewTest.java", "overview0.html");
// tests doctreepath using package element and package.html
test.runDocTreePath("pkg/Anchor.java", "package.html");
// test for correct parsing using valid and some invalid html tags
for (int i = 0; i < 7; i++) {
String hname = "overview" + i + ".html";
test.runFileObjectTest(hname);
}
} finally {
test.status();
}
}
void status() throws Exception {
System.err.println("pass:" + pass + " fail: " + fail);
if (fail > 0) {
throw new Exception("Fails");
}
}
/**
* Tests getting a DocCommentTree from an element, as well
* as test if break iterator setter/getter works correctly.
*
* @param javaFileName a test file to be processed
* @param expected the expected output
* @throws java.io.IOException
*/
public void runElementAndBreakIteratorTests(String javaFileName, String expected) throws IOException {
List<File> javaFiles = new ArrayList<>();
javaFiles.add(new File(testSrc, javaFileName));
List<File> dirs = new ArrayList<>();
dirs.add(new File(testSrc));
try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs);
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);
final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
final DocTrees trees = DocTrees.instance(t);
Iterable<? extends Element> elements = t.analyze();
Element klass = elements.iterator().next();
DocCommentTree dcTree = trees.getDocCommentTree(klass);
List<? extends DocTree> firstSentence = dcTree.getFirstSentence();
StringWriter sw = new StringWriter();
DocPretty pretty = new DocPretty(sw);
pretty.print(firstSentence);
check("getDocCommentTree(Element)", expected, sw.toString());
BreakIterator bi = BreakIterator.getSentenceInstance(Locale.FRENCH);
trees.setBreakIterator(bi);
BreakIterator nbi = trees.getBreakIterator();
if (bi.equals(nbi)) {
pass++;
check("getDocCommentTree(Element) with BreakIterator", expected, sw.toString());
} else {
fail++;
System.err.println("BreakIterators don't match");
}
}
}
/**
* Tests DocTrees.getDocCommentTree(Element e, String relpath) using relative path.
*
* @param javaFileName the reference java file
* @param fileName the relative html file
* @throws java.lang.Exception ouch
*/
public void runRelativePathTest(String javaFileName, String fileName) throws Exception {
List<File> javaFiles = new ArrayList<>();
javaFiles.add(new File(testSrc, javaFileName));
List<File> dirs = new ArrayList<>();
dirs.add(new File(testSrc));
try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs);
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);
final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
final DocTrees trees = DocTrees.instance(t);
Iterable<? extends Element> elements = t.analyze();
Element klass = elements.iterator().next();
DocCommentTree dcTree = trees.getDocCommentTree(klass, fileName);
StringWriter sw = new StringWriter();
printer.print(dcTree, sw);
String found = sw.toString();
FileObject htmlFo = fm.getFileForInput(javax.tools.StandardLocation.SOURCE_PATH,
t.getElements().getPackageOf(klass).getQualifiedName().toString(),
fileName);
String expected = getExpected(htmlFo.openReader(true));
astcheck(fileName, expected, found);
}
}
/**
* Tests DocTrees.getDocCommentTree(FileObject fo).
*
* @param htmlfileName the file to be parsed
* @throws Exception when an error occurs.
*/
public void runFileObjectTest(String htmlfileName) throws Exception {
List<File> javaFiles = Collections.emptyList();
List<File> otherFiles = new ArrayList<>();
otherFiles.add(new File(testSrc, htmlfileName));
try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);
Iterable<? extends JavaFileObject> others = fm.getJavaFileObjectsFromFiles(otherFiles);
final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
final DocTrees trees = DocTrees.instance(t);
StringWriter sw = new StringWriter();
printer.print(trees.getDocCommentTree(others.iterator().next()), sw);
String found = sw.toString();
String expected = getExpected(otherFiles.iterator().next().toPath());
astcheck(otherFiles.toString(), expected, found);
}
}
/**
* Tests DocTrees.getDocTreePath(PackageElement p, FileObject fo).
*
* @param javaFileName the java anchor file
* @param pkgFileName the package file name
* @throws Exception e if something goes awry
*/
public void runDocTreePath(String javaFileName, String pkgFileName) throws Exception {
List<File> javaFiles = new ArrayList<>();
javaFiles.add(new File(testSrc, javaFileName));
List<File> dirs = new ArrayList<>();
dirs.add(new File(testSrc));
try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs);
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);
final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
final DocTrees trees = DocTrees.instance(t);
final Elements elementUtils = t.getElements();
Iterable<? extends Element> elements = t.analyze();
Element klass = elements.iterator().next();
PackageElement pkg = elementUtils.getPackageOf(klass);
FileObject htmlFo = fm.getFileForInput(javax.tools.StandardLocation.SOURCE_PATH,
t.getElements().getPackageOf(klass).getQualifiedName().toString(),
"package.html");
System.out.println();
DocTreePath treePath = trees.getDocTreePath(htmlFo, pkg);
DocCommentTree dcTree = treePath.getDocComment();
StringWriter sw = new StringWriter();
printer.print(dcTree, sw);
String found = sw.toString();
String expected = getExpected(htmlFo.openReader(true));
astcheck(pkgFileName, expected, found);
}
}
void astcheck(String testinfo, String expected, String found) {
System.err.print("ASTChecker: " + testinfo);
check0(expected, found);
}
void check(String testinfo, String expected, String found) {
System.err.print(testinfo);
check0(expected, found);
}
void check0(String expected, String found) {
if (expected.equals(found)) {
pass++;
System.err.println(" PASS");
} else {
fail++;
System.err.println(" FAILED");
System.err.println("Expect:\n" + expected);
System.err.println("Found:\n" + found);
}
}
String getExpected(Reader inrdr) throws IOException {
BufferedReader rdr = new BufferedReader(inrdr);
List<String> lines = new ArrayList<>();
String line = rdr.readLine();
while (line != null) {
lines.add(line);
line = rdr.readLine();
}
return getExpected(lines);
}
String getExpected(Path p) throws IOException {
return getExpected(Files.readAllLines(p));
}
String getExpected(List<String> lines) {
boolean start = false;
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
for (String line : lines) {
if (!start) {
start = line.startsWith(MARKER_START);
continue;
}
if (line.startsWith(MARKER_END)) {
out.flush();
return sw.toString();
}
out.println(line);
}
return out.toString() + "Warning: html comment end not found";
}
}
|