return;
}
try {
final Process externalCoojaProcess;
final MessageList testOutput = new MessageList();
JPanel progressPanel = new JPanel(new BorderLayout());
final JDialog progressDialog = new JDialog((Window)GUI.getTopParentContainer(), (String) null);
progressDialog.setTitle("Running test...");
String command[] = {
"java",
"-jar",
"../dist/cooja.jar",
"-nogui",
"-test=" + testName
};
externalCoojaProcess = Runtime.getRuntime().exec(command, null, testDir);
final BufferedReader input = new BufferedReader(new InputStreamReader(externalCoojaProcess.getInputStream()));
final BufferedReader err = new BufferedReader(new InputStreamReader(externalCoojaProcess.getErrorStream()));
final JButton button = new JButton("Abort test");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
externalCoojaProcess.destroy();
if (progressDialog.isDisplayable()) {
progressDialog.dispose();
}
}
});
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(500, 300);
progressDialog.setLocationRelativeTo(ScriptRunner.this.pluginGUI);
progressDialog.setVisible(true);
Thread readInput = new Thread(new Runnable() {
public void run() {
String readLine;
try {
while ((readLine = input.readLine()) != null) {
if (testOutput != null) {
testOutput.addMessage(readLine, MessageList.NORMAL);
}
}
} catch (IOException e) {
logger.warn("Error while reading from process");
}
testOutput.addMessage("", MessageList.NORMAL);
testOutput.addMessage("", MessageList.NORMAL);
testOutput.addMessage("", MessageList.NORMAL);
/* Parse log file for success info */
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(logFile)));
boolean testSucceeded = false;
while (in.ready()) {
String line = in.readLine();
if (line == null) {
line = "";
}
testOutput.addMessage(line, MessageList.NORMAL);
if (line.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("File not found: " + e);
progressDialog.setTitle("Test run completed. Test failed! (no logfile)");
button.setText("Test failed");
} catch (IOException e) {
logger.fatal("IO error: " + e);
progressDialog.setTitle("Test run completed. Test failed! (IO exception)");
button.setText("Test failed");
}
}
}, "read input stream thread");
Thread readError = new Thread(new Runnable() {
public void run() {
String readLine;
try {
while ((readLine = err.readLine()) != null) {
if (testOutput != null) {
testOutput.addMessage(readLine, MessageList.ERROR);
}
}
} catch (IOException e) {
logger.warn("Error while reading from process");
}