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