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