DocCommentTreeApiTester.java revision 3074:522e516b8a83
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 8132096 27 * @summary test the APIs in the DocTree interface 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.file 30 * jdk.compiler/com.sun.tools.javac.tree 31 * jdk.compiler/com.sun.tools.javac.util 32 * @compile ../DocCommentTester.java DocCommentTreeApiTester.java 33 * @run main DocCommentTreeApiTester 34 */ 35 36import java.io.BufferedReader; 37import java.io.File; 38import java.io.IOException; 39import java.io.PrintWriter; 40import java.io.Reader; 41import java.io.StringWriter; 42import java.nio.file.Files; 43import java.nio.file.Path; 44import java.text.BreakIterator; 45import java.util.ArrayList; 46import java.util.Collections; 47import java.util.List; 48import java.util.Locale; 49 50import javax.lang.model.element.Element; 51import javax.tools.FileObject; 52import javax.tools.JavaFileObject; 53import javax.tools.StandardJavaFileManager; 54 55import com.sun.source.doctree.DocTree; 56import com.sun.source.doctree.DocCommentTree; 57import com.sun.source.util.DocTrees; 58import com.sun.source.util.JavacTask; 59import com.sun.tools.javac.api.JavacTool; 60import com.sun.tools.javac.tree.DocPretty; 61 62public class DocCommentTreeApiTester { 63 64 private static final String MARKER_START = "<!-- EXPECT_START"; 65 private static final String MARKER_END = "EXPECT_END -->"; 66 67 private static final String testSrc = System.getProperty("test.src", "."); 68 69 private static final JavacTool javac = JavacTool.create(); 70 71 private static final DocCommentTester.ASTChecker.Printer printer = 72 new DocCommentTester.ASTChecker.Printer(); 73 int pass; 74 int fail; 75 76 public DocCommentTreeApiTester() { 77 pass = 0; 78 fail = 0; 79 } 80 81 public static void main(String... args) throws Exception { 82 DocCommentTreeApiTester test = new DocCommentTreeApiTester(); 83 try { 84 // test getting a DocTree from an element 85 test.runElementAndBreakIteratorTests("OverviewTest.java", "OverviewTest test."); 86 87 // test relative paths in a class within a package 88 test.runRelativePathTest("pkg/Anchor.java", "package.html"); 89 90 // tests files relative path in an unnamed package 91 test.runRelativePathTest("OverviewTest.java", "overview0.html"); 92 93 // test for correct parsing using valid and some invalid html tags 94 for (int i = 0; i < 7; i++) { 95 String hname = "overview" + i + ".html"; 96 test.runFileObjectTest(hname); 97 } 98 99 } finally { 100 test.status(); 101 } 102 } 103 void status() throws Exception { 104 System.err.println("pass:" + pass + " fail: " + fail); 105 if (fail > 0) { 106 throw new Exception("Fails"); 107 } 108 } 109 110 /** 111 * Tests getting a DocCommentTree from an element, as well 112 * as test if break iterator setter/getter works correctly. 113 * 114 * @param javaFileName a test file to be processed 115 * @param expected the expected output 116 * @throws java.io.IOException 117 */ 118 public void runElementAndBreakIteratorTests(String javaFileName, String expected) throws IOException { 119 List<File> javaFiles = new ArrayList<>(); 120 javaFiles.add(new File(testSrc, javaFileName)); 121 122 List<File> dirs = new ArrayList<>(); 123 dirs.add(new File(testSrc)); 124 125 try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) { 126 fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs); 127 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles); 128 129 final JavacTask t = javac.getTask(null, fm, null, null, null, fos); 130 final DocTrees trees = DocTrees.instance(t); 131 132 Iterable<? extends Element> elements = t.analyze(); 133 134 Element klass = elements.iterator().next(); 135 DocCommentTree dcTree = trees.getDocCommentTree(klass); 136 137 List<? extends DocTree> firstSentence = dcTree.getFirstSentence(); 138 StringWriter sw = new StringWriter(); 139 DocPretty pretty = new DocPretty(sw); 140 pretty.print(firstSentence); 141 check("getDocCommentTree(Element)", expected, sw.toString()); 142 143 BreakIterator bi = BreakIterator.getSentenceInstance(Locale.FRENCH); 144 trees.setBreakIterator(bi); 145 BreakIterator nbi = trees.getBreakIterator(); 146 if (bi.equals(nbi)) { 147 pass++; 148 check("getDocCommentTree(Element) with BreakIterator", expected, sw.toString()); 149 } else { 150 fail++; 151 System.err.println("BreakIterators don't match"); 152 } 153 } 154 } 155 /** 156 * Tests DocTrees.getDocCommentTree(Element e, String relpath) using relative path. 157 * 158 * @param javaFileName the reference java file 159 * @param fileName the relative html file 160 * @throws java.lang.Exception ouch 161 */ 162 public void runRelativePathTest(String javaFileName, String fileName) throws Exception { 163 List<File> javaFiles = new ArrayList<>(); 164 javaFiles.add(new File(testSrc, javaFileName)); 165 166 List<File> dirs = new ArrayList<>(); 167 dirs.add(new File(testSrc)); 168 169 try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) { 170 fm.setLocation(javax.tools.StandardLocation.SOURCE_PATH, dirs); 171 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles); 172 173 final JavacTask t = javac.getTask(null, fm, null, null, null, fos); 174 final DocTrees trees = DocTrees.instance(t); 175 176 Iterable<? extends Element> elements = t.analyze(); 177 178 Element klass = elements.iterator().next(); 179 180 DocCommentTree dcTree = trees.getDocCommentTree(klass, fileName); 181 StringWriter sw = new StringWriter(); 182 printer.print(dcTree, sw); 183 String found = sw.toString(); 184 185 FileObject htmlFo = fm.getFileForInput(javax.tools.StandardLocation.SOURCE_PATH, 186 t.getElements().getPackageOf(klass).getQualifiedName().toString(), 187 fileName); 188 189 String expected = getExpected(htmlFo.openReader(true)); 190 astcheck(fileName, expected, found); 191 } 192 } 193 194 /** 195 * Tests DocTrees.getDocCommentTree(FileObject fo). 196 * 197 * @param htmlfileName the file to be parsed 198 * @throws Exception when an error occurs. 199 */ 200 public void runFileObjectTest(String htmlfileName) throws Exception { 201 List<File> javaFiles = Collections.emptyList(); 202 203 List<File> otherFiles = new ArrayList<>(); 204 otherFiles.add(new File(testSrc, htmlfileName)); 205 206 try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) { 207 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles); 208 Iterable<? extends JavaFileObject> others = fm.getJavaFileObjectsFromFiles(otherFiles); 209 210 final JavacTask t = javac.getTask(null, fm, null, null, null, fos); 211 final DocTrees trees = DocTrees.instance(t); 212 213 StringWriter sw = new StringWriter(); 214 215 printer.print(trees.getDocCommentTree(others.iterator().next()), sw); 216 String found = sw.toString(); 217 String expected = getExpected(otherFiles.iterator().next().toPath()); 218 astcheck(otherFiles.toString(), expected, found); 219 } 220 } 221 222 void astcheck(String testinfo, String expected, String found) { 223 System.err.print("ASTChecker: " + testinfo); 224 check0(expected, found); 225 } 226 void check(String testinfo, String expected, String found) { 227 System.err.print(testinfo); 228 check0(expected, found); 229 } 230 void check0(String expected, String found) { 231 if (expected.equals(found)) { 232 pass++; 233 System.err.println(" PASS"); 234 } else { 235 fail++; 236 System.err.println(" FAILED"); 237 System.err.println("Expect:\n" + expected); 238 System.err.println("Found:\n" + found); 239 } 240 } 241 242 String getExpected(Reader inrdr) throws IOException { 243 BufferedReader rdr = new BufferedReader(inrdr); 244 List<String> lines = new ArrayList<>(); 245 String line = rdr.readLine(); 246 while (line != null) { 247 lines.add(line); 248 line = rdr.readLine(); 249 } 250 return getExpected(lines); 251 } 252 253 String getExpected(Path p) throws IOException { 254 return getExpected(Files.readAllLines(p)); 255 } 256 257 String getExpected(List<String> lines) { 258 boolean start = false; 259 StringWriter sw = new StringWriter(); 260 PrintWriter out = new PrintWriter(sw); 261 for (String line : lines) { 262 if (!start) { 263 start = line.startsWith(MARKER_START); 264 continue; 265 } 266 if (line.startsWith(MARKER_END)) { 267 out.flush(); 268 return sw.toString(); 269 } 270 out.println(line); 271 } 272 return out.toString() + "Warning: html comment end not found"; 273 } 274} 275 276