Path: blob/master/test/langtools/jdk/javadoc/tool/6964914/TestUserDoclet.java
40971 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 696491426* @summary javadoc does not output number of warnings using user written doclet27* @modules jdk.javadoc28*/2930import java.io.*;3132import java.util.ArrayList;33import java.util.Arrays;34import java.util.Collections;35import java.util.List;36import java.util.Locale;37import java.util.Set;3839import javax.lang.model.SourceVersion;4041import jdk.javadoc.doclet.Doclet;42import jdk.javadoc.doclet.Reporter;43import jdk.javadoc.doclet.DocletEnvironment;4445public class TestUserDoclet implements Doclet {46public static void main(String... args) throws Exception {47new TestUserDoclet().run();48}4950static final String docletWarning = "warning from test doclet";5152/** Main doclet method. */53public boolean run(DocletEnvironment root) {54reporter.print(javax.tools.Diagnostic.Kind.WARNING, docletWarning);55return true;56}5758/** Main test method. */59void run() throws Exception {60File javaHome = new File(System.getProperty("java.home"));61File javadoc = new File(new File(javaHome, "bin"), "javadoc");62File testSrc = new File(System.getProperty("test.src"));63File testClasses = new File(System.getProperty("test.classes"));6465// run javadoc in separate process to ensure doclet executed under66// normal user conditions w.r.t. classloader67String thisClassName = TestUserDoclet.class.getName();68List<String> cmdArgs = new ArrayList<>();69cmdArgs.add(javadoc.getPath());70cmdArgs.addAll(Arrays.asList(71"-doclet", thisClassName,72"-docletpath", testClasses.getPath(),73new File(testSrc, thisClassName + ".java").getPath()74));75Process p = new ProcessBuilder()76.command(cmdArgs)77.redirectErrorStream(true)78.start();7980int actualDocletWarnCount = 0;81int reportedDocletWarnCount = 0;82BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));83try {84String line;85while ((line = in.readLine()) != null) {86System.err.println(line);87if (line.contains(docletWarning))88actualDocletWarnCount++;89if (line.matches("[0-9]+ warning(s)?"))90reportedDocletWarnCount =91Integer.valueOf(line.substring(0, line.indexOf(" ")));92}93} finally {94in.close();95}96int rc = p.waitFor();97if (rc != 0)98System.err.println("javadoc failed, rc:" + rc);99100int expectedDocletWarnCount = 1;101checkEqual("actual", actualDocletWarnCount, "expected", expectedDocletWarnCount);102checkEqual("actual", actualDocletWarnCount, "reported", reportedDocletWarnCount);103}104105void checkEqual(String l1, int i1, String l2, int i2) throws Exception {106if (i1 != i2)107throw new Exception(l1 + " warn count, " + i1 + ", does not match "108+ l2 + " warn count, " + i2);109}110111@Override112public String getName() {113return "Test";114}115116@Override117public Set<Option> getSupportedOptions() {118return Collections.emptySet();119}120121@Override122public SourceVersion getSupportedSourceVersion() {123return SourceVersion.latest();124}125126Reporter reporter;127Locale locale;128public void init(Locale locale, Reporter reporter) {129this.locale = locale;130this.reporter = reporter;131}132}133134135