Wed, 01 Dec 2010 11:02:38 -0800
6851834: Javadoc doclet needs a structured approach to generate the output HTML.
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2010, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 /*
27 * @test
28 * @bug 6851834
29 * @summary This test verifies the HTML document generation for javadoc output.
30 * @author Bhavesh Patel
31 * @build TestHtmlDocument
32 * @run main TestHtmlDocument
33 */
35 import java.io.*;
36 import com.sun.tools.doclets.formats.html.markup.*;
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 */
42 public class TestHtmlDocument {
44 private static final String BUGID = "6851834";
45 private static final String BUGNAME = "TestHtmlDocument";
46 private static final String FS = System.getProperty("file.separator");
47 private static String srcdir = System.getProperty("test.src", ".");
49 // Entry point
50 public static void main(String[] args) throws IOException {
51 // Check whether the generated markup is same as the existing markup.
52 if (generateHtmlTree().equals(readFileToString(srcdir + FS + "testMarkup.html"))) {
53 System.out.println("\nTest passed for bug " + BUGID + " (" + BUGNAME + ")\n");
54 } else {
55 throw new Error("\nTest failed for bug " + BUGID + " (" + BUGNAME + ")\n");
56 }
57 }
59 // Generate the HTML output using the HTML document generation within doclet.
60 public static String generateHtmlTree() {
61 // Document type for the HTML document
62 DocType htmlDocType = DocType.Transitional();
63 HtmlTree html = new HtmlTree(HtmlTag.HTML);
64 HtmlTree head = new HtmlTree(HtmlTag.HEAD);
65 HtmlTree title = new HtmlTree(HtmlTag.TITLE);
66 // String content within the document
67 StringContent titleContent = new StringContent("Markup test");
68 title.addContent(titleContent);
69 head.addContent(title);
70 // Test META tag
71 HtmlTree meta = new HtmlTree(HtmlTag.META);
72 meta.addAttr(HtmlAttr.NAME, "keywords");
73 meta.addAttr(HtmlAttr.CONTENT, "testContent");
74 head.addContent(meta);
75 // Test invalid META tag
76 HtmlTree invmeta = new HtmlTree(HtmlTag.META);
77 head.addContent(invmeta);
78 // Test LINK tag
79 HtmlTree link = new HtmlTree(HtmlTag.LINK);
80 link.addAttr(HtmlAttr.REL, "testRel");
81 link.addAttr(HtmlAttr.HREF, "testLink.html");
82 head.addContent(link);
83 // Test invalid LINK tag
84 HtmlTree invlink = new HtmlTree(HtmlTag.LINK);
85 head.addContent(invlink);
86 html.addContent(head);
87 // Comment within the document
88 Comment bodyMarker = new Comment("======== START OF BODY ========");
89 html.addContent(bodyMarker);
90 HtmlTree body = new HtmlTree(HtmlTag.BODY);
91 Comment pMarker = new Comment("======== START OF PARAGRAPH ========");
92 body.addContent(pMarker);
93 HtmlTree p = new HtmlTree(HtmlTag.P);
94 StringContent bodyContent = new StringContent(
95 "This document is generated from sample source code and HTML " +
96 "files with examples of a wide variety of Java language constructs: packages, " +
97 "subclasses, subinterfaces, nested classes, nested interfaces," +
98 "inheriting from other packages, constructors, fields," +
99 "methods, and so forth. ");
100 p.addContent(bodyContent);
101 StringContent anchorContent = new StringContent("Click Here");
102 p.addContent(HtmlTree.A("testLink.html", anchorContent));
103 StringContent pContent = new StringContent(" to <test> out a link.");
104 p.addContent(pContent);
105 body.addContent(p);
106 HtmlTree p1 = new HtmlTree(HtmlTag.P);
107 // Test another version of A tag.
108 HtmlTree anchor = new HtmlTree(HtmlTag.A);
109 anchor.addAttr(HtmlAttr.HREF, "testLink.html");
110 anchor.addAttr(HtmlAttr.NAME, "Another version of a tag");
111 p1.addContent(anchor);
112 body.addContent(p1);
113 // Test for empty tags.
114 HtmlTree dl = new HtmlTree(HtmlTag.DL);
115 html.addContent(dl);
116 // Test for empty nested tags.
117 HtmlTree dlTree = new HtmlTree(HtmlTag.DL);
118 dlTree.addContent(new HtmlTree(HtmlTag.DT));
119 dlTree.addContent(new HtmlTree (HtmlTag.DD));
120 html.addContent(dlTree);
121 HtmlTree dlDisplay = new HtmlTree(HtmlTag.DL);
122 dlDisplay.addContent(new HtmlTree(HtmlTag.DT));
123 HtmlTree dd = new HtmlTree (HtmlTag.DD);
124 StringContent ddContent = new StringContent("Test DD");
125 dd.addContent(ddContent);
126 dlDisplay.addContent(dd);
127 body.addContent(dlDisplay);
128 StringContent emptyString = new StringContent("");
129 body.addContent(emptyString);
130 Comment emptyComment = new Comment("");
131 body.addContent(emptyComment);
132 HtmlTree hr = new HtmlTree(HtmlTag.HR);
133 body.addContent(hr);
134 html.addContent(body);
135 HtmlDocument htmlDoc = new HtmlDocument(htmlDocType, html);
136 return htmlDoc.toString();
137 }
139 // Read the file into a String
140 public static String readFileToString(String filename) throws IOException {
141 File file = new File(filename);
142 if ( !file.exists() ) {
143 System.out.println("\nFILE DOES NOT EXIST: " + filename);
144 }
145 BufferedReader in = new BufferedReader(new FileReader(file));
146 // Create an array of characters the size of the file
147 char[] allChars = new char[(int)file.length()];
148 // Read the characters into the allChars array
149 in.read(allChars, 0, (int)file.length());
150 in.close();
151 // Convert to a string
152 String allCharsString = new String(allChars);
153 return allCharsString;
154 }
155 }