test/com/sun/javadoc/testHtmlDocument/TestHtmlDocument.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1410
bfec2a1cc869
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial