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