001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Fayçal SOUGRATI, Vincent Pautret, Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package salomeTMF_plug.beanshell;
025:
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.net.URL;
029: import java.util.Enumeration;
030: import java.util.Hashtable;
031:
032: import org.java.plugin.Plugin;
033: import org.java.plugin.PluginDescriptor;
034: import org.java.plugin.PluginManager;
035: import org.objectweb.salome_tmf.api.Util;
036: import org.objectweb.salome_tmf.data.Script;
037: import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
038: import org.objectweb.salome_tmf.plugins.PluginClassLoader;
039: import org.objectweb.salome_tmf.plugins.core.ScriptEngine;
040: import org.objectweb.salome_tmf.plugins.core.TestDriver;
041:
042: import bsh.NameSpace;
043:
044: /**
045: *
046: * @author marchemi
047: */
048: public class BeanShellPlugin extends Plugin implements TestDriver,
049: ScriptEngine {
050:
051: bsh.Interpreter interp = null;
052: bsh.Interpreter edit_inter = null;
053: //ParentClassLoader pParentClassLoader = null;
054: PluginClassLoader pParentClassLoader = null;
055: String tmpLog = "";
056: String testDriverArg = "";
057: org.objectweb.salome_tmf.data.AutomaticTest pTest = null;
058: NameSpace pGlobalNameSpace;
059: NameSpace pTestNameSpace;
060: NameSpace pEditNameSpace = null;
061:
062: boolean runENV_SCRIPT = false;
063:
064: String url_txt = null;
065:
066: /********************* extends Plugin**********************************/
067:
068: /** Creates a new instance of BeanShellPlugin
069: * @param manager
070: * @param descr
071: */
072: public BeanShellPlugin(PluginManager manager, PluginDescriptor descr) {
073: super (manager, descr);
074: }
075:
076: /**
077: * @see org.java.plugin.Plugin()
078: */
079: protected void doStart() throws Exception {
080: // no-op
081: String _urlSalome = SalomeTMFContext.getInstance().getUrlBase()
082: .toString();
083: url_txt = _urlSalome.substring(0, _urlSalome.lastIndexOf("/"));
084:
085: Util
086: .log("[BeanShellPlugin->doStart] : interp = new bsh.Interpreter(); ]");
087: interp = new bsh.Interpreter();
088:
089: Util
090: .log("[BeanShellPlugin->doStart] : interp.setExitOnEOF(false);]");
091: interp.setExitOnEOF(true);
092:
093: Util
094: .log("[BeanShellPlugin->doStart] : set bsh.system.shutdownOnExit = false]");
095: interp.set("bsh.system.shutdownOnExit", false);
096:
097: pGlobalNameSpace = interp.getNameSpace();
098: Util
099: .log("[BeanShellPlugin->doStart] : load salome_testcmd.bsh in "
100: + interp);
101: loadCommand("salome_testcmd.bsh", interp);
102: Util
103: .log("[BeanShellPlugin->doStart] : Create parent Class Loader ");
104: //pParentClassLoader = new ParentClassLoader(this.getClass().getClassLoader());
105: pParentClassLoader = SalomeTMFContext.getInstance()
106: .getSalomeClassLoader();
107: Util
108: .log("[BeanShellPlugin->doStart] : set parent Class Loader "
109: + pParentClassLoader + " to " + interp);
110: interp.set("salome_classloder", pParentClassLoader);
111:
112: }
113:
114: /**
115: * @see org.java.plugin.Plugin()
116: */
117: protected void doStop() throws Exception {
118: // no-op
119: }
120:
121: /*********************** Interface TestDriver ****************************/
122:
123: /**
124: * @see salome.plugins.core.TestDriver
125: */
126: public void initTestDriver(URL urlSalome) {
127: Util.log("Init test driver");
128: String _urlSalome = urlSalome.toString();
129: url_txt = _urlSalome.substring(0, _urlSalome.lastIndexOf("/"));
130: }
131:
132: /**
133: * @see salome.plugins.core.TestDriver
134: */
135: public void getHelp() {
136: Util.log("BeanShell TestDriver");
137: }
138:
139: /**
140: * @see salome.plugins.core.TestDriver
141: */
142: public String getDefaultTestDiverAgument() {
143: return testDriverArg;
144: }
145:
146: /**
147: * @see salome.plugins.core.TestDriver
148: */
149: public String modifyTestDiverAgument(String oldArg) {
150: return testDriverArg;
151: }
152:
153: /**
154: * @see salome.plugins.core.TestDriver
155: */
156: public int runTest(String testScript,
157: org.objectweb.salome_tmf.data.AutomaticTest pTest,
158: Hashtable argTest, String plugArg) throws Exception {
159: if (argTest != null) {
160: interp.set("testargs", argTest);
161: Enumeration e = argTest.keys();
162: while (e.hasMoreElements()) {
163: Object o = e.nextElement();
164: String key = o.toString();
165:
166: //String key = (String) e.nextElement();
167: Object value = argTest.get(o);
168: if (value instanceof java.lang.Boolean) {
169: interp.set(key, ((java.lang.Boolean) value)
170: .booleanValue());
171: } else {
172: interp.set(key, value);
173: }
174: Util.log("[BeanShellPlugin->runTest] : set arg : "
175: + key + " = " + value + " ]");
176: }
177: }
178: //interp.eval(testScript);
179: //interp.set("test", pTest);
180: pTestNameSpace = new NameSpace(interp.getNameSpace(), "testNS");
181: Object res = interp.eval("source(\"" + testScript + "\")",
182: pTestNameSpace);
183: Util.log("[BeanShellPlugin->runTest] : res eval = : " + res
184: + " ]");
185: tmpLog = (String) interp.get("testLog");
186: String verdict = (String) interp.get("Verdict");
187: verdict = verdict.toLowerCase();
188: verdict.trim();
189: Util.log("[BeanShellPlugin->runTest] verdict get : " + verdict);
190: pTestNameSpace.clear();
191: if (verdict.equals("")) {
192: Util.log("[BeanShellPlugin->runTest] return pass");
193: return 0;
194: } else if (verdict.equals("pass")) {
195: Util.log("[BeanShellPlugin->runTest] return pass");
196: return 0;
197: } else if (verdict.equals("fail")) {
198: Util.log("[BeanShellPlugin->runTest] return fail");
199: return 1;
200: } else if (verdict.equals("unconclusif")) {
201: Util.log("[BeanShellPlugin->runTest] return unconclusif");
202: return 2;
203: }
204: Util
205: .log("[BeanShellPlugin->runTest] default return unconclusif for "
206: + verdict);
207: return 2;
208: }
209:
210: /**
211: * @see salome.plugins.core.TestDriver
212: */
213: public String getTestLog() {
214: Util.log("[BeanShellPlugin->getTestLog] : return = : "
215: + tmpLog + " ]");
216: return tmpLog;
217: }
218:
219: /**
220: * @see salome.plugins.core.TestDriver
221: */
222: public void stopTest() throws Exception {
223: pTestNameSpace.clear();
224: interp.eval("exit();");
225: }
226:
227: /**
228: * Odre d'édition du test
229: */
230: public void editTest(String testScript,
231: org.objectweb.salome_tmf.data.AutomaticTest pTest,
232: Hashtable arg, String plugParam) throws Exception {
233: setCommands();
234: /*
235: if (edit_inter == null){
236: edit_inter = new bsh.Interpreter();
237: URL pURL;
238: //pURL = BeanShellPlugin.class.getResource("salome/plugins/beanshell/desktop.bsh");
239: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_workspaceEditor.bsh\");");
240: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_desktop.bsh\");");
241: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_makeWorkspace.bsh\");");
242: }*/
243: if (arg != null) {
244: Enumeration e = arg.keys();
245: interp.set("testargs", arg);
246: while (e.hasMoreElements()) {
247: Object o = e.nextElement();
248: String key = o.toString();
249: //String key = (String) e.nextElement();
250: Object value = arg.get(o);
251: if (value instanceof java.lang.Boolean) {
252: edit_inter.set(key, ((java.lang.Boolean) value)
253: .booleanValue());
254: } else {
255: edit_inter.set(key, value);
256: }
257: Util.log("[BeanShellPlugin->editTest] : set arg : "
258: + key + " = " + value + " ]");
259: }
260: }
261: edit_inter.set("salom_TestObject", pTest);
262: pEditNameSpace = new NameSpace(edit_inter.getNameSpace(),
263: "editNS");
264:
265: if (pEditNameSpace == null) {
266: //pEditNameSpace = new NameSpace(pGlobalNameSpace, "editNS");
267: pEditNameSpace = new NameSpace(edit_inter.getNameSpace(),
268: "editNS");
269: } else {
270: pEditNameSpace.clear();
271: //edit_inter.set("salome_ns", edit_inter.getNameSpace());
272: //pEditNameSpace = new NameSpace(edit_inter.getNameSpace(), "editNS");
273: }
274: //edit_inter.set("salome_ns", edit_inter.getNameSpace());
275:
276: Util
277: .log("[BeanShellPlugin->editTest] : call salome_desktop for : "
278: + testScript + " ]");
279: edit_inter.eval("salome_desktop(\"" + testScript + "\");",
280: pEditNameSpace);
281: Util.log("[BeanShellPlugin->editTest] : return ]");
282:
283: //pEditNameSpace.clear();
284:
285: }
286:
287: /**
288: * Choix d'un test
289: * @return le test choisi
290: */
291: public java.io.File choiceTest(
292: org.objectweb.salome_tmf.data.AutomaticTest pTest) {
293: GuiScriptChoice bshChoice = new GuiScriptChoice(
294: SalomeTMFContext.getInstance().getSalomeFrame());
295: return bshChoice.getSelectedFile();
296: /*java.io.File file = null;
297: javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
298: fileChooser.setApproveButtonText("Valider");
299: fileChooser.addChoosableFileFilter(new BeanShellFileFilter("BeanShell",".bsh"));
300: int returnVal = fileChooser.showOpenDialog(null);
301: if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
302: file = fileChooser.getSelectedFile();
303: }
304: return file;*/
305: }
306:
307: public void onDeleteTestScript(
308: org.objectweb.salome_tmf.data.AutomaticTest pTest) {
309:
310: }
311:
312: public void updateTestScriptFromImport(String testScript,
313: org.objectweb.salome_tmf.data.AutomaticTest pTest) {
314:
315: }
316:
317: /************************** Interface ScriptEngine **********************************/
318:
319: /**
320: * @see salome.plugins.core.ScriptEngine
321: */
322: public void initScriptEngine(URL urlSalome) {
323: Util.log("Init test driver");
324: String _urlSalome = urlSalome.toString();
325: url_txt = _urlSalome.substring(0, _urlSalome.lastIndexOf("/"));
326: }
327:
328: /**
329: * Lancemement du test.
330: * @param type de script ENV_SCRIPT, PRE_SCRIPT, POST_SCRIPT
331: * @return Valeur de terminaison du script (0 : OK, <>0 code d'erreur)
332: */
333: public int runScript(int type, String urlScriptFile,
334: Script pScript, Hashtable arg, String plugParam)
335: throws Exception {
336: if (type == ENV_SCRIPT) {
337: Util
338: .log("[BeanShellPlugin->runScript] : clearNameSpace in ENV_SCRIPT ]");
339: interp.getNameSpace().clear();
340: loadCommand("salome_testcmd.bsh", interp);
341: //loadCommand("addJar.bsh", interp);
342: interp.set("salome_classloder", pParentClassLoader);
343: runENV_SCRIPT = true;
344: } else if (type == PRE_SCRIPT && runENV_SCRIPT == false) {
345: Util
346: .log("[BeanShellPlugin->runScript] : clearNameSpace in PRE_SCRIPT ]");
347: interp.getNameSpace().clear();
348: loadCommand("salome_testcmd.bsh", interp);
349: //loadCommand("addJar.bsh", interp);
350: interp.set("salome_classloder", pParentClassLoader);
351: }
352: try {
353: if (arg != null) {
354: Enumeration e = arg.keys();
355: while (e.hasMoreElements()) {
356: Object o = e.nextElement();
357: Util.log("key elements is : " + o);
358: String key = o.toString();
359: Object value = arg.get(o);
360: if (value instanceof java.lang.Boolean) {
361: interp.set(key, ((java.lang.Boolean) value)
362: .booleanValue());
363: } else {
364: interp.set(key, value);
365: }
366: Util
367: .log("[BeanShellPlugin->runScript] : set arg : "
368: + key + " = " + value + " ]");
369: }
370: }
371: } catch (Exception e) {
372: e.printStackTrace();
373: throw e;
374: }
375: interp.set("script", pScript);
376: Object res = interp.eval("source(\"" + urlScriptFile + "\")");
377: Util.log("[BeanShellPlugin->runScript] : res eval = : " + res
378: + " ]");
379:
380: if (type == POST_SCRIPT) {
381: Util
382: .log("[BeanShellPlugin->runScript] : clearNameSpace in POST_SCRIPT ]");
383: interp.getNameSpace().clear();
384: loadCommand("salome_testcmd.bsh", interp);
385: //loadCommand("addJar.bsh", interp);
386: interp.set("salome_classloder", pParentClassLoader);
387: runENV_SCRIPT = false;
388: }
389: return 0;
390: }
391:
392: /**
393: * Odre d'édition du script
394: * @param type de script ENV_SCRIPT, PRE_SCRIPT, POST_SCRIPT
395: */
396: public void editScript(String urlScriptFile, int type,
397: Script pScript, Hashtable arg, String plugParam)
398: throws Exception {
399: setCommands();
400: /*if (edit_inter == null){
401: edit_inter = new bsh.Interpreter();
402: URL pURL;
403: //pURL = BeanShellPlugin.class.getResource("salome/plugins/beanshell/desktop.bsh");
404: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_workspaceEditor.bsh\");");
405: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_desktop.bsh\");");
406: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_makeWorkspace.bsh\");");
407: }*/
408: if (arg != null) {
409: Enumeration e = arg.keys();
410: while (e.hasMoreElements()) {
411: Object o = e.nextElement();
412: String key = o.toString();
413:
414: //String key = (String) e.nextElement();
415: Object value = arg.get(o);
416: if (value instanceof java.lang.Boolean) {
417: edit_inter.set(key, ((java.lang.Boolean) value)
418: .booleanValue());
419: } else {
420: edit_inter.set(key, value);
421: }
422: Util.log("[BeanShellPlugin->editScript] : set arg : "
423: + key + " = " + value + " ]");
424: }
425: }
426: if (pEditNameSpace == null) {
427: pEditNameSpace = new NameSpace(edit_inter.getNameSpace(),
428: "editNS");
429: } else {
430: pEditNameSpace.clear();
431: }
432: edit_inter.set("script", pScript);
433: Util
434: .log("[BeanShellPlugin->editScript] : call salome_desktop for : "
435: + urlScriptFile + " ]");
436: edit_inter.eval("salome_desktop(\"" + urlScriptFile + "\");",
437: pEditNameSpace);
438: Util.log("[BeanShellPlugin->editScript] : return ]");
439: }
440:
441: /**
442: * This method is called when user want to modify/set free arg of plugin script
443: * @return a string represented arg for test the testDriver
444: */
445: public String modifyEngineAgument(Script pScript) {
446: return "";
447: }
448:
449: public void stopScript() throws Exception {
450: interp.eval("exit()");
451: }
452:
453: public String getScriptLog() {
454: return "";
455: }
456:
457: //************************ Interface Common ***************************************/
458:
459: private void setCommands() throws Exception {
460: if (edit_inter == null) {
461: edit_inter = new bsh.Interpreter();
462: loadCommand("salome_desktop.bsh", edit_inter);
463: loadCommand("salome_makeWorkspace.bsh", edit_inter);
464: loadCommand("salome_workspaceEditor.bsh", edit_inter);
465: loadCommand("salome_testcmd.bsh", edit_inter);
466: edit_inter.set("salome_ns", edit_inter.getNameSpace());
467: //loadCommand("addJar.bsh", edit_inter);
468: edit_inter.set("salome_classloder", pParentClassLoader);
469: }
470: /*edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_workspaceEditor.bsh\");");
471: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_desktop.bsh\");");
472: edit_inter.eval("source(\"/home/marchemi/pri/FTRD/VoiceTesting/WorkDir/VoiceTesting/Dev/BDD_Test/Salome/src/salome/plugins/beanshell/salome_makeWorkspace.bsh\");");
473: */
474: }
475:
476: private void loadCommand(String cmd, bsh.Interpreter pInterpreter)
477: throws Exception {
478: InputStream in = null;
479:
480: try {
481: URL url = new URL(url_txt + "/plugins/beanshell/commands/"
482: + cmd);
483: Util
484: .log("[BeanShellPlugin->loadCommand] : command url is : "
485: + url);
486: in = url.openStream();
487:
488: } catch (Exception e) {
489: e.printStackTrace();
490: Util
491: .log("[BeanShellPlugin->loadCommand] Error when loading URL try to load jar commands");
492: try {
493: in = BeanShellPlugin.class
494: .getResourceAsStream("/plugins/beanshell/commands/"
495: + cmd);
496: } catch (Exception e2) {
497: Util
498: .log("[BeanShellPlugin->loadCommand] Error when loading ResourceAsStream");
499: e2.printStackTrace();
500: throw e2;
501: }
502: }
503: InputStreamReader reader = new InputStreamReader(in);
504: pInterpreter.eval(reader);
505: reader.close();
506: }
507: }
|