automatically activate test for new simulations + simplified exporting tests

This commit is contained in:
fros4943 2009-03-03 13:50:02 +00:00
parent 6367a73fa4
commit e91cc0f1ef
1 changed files with 74 additions and 69 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: ScriptRunner.java,v 1.13 2009/02/18 16:43:42 fros4943 Exp $ * $Id: ScriptRunner.java,v 1.14 2009/03/03 13:50:02 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -49,9 +49,6 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.StringReader; import java.io.StringReader;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptException; import javax.script.ScriptException;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
@ -127,6 +124,19 @@ public class ScriptRunner implements Plugin {
return; return;
} }
/* Automatically activate test for new simulations */
gui.addObserver(new Observer() {
public void update(Observable obs, Object obj) {
Simulation sim = ScriptRunner.this.gui.getSimulation();
if (sim == null) {
setScriptActive(false);
return;
}
setScriptActive(true);
}
});
/* GUI components */ /* GUI components */
pluginGUI = new JInternalFrame( pluginGUI = new JInternalFrame(
"Contiki Test Editor", "Contiki Test Editor",
@ -206,29 +216,9 @@ public class ScriptRunner implements Plugin {
toggleButton.addActionListener(new ActionListener() { toggleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) { public void actionPerformed(ActionEvent ev) {
if (toggleButton.getText().equals("Activate")) { if (toggleButton.getText().equals("Activate")) {
engine = new LogScriptEngine(ScriptRunner.this.gui); setScriptActive(true);
engine.setScriptLogObserver(new Observer() {
public void update(Observable obs, Object obj) {
logTextArea.append((String) obj);
logTextArea.setCaretPosition(logTextArea.getText().length());
}
});
try {
engine.activateScript(scriptTextArea.getText());
} catch (ScriptException e) {
e.printStackTrace();
}
toggleButton.setText("Deactivate");
logTextArea.setText("");
scriptTextArea.setEnabled(false);
} else { } else {
if (engine != null) { setScriptActive(false);
engine.deactivateScript();
engine = null;
}
toggleButton.setText("Activate");
scriptTextArea.setEnabled(true);
} }
} }
}); });
@ -238,15 +228,7 @@ public class ScriptRunner implements Plugin {
public void actionPerformed(ActionEvent ev) { public void actionPerformed(ActionEvent ev) {
Runnable doImport = new Runnable() { Runnable doImport = new Runnable() {
public void run() { public void run() {
if (!toggleButton.getText().equals("Activate")) { setScriptActive(false);
if (engine != null) {
engine.deactivateScript();
engine = null;
}
toggleButton.setText("Activate");
scriptTextArea.setEnabled(true);
}
importContikiTest(); importContikiTest();
} }
}; };
@ -289,6 +271,40 @@ public class ScriptRunner implements Plugin {
pluginGUI.pack(); pluginGUI.pack();
} }
private void setScriptActive(boolean active) {
if (active) {
setScriptActive(false);
engine = new LogScriptEngine(ScriptRunner.this.gui);
engine.setScriptLogObserver(new Observer() {
public void update(Observable obs, Object obj) {
logTextArea.append((String) obj);
logTextArea.setCaretPosition(logTextArea.getText().length());
}
});
try {
engine.activateScript(scriptTextArea.getText());
} catch (ScriptException e) {
e.printStackTrace();
setScriptActive(false);
}
toggleButton.setText("Deactivate");
logTextArea.setText("");
scriptTextArea.setEnabled(false);
logger.info("Test script activated");
} else {
if (engine != null) {
engine.deactivateScript();
engine = null;
toggleButton.setText("Activate");
scriptTextArea.setEnabled(true);
logger.info("Test script deactivated");
}
}
}
public JInternalFrame getGUI() { public JInternalFrame getGUI() {
return pluginGUI; return pluginGUI;
} }
@ -364,6 +380,7 @@ public class ScriptRunner implements Plugin {
} }
private void exportAsContikiTest() { private void exportAsContikiTest() {
String s1, s2; int n; Object[] options;
Simulation simulation = ScriptRunner.this.gui.getSimulation(); Simulation simulation = ScriptRunner.this.gui.getSimulation();
if (simulation == null) { if (simulation == null) {
@ -372,23 +389,24 @@ public class ScriptRunner implements Plugin {
return; return;
} }
/* Confirm test directory */
File testDir = new File(GUI.getExternalToolsSetting("PATH_CONTIKI") + "/tools/cooja/contiki_tests"); File testDir = new File(GUI.getExternalToolsSetting("PATH_CONTIKI") + "/tools/cooja/contiki_tests");
String s1 = "Ok"; if (!testDir.exists()) {
String s2 = "Cancel"; logger.fatal("Test directory does not exist: " + testDir.getPath());
Object[] options = { s1, s2 }; return;
int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(), }
/* Confirm test directory */
/*s1 = "Ok";
s2 = "Cancel";
options = new Object[] { s1, s2 };
n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"The current simulation config (.csc) and test script (.js)\n" + "The current simulation config (.csc) and test script (.js)\n" +
"will be stored in directory '" + testDir.getPath() + "'", "will be stored in directory '" + testDir.getPath() + "'",
"Export Contiki test", JOptionPane.YES_NO_OPTION, "Export Contiki test", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
if (n != JOptionPane.YES_OPTION) { if (n != JOptionPane.YES_OPTION) {
return; return;
} }*/
if (!testDir.exists()) {
logger.fatal("Test directory does not exist: " + testDir.getPath());
return;
}
/* Name test to export */ /* Name test to export */
if (oldTestName == null) { if (oldTestName == null) {
@ -418,6 +436,7 @@ public class ScriptRunner implements Plugin {
if (cscFile.exists() || jsFile.exists() || infoFile.exists()) { if (cscFile.exists() || jsFile.exists() || infoFile.exists()) {
s1 = "Overwrite"; s1 = "Overwrite";
s2 = "Cancel"; s2 = "Cancel";
options = new Object[] { s1, s2 };
n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(), n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"Some output files already exist. Overwrite?", "Some output files already exist. Overwrite?",
"Test already exist", JOptionPane.YES_NO_OPTION, "Test already exist", JOptionPane.YES_NO_OPTION,
@ -448,32 +467,10 @@ public class ScriptRunner implements Plugin {
if (pluginsConfig != null) { if (pluginsConfig != null) {
root.addContent(pluginsConfig); root.addContent(pluginsConfig);
} }
// if (pluginsConfig != null) {
// JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
// "Stripping plugin configuration.\n" +
// "(Exporting non-GUI plugins not implemented.)",
// "Plugins detected", JOptionPane.WARNING_MESSAGE);
// }
/* Fix simulation delay */
root.detach();
String configString = new XMLOutputter().outputString(new Document(root));
String identifierExtraction = "<delaytime>([^<]*)</delaytime>";
Matcher matcher = Pattern.compile(identifierExtraction).matcher(configString);
while (matcher.find()) {
int delay = Integer.parseInt(matcher.group(1));
if (delay != 0) {
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Simulation delay currently set to " + delay + ".\n" +
"Changing delay time to 0 in exported test.",
"Non-zero delay time detected", JOptionPane.WARNING_MESSAGE);
}
configString = configString.replace(
"<delaytime>" + matcher.group(1) + "</delaytime>",
"<delaytime>0</delaytime>");
}
/* Export .csc */ /* Export .csc */
root.detach();
String configString = new XMLOutputter().outputString(new Document(root));
try { try {
Element newRoot = new SAXBuilder().build(new StringReader(configString)).getRootElement(); Element newRoot = new SAXBuilder().build(new StringReader(configString)).getRootElement();
newRoot.detach(); newRoot.detach();
@ -529,7 +526,14 @@ public class ScriptRunner implements Plugin {
/* Run exported test (optional) */ /* Run exported test (optional) */
s1 = "Run test"; s1 = "Run test";
s2 = "No"; s2 = "No";
options = new Object[] { s1, s2 };
n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(), n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"Test files created:\n" +
(cscFile.exists()?"Config: " + cscFile.getName() + "\n": "") +
(jsFile.exists()?"Script: " + jsFile.getName() + "\n": "") +
(infoFile.exists()?"Info: " + infoFile.getName() + "\n": "") +
"\n" +
"Run exported test in forked Cooja now?", "Run exported test in forked Cooja now?",
"Run test?", JOptionPane.YES_NO_OPTION, "Run test?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
@ -665,6 +669,7 @@ public class ScriptRunner implements Plugin {
gui.doQuit(false); gui.doQuit(false);
return false; return false;
} }
sim.setDelayTime(0);
gui.setSimulation(sim); gui.setSimulation(sim);
/* Load test script */ /* Load test script */