Path: blob/master/test/functional/cmdLineTests/shareClassTests/utils/src/Utilities/RunCommand.java
6005 views
/*******************************************************************************1* Copyright (c) 2005, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package Utilities;2223import java.io.InputStream;2425import java.util.*;26import java.io.*;2728/**29* Instances of StreamGobbler are used to capture output from the spawned process30*/31class StreamGobbler extends Thread {32private InputStream is; // Can be used for stderr/stdout33private String type; // some identifier tag for this gobbler (possibly STDERR or STDOUT)3435private List expected = new ArrayList(); // Expected lines of output to be found in the gobbled output3637private StringBuffer gobbledData = new StringBuffer();38private List lines = new ArrayList();39public boolean finished = false;4041StreamGobbler(InputStream is, String type, String[] expected) {42this.is = is;43this.type= type;44if (expected!=null) {45for (int i = 0; i < expected.length; i++) {46String string = expected[i];47if (string!=null)48this.expected.add(string);49}50}51}5253public String getType() { return type; }5455public void run() {56try {57InputStreamReader isr = new InputStreamReader(is);58BufferedReader br = new BufferedReader(isr);59String line=null;60while ( (line = br.readLine()) != null) {61// System.out.println(type + ">" + line);62gobbledData.append(/*type + ">" + */line+"\n");63lines.add(line);64int pos = 0;int found =-1;65for (Iterator iterator = expected.iterator(); iterator66.hasNext();) {67String name = (String) iterator.next();68if (line.indexOf(name)!=-1) {found = pos;break;}69pos++;70}71if (found!=-1) expected.remove(found);72}73finished=true;74} catch (IOException ioe) {75ioe.printStackTrace();76}77}7879public boolean allExpectedMessagesWereFound() {80return expected.isEmpty();81}8283public void dumpOutput() {84System.out.println(gobbledData.toString());85}8687public String getOutput() {88return gobbledData.toString();89}9091public String[] getOutputAsLines() {92return (String[])lines.toArray(new String[]{});93}94}9596public class RunCommand {97public static String lastCommand;98public static String lastCommandStdout;99public static String[] lastCommandStdoutLines;100public static String lastCommandStderr;101public static String[] lastCommandStderrLines;102public static boolean logCommands = false;103public static int processRunawayTimeout = 120000; // 2 min timer, kill process if its taking longer!104105public static void execute(String cmd) {106execute(cmd,(String[])null,(String[])null,true);107}108109public static void execute(String cmd,boolean careAboutExitValue) {110execute(cmd,(String[])null,(String[])null,careAboutExitValue);111}112113public static void execute(String cmd,String sysoutMessages,String syserrMessages,boolean careAboutExitValue) {114execute(cmd,new String[]{sysoutMessages},new String[]{syserrMessages},careAboutExitValue);115}116117public static void execute(String cmd,String[] expectedSysoutMessages,String[] expectedSyserrMessages,boolean careAboutExitValue)118{119if (cmd==null) throw new RuntimeException("RunCommand.execute() - dont ask it to run a null command");120int exitVal = 0;121Runtime rt = Runtime.getRuntime();122lastCommand = null;123lastCommandStdout = null;lastCommandStderr = null;124lastCommandStdoutLines = null;lastCommandStderrLines = null;125try {126if (logCommands)127System.out.println("Executing command: "+cmd);128long processStartTime = System.currentTimeMillis();129Process proc = rt.exec(cmd);130// any error message?131StreamGobbler errorGobbler = new132StreamGobbler(proc.getErrorStream(), "ERROR",expectedSyserrMessages);133134// any output?135StreamGobbler outputGobbler = new136StreamGobbler(proc.getInputStream(), "OUTPUT",expectedSysoutMessages);137138// kick them off139errorGobbler.start();140outputGobbler.start();141142// any error???143exitVal = proc.waitFor();144145while (!outputGobbler.finished || !errorGobbler.finished) {146try { Thread.sleep(250); } catch (Exception e) {}147if ((System.currentTimeMillis()-processStartTime)>processRunawayTimeout) {148// kill the process, it is taking too long!149proc.destroy();150System.err.println("Process killed, was executing for longer than "+(processRunawayTimeout/1000)+"seconds");151}152}153lastCommandStdout = outputGobbler.getOutput();154lastCommandStderr = errorGobbler.getOutput();155lastCommandStdoutLines = outputGobbler.getOutputAsLines();156lastCommandStderrLines = errorGobbler.getOutputAsLines();157lastCommand = cmd;158if (!outputGobbler.allExpectedMessagesWereFound()) {159outputGobbler.dumpOutput();160errorGobbler.dumpOutput();161throw new RuntimeException("Not all expected messages were found");162}163if (!errorGobbler.allExpectedMessagesWereFound()) {164outputGobbler.dumpOutput();165errorGobbler.dumpOutput();166throw new RuntimeException("Not all expected messages were found");167}168//System.out.println("ExitValue: " + exitVal);169if (careAboutExitValue && exitVal!=0) {170outputGobbler.dumpOutput();171errorGobbler.dumpOutput();172throw new RuntimeException("TestFailed: exitvalue="+exitVal);173}174} catch (IOException ioe) {175throw new RuntimeException("Problem invoking command: "+ioe.toString());176} catch (InterruptedException ie) {177throw new RuntimeException("Problem invoking command: "+ie.toString());178}179180}181}182183184