Path: blob/master/test/langtools/jdk/javadoc/tool/6227454/Test.java
40971 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 622745426* @summary package.html and overview.html may not be read fully27* @modules jdk.javadoc/jdk.javadoc.internal.tool28*/2930import java.io.*;31import java.util.ArrayList;32import java.util.Arrays;3334import java.util.HashSet;35import java.util.List;36import java.util.Locale;37import java.util.Set;3839import javax.lang.model.SourceVersion;40import javax.lang.model.element.Element;41import javax.lang.model.util.ElementFilter;4243import com.sun.source.doctree.DocCommentTree;44import com.sun.source.util.DocTrees;45import jdk.javadoc.doclet.Doclet;46import jdk.javadoc.doclet.Reporter;47import jdk.javadoc.doclet.DocletEnvironment;4849public class Test implements Doclet {50public static void main(String... args) throws Exception {51new Test().run();52}5354File referenceFile = new File("Foo.java");5556void run() throws Exception {57test("<body>ABC XYZ</body>");58test("<body>ABC XYZ</BODY>");59test("<BODY>ABC XYZ</body>");60test("<BODY>ABC XYZ</BODY>");61test("<BoDy>ABC XYZ</bOdY>");62test("<body>ABC" + bigText(8192, 40) + "XYZ</body>");6364if (errors > 0)65throw new Exception(errors + " errors occurred");66}6768void test(String body) throws IOException {69test(body, null);70}7172void test(String body, String expectError) throws IOException {73if (!referenceFile.exists()) {74writeFile(referenceFile.getName(), "public class Foo {}");75}76String docType = """77<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""";78String headTag = "<head><title>Title </title></head>";79String text = docType + "<html>" + headTag + body + "</html>";80testNum++;81System.err.println("test " + testNum);82File file = writeFile("overview" + testNum + ".html", text);83String thisClassName = Test.class.getName();84File testSrc = new File(System.getProperty("test.src"));85String[] args = {86"-classpath", ".",87"-package",88"-overview", file.getPath(),89new File(testSrc, thisClassName + ".java").getPath()90};9192StringWriter sw = new StringWriter();93PrintWriter pw = new PrintWriter(sw);94int rc = jdk.javadoc.internal.tool.Main.execute(args, pw);95pw.close();96String out = sw.toString();97if (!out.isEmpty())98System.err.println(out);99System.err.println("javadoc exit: rc=" + rc);100101if (expectError == null) {102if (rc != 0)103error("unexpected exit from javadoc; rc:" + rc);104} else {105if (!out.contains(expectError))106error("expected error text not found: " + expectError);107}108}109110String bigText(int lines, int lineLength) {111StringBuilder sb = new StringBuilder();112for (int i = 0; i < lineLength; i++)113sb.append(String.valueOf(i % 10));114sb.append("\n");115String line = sb.toString();116sb.setLength(0);117for (int i = 0; i < lines; i++)118sb.append(line);119return sb.toString();120}121122File writeFile(String path, String body) throws IOException {123File f = new File(path);124FileWriter out = new FileWriter(f);125try {126out.write(body);127} finally {128out.close();129}130return f;131}132133void error(String msg) {134System.err.println("Error: " + msg);135errors++;136}137138int testNum;139int errors;140141public boolean run(DocletEnvironment root) {142DocTrees docTrees = root.getDocTrees();143System.out.println("classes:" + ElementFilter.typesIn(root.getIncludedElements()));144145Element klass = ElementFilter.typesIn(root.getIncludedElements()).iterator().next();146String text = "";147try {148DocCommentTree dcTree = docTrees.getDocCommentTree(klass, overviewpath);149text = dcTree.getFullBody().toString();150} catch (IOException ioe) {151throw new Error(ioe);152}153154if (text.length() < 64)155System.err.println("text: '" + text + "'");156else157System.err.println("text: '"158+ text.substring(0, 20)159+ "..."160+ text.substring(text.length() - 20)161+ "'");162return text.startsWith("ABC") && text.endsWith("XYZ");163}164165@Override166public String getName() {167return "Test";168}169170private String overviewpath;171172@Override173public Set<Option> getSupportedOptions() {174Option[] options = {175new Option() {176177@Override178public int getArgumentCount() {179return 1;180}181182@Override183public String getDescription() {184return "overview";185}186187@Override188public Option.Kind getKind() {189return Option.Kind.STANDARD;190}191192@Override193public List<String> getNames() {194List<String> out = new ArrayList<>();195out.add("overview");196return out;197}198199@Override200public String getParameters() {201return "url";202}203204@Override205public boolean process(String option, List<String> arguments) {206overviewpath = arguments.get(0);207return true;208}209}210};211return new HashSet<>(Arrays.asList(options));212}213214@Override215public SourceVersion getSupportedSourceVersion() {216return SourceVersion.latest();217}218219public void init(Locale locale, Reporter reporter) {220return;221}222}223224225