V
Vic
I am not very well conversant with java and have a very basic and
limited java experience being a QA engg. I am currently looking at a
code which isn't very consistent in our testing atuomation framework.
Here is the code ->
// executes system commands and captures STDOUT and STDERR
import java.io.*;
import java.util.*;
class StreamGobbler extends Thread {
InputStream is;
String type;
String result;
StreamGobbler(InputStream is, String type){
this.is = is;
this.type = type;
}
public void run(){
StringBuffer buffer = new StringBuffer();
try{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null){
buffer.append(line);
buffer.append("\n");
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
result = buffer.toString();
}
}
public String getResult(){
return result;
}
}
class NewCommandExecuter {
public String stdout = null;
public String stderr = null;
private String line;
public NewCommandExecuter (String cmd) {
try{
//VikP - This needed to be modified to incorporate the change in
syntax for executing getData.pl by Mark Silverman for PR#5026342
System.out.println("New Command Executer: EXECUTING COMMAND:
"+cmd);
String [] execmd = {"bash","-c",cmd};
//Process proc = Runtime.getRuntime().exec(new String[]{"bash","-
c",cmd});
Process proc = Runtime.getRuntime().exec(execmd);
//Process proc = Runtime.getRuntime().exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream
(),"ERROR");
StreamGobbler outputGobbler = new StreamGobbler
(proc.getInputStream(),"OUTPUT");
errorGobbler.start();
outputGobbler.start();
int val = proc.waitFor();
System.out.println("ExitValue for Command "+cmd+" is: "+val);
stdout = outputGobbler.getResult();
stderr = errorGobbler.getResult();
//VikP - Waits for these threads to die
errorGobbler.join();
outputGobbler.join();
//
}
catch (Throwable t){
t.printStackTrace();
}
if (stdout.equals(""))
stdout = null;
if (stderr.equals(""))
stderr = null;
}
}
I call the the above in a loop (of about 2000 cases) as ->
NewCommandExecuter xmlOut = new NewCommandExecuter(xmlOutCmd);
// verity xml output
if (xmlOut.stdout == null)
throw new Exception ("Xml-out query '"+xmlOutCmd+"' didn't return
any results. STDERR returned:\n"+xmlOut.stderr);
// save output to a temp file
writeStringToFile(tempXmlFile, xmlOut.stdout); // save xml output
to a file
The problem that I am facing is it works fine but it doesn't go
through the entire loop (all the test cases we have) and almost always
gets terminated prematurely completing only a partial list of cases.
So that makes me wonder if there is anything wrong in the
StreamGobbler and/or NewCommandExecuter. I tried a bunch of things in
those classes but definitely I am missing something here. Please help.
Thanks
limited java experience being a QA engg. I am currently looking at a
code which isn't very consistent in our testing atuomation framework.
Here is the code ->
// executes system commands and captures STDOUT and STDERR
import java.io.*;
import java.util.*;
class StreamGobbler extends Thread {
InputStream is;
String type;
String result;
StreamGobbler(InputStream is, String type){
this.is = is;
this.type = type;
}
public void run(){
StringBuffer buffer = new StringBuffer();
try{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null){
buffer.append(line);
buffer.append("\n");
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
result = buffer.toString();
}
}
public String getResult(){
return result;
}
}
class NewCommandExecuter {
public String stdout = null;
public String stderr = null;
private String line;
public NewCommandExecuter (String cmd) {
try{
//VikP - This needed to be modified to incorporate the change in
syntax for executing getData.pl by Mark Silverman for PR#5026342
System.out.println("New Command Executer: EXECUTING COMMAND:
"+cmd);
String [] execmd = {"bash","-c",cmd};
//Process proc = Runtime.getRuntime().exec(new String[]{"bash","-
c",cmd});
Process proc = Runtime.getRuntime().exec(execmd);
//Process proc = Runtime.getRuntime().exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream
(),"ERROR");
StreamGobbler outputGobbler = new StreamGobbler
(proc.getInputStream(),"OUTPUT");
errorGobbler.start();
outputGobbler.start();
int val = proc.waitFor();
System.out.println("ExitValue for Command "+cmd+" is: "+val);
stdout = outputGobbler.getResult();
stderr = errorGobbler.getResult();
//VikP - Waits for these threads to die
errorGobbler.join();
outputGobbler.join();
//
}
catch (Throwable t){
t.printStackTrace();
}
if (stdout.equals(""))
stdout = null;
if (stderr.equals(""))
stderr = null;
}
}
I call the the above in a loop (of about 2000 cases) as ->
NewCommandExecuter xmlOut = new NewCommandExecuter(xmlOutCmd);
// verity xml output
if (xmlOut.stdout == null)
throw new Exception ("Xml-out query '"+xmlOutCmd+"' didn't return
any results. STDERR returned:\n"+xmlOut.stderr);
// save output to a temp file
writeStringToFile(tempXmlFile, xmlOut.stdout); // save xml output
to a file
The problem that I am facing is it works fine but it doesn't go
through the entire loop (all the test cases we have) and almost always
gets terminated prematurely completing only a partial list of cases.
So that makes me wonder if there is anything wrong in the
StreamGobbler and/or NewCommandExecuter. I tried a bunch of things in
those classes but definitely I am missing something here. Please help.
Thanks