some minor fixes

This commit is contained in:
fros4943 2008-09-17 16:30:57 +00:00
parent 00930b72c3
commit fc85534698
2 changed files with 52 additions and 20 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: LogScriptEngine.java,v 1.1 2008/04/22 10:12:16 fros4943 Exp $ * $Id: LogScriptEngine.java,v 1.2 2008/09/17 16:30:57 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -99,8 +99,9 @@ public class LogScriptEngine {
scriptedLogObserver.update(obs, obj); scriptedLogObserver.update(obs, obj);
} }
} catch (UndeclaredThrowableException e) { } catch (UndeclaredThrowableException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(GUI.getTopParentContainer(), JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
e.getMessage(), "See console for more information.",
"Script error", JOptionPane.ERROR_MESSAGE); "Script error", JOptionPane.ERROR_MESSAGE);
unregisterLogObserver(); unregisterLogObserver();
} }
@ -111,8 +112,9 @@ public class LogScriptEngine {
try { try {
createScriptEngine(scriptCode); createScriptEngine(scriptCode);
} catch (ScriptException e) { } catch (ScriptException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(GUI.getTopParentContainer(), JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
e.getMessage(), "See console for more information.",
"Script error", JOptionPane.ERROR_MESSAGE); "Script error", JOptionPane.ERROR_MESSAGE);
unregisterLogObserver(); unregisterLogObserver();
} }
@ -196,6 +198,9 @@ public class LogScriptEngine {
} }
}); });
Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
engine.put("global", hash);
/* TODO Test script */ /* TODO Test script */
logObserver.update(null, null); logObserver.update(null, null);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ScriptRunner.java,v 1.2 2008/09/17 15:22:39 fros4943 Exp $ * $Id: ScriptRunner.java,v 1.3 2008/09/17 16:30:57 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -71,15 +71,14 @@ public class ScriptRunner extends VisPlugin {
private JTextArea scriptTextArea; private JTextArea scriptTextArea;
private JTextArea logTextArea; private JTextArea logTextArea;
private GUI gui; private GUI gui;
private LogScriptEngine scriptTester = null; private LogScriptEngine scriptTester = null;
private JButton toggleButton; private JButton toggleButton;
private String oldTestName = null;
private String oldInfo = null;
public ScriptRunner(GUI gui) { public ScriptRunner(GUI gui) {
super("Script Runner (Log)", gui); super("(GUI) Test Script Editor", gui);
this.gui = gui; this.gui = gui;
scriptTextArea = new JTextArea(8,50); scriptTextArea = new JTextArea(8,50);
@ -95,7 +94,12 @@ public class ScriptRunner extends VisPlugin {
"log.log('TIME=' + mote.getSimulation().getSimulationTime() + '\\n');\n" + "log.log('TIME=' + mote.getSimulation().getSimulationTime() + '\\n');\n" +
"log.log('MSG=' + msg + '\\n');\n" + "log.log('MSG=' + msg + '\\n');\n" +
"\n" + "\n" +
"log.log('STORED VAR=' + global.get('storedVar') + '\\n');\n" +
"global.put('storedVar', msg);\n" +
"\n" +
"log.log('TEST OK\\n'); /* Report test success */\n" + "log.log('TEST OK\\n'); /* Report test success */\n" +
"\n" +
"/* To increase test run speed, close the simulator when done */\n" +
"//mote.getSimulation().getGUI().doQuit(false); /* Quit simulator (to end test run)*/\n" + "//mote.getSimulation().getGUI().doQuit(false); /* Quit simulator (to end test run)*/\n" +
"\n" + "\n" +
"//mote.getSimulation().getGUI().reloadCurrentSimulation(true); /* Reload simulation */\n" "//mote.getSimulation().getGUI().reloadCurrentSimulation(true); /* Reload simulation */\n"
@ -103,7 +107,7 @@ public class ScriptRunner extends VisPlugin {
logTextArea = new JTextArea(8,50); logTextArea = new JTextArea(8,50);
logTextArea.setMargin(new Insets(5,5,5,5)); logTextArea.setMargin(new Insets(5,5,5,5));
logTextArea.setEditable(false); logTextArea.setEditable(true);
logTextArea.setCursor(null); logTextArea.setCursor(null);
toggleButton = new JButton("Activate"); toggleButton = new JButton("Activate");
@ -111,12 +115,13 @@ public class ScriptRunner extends VisPlugin {
public void actionPerformed(ActionEvent ev) { public void actionPerformed(ActionEvent ev) {
if (toggleButton.getText().equals("Activate")) { if (toggleButton.getText().equals("Activate")) {
scriptTester = new LogScriptEngine(ScriptRunner.this.gui, scriptTextArea.getText()); scriptTester = new LogScriptEngine(ScriptRunner.this.gui, scriptTextArea.getText());
scriptTester.activateScript();
scriptTester.setScriptLogObserver(new Observer() { scriptTester.setScriptLogObserver(new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
logTextArea.append((String) obj); logTextArea.append((String) obj);
logTextArea.setCaretPosition(logTextArea.getText().length());
} }
}); });
scriptTester.activateScript();
toggleButton.setText("Deactivate"); toggleButton.setText("Deactivate");
scriptTextArea.setEnabled(false); scriptTextArea.setEnabled(false);
@ -160,13 +165,17 @@ public class ScriptRunner extends VisPlugin {
} }
/* Name test to export */ /* Name test to export */
if (oldTestName == null) {
oldTestName = "mytest";
}
String testName = (String) JOptionPane.showInputDialog(GUI.getTopParentContainer(), String testName = (String) JOptionPane.showInputDialog(GUI.getTopParentContainer(),
"Enter test name. No spaces or strange chars allowed.", "Enter test name. No spaces or strange chars allowed.",
"Test name", JOptionPane.PLAIN_MESSAGE, null, null, "Test name", JOptionPane.PLAIN_MESSAGE, null, null,
"mytest"); oldTestName);
if (testName == null) { if (testName == null) {
return; return;
} }
oldTestName = testName;
if (testName.equals("") || testName.contains(" ")) { if (testName.equals("") || testName.contains(" ")) {
JOptionPane.showMessageDialog(GUI.getTopParentContainer(), JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Bad test name: '" + testName + "'", "Bad test name", JOptionPane.ERROR_MESSAGE); "Bad test name: '" + testName + "'", "Bad test name", JOptionPane.ERROR_MESSAGE);
@ -263,14 +272,21 @@ public class ScriptRunner extends VisPlugin {
/* Export .info (optional) */ /* Export .info (optional) */
try { try {
String info = JOptionPane.showInputDialog(GUI.getTopParentContainer(), if (oldInfo == null) {
"(OPTIONAL) Enter test description", oldInfo = "";
"Optional test info", JOptionPane.QUESTION_MESSAGE); }
String info = (String) JOptionPane.showInputDialog(GUI.getTopParentContainer(),
"Optional test info",
"(OPTIONAL) Enter test description", JOptionPane.PLAIN_MESSAGE, null, null,
oldInfo);
if (info != null && !info.equals("")) { if (info != null && !info.equals("")) {
oldInfo = info;
BufferedWriter writer = BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(infoFile))); new BufferedWriter(new OutputStreamWriter(new FileOutputStream(infoFile)));
writer.write(info); writer.write(info);
writer.close(); writer.close();
} else {
oldInfo = null;
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -309,7 +325,7 @@ public class ScriptRunner extends VisPlugin {
final BufferedReader input = new BufferedReader(new InputStreamReader(externalCoojaProcess.getInputStream())); final BufferedReader input = new BufferedReader(new InputStreamReader(externalCoojaProcess.getInputStream()));
final BufferedReader err = new BufferedReader(new InputStreamReader(externalCoojaProcess.getErrorStream())); final BufferedReader err = new BufferedReader(new InputStreamReader(externalCoojaProcess.getErrorStream()));
JButton button = new JButton("Abort test/Close dialog"); final JButton button = new JButton("Abort test");
button.addActionListener(new ActionListener() { button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (externalCoojaProcess != null) { if (externalCoojaProcess != null) {
@ -339,7 +355,7 @@ public class ScriptRunner extends VisPlugin {
String readLine; String readLine;
try { try {
while ((readLine = input.readLine()) != null) { while ((readLine = input.readLine()) != null) {
if (normal != null && readLine != null) { if (normal != null) {
normal.println(readLine); normal.println(readLine);
} }
} }
@ -348,13 +364,22 @@ public class ScriptRunner extends VisPlugin {
logger.warn("Error while reading from process"); logger.warn("Error while reading from process");
} }
normal.println("");
normal.println("");
normal.println("");
/* Parse log file for success info */ /* Parse log file for success info */
try { try {
BufferedReader in = new BufferedReader(new InputStreamReader( BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(logFile))); new FileInputStream(logFile)));
boolean testSucceeded = false; boolean testSucceeded = false;
while (in.ready()) { while (in.ready()) {
String line = in.readLine(); String line = in.readLine();
if (line == null) {
line = "";
}
normal.println(line);
if (line.contains("TEST OK")) { if (line.contains("TEST OK")) {
testSucceeded = true; testSucceeded = true;
break; break;
@ -362,8 +387,10 @@ public class ScriptRunner extends VisPlugin {
} }
if (testSucceeded) { if (testSucceeded) {
progressDialog.setTitle("Test run completed. Test succeeded!"); progressDialog.setTitle("Test run completed. Test succeeded!");
button.setText("Test OK");
} else { } else {
progressDialog.setTitle("Test run completed. Test failed!"); progressDialog.setTitle("Test run completed. Test failed!");
button.setText("Test failed");
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.fatal("File not found: " + e); logger.fatal("File not found: " + e);
@ -379,7 +406,7 @@ public class ScriptRunner extends VisPlugin {
String readLine; String readLine;
try { try {
while ((readLine = err.readLine()) != null) { while ((readLine = err.readLine()) != null) {
if (error != null && readLine != null) { if (error != null) {
error.println(readLine); error.println(readLine);
} }
} }