TestHtmlDocument.java revision 3233:b5d08bc0d224
1/*
2 * Copyright (c) 2010, 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 6851834
27 * @summary This test verifies the HTML document generation for javadoc output.
28 * @library ../lib
29 * @modules jdk.javadoc/com.sun.tools.doclets.formats.html.markup
30 *          jdk.javadoc/com.sun.tools.doclets.internal.toolkit
31 * @build JavadocTester
32 * @author Bhavesh Patel
33 * @run main TestHtmlDocument
34 */
35
36
37import jdk.javadoc.internal.doclets.formats.html.markup.*;
38
39import static jdk.javadoc.internal.doclets.toolkit.util.DocletConstants.NL;
40
41/**
42 * The class reads each file, complete with newlines, into a string to easily
43 * compare the existing markup with the generated markup.
44 */
45public class TestHtmlDocument extends JavadocTester {
46
47    // Entry point
48    public static void main(String... args) throws Exception {
49        TestHtmlDocument tester = new TestHtmlDocument();
50        tester.runTests();
51    }
52
53    @Test
54    void test() {
55        checking("markup");
56        // Check whether the generated markup is same as the existing markup.
57        String expected = readFile(testSrc, "testMarkup.html").replace("\n", NL);
58        String actual = generateHtmlTree();
59        if (actual.equals(expected)) {
60            passed("");
61        } else {
62            failed("expected content in " + testSrc("testMarkup.html") + "\n"
63                + "Actual output:\n"
64                + actual);
65        }
66    }
67
68    // Generate the HTML output using the HTML document generation within doclet.
69    public static String generateHtmlTree() {
70        // Document type for the HTML document
71        DocType htmlDocType = DocType.TRANSITIONAL;
72        HtmlTree html = new HtmlTree(HtmlTag.HTML);
73        HtmlTree head = new HtmlTree(HtmlTag.HEAD);
74        HtmlTree title = new HtmlTree(HtmlTag.TITLE);
75        // String content within the document
76        StringContent titleContent = new StringContent("Markup test");
77        title.addContent(titleContent);
78        head.addContent(title);
79        // Test META tag
80        HtmlTree meta = new HtmlTree(HtmlTag.META);
81        meta.addAttr(HtmlAttr.NAME, "keywords");
82        meta.addAttr(HtmlAttr.CONTENT, "testContent");
83        head.addContent(meta);
84        // Test invalid META tag
85        HtmlTree invmeta = new HtmlTree(HtmlTag.META);
86        head.addContent(invmeta);
87        // Test LINK tag
88        HtmlTree link = new HtmlTree(HtmlTag.LINK);
89        link.addAttr(HtmlAttr.REL, "testRel");
90        link.addAttr(HtmlAttr.HREF, "testLink.html");
91        head.addContent(link);
92        // Test invalid LINK tag
93        HtmlTree invlink = new HtmlTree(HtmlTag.LINK);
94        head.addContent(invlink);
95        html.addContent(head);
96        // Comment within the document
97        Comment bodyMarker = new Comment("======== START OF BODY ========");
98        html.addContent(bodyMarker);
99        HtmlTree body = new HtmlTree(HtmlTag.BODY);
100        Comment pMarker = new Comment("======== START OF PARAGRAPH ========");
101        body.addContent(pMarker);
102        HtmlTree p = new HtmlTree(HtmlTag.P);
103        StringContent bodyContent = new StringContent(
104                "This document is generated from sample source code and HTML " +
105                "files with examples of a wide variety of Java language constructs: packages, " +
106                "subclasses, subinterfaces, nested classes, nested interfaces," +
107                "inheriting from other packages, constructors, fields," +
108                "methods, and so forth. ");
109        p.addContent(bodyContent);
110        StringContent anchorContent = new StringContent("Click Here");
111        p.addContent(HtmlTree.A("testLink.html", anchorContent));
112        StringContent pContent = new StringContent(" to <test> out a link.");
113        p.addContent(pContent);
114        body.addContent(p);
115        HtmlTree p1 = new HtmlTree(HtmlTag.P);
116        // Test another version of A tag.
117        HtmlTree anchor = new HtmlTree(HtmlTag.A);
118        anchor.addAttr(HtmlAttr.HREF, "testLink.html");
119        anchor.addAttr(HtmlAttr.ID, "Another version of a tag");
120        p1.addContent(anchor);
121        body.addContent(p1);
122        // Test for empty tags.
123        HtmlTree dl = new HtmlTree(HtmlTag.DL);
124        html.addContent(dl);
125        // Test for empty nested tags.
126        HtmlTree dlTree = new HtmlTree(HtmlTag.DL);
127        dlTree.addContent(new HtmlTree(HtmlTag.DT));
128        dlTree.addContent(new HtmlTree (HtmlTag.DD));
129        html.addContent(dlTree);
130        HtmlTree dlDisplay = new HtmlTree(HtmlTag.DL);
131        dlDisplay.addContent(new HtmlTree(HtmlTag.DT));
132        HtmlTree dd = new HtmlTree (HtmlTag.DD);
133        StringContent ddContent = new StringContent("Test DD");
134        dd.addContent(ddContent);
135        dlDisplay.addContent(dd);
136        body.addContent(dlDisplay);
137        StringContent emptyString = new StringContent("");
138        body.addContent(emptyString);
139        Comment emptyComment = new Comment("");
140        body.addContent(emptyComment);
141        HtmlTree hr = new HtmlTree(HtmlTag.HR);
142        body.addContent(hr);
143        html.addContent(body);
144        HtmlDocument htmlDoc = new HtmlDocument(htmlDocType, html);
145        return htmlDoc.toString();
146    }
147}
148