bpatel@766: /* bpatel@766: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. bpatel@766: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. bpatel@766: * bpatel@766: * This code is free software; you can redistribute it and/or modify it bpatel@766: * under the terms of the GNU General Public License version 2 only, as bpatel@766: * published by the Free Software Foundation. Oracle designates this bpatel@766: * particular file as subject to the "Classpath" exception as provided bpatel@766: * by Oracle in the LICENSE file that accompanied this code. bpatel@766: * bpatel@766: * This code is distributed in the hope that it will be useful, but WITHOUT bpatel@766: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or bpatel@766: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License bpatel@766: * version 2 for more details (a copy is included in the LICENSE file that bpatel@766: * accompanied this code). bpatel@766: * bpatel@766: * You should have received a copy of the GNU General Public License version bpatel@766: * 2 along with this work; if not, write to the Free Software Foundation, bpatel@766: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. bpatel@766: * bpatel@766: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA bpatel@766: * or visit www.oracle.com if you need additional information or have any bpatel@766: * questions. bpatel@766: */ bpatel@766: bpatel@766: /* bpatel@766: * @test bpatel@766: * @bug 6851834 bpatel@766: * @summary This test verifies the HTML document generation for javadoc output. bpatel@766: * @author Bhavesh Patel bpatel@766: * @build TestHtmlDocument bpatel@766: * @run main TestHtmlDocument bpatel@766: */ bpatel@766: bpatel@766: import java.io.*; bpatel@766: import com.sun.tools.doclets.formats.html.markup.*; bpatel@766: bpatel@766: /** bpatel@766: * The class reads each file, complete with newlines, into a string to easily bpatel@766: * compare the existing markup with the generated markup. bpatel@766: */ bpatel@766: public class TestHtmlDocument { bpatel@766: bpatel@766: private static final String BUGID = "6851834"; bpatel@766: private static final String BUGNAME = "TestHtmlDocument"; bpatel@766: private static final String FS = System.getProperty("file.separator"); bpatel@766: private static String srcdir = System.getProperty("test.src", "."); bpatel@766: bpatel@766: // Entry point bpatel@766: public static void main(String[] args) throws IOException { bpatel@766: // Check whether the generated markup is same as the existing markup. bpatel@766: if (generateHtmlTree().equals(readFileToString(srcdir + FS + "testMarkup.html"))) { bpatel@766: System.out.println("\nTest passed for bug " + BUGID + " (" + BUGNAME + ")\n"); bpatel@766: } else { bpatel@766: throw new Error("\nTest failed for bug " + BUGID + " (" + BUGNAME + ")\n"); bpatel@766: } bpatel@766: } bpatel@766: bpatel@766: // Generate the HTML output using the HTML document generation within doclet. bpatel@766: public static String generateHtmlTree() { bpatel@766: // Document type for the HTML document bpatel@766: DocType htmlDocType = DocType.Transitional(); bpatel@766: HtmlTree html = new HtmlTree(HtmlTag.HTML); bpatel@766: HtmlTree head = new HtmlTree(HtmlTag.HEAD); bpatel@766: HtmlTree title = new HtmlTree(HtmlTag.TITLE); bpatel@766: // String content within the document bpatel@766: StringContent titleContent = new StringContent("Markup test"); bpatel@766: title.addContent(titleContent); bpatel@766: head.addContent(title); bpatel@766: // Test META tag bpatel@766: HtmlTree meta = new HtmlTree(HtmlTag.META); bpatel@766: meta.addAttr(HtmlAttr.NAME, "keywords"); bpatel@766: meta.addAttr(HtmlAttr.CONTENT, "testContent"); bpatel@766: head.addContent(meta); bpatel@766: // Test invalid META tag bpatel@766: HtmlTree invmeta = new HtmlTree(HtmlTag.META); bpatel@766: head.addContent(invmeta); bpatel@766: // Test LINK tag bpatel@766: HtmlTree link = new HtmlTree(HtmlTag.LINK); bpatel@766: link.addAttr(HtmlAttr.REL, "testRel"); bpatel@766: link.addAttr(HtmlAttr.HREF, "testLink.html"); bpatel@766: head.addContent(link); bpatel@766: // Test invalid LINK tag bpatel@766: HtmlTree invlink = new HtmlTree(HtmlTag.LINK); bpatel@766: head.addContent(invlink); bpatel@766: html.addContent(head); bpatel@766: // Comment within the document bpatel@766: Comment bodyMarker = new Comment("======== START OF BODY ========"); bpatel@766: html.addContent(bodyMarker); bpatel@766: HtmlTree body = new HtmlTree(HtmlTag.BODY); bpatel@766: Comment pMarker = new Comment("======== START OF PARAGRAPH ========"); bpatel@766: body.addContent(pMarker); bpatel@766: HtmlTree p = new HtmlTree(HtmlTag.P); bpatel@766: StringContent bodyContent = new StringContent( bpatel@766: "This document is generated from sample source code and HTML " + bpatel@766: "files with examples of a wide variety of Java language constructs: packages, " + bpatel@766: "subclasses, subinterfaces, nested classes, nested interfaces," + bpatel@766: "inheriting from other packages, constructors, fields," + bpatel@766: "methods, and so forth. "); bpatel@766: p.addContent(bodyContent); bpatel@766: StringContent anchorContent = new StringContent("Click Here"); bpatel@766: p.addContent(HtmlTree.A("testLink.html", anchorContent)); bpatel@766: StringContent pContent = new StringContent(" to out a link."); bpatel@766: p.addContent(pContent); bpatel@766: body.addContent(p); bpatel@766: HtmlTree p1 = new HtmlTree(HtmlTag.P); bpatel@766: // Test another version of A tag. bpatel@766: HtmlTree anchor = new HtmlTree(HtmlTag.A); bpatel@766: anchor.addAttr(HtmlAttr.HREF, "testLink.html"); bpatel@766: anchor.addAttr(HtmlAttr.NAME, "Another version of a tag"); bpatel@766: p1.addContent(anchor); bpatel@766: body.addContent(p1); bpatel@766: // Test for empty tags. bpatel@766: HtmlTree dl = new HtmlTree(HtmlTag.DL); bpatel@766: html.addContent(dl); bpatel@766: // Test for empty nested tags. bpatel@766: HtmlTree dlTree = new HtmlTree(HtmlTag.DL); bpatel@766: dlTree.addContent(new HtmlTree(HtmlTag.DT)); bpatel@766: dlTree.addContent(new HtmlTree (HtmlTag.DD)); bpatel@766: html.addContent(dlTree); bpatel@766: HtmlTree dlDisplay = new HtmlTree(HtmlTag.DL); bpatel@766: dlDisplay.addContent(new HtmlTree(HtmlTag.DT)); bpatel@766: HtmlTree dd = new HtmlTree (HtmlTag.DD); bpatel@766: StringContent ddContent = new StringContent("Test DD"); bpatel@766: dd.addContent(ddContent); bpatel@766: dlDisplay.addContent(dd); bpatel@766: body.addContent(dlDisplay); bpatel@766: StringContent emptyString = new StringContent(""); bpatel@766: body.addContent(emptyString); bpatel@766: Comment emptyComment = new Comment(""); bpatel@766: body.addContent(emptyComment); bpatel@766: HtmlTree hr = new HtmlTree(HtmlTag.HR); bpatel@766: body.addContent(hr); bpatel@766: html.addContent(body); bpatel@766: HtmlDocument htmlDoc = new HtmlDocument(htmlDocType, html); bpatel@766: return htmlDoc.toString(); bpatel@766: } bpatel@766: bpatel@766: // Read the file into a String bpatel@766: public static String readFileToString(String filename) throws IOException { bpatel@766: File file = new File(filename); bpatel@766: if ( !file.exists() ) { bpatel@766: System.out.println("\nFILE DOES NOT EXIST: " + filename); bpatel@766: } bpatel@766: BufferedReader in = new BufferedReader(new FileReader(file)); bpatel@766: // Create an array of characters the size of the file bpatel@766: char[] allChars = new char[(int)file.length()]; bpatel@766: // Read the characters into the allChars array bpatel@766: in.read(allChars, 0, (int)file.length()); bpatel@766: in.close(); bpatel@766: // Convert to a string bpatel@766: String allCharsString = new String(allChars); bpatel@766: return allCharsString; bpatel@766: } bpatel@766: }