1 /*
   2  * Copyright (c) 2011, 2016, 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 import java.util.Arrays;
  33 
  34 import java.util.HashSet;
  35 import java.util.ListIterator;
  36 import java.util.Locale;
  37 import java.util.Set;
  38 
  39 import javax.lang.model.SourceVersion;
  40 import javax.lang.model.element.Element;
  41 
  42 import com.sun.source.doctree.DocCommentTree;
  43 import com.sun.source.util.DocTrees;
  44 import jdk.javadoc.doclet.Doclet;
  45 import jdk.javadoc.doclet.Reporter;
  46 import jdk.javadoc.doclet.DocletEnvironment;
  47 
  48 
  49 public class Test implements Doclet {
  50     public static void main(String... args) throws Exception {
  51         new Test().run();
  52     }
  53 
  54     File referenceFile = new File("Foo.java");
  55 
  56     void run() throws Exception {
  57         test("<body>ABC      XYZ</body>");
  58         test("<body>ABC      XYZ</BODY>");
  59         test("<BODY>ABC      XYZ</body>");
  60         test("<BODY>ABC      XYZ</BODY>");
  61         test("<BoDy>ABC      XYZ</bOdY>");
  62         test("<body>ABC" + bigText(8192, 40) + "XYZ</body>");
  63 
  64         if (errors > 0)
  65             throw new Exception(errors + " errors occurred");
  66     }
  67 
  68     void test(String body) throws IOException {
  69         test(body, null);
  70     }
  71 
  72     void test(String body, String expectError) throws IOException {
  73         if (!referenceFile.exists()) {
  74             writeFile(referenceFile.getName(), "public class Foo {}");
  75         }
  76         String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
  77                          + "\"http://www.w3.org/TR/html4/loose.dtd\">";
  78         String headTag = "<head><title>Title </title></head>";
  79         String text = docType + "<html>" + headTag + body + "</html>";
  80         testNum++;
  81         System.err.println("test " + testNum);
  82         File file = writeFile("overview" + testNum + ".html", text);
  83         String thisClassName = Test.class.getName();
  84         String[] args = {
  85             "-bootclasspath",
  86                 System.getProperty("java.class.path")
  87                 + File.pathSeparator
  88                 + System.getProperty("sun.boot.class.path"),
  89             "-classpath", ".",
  90             "-docletpath", System.getProperty("test.classes"),
  91             "-doclet", thisClassName,
  92             "-package",
  93             "-overview", file.getPath(),
  94             "-sourcepath", ".",
  95             referenceFile.getPath()
  96         };
  97 
  98         StringWriter sw = new StringWriter();
  99         PrintWriter pw = new PrintWriter(sw);
 100         int rc = jdk.javadoc.internal.tool.Main.execute(args, pw);
 101         pw.close();
 102         String out = sw.toString();
 103         if (!out.isEmpty())
 104             System.err.println(out);
 105         System.err.println("javadoc exit: rc=" + rc);
 106 
 107         if (expectError == null) {
 108             if (rc != 0)
 109                 error("unexpected exit from javadoc; rc:" + rc);
 110         } else {
 111             if (!out.contains(expectError))
 112                 error("expected error text not found: " + expectError);
 113         }
 114     }
 115 
 116     String bigText(int lines, int lineLength) {
 117         StringBuilder sb = new StringBuilder();
 118         for (int i = 0; i < lineLength; i++)
 119             sb.append(String.valueOf(i % 10));
 120         sb.append("\n");
 121         String line = sb.toString();
 122         sb.setLength(0);
 123         for (int i = 0; i < lines; i++)
 124             sb.append(line);
 125         return sb.toString();
 126     }
 127 
 128     File writeFile(String path, String body) throws IOException {
 129         File f = new File(path);
 130         FileWriter out = new FileWriter(f);
 131         try {
 132             out.write(body);
 133         } finally {
 134             out.close();
 135         }
 136         return f;
 137     }
 138 
 139     void error(String msg) {
 140         System.err.println("Error: " + msg);
 141         errors++;
 142     }
 143 
 144     int testNum;
 145     int errors;
 146 
 147     public boolean run(DocletEnvironment root) {
 148         DocTrees docTrees = root.getDocTrees();
 149         System.out.println("classes:" + root.getIncludedClasses());
 150 
 151         Element klass = root.getIncludedClasses().iterator().next();
 152         String text = "";
 153         try {
 154             DocCommentTree dcTree = docTrees.getDocCommentTree(klass, overviewpath);
 155             text = dcTree.getFullBody().toString();
 156         } catch (IOException ioe) {
 157             throw new Error(ioe);
 158         }
 159 
 160         if (text.length() < 64)
 161             System.err.println("text: '" + text + "'");
 162         else
 163             System.err.println("text: '"
 164                     + text.substring(0, 20)
 165                     + "..."
 166                     + text.substring(text.length() - 20)
 167                     + "'");
 168         return text.startsWith("ABC") && text.endsWith("XYZ");
 169     }
 170 
 171     @Override
 172     public String getName() {
 173         return "Test";
 174     }
 175 
 176     private String overviewpath;
 177 
 178     @Override
 179     public Set<Option> getSupportedOptions() {
 180         Option[] options = {
 181             new Option() {
 182 
 183                 @Override
 184                 public int getArgumentCount() {
 185                     return 1;
 186                 }
 187 
 188                 @Override
 189                 public String getDescription() {
 190                     return "overview";
 191                 }
 192 
 193                 @Override
 194                 public Option.Kind getKind() {
 195                     return Option.Kind.STANDARD;
 196                 }
 197 
 198                 @Override
 199                 public String getName() {
 200                     return "overview";
 201                 }
 202 
 203                 @Override
 204                 public String getParameters() {
 205                     return "url";
 206                 }
 207 
 208                 @Override
 209                 public boolean matches(String option) {
 210                     String opt = option.startsWith("-") ? option.substring(1) : option;
 211                     return getName().equals(opt);
 212                 }
 213 
 214                 @Override
 215                 public boolean process(String option, ListIterator<String> arguments) {
 216                     if (matches(option)) {
 217                         overviewpath = arguments.next();
 218                     }
 219                     return true;
 220                 }
 221             }
 222         };
 223         return new HashSet<Option>(Arrays.asList(options));
 224     }
 225 
 226     @Override
 227     public SourceVersion getSupportedSourceVersion() {
 228         return SourceVersion.latest();
 229     }
 230 
 231     public void init(Locale locale, Reporter reporter) {
 232         return;
 233     }
 234 }