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