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