Path: blob/master/test/langtools/jdk/javadoc/tool/ToolProviderTest.java
40957 views
/*1* Copyright (c) 2016, 2018, 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 815985526* @summary test javadoc's ToolProvider27* @library /tools/lib28* @build toolbox.TestRunner toolbox.ToolBox29* @run main ToolProviderTest30*/3132import java.io.*;33import java.nio.file.Path;34import java.nio.file.Paths;35import java.util.*;36import java.util.spi.ToolProvider;3738import toolbox.TestRunner;39import toolbox.ToolBox;4041public class ToolProviderTest extends TestRunner {42public static void main(String... args) throws Exception {43new ToolProviderTest().runTests();44}4546ToolBox tb = new ToolBox();47ToolProvider javadoc;4849ToolProviderTest() {50super(System.err);51javadoc = ToolProvider.findFirst("javadoc").get();52}5354@Test55public void testProviders() throws Exception {56Map<String, ToolProvider> providers = new LinkedHashMap<>();57for (ToolProvider tp : ServiceLoader.load(ToolProvider.class,58ClassLoader.getSystemClassLoader())) {59System.out.println("Provider: " + tp.name() + ": " + tp.getClass().getName());60providers.put(tp.name(), tp);61}62if (!providers.containsKey("javadoc")) {63error("javadoc ToolProvider not found");64}65}6667@Test68public void testOneStream() throws Exception {69StringWriter sw = new StringWriter();70try (PrintWriter pw = new PrintWriter(sw)) {71int rc = javadoc.run(pw, pw, "--help");72if (rc != 0) {73error("unexpected exit code: " + rc);74}75}76String out = sw.toString();77if (!out.contains("Usage:")) {78error("expected output not found");79}80}8182@Test83public void testTwoStreamsOut() throws Exception {84StringWriter swOut = new StringWriter();85StringWriter swErr = new StringWriter();86try (PrintWriter pwOut = new PrintWriter(swOut);87PrintWriter pwErr = new PrintWriter(swErr)) {88int rc = javadoc.run(pwOut, pwErr, "--help");89if (rc != 0) {90error("unexpected exit code: " + rc);91}92}93String out = swOut.toString();94String err = swErr.toString();95if (!out.contains("Usage:")) {96error("stdout: expected output not found");97}98if (!err.isEmpty()) {99error("stderr: unexpected output");100}101}102103@Test104public void testTwoStreamsErr() throws Exception {105Path src = Paths.get("src");106Path classes = Paths.get("classes");107tb.writeJavaFiles(src,108"import java.util.*; class C { # }");109110StringWriter swOut = new StringWriter();111StringWriter swErr = new StringWriter();112try (PrintWriter pwOut = new PrintWriter(swOut);113PrintWriter pwErr = new PrintWriter(swErr)) {114int rc = javadoc.run(pwOut, pwErr,115"-d", classes.toString(),116src.resolve("C.java").toString());117if (rc != 1) {118error("unexpected exit code: " + rc);119}120}121122String out = swOut.toString();123String err = swErr.toString();124125if (!out.contains("Loading")) {126error("stdout: unexpected output");127}128if (!err.contains("illegal character")) {129error("stderr: expected output not found");130}131}132}133134135