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 org.objectweb.salome_tmf.ihm.datawrapper;
025:
026: import java.awt.Component;
027: import java.io.BufferedWriter;
028: import java.io.File;
029: import java.io.FileWriter;
030: import java.io.IOException;
031: import java.sql.Date;
032: import java.sql.Time;
033: import java.util.ArrayList;
034: import java.util.GregorianCalendar;
035: import java.util.HashMap;
036: import java.util.HashSet;
037: import java.util.Hashtable;
038: import java.util.Iterator;
039: import java.util.Properties;
040: import java.util.Set;
041: import java.util.Vector;
042:
043: import javax.swing.JOptionPane;
044:
045: import org.java.plugin.Extension;
046: import org.objectweb.salome_tmf.api.Api;
047: import org.objectweb.salome_tmf.api.ApiConstants;
048: import org.objectweb.salome_tmf.data.AutomaticTest;
049: import org.objectweb.salome_tmf.data.Campaign;
050: import org.objectweb.salome_tmf.data.ConnectionData;
051: import org.objectweb.salome_tmf.data.DataConstants;
052: import org.objectweb.salome_tmf.data.DataSet;
053: import org.objectweb.salome_tmf.data.Element;
054: import org.objectweb.salome_tmf.data.Environment;
055: import org.objectweb.salome_tmf.data.Execution;
056: import org.objectweb.salome_tmf.data.ExecutionResult;
057: import org.objectweb.salome_tmf.data.ExecutionTestResult;
058: import org.objectweb.salome_tmf.data.ManualTest;
059: import org.objectweb.salome_tmf.data.Parameter;
060: import org.objectweb.salome_tmf.data.Project;
061: import org.objectweb.salome_tmf.data.ProjectData;
062: import org.objectweb.salome_tmf.data.Script;
063: import org.objectweb.salome_tmf.data.Test;
064: import org.objectweb.salome_tmf.ihm.AutomaticExecution;
065: import org.objectweb.salome_tmf.ihm.ManualExecution;
066: import org.objectweb.salome_tmf.ihm.SalomeTMF;
067: import org.objectweb.salome_tmf.ihm.languages.Language;
068: import org.objectweb.salome_tmf.ihm.models.ScriptExecutionException;
069: import org.objectweb.salome_tmf.ihm.tools.Tools;
070: import org.objectweb.salome_tmf.plugins.core.ScriptEngine;
071: import org.objectweb.salome_tmf.plugins.core.TestDriver;
072:
073: public class TestMethods implements ApiConstants, DataConstants {
074:
075: // static CallScript c = null;
076: static ScriptEngine pEngine = null;
077: static TestDriver driver = null;
078:
079: public static String notValuedParamListCreation(HashSet setOfParam,
080: Environment env, DataSet dataSet,
081: ArrayList notValuedParamList) {
082: String message = "";
083: for (Iterator iter = setOfParam.iterator(); iter.hasNext();) {
084: Parameter param = (Parameter) iter.next();
085: if (!env.containsParameter(param)
086: && !dataSet.getParametersHashMap().containsKey(
087: param.getName())) {
088: notValuedParamList.add(param);
089: message = message + "* " + param.getName() + "\n";
090: }
091: }
092: return message;
093: }
094:
095: public static String notValuedParamListInDataSet(
096: HashSet setOfParam, DataSet dataSet,
097: ArrayList notValuedParamList) {
098: String message = "";
099: for (Iterator iter = setOfParam.iterator(); iter.hasNext();) {
100: Parameter param = (Parameter) iter.next();
101: if (!dataSet.getParametersHashMap().containsKey(
102: param.getName())) {
103: notValuedParamList.add(param);
104: message = message + "* " + param.getName() + "\n";
105: }
106: }
107: return message;
108: }
109:
110: /**
111: * M?thode pour appeler un script bsh
112: * @param args les param?tres du script
113: * @param script le script
114: */
115: public static String callScript(HashMap params, Script script,
116: AutomaticTest test, Campaign campaign, Project project,
117: Environment env, ExecutionResult execResult,
118: Execution exec, ExecutionTestResult execTestResult)
119: throws ScriptExecutionException {
120: String result = null;
121: File file = null;
122: String scriptFile = "";
123:
124: try {
125: if (ConnectionData.isConnected()) {
126: try {
127: if (script.getType().equals(POST_SCRIPT)) {
128: //file = ConnectionData.getCampTestSelect().getScriptOfExecution(campaign.getName(), exec.getName(), script.getName(), POST_SCRIPT);
129: file = exec.getExecScriptFromDB(script
130: .getName(), POST_SCRIPT);
131: } else if (script.getType().equals(PRE_SCRIPT)) {
132: //file = ConnectionData.getCampTestSelect().getScriptOfExecution(campaign.getName(), exec.getName(), script.getName(), PRE_SCRIPT);
133: file = exec.getExecScriptFromDB(script
134: .getName(), PRE_SCRIPT);
135: } else if (script.getType().equals(INIT_SCRIPT)) {
136: //file = ConnectionData.getCampTestSelect().getScriptOfEnvironment(env.getName(), script.getName());
137: file = env.getEnvScriptFromDB(script.getName());
138: } else {
139: //file = ConnectionData.getSuiteTestSelect().getScriptOfTest(test.getTestList().getFamily().getName(), test.getTestList().getName(), test.getName(), script.getName());
140: file = test.getTestScriptFromDB(script
141: .getName());
142: }
143: //c = new CallScript(Tools.speedpurge(file.getAbsolutePath()));
144: scriptFile = Tools.speedpurge(file
145: .getAbsolutePath());
146: } catch (Exception exception) {
147: Tools.ihmExceptionView(exception.toString());
148: }
149:
150: } else {
151: //c = new CallScript(Tools.speedpurge(script.getName()));
152: scriptFile = Tools.speedpurge(script.getName());
153: }
154: } catch (Exception e) {
155: if (script.getType().equals(TEST_SCRIPT)) {
156: result = UNKNOWN;
157: fileCreationAndAttachToExecResult(execResult,
158: execTestResult, e.toString(), exec, Language
159: .getInstance().getText("erreur_exec_")
160: + test.getName());
161: } else {
162: throw new ScriptExecutionException(e);
163: }
164: return result;
165: }
166:
167: Hashtable pParams = new Hashtable(params);
168: //if (script.getClassPath() != null) {
169: // obj.eval("addClassPath(\""+ Tools.speedpurge(script.getClassPath()) +"\")");
170: //}
171: pParams.put("date", new Date(GregorianCalendar.getInstance()
172: .getTimeInMillis()));
173: pParams.put("time", new Time(GregorianCalendar.getInstance()
174: .getTimeInMillis()));
175: //On ajoute une r?f?rence sur l'obejt de r?sultat de test
176:
177: //obj.eval("setAccessibility(true)"); // turn off access restrictions
178: pParams.put("salome_projectName", project.getName());
179: pParams.put("salome_ProjectObject", project);
180: pParams.put("salome_debug", new Boolean(false));
181: if (execResult != null) {
182: pParams.put("salome_ExecResultObject", execResult);
183: }
184: if (execTestResult != null) {
185: pParams.put("salome_ExecTestResultObject", execTestResult);
186: }
187: if (campaign != null) {
188: pParams.put("salome_CampagneName", campaign.getName());
189: pParams.put("salome_CampagneObject", campaign);
190: } else {
191: pParams.put("salome_CampagneName", "");
192: }
193: if (env != null) {
194: pParams.put("salome_environmentName", env.getName()); // Add MM
195: pParams.put("salome_environmentObject", env); // Add MM
196: } else {
197: pParams.put("salome_environmentName", ""); // Add MM
198: }
199: if (exec != null) {
200: pParams.put("salome_ExecName", exec.getName()); // Add MM
201: pParams.put("salome_ExecObject", exec); // Add MM
202: } else {
203: pParams.put("salome_ExecName", "");
204: }
205: if (test != null) {
206: pParams.put("salome_TestName", test.getName());
207: pParams.put("salome_TestObject", test);
208: if (test.getTestList() != null) {
209: pParams.put("salome_SuiteTestName", test.getTestList()
210: .getName());
211: pParams.put("salome_SuiteTestObject", test
212: .getTestList());
213: } else {
214: pParams.put("salome_SuiteTestName", "");
215: }
216: if (test.getTestList().getFamily() != null) {
217: pParams.put("salome_FamilyName", test.getTestList()
218: .getFamily().getName());
219: pParams.put("salome_FamilyObject", test.getTestList()
220: .getFamily());
221: } else {
222: pParams.put("salome_FamilyName", "");
223: }
224: } else {
225: pParams.put("salome_TestName", "");
226: pParams.put("salome_SuiteTestName", "");
227: }
228:
229: pParams.put("testLog", "");
230: pParams.put("Verdict", "");
231:
232: // valuation des parametres de tests
233: if (params != null) {
234: Set keysSet = params.keySet();
235: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
236: String paramName;
237: Object o = iter.next();
238: if (o instanceof Element) {
239: paramName = ((Element) o).getName();
240: } else {
241: paramName = (String) o;
242: }
243: Object val = params.get(o);
244: Api.log(Language.getInstance().getText("valeur_de_")
245: + paramName + " = " + val);
246: pParams.put(paramName, params.get(o));
247: }
248: }
249:
250: // Lancement du script
251: int plugScriptType = -1;
252: //Script pScript = null;
253: pEngine = null;
254: driver = null;
255: String plugArg = "";
256: String log;
257: int status = -3;
258: try {
259: if (script.getType().equals(PRE_SCRIPT)) {
260: //script = exec.getInitScript();
261: pEngine = script.getScriptEngine(
262: (Extension) SalomeTMF.associatedExtension
263: .get(script.getScriptExtension()),
264: SalomeTMF.urlSalome, SalomeTMF.jpf);
265: plugScriptType = ScriptEngine.PRE_SCRIPT;
266: plugArg = script.getPlugArg();
267: } else if (script.getType().equals(POST_SCRIPT)) {
268: //script = exec.getPostScript();
269: pEngine = script.getScriptEngine(
270: (Extension) SalomeTMF.associatedExtension
271: .get(script.getScriptExtension()),
272: SalomeTMF.urlSalome, SalomeTMF.jpf);
273: plugScriptType = ScriptEngine.POST_SCRIPT;
274: plugArg = script.getPlugArg();
275: } else if (script.getType().equals(INIT_SCRIPT)) {
276: //script = exec.getPostScript();
277: pEngine = script.getScriptEngine(
278: (Extension) SalomeTMF.associatedExtension
279: .get(script.getScriptExtension()),
280: SalomeTMF.urlSalome, SalomeTMF.jpf);
281: plugScriptType = ScriptEngine.ENV_SCRIPT;
282: plugArg = script.getPlugArg();
283: } else {
284: driver = test.ActivateExtention(
285: (Extension) SalomeTMF.associatedExtension
286: .get(test.getExtension()),
287: SalomeTMF.urlSalome, SalomeTMF.jpf);
288: plugArg = test.getScript().getPlugArg();
289: }
290:
291: if (driver != null) {
292: status = driver.runTest(scriptFile, test, pParams,
293: plugArg);
294: log = driver.getTestLog();
295: // Erreur; -1 : Inconclusif; 2 : Fail; 1 : Pass 0
296: if (status == -1) {
297: result = FAIL;
298: execResult.addFail(1);
299: if (log == null) {
300: log = "unknow";
301: }
302: fileCreationAndAttachToExecResult(execResult,
303: execTestResult, log, exec, Language
304: .getInstance().getText(
305: "erreur_exec_")
306: + test.getName());
307: } else if (status == 0) {
308: result = SUCCESS;
309: execResult.addSuccess(1);
310: }
311: if (status == 1) {
312: result = FAIL;
313: execResult.addFail(1);
314: }
315: if (status == 2) {
316: result = UNKNOWN;
317: execResult.addUnknow(1);
318: }
319: if (log != null && !log.equals("")) {
320: fileCreationAndAttachToExecResult(execResult,
321: execTestResult, log, exec, "log_exec_"
322: + test.getName());
323: }
324: } else if (pEngine != null) {
325: status = pEngine.runScript(plugScriptType, scriptFile,
326: script, pParams, plugArg);
327: //log = pEngine.
328: if (status != 0) {
329: throw new ScriptExecutionException(new Exception(
330: pEngine.getScriptLog()));
331: }
332:
333: } else
334: throw new Exception("No plugin foud");
335: } catch (Exception ePlug) {
336: log = ePlug.toString();
337: if (driver != null) {
338: if (status == -3) {
339: result = FAIL;
340: execResult.addFail(1);
341: }
342: fileCreationAndAttachToExecResult(execResult,
343: execTestResult, log, exec, Language
344: .getInstance().getText("erreur_exec_")
345: + test.getName());
346: } else {
347: if (status == -3) {
348: result = UNKNOWN;
349: execResult.addUnknow(1);
350: }
351: throw new ScriptExecutionException(ePlug);
352: }
353: }
354: Api.log("stop");
355: if (file != null)
356: file.delete();
357:
358: return result;
359: }
360:
361: public static void fileCreationAndAttachToExecResult(
362: ExecutionResult execResult,
363: ExecutionTestResult execTestResult, String text,
364: Execution exec, String fileName) {
365:
366: Properties sys = System.getProperties();
367: String tempDir = sys.getProperty("java.io.tmpdir");
368: String fs = sys.getProperty("file.separator");
369: int num = 0;
370: String pFileName = tempDir + fs + fileName + ".txt";
371: File errorFile = new File(pFileName);
372: while (errorFile.exists()) {
373: pFileName = tempDir + fs + fileName + num + ".txt";
374: errorFile = new File(pFileName);
375: num++;
376: //errorFile.delete();
377: }
378: try {
379: errorFile.createNewFile();
380:
381: //BufferedWriter a besoin d un FileWriter,
382: //les 2 vont ensemble, on donne comme argument le nom du fichier
383: //true signifie qu on ajoute dans le fichier (append), on ne marque pas par dessus
384: FileWriter fw = new FileWriter(pFileName, true);
385:
386: // le BufferedWriter output auquel on donne comme argument le FileWriter fw cree juste au dessus
387: BufferedWriter output = new BufferedWriter(fw);
388:
389: //on marque dans le fichier ou plutot dans le BufferedWriter qui sert comme un tampon(stream)
390: output.write(text);
391: //on peut utiliser plusieurs fois methode write
392:
393: output.flush();
394: //ensuite flush envoie dans le fichier, ne pas oublier cette methode pour le BufferedWriter
395:
396: output.close();
397: //et on le ferme
398:
399: } catch (IOException ioe) {
400: Api.log(Language.getInstance().getText(
401: "erreur_lors_de_la_création_du_fichier_d'erreur:_")
402: + ioe);
403: //on "catch" l exception ici si il y en a une, et on l affiche sur la console
404: return;
405: }
406:
407: if (execTestResult == null && execResult != null) {
408: execResult.addAttchment(errorFile);
409: //execResult.setAttachmentMap(attachMap);
410: } else if (execTestResult != null) {
411: //execTestResult.setAttachmentMap(attachMap);
412: execTestResult.addAttchment(errorFile);
413: }
414:
415: }
416:
417: /**
418: * @return
419: */
420: public static void annulScript() {
421: try {
422: if (driver != null) {
423: driver.stopTest();
424: } else if (pEngine != null) {
425: pEngine.stopScript();
426: }
427: } catch (Exception e) {
428: e.printStackTrace();
429: }
430: }
431:
432: public static void continueExecution(Execution exec,
433: ExecutionResult finalExecResult, Component pComponent,
434: boolean stopOnError) {
435: finalExecResult.setExecutionDate(new Date(GregorianCalendar
436: .getInstance().getTimeInMillis()));
437: finalExecResult.setTester(ProjectData.getCurrentUser()
438: .getLastName()
439: + " " + ProjectData.getCurrentUser().getFirstName());
440:
441: Vector pAllTestToExecute = new Vector();
442: //Liste des tests groupés en sous-listes (manuels/automatiques)
443: ArrayList splittedListOfTest = Tools
444: .getListOfTestManualOrAutomatic(DataModel
445: .getCurrentCampaign().getTestList(),
446: finalExecResult, pAllTestToExecute);
447: Vector listExec = new Vector();
448: // On parcourt la liste des sous-listes
449: for (int h = 0; h < splittedListOfTest.size(); h++) {
450: if (((ArrayList) splittedListOfTest.get(h)).get(0) instanceof ManualTest) {
451: listExec.add(new ManualExecution(
452: (ArrayList) splittedListOfTest.get(h), exec,
453: finalExecResult));
454:
455: } else {
456: //Sous liste de tests automatiques
457: ArrayList automaticTestList = (ArrayList) splittedListOfTest
458: .get(h);
459: listExec.add(new AutomaticExecution(automaticTestList,
460: exec, finalExecResult));
461: }
462: }
463: //new callExecThread(listExec, exec, splittedListOfTest, selectedRowIndex, attachToBeSuppress, false, finalExecResult, null, pAllTestToExecute.toArray()).start();
464: new CallExecThread(listExec, exec, splittedListOfTest, false,
465: finalExecResult, null, pAllTestToExecute.toArray(),
466: pComponent, stopOnError).start();
467:
468: }
469:
470: public static void runExecution(ArrayList pListExec,
471: Component pComponent, boolean stopOnError) {
472: //Campaign pCapaign = exec.getCampagne();
473: Execution pExec;
474: Campaign pCapaign;
475: // il doit y avoir au moins une exécution sélectionnée
476: int nbExec = pListExec.size();
477: if (nbExec > 0) {
478: String message = "";
479: for (int i = 0; i < nbExec; i++) { // FOR 1
480: HashSet setOfParam = new HashSet();
481: pExec = (Execution) pListExec.get(i);
482: pCapaign = pExec.getCampagne();
483: // Récupération de tous les paramétres de la campagne
484: for (int k = 0; k < pCapaign.getTestList().size(); k++) {
485: Test test = (Test) pCapaign.getTestList().get(k);
486: for (int j = 0; j < test.getParameterList().size(); j++) {
487: setOfParam.add(test.getParameterList().get(j));
488: }
489: }
490: // On vérifie que tous les paramètres ont une valuation;
491: ArrayList notValuedParamList = new ArrayList();
492: message = TestMethods.notValuedParamListInDataSet(
493: setOfParam, pExec.getDataSet(),
494: notValuedParamList);
495: if (notValuedParamList.size() > 0) {
496: message = Language.getInstance().getText(
497: "Les_paramètres_:\n")
498: + message
499: + Language.getInstance().getText(
500: "de_l'exécution_<")
501: + pExec.getName()
502: + Language.getInstance().getText(
503: ">_ne_sont_pas_valués.");
504: }
505: }
506:
507: if (!message.equals("") && pComponent != null) {
508: JOptionPane
509: .showMessageDialog(pComponent, Language
510: .getInstance().getText("Attention_!\n")
511: + message, Language.getInstance()
512: .getText("Attention_!"),
513: JOptionPane.INFORMATION_MESSAGE);
514: }
515:
516: CallExecThread precCallExecThread = null;
517: // on parcourt la liste des exécutions selectionnées
518: for (int i = nbExec - 1; i >= 0; i--) { // FOR 1
519: // Execution courante
520: pExec = (Execution) pListExec.get(i);
521: pCapaign = pExec.getCampagne();
522: Api.log(">>>>EXEC LANCEE = " + pExec.getName());
523: // R?sultat d'ex?cution final
524: ExecutionResult finalExecResult = new ExecutionResult();
525: finalExecResult.setNumberOfFail(0);
526: finalExecResult.setNumberOfSuccess(0);
527: finalExecResult.setNumberOfUnknow(0);
528: finalExecResult.setName(pExec.getName() + "_"
529: + pExec.getExecutionResultList().size());
530: finalExecResult.setExecutionDate(new Date(
531: GregorianCalendar.getInstance()
532: .getTimeInMillis()));
533: finalExecResult.setTester(ProjectData.getCurrentUser()
534: .getLastName()
535: + " "
536: + ProjectData.getCurrentUser().getFirstName());
537:
538: Tools.initExecutionResultMap(pCapaign.getTestList(),
539: finalExecResult, pCapaign);
540: Vector pAllTestToExecute = new Vector();
541: ArrayList splittedListOfTest = Tools
542: .getListOfTestManualOrAutomatic(pCapaign
543: .getTestList(), null, pAllTestToExecute);
544: Vector listExec = new Vector();
545: // On parcourt la liste des sous-listes
546: for (int h = 0; h < splittedListOfTest.size(); h++) {
547: if (((ArrayList) splittedListOfTest.get(h)).get(0) instanceof ManualTest) {
548: // Sous liste de tests manuels
549: listExec.add(new ManualExecution(
550: (ArrayList) splittedListOfTest.get(h),
551: pExec, finalExecResult));
552:
553: } else {
554: //Sous liste de tests automatiques
555: ArrayList automaticTestList = (ArrayList) splittedListOfTest
556: .get(h);
557: listExec.add(new AutomaticExecution(
558: automaticTestList, pExec,
559: finalExecResult));
560: }
561: }
562: precCallExecThread = new CallExecThread(listExec,
563: pExec, splittedListOfTest, true,
564: finalExecResult, precCallExecThread,
565: pAllTestToExecute.toArray(), pComponent,
566: stopOnError);
567: //
568: } // FIN FOR 1
569: precCallExecThread.start();
570: } else {
571: if (pComponent != null) {
572: JOptionPane
573: .showMessageDialog(
574: pComponent,
575: Language
576: .getInstance()
577: .getText(
578: "Vous_devez_sélectionner_au_moins_une_exécution..."),
579: Language.getInstance().getText(
580: "Attention_!"),
581: JOptionPane.WARNING_MESSAGE);
582: }
583: }
584: }
585:
586: } // Fin de la classe TestMethods
|