final Process process = Runtime.getRuntime().exec(command, null, coojaBuild);
final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
final BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
/* GUI components */
final MessageList testOutput = new MessageList();
final AbstractAction abort = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
process.destroy();
if (progressDialog.isDisplayable()) {
progressDialog.dispose();
}
}
};
abort.putValue(AbstractAction.NAME, "Abort test");
final JButton button = new JButton(abort);
progressPanel.add(BorderLayout.CENTER, new JScrollPane(testOutput));
progressPanel.add(BorderLayout.SOUTH, button);
progressPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
progressPanel.setVisible(true);
progressDialog.getContentPane().add(progressPanel);
progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
progressDialog.getRootPane().setDefaultButton(button);
progressDialog.setSize(800, 300);
progressDialog.setLocationRelativeTo(ScriptRunner.this);
progressDialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
abort.actionPerformed(null);
}
});
progressDialog.setVisible(true);
Thread readInput = new Thread(new Runnable() {
public void run() {
String line;
try {
while ((line = input.readLine()) != null) {
testOutput.addMessage(line, MessageList.NORMAL);
}
} catch (IOException e) {
}
testOutput.addMessage("", MessageList.NORMAL);
testOutput.addMessage("", MessageList.NORMAL);
testOutput.addMessage("", MessageList.NORMAL);
/* Parse log file, check if test succeeded */
try {
String log = StringUtils.loadFromFile(logFile);
if (log == null) {
throw new FileNotFoundException(logFile.getPath());
}
String[] lines = log.split("\n");
boolean testSucceeded = false;
for (String l: lines) {
if (l == null) {
line = "";
}
testOutput.addMessage(l, MessageList.NORMAL);
if (l.contains("TEST OK")) {
testSucceeded = true;
break;
}
}
if (testSucceeded) {
progressDialog.setTitle("Test run completed. Test succeeded.");
button.setText("Test OK");
} else {
progressDialog.setTitle("Test run completed. Test failed.");
button.setText("Test failed");
}
} catch (FileNotFoundException e) {
logger.fatal("No test output : " + logFile);
progressDialog.setTitle("Test run completed. Test failed! (no logfile)");
button.setText("Test failed");
}
}
});
Thread readError = new Thread(new Runnable() {
public void run() {
String line;
try {
while ((line = err.readLine()) != null) {
testOutput.addMessage(line, MessageList.ERROR);
}
} catch (IOException e) {
}
}
});