📄 scriptrunner.java
字号:
/* Name test to export */ if (oldTestName == null) { oldTestName = "mytest"; } String testName = (String) JOptionPane.showInputDialog(GUI.getTopParentContainer(), "The test name correponds to the exported test files:\n" + "[testname].csc, [testname].js, [testname].info\n", "Enter test name", JOptionPane.PLAIN_MESSAGE, null, null, oldTestName); if (testName == null) { return; } oldTestName = testName; if (testName.equals("") || testName.contains(" ")) { JOptionPane.showMessageDialog(GUI.getTopParentContainer(), "Bad test name: '" + testName + "'", "Bad test name", JOptionPane.ERROR_MESSAGE); return; } File cscFile = new File(testDir, testName + ".csc"); File jsFile = new File(testDir, testName + ".js"); File infoFile = new File(testDir, testName + ".info"); final File logFile = new File(testDir, testName + ".log"); /* Overwrite existing test */ if (cscFile.exists() || jsFile.exists() || infoFile.exists()) { s1 = "Overwrite"; s2 = "Cancel"; n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(), "Some output files already exist. Overwrite?", "Test already exist", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, s1); if (n != JOptionPane.YES_OPTION) { return; } } if (cscFile.exists()) { cscFile.delete(); } if (jsFile.exists()) { jsFile.delete(); } if (infoFile.exists()) { infoFile.delete(); } /* Get current simulation configuration */ Element root = new Element("simconf"); Element simulationElement = new Element("simulation"); simulationElement.addContent(simulation.getConfigXML()); root.addContent(simulationElement); /* Strip plugins */ Collection<Element> pluginsConfig = ScriptRunner.this.gui.getPluginsConfigXML(); if (pluginsConfig != null) { 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 */ try { Element newRoot = new SAXBuilder().build(new StringReader(configString)).getRootElement(); newRoot.detach(); Document doc = new Document(newRoot); FileOutputStream out = new FileOutputStream(cscFile); XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat()); outputter.output(doc, out); out.close(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* Export .js */ try { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsFile))); writer.write(scriptTextArea.getText()); writer.close(); } catch (Exception ex) { ex.printStackTrace(); return; } /* Export .info (optional) */ try { if (oldInfo == null) { oldInfo = ""; } String info = (String) JOptionPane.showInputDialog(GUI.getTopParentContainer(), "This text describes the Contiki test and may contain\n" + "information about the simulation setup, radio medium,\n" + "node types, Contiki processes etc.\n\n", "Enter test description", JOptionPane.PLAIN_MESSAGE, null, null, oldInfo); if (info != null && !info.equals("")) { oldInfo = info; BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(infoFile))); writer.write(info); writer.write("\n"); writer.close(); } else { oldInfo = null; } } catch (Exception ex) { ex.printStackTrace(); return; } /* Run exported test (optional) */ s1 = "Run test"; s2 = "No"; n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(), "Run exported test in forked Cooja now?", "Run test?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, s1); if (n != JOptionPane.YES_OPTION) { return; } try { final Process externalCoojaProcess; MessageList testOutput = new MessageList(); final PrintStream normal = testOutput.getInputStream(MessageList.NORMAL); final PrintStream error = testOutput.getInputStream(MessageList.ERROR); 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); progressDialog.setVisible(true); Thread readInput = new Thread(new Runnable() { public void run() { String readLine; try { while ((readLine = input.readLine()) != null) { if (normal != null) { normal.println(readLine); } } } catch (IOException e) { logger.warn("Error while reading from process"); } normal.println(""); normal.println(""); normal.println(""); /* 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 = ""; } normal.println(line); 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 (error != null) { error.println(readLine); } } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); readInput.start(); readError.start(); } catch (IOException e) { e.printStackTrace(); } } public void closePlugin() { if (scriptTester != null) { scriptTester.deactiveScript(); scriptTester.setScriptLogObserver(null); } } public Collection<Element> getConfigXML() { return null; } public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) { return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -