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