1/*
2 * Copyright (c) 2011, 2016, 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 6227454
27 * @summary package.html and overview.html may not be read fully
28 * @modules jdk.javadoc/jdk.javadoc.internal.tool
29 */
30
31import java.io.*;
32import java.util.ArrayList;
33import java.util.Arrays;
34
35import java.util.HashSet;
36import java.util.List;
37import java.util.Locale;
38import java.util.Set;
39
40import javax.lang.model.SourceVersion;
41import javax.lang.model.element.Element;
42import javax.lang.model.util.ElementFilter;
43
44import com.sun.source.doctree.DocCommentTree;
45import com.sun.source.util.DocTrees;
46import jdk.javadoc.doclet.Doclet;
47import jdk.javadoc.doclet.Reporter;
48import jdk.javadoc.doclet.DocletEnvironment;
49
50public class Test implements Doclet {
51    public static void main(String... args) throws Exception {
52        new Test().run();
53    }
54
55    File referenceFile = new File("Foo.java");
56
57    void run() throws Exception {
58        test("<body>ABC      XYZ</body>");
59        test("<body>ABC      XYZ</BODY>");
60        test("<BODY>ABC      XYZ</body>");
61        test("<BODY>ABC      XYZ</BODY>");
62        test("<BoDy>ABC      XYZ</bOdY>");
63        test("<body>ABC" + bigText(8192, 40) + "XYZ</body>");
64
65        if (errors > 0)
66            throw new Exception(errors + " errors occurred");
67    }
68
69    void test(String body) throws IOException {
70        test(body, null);
71    }
72
73    void test(String body, String expectError) throws IOException {
74        if (!referenceFile.exists()) {
75            writeFile(referenceFile.getName(), "public class Foo {}");
76        }
77        String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
78                         + "\"http://www.w3.org/TR/html4/loose.dtd\">";
79        String headTag = "<head><title>Title </title></head>";
80        String text = docType + "<html>" + headTag + body + "</html>";
81        testNum++;
82        System.err.println("test " + testNum);
83        File file = writeFile("overview" + testNum + ".html", text);
84        String thisClassName = Test.class.getName();
85        File testSrc = new File(System.getProperty("test.src"));
86        String[] args = {
87            "-classpath", ".",
88            "-package",
89            "-overview", file.getPath(),
90            new File(testSrc, thisClassName + ".java").getPath()
91        };
92
93        StringWriter sw = new StringWriter();
94        PrintWriter pw = new PrintWriter(sw);
95        int rc = jdk.javadoc.internal.tool.Main.execute(args, pw);
96        pw.close();
97        String out = sw.toString();
98        if (!out.isEmpty())
99            System.err.println(out);
100        System.err.println("javadoc exit: rc=" + rc);
101
102        if (expectError == null) {
103            if (rc != 0)
104                error("unexpected exit from javadoc; rc:" + rc);
105        } else {
106            if (!out.contains(expectError))
107                error("expected error text not found: " + expectError);
108        }
109    }
110
111    String bigText(int lines, int lineLength) {
112        StringBuilder sb = new StringBuilder();
113        for (int i = 0; i < lineLength; i++)
114            sb.append(String.valueOf(i % 10));
115        sb.append("\n");
116        String line = sb.toString();
117        sb.setLength(0);
118        for (int i = 0; i < lines; i++)
119            sb.append(line);
120        return sb.toString();
121    }
122
123    File writeFile(String path, String body) throws IOException {
124        File f = new File(path);
125        FileWriter out = new FileWriter(f);
126        try {
127            out.write(body);
128        } finally {
129            out.close();
130        }
131        return f;
132    }
133
134    void error(String msg) {
135        System.err.println("Error: " + msg);
136        errors++;
137    }
138
139    int testNum;
140    int errors;
141
142    public boolean run(DocletEnvironment root) {
143        DocTrees docTrees = root.getDocTrees();
144        System.out.println("classes:" + ElementFilter.typesIn(root.getIncludedElements()));
145
146        Element klass = ElementFilter.typesIn(root.getIncludedElements()).iterator().next();
147        String text = "";
148        try {
149            DocCommentTree dcTree = docTrees.getDocCommentTree(klass, overviewpath);
150            text = dcTree.getFullBody().toString();
151        } catch (IOException ioe) {
152            throw new Error(ioe);
153        }
154
155        if (text.length() < 64)
156            System.err.println("text: '" + text + "'");
157        else
158            System.err.println("text: '"
159                    + text.substring(0, 20)
160                    + "..."
161                    + text.substring(text.length() - 20)
162                    + "'");
163        return text.startsWith("ABC") && text.endsWith("XYZ");
164    }
165
166    @Override
167    public String getName() {
168        return "Test";
169    }
170
171    private String overviewpath;
172
173    @Override
174    public Set<Option> getSupportedOptions() {
175        Option[] options = {
176            new Option() {
177
178                @Override
179                public int getArgumentCount() {
180                    return 1;
181                }
182
183                @Override
184                public String getDescription() {
185                    return "overview";
186                }
187
188                @Override
189                public Option.Kind getKind() {
190                    return Option.Kind.STANDARD;
191                }
192
193                @Override
194                public List<String> getNames() {
195                    List<String> out = new ArrayList<>();
196                    out.add("overview");
197                    return out;
198                }
199
200                @Override
201                public String getParameters() {
202                    return "url";
203                }
204
205                @Override
206                public boolean process(String option, List<String> arguments) {
207                    overviewpath = arguments.get(0);
208                    return true;
209                }
210            }
211        };
212        return new HashSet<>(Arrays.asList(options));
213    }
214
215    @Override
216    public SourceVersion getSupportedSourceVersion() {
217        return SourceVersion.latest();
218    }
219
220    public void init(Locale locale, Reporter reporter) {
221        return;
222    }
223}
224