QuietOption.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2015, 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 8035473
27 * @summary make sure tool is quiet when told to, and chatty otherwise
28 */
29
30
31import java.io.BufferedReader;
32import java.io.File;
33import java.io.InputStreamReader;
34import java.util.ArrayList;
35import java.util.List;
36
37/**
38 * Dummy javadoc comment.
39 */
40public class QuietOption {
41
42    final File javadoc;
43    final File testSrc;
44    final String thisClassName;
45
46    public QuietOption() {
47        File javaHome = new File(System.getProperty("java.home"));
48        if (javaHome.getName().endsWith("jre"))
49            javaHome = javaHome.getParentFile();
50        javadoc = new File(new File(javaHome, "bin"), "javadoc");
51        testSrc = new File(System.getProperty("test.src"));
52        thisClassName = QuietOption.class.getName();
53    }
54
55    public static void main(String... args) throws Exception {
56        QuietOption test = new QuietOption();
57        test.run1();
58        test.run2();
59    }
60
61    // make sure javadoc is quiet
62    void run1() throws Exception {
63        List<String> output = doTest(javadoc.getPath(),
64                "-classpath", ".", // insulates us from ambient classpath
65                "-quiet",
66                new File(testSrc, thisClassName + ".java").getPath());
67
68        if (!output.isEmpty()) {
69            System.out.println(output);
70            throw new Exception("run1: Shhh!, very chatty javadoc!.");
71        }
72    }
73
74    // make sure javadoc is chatty
75    void run2() throws Exception {
76        List<String> output = doTest(javadoc.getPath(),
77                "-classpath", ".", // insulates us from ambient classpath
78                new File(testSrc, thisClassName + ".java").getPath());
79
80        if (output.isEmpty()) {
81            System.out.println(output);
82            throw new Exception("run2: speak up and please be heard!.");
83        }
84    }
85
86    /**
87     * More dummy comments.
88     */
89    List<String> doTest(String... args) throws Exception {
90        List<String> output = new ArrayList<>();
91        // run javadoc in separate process to ensure doclet executed under
92        // normal user conditions w.r.t. classloader
93        Process p = new ProcessBuilder()
94                .command(args)
95                .redirectErrorStream(true)
96                .start();
97        try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
98            String line = in.readLine();
99            while (line != null) {
100                output.add(line.trim());
101                line = in.readLine();
102            }
103        }
104        int rc = p.waitFor();
105        if (rc != 0) {
106            throw new Exception("javadoc failed, rc:" + rc);
107        }
108        return output;
109    }
110}
111