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