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 6964914
  27  * @summary javadoc does not output number of warnings using user written doclet
  28  * @modules jdk.javadoc
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 
  34 import com.sun.javadoc.Doclet;
  35 import com.sun.javadoc.RootDoc;
  36 
  37 public class TestUserDoclet extends Doclet {
  38     public static void main(String... args) throws Exception {
  39         new TestUserDoclet().run();
  40     }
  41 
  42     static final String docletWarning = "warning from test doclet";
  43 
  44     /** Main doclet method. */
  45     public static boolean start(RootDoc root) {
  46         root.printWarning(null, docletWarning);
  47         return true;
  48     }
  49 
  50     /** Main test method. */
  51     void run() throws Exception {
  52         File javaHome = new File(System.getProperty("java.home"));
  53         File javadoc = new File(new File(javaHome, "bin"), "javadoc");
  54         File testSrc = new File(System.getProperty("test.src"));
  55         File testClasses = new File(System.getProperty("test.classes"));
  56 
  57         // run javadoc in separate process to ensure doclet executed under
  58         // normal user conditions w.r.t. classloader
  59         String thisClassName = TestUserDoclet.class.getName();
  60         List<String> cmdArgs = new ArrayList<>();
  61         cmdArgs.add(javadoc.getPath());
  62         cmdArgs.addAll(Arrays.asList(
  63                 "-doclet", thisClassName,
  64                 "-docletpath", testClasses.getPath(),
  65                 new File(testSrc, thisClassName + ".java").getPath()
  66         ));
  67         Process p = new ProcessBuilder()
  68             .command(cmdArgs)
  69             .redirectErrorStream(true)
  70             .start();
  71 
  72         int actualDocletWarnCount = 0;
  73         int reportedDocletWarnCount = 0;
  74         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  75         try {
  76             String line;
  77             while ((line = in.readLine()) != null) {
  78                 System.err.println(line);
  79                 if (line.contains(docletWarning))
  80                     actualDocletWarnCount++;
  81                 if (line.matches("[0-9]+ warning(s)?"))
  82                     reportedDocletWarnCount =
  83                             Integer.valueOf(line.substring(0, line.indexOf(" ")));
  84             }
  85         } finally {
  86             in.close();
  87         }
  88         int rc = p.waitFor();
  89         if (rc != 0)
  90             System.err.println("javadoc failed, rc:" + rc);
  91 
  92         int expectedDocletWarnCount = 1;
  93         checkEqual("actual", actualDocletWarnCount, "expected", expectedDocletWarnCount);
  94         checkEqual("actual", actualDocletWarnCount, "reported", reportedDocletWarnCount);
  95     }
  96 
  97     void checkEqual(String l1, int i1, String l2, int i2) throws Exception {
  98         if (i1 != i2)
  99             throw new Exception(l1 + " warn count, " + i1 + ", does not match "
 100                         + l2 + " warn count, " + i2);
 101     }
 102 
 103 }