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 
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.List;
  37 import java.util.Locale;
  38 import java.util.Set;
  39 
  40 import javax.lang.model.SourceVersion;
  41 
  42 import jdk.javadoc.doclet.Doclet;
  43 import jdk.javadoc.doclet.Reporter;
  44 import jdk.javadoc.doclet.DocletEnvironment;
  45 
  46 public class TestUserDoclet implements Doclet {
  47     public static void main(String... args) throws Exception {
  48         new TestUserDoclet().run();
  49     }
  50 
  51     static final String docletWarning = "warning from test doclet";
  52 
  53     /** Main doclet method. */
  54     public boolean run(DocletEnvironment root) {
  55         reporter.print(javax.tools.Diagnostic.Kind.WARNING, docletWarning);
  56         return true;
  57     }
  58 
  59     /** Main test method. */
  60     void run() throws Exception {
  61         File javaHome = new File(System.getProperty("java.home"));
  62         File javadoc = new File(new File(javaHome, "bin"), "javadoc");
  63         File testSrc = new File(System.getProperty("test.src"));
  64         File testClasses = new File(System.getProperty("test.classes"));
  65 
  66         // run javadoc in separate process to ensure doclet executed under
  67         // normal user conditions w.r.t. classloader
  68         String thisClassName = TestUserDoclet.class.getName();
  69         List<String> cmdArgs = new ArrayList<>();
  70         cmdArgs.add(javadoc.getPath());
  71         int i = 0;
  72         String prop;
  73         while ((prop = System.getProperty("jdk.launcher.patch." + (i++))) != null) {
  74             cmdArgs.add("-J-Xpatch:" + prop);
  75         }
  76         cmdArgs.addAll(Arrays.asList(
  77                 "-doclet", thisClassName,
  78                 "-docletpath", testClasses.getPath(),
  79                 new File(testSrc, thisClassName + ".java").getPath()
  80         ));
  81         Process p = new ProcessBuilder()
  82             .command(cmdArgs)
  83             .redirectErrorStream(true)
  84             .start();
  85 
  86         int actualDocletWarnCount = 0;
  87         int reportedDocletWarnCount = 0;
  88         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  89         try {
  90             String line;
  91             while ((line = in.readLine()) != null) {
  92                 System.err.println(line);
  93                 if (line.contains(docletWarning))
  94                     actualDocletWarnCount++;
  95                 if (line.matches("[0-9]+ warning(s)?"))
  96                     reportedDocletWarnCount =
  97                             Integer.valueOf(line.substring(0, line.indexOf(" ")));
  98             }
  99         } finally {
 100             in.close();
 101         }
 102         int rc = p.waitFor();
 103         if (rc != 0)
 104             System.err.println("javadoc failed, rc:" + rc);
 105 
 106         int expectedDocletWarnCount = 1;
 107         checkEqual("actual", actualDocletWarnCount, "expected", expectedDocletWarnCount);
 108         checkEqual("actual", actualDocletWarnCount, "reported", reportedDocletWarnCount);
 109     }
 110 
 111     void checkEqual(String l1, int i1, String l2, int i2) throws Exception {
 112         if (i1 != i2)
 113             throw new Exception(l1 + " warn count, " + i1 + ", does not match "
 114                         + l2 + " warn count, " + i2);
 115     }
 116 
 117     @Override
 118     public String getName() {
 119         return "Test";
 120     }
 121 
 122     @Override
 123     public Set<Option> getSupportedOptions() {
 124         return Collections.emptySet();
 125     }
 126 
 127     @Override
 128     public SourceVersion getSupportedSourceVersion() {
 129         return SourceVersion.latest();
 130     }
 131 
 132     Reporter reporter;
 133     Locale locale;
 134     public void init(Locale locale, Reporter reporter) {
 135         this.locale = locale;
 136         this.reporter = reporter;
 137     }
 138 }