test/langtools/jdk/javadoc/tool/ToolProviderTest.java
author bobv
Tue, 07 Nov 2017 10:30:53 -0500
changeset 47801 c7b50c23ea71
parent 47216 71c04702a3d5
child 48543 7067fe4e054e
permissions -rw-r--r--
8190283: Default heap sizing options select a MaxHeapSize larger than available physical memory in some cases Reviewed-by: tschatzl, sjohanss
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41448
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     1
/*
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     4
 *
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     8
 *
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    13
 * accompanied this code).
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    14
 *
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    18
 *
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    21
 * questions.
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    22
 */
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    23
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    24
/*
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    25
 * @test
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    26
 * @bug 8159855
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    27
 * @summary test javadoc's ToolProvider
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    28
 * @library /tools/lib
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    29
 * @build toolbox.TestRunner toolbox.ToolBox
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    30
 * @run main ToolProviderTest
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    31
 */
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    32
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    33
import java.io.*;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    34
import java.nio.file.Path;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    35
import java.nio.file.Paths;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    36
import java.util.*;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    37
import java.util.spi.ToolProvider;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    38
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    39
import toolbox.TestRunner;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    40
import toolbox.ToolBox;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    41
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    42
public class ToolProviderTest extends TestRunner {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    43
    public static void main(String... args) throws Exception {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    44
        new ToolProviderTest().runTests();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    45
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    46
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    47
    ToolBox tb = new ToolBox();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    48
    ToolProvider javadoc;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    49
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    50
    ToolProviderTest() {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    51
        super(System.err);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    52
        javadoc = ToolProvider.findFirst("javadoc").get();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    53
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    54
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    55
    @Test
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    56
    public void testProviders() throws Exception {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    57
        Map<String, ToolProvider> providers = new LinkedHashMap<>();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    58
        for (ToolProvider tp : ServiceLoader.load(ToolProvider.class,
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    59
                ClassLoader.getSystemClassLoader())) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    60
            System.out.println("Provider: " + tp.name() + ": " + tp.getClass().getName());
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    61
            providers.put(tp.name(), tp);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    62
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    63
        if (!providers.containsKey("javadoc")) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    64
            error("javadoc ToolProvider not found");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    65
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    66
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    67
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    68
    @Test
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    69
    public void testOneStream() throws Exception {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    70
        StringWriter sw = new StringWriter();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    71
        try (PrintWriter pw = new PrintWriter(sw)) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    72
            int rc = javadoc.run(pw, pw, "-help");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    73
            if (rc != 0) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    74
                error("unexpected exit code: " + rc);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    75
            }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    76
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    77
        String out = sw.toString();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    78
        if (!out.contains("Usage:")) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    79
            error("expected output not found");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    80
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    81
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    82
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    83
    @Test
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    84
    public void testTwoStreamsOut() throws Exception {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    85
        StringWriter swOut = new StringWriter();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    86
        StringWriter swErr = new StringWriter();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    87
        try (PrintWriter pwOut = new PrintWriter(swOut);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    88
                PrintWriter pwErr = new PrintWriter(swErr)) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    89
            int rc = javadoc.run(pwOut, pwErr, "-help");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    90
            if (rc != 0) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    91
                error("unexpected exit code: " + rc);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    92
            }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    93
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    94
        String out = swOut.toString();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    95
        String err = swErr.toString();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    96
        if (!out.contains("Usage:")) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    97
            error("stdout: expected output not found");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    98
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    99
        if (!err.isEmpty()) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   100
            error("stderr: unexpected output");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   101
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   102
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   103
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   104
    @Test
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   105
    public void testTwoStreamsErr() throws Exception {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   106
        Path src = Paths.get("src");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   107
        Path classes = Paths.get("classes");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   108
        tb.writeJavaFiles(src,
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   109
            "import java.util.*; class C { # }");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   110
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   111
        StringWriter swOut = new StringWriter();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   112
        StringWriter swErr = new StringWriter();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   113
        try (PrintWriter pwOut = new PrintWriter(swOut);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   114
                PrintWriter pwErr = new PrintWriter(swErr)) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   115
            int rc = javadoc.run(pwOut, pwErr,
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   116
                "-d", classes.toString(),
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   117
                src.resolve("C.java").toString());
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   118
            if (rc != 1) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   119
                error("unexpected exit code: " + rc);
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   120
            }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   121
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   122
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   123
        String out = swOut.toString();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   124
        String err = swErr.toString();
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   125
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   126
        if (!out.contains("Loading")) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   127
            error("stdout: unexpected output");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   128
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   129
        if (!err.contains("illegal character")) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   130
            error("stderr: expected output not found");
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   131
        }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   132
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   133
}