1 /*
   2  * Copyright (c) 2011, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6227454
  27  * @summary package.html and overview.html may not be read fully
  28  * @modules jdk.javadoc
  29  */
  30 
  31 import java.io.*;
  32 
  33 import com.sun.javadoc.Doclet;
  34 import com.sun.javadoc.RootDoc;
  35 
  36 public class Test extends Doclet {
  37     public static void main(String... args) throws Exception {
  38         new Test().run();
  39     }
  40 
  41     void run() throws Exception {
  42         test("<body>ABC      XYZ</body>");
  43         test("<body>ABC      XYZ</BODY>");
  44         test("<BODY>ABC      XYZ</body>");
  45         test("<BODY>ABC      XYZ</BODY>");
  46         test("<BoDy>ABC      XYZ</bOdY>");
  47         test("      ABC      XYZ</bOdY>", "Body tag missing from HTML");
  48         test("<body>ABC      XYZ       ", "Close body tag missing from HTML");
  49         test("      ABC      XYZ       ", "Body tag missing from HTML");
  50         test("<body>ABC" + bigText(8192, 40) + "XYZ</body>");
  51 
  52         if (errors > 0)
  53             throw new Exception(errors + " errors occurred");
  54     }
  55 
  56     void test(String body) throws IOException {
  57         test(body, null);
  58     }
  59 
  60     void test(String body, String expectError) throws IOException {
  61         String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
  62                          + "\"http://www.w3.org/TR/html4/loose.dtd\">";
  63         String headTag = "<head><title>Title </title></head>";
  64         String text = docType + "<html>" + headTag + body + "</html>";
  65         testNum++;
  66         System.err.println("test " + testNum);
  67         File file = writeFile("overview" + testNum + ".html", text);
  68         String thisClassName = Test.class.getName();
  69         File testSrc = new File(System.getProperty("test.src"));
  70         String[] args = {
  71             "-bootclasspath",
  72                 System.getProperty("java.class.path")
  73                 + File.pathSeparator
  74                 + System.getProperty("sun.boot.class.path"),
  75             "-classpath", ".",
  76             "-package",
  77             "-overview", file.getPath(),
  78             new File(testSrc, thisClassName + ".java").getPath()
  79         };
  80 
  81         StringWriter sw = new StringWriter();
  82         PrintWriter pw = new PrintWriter(sw);
  83         int rc = com.sun.tools.javadoc.Main.execute(
  84                 "javadoc",
  85                 pw, pw, pw,
  86                 thisClassName,
  87                 args);
  88         pw.close();
  89         String out = sw.toString();
  90         if (!out.isEmpty())
  91             System.err.println(out);
  92         System.err.println("javadoc exit: rc=" + rc);
  93 
  94         if (expectError == null) {
  95             if (rc != 0)
  96                 error("unexpected exit from javadoc; rc:" + rc);
  97         } else {
  98             if (!out.contains(expectError))
  99                 error("expected error text not found: " + expectError);
 100         }
 101     }
 102 
 103     String bigText(int lines, int lineLength) {
 104         StringBuilder sb = new StringBuilder();
 105         for (int i = 0; i < lineLength; i++)
 106             sb.append(String.valueOf(i % 10));
 107         sb.append("\n");
 108         String line = sb.toString();
 109         sb.setLength(0);
 110         for (int i = 0; i < lines; i++)
 111             sb.append(line);
 112         return sb.toString();
 113     }
 114 
 115     File writeFile(String path, String body) throws IOException {
 116         File f = new File(path);
 117         FileWriter out = new FileWriter(f);
 118         try {
 119             out.write(body);
 120         } finally {
 121             out.close();
 122         }
 123         return f;
 124     }
 125 
 126     void error(String msg) {
 127         System.err.println("Error: " + msg);
 128         errors++;
 129     }
 130 
 131     int testNum;
 132     int errors;
 133 
 134     public static boolean start(RootDoc root) {
 135         String text = root.commentText();
 136         if (text.length() < 64)
 137             System.err.println("text: '" + text + "'");
 138         else
 139             System.err.println("text: '"
 140                     + text.substring(0, 20)
 141                     + "..."
 142                     + text.substring(text.length() - 20)
 143                     + "'");
 144         return text.startsWith("ABC") && text.endsWith("XYZ");
 145     }
 146 }