Path: blob/master/test/langtools/tools/jdeps/lib/JdepsRunner.java
41149 views
/*1* Copyright (c) 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*/222324import java.io.PrintStream;25import java.io.PrintWriter;26import java.io.StringWriter;27import java.util.Arrays;28import java.util.List;29import java.util.spi.ToolProvider;30import java.util.stream.Collectors;3132/**33* JdepsRunner class to invoke jdeps with the given command line argument34*/35public class JdepsRunner {36private static final ToolProvider JDEPS_TOOL = ToolProvider.findFirst("jdeps")37.orElseThrow(() -> new RuntimeException("jdeps tool not found"));3839public static JdepsRunner run(String... args) {40JdepsRunner jdeps = new JdepsRunner(args);41int rc = jdeps.run(true);42if (rc != 0)43throw new Error("jdeps failed: rc=" + rc);44return jdeps;45}4647final StringWriter stdout = new StringWriter();48final StringWriter stderr = new StringWriter();49final String[] args;50public JdepsRunner(String... args) {51System.out.println("jdeps " + Arrays.stream(args)52.collect(Collectors.joining(" ")));53this.args = args;54}5556public JdepsRunner(List<String> args) {57this(args.toArray(new String[0]));58}5960public int run() {61return run(false);62}6364public int run(boolean showOutput) {65try (PrintWriter pwout = new PrintWriter(stdout);66PrintWriter pwerr = new PrintWriter(stderr)) {67int rc = JDEPS_TOOL.run(pwout, pwerr, args);68if (showOutput) {69printStdout(System.out);70printStderr(System.out);71}72return rc;73}74}7576public boolean outputContains(String s) {77return stdout.toString().contains(s) || stderr.toString().contains(s);78}7980public void printStdout(PrintStream stream) {81stream.println(stdout.toString());82}8384public void printStderr(PrintStream stream) {85stream.println(stderr.toString());86}8788public String[] output() {89String lineSep = System.getProperty("line.separator");90return stdout.toString().split(lineSep);91}92}939495