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.main.datawrapper;
025:
026: import java.awt.Component;
027: import java.sql.Time;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: import javax.swing.JOptionPane;
035:
036: import org.objectweb.salome_tmf.api.Api;
037: import org.objectweb.salome_tmf.api.ApiConstants;
038: import org.objectweb.salome_tmf.api.Util;
039: import org.objectweb.salome_tmf.data.Action;
040: import org.objectweb.salome_tmf.data.Attachment;
041: import org.objectweb.salome_tmf.data.Campaign;
042: import org.objectweb.salome_tmf.data.Execution;
043: import org.objectweb.salome_tmf.data.ExecutionResult;
044: import org.objectweb.salome_tmf.data.FileAttachment;
045: import org.objectweb.salome_tmf.data.ManualTest;
046: import org.objectweb.salome_tmf.data.Test;
047: import org.objectweb.salome_tmf.data.UrlAttachment;
048: import org.objectweb.salome_tmf.ihm.languages.Language;
049: import org.objectweb.salome_tmf.ihm.main.AutomaticExecution;
050: import org.objectweb.salome_tmf.ihm.main.ExecutionView;
051: import org.objectweb.salome_tmf.ihm.main.ManualExecution;
052: import org.objectweb.salome_tmf.ihm.main.ProgressBar;
053: import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
054: import org.objectweb.salome_tmf.ihm.models.ScriptExecutionException;
055: import org.objectweb.salome_tmf.ihm.tools.Tools;
056:
057: public class CallExecThread extends Thread implements ApiConstants {
058: Vector listeExec;
059: //ArrayList execResultList;
060: Execution exec;
061: ArrayList splittedListOfTest;
062: //int selectedRowIndex;
063: //HashMap attachToBeSuppress;
064: boolean newLaunch;
065: ExecutionResult finalExecResult;
066: CallExecThread execToLaunch;
067: boolean stoped;
068: Object[] ptrTestToBeExec = null;
069:
070: Campaign pCampaign;
071: Component pComponent = null;
072: boolean stopOnError;
073: boolean onlySave;
074:
075: public CallExecThread(Vector listeExec, Execution exec,
076: ArrayList splittedListOfTest, boolean newLaunch,
077: ExecutionResult finalExecResult,
078: CallExecThread execToLaunch, Object[] ptrTestToBeExec,
079: Component pComponent, boolean stopOnError, boolean onlySave) {
080:
081: pCampaign = exec.getCampagneFromModel();
082: this .stopOnError = stopOnError;
083: this .finalExecResult = finalExecResult;
084: this .listeExec = listeExec;
085: this .exec = exec;
086: this .splittedListOfTest = splittedListOfTest;
087: //this.selectedRowIndex = selectedRowIndex;
088: //this.attachToBeSuppress = attachToBeSuppress;
089: this .newLaunch = newLaunch;
090: this .execToLaunch = execToLaunch;
091: this .pComponent = pComponent;
092: stoped = false;
093: this .ptrTestToBeExec = ptrTestToBeExec;
094: this .onlySave = onlySave;
095: }
096:
097: public void run() {
098: // on lock l'arbre des tests
099:
100: try {
101: Api.lockTestPlan();
102: } catch (Exception e) {
103: if (Api.isDEBUG()) {
104: Util.log("[CallExecThread->run] : Exception " + e);
105: e.printStackTrace();
106: }
107: }
108: if (!onlySave) {
109: Util.log("[CallExecThread] : start");
110: AutomaticExecution automaticExecution;
111: ManualExecution manualExecution;
112: preExec();
113:
114: while (!listeExec.isEmpty() && !stoped) {
115:
116: Util.log("[CallExecThread] exec un test ");
117: Object o = listeExec.remove(0);
118: if (o instanceof AutomaticExecution) {
119: Util.log("[CallExecThread] Auto Test");
120: automaticExecution = (AutomaticExecution) o;
121: automaticExecution.lauchAutomaticExecution();
122: Util.log("[CallExecThread] wait begin "
123: + exec.getNameFromModel());
124: while (!automaticExecution.isFinished()) {
125: //wait(500);
126: yield();
127: try {
128: Thread.sleep(500);
129: } catch (Exception e) {
130:
131: }
132: }
133: Util.log("[CallExecThread] wait end "
134: + exec.getNameFromModel());
135: automaticExecution.setFinished(false);
136: } else {
137: Util.log("[CallExecThread] Manual Test");
138: manualExecution = (ManualExecution) o;
139: manualExecution.lauchManualExecution();
140: Util.log("[CallExecThread] wait begin "
141: + exec.getNameFromModel());
142: while (!manualExecution.isFinished()) {
143: //wait(500);
144: yield();
145: try {
146: Thread.sleep(500);
147: } catch (Exception e) {
148:
149: }
150: }
151: Util.log("[CallExecThread] wait end "
152: + exec.getNameFromModel());
153: manualExecution.setFinished(false);
154:
155: }
156: if (finalExecResult.getExecutionStatusFromModel() != null) {
157: stoped = finalExecResult
158: .getExecutionStatusFromModel().equals(
159: INTERRUPT);
160: }
161: }
162: Util.log("[CallExecThread] do postexec");
163: }
164: postexec();
165: }
166:
167: public void do_stop() {
168: stoped = true;
169: }
170:
171: private void preExec() {
172: Object[] options = { Language.getInstance().getText("Oui"),
173: Language.getInstance().getText("Non") };
174: // On lance le script de l'environnement
175: if (exec.getEnvironmentFromModel().getInitScriptFromModel() != null) {
176: ProgressBar pProgressBar = new ProgressBar(
177: "Env script execution", "", 2);
178: pProgressBar.Show();
179: try {
180: HashMap map = new HashMap();
181: map.putAll(exec.getEnvironmentFromModel()
182: .getParametersHashTableFromModel());
183: pProgressBar.doTask("execute "
184: + exec.getEnvironmentFromModel()
185: .getInitScriptFromModel()
186: .getNameFromModel());
187: TestMethods.callScript(map, exec
188: .getEnvironmentFromModel()
189: .getInitScriptFromModel(), null, pCampaign,
190: DataModel.currentProject, exec
191: .getEnvironmentFromModel(),
192: finalExecResult, exec, null);
193: pProgressBar.finishAllTask();
194: } catch (ScriptExecutionException see) {
195: pProgressBar.finishAllTask();
196: if (Api.isDEBUG()) {
197: see.printStackTrace();
198: }
199: if (stopOnError) {
200: this .do_stop();
201: } else {
202: int choice = SalomeTMFContext
203: .getInstance()
204: .askQuestion(
205: Language
206: .getInstance()
207: .getText(
208: "Le_script_d'initialisation_de_l'environnement_ne_s'est_pas_déroulé_correctement._\nSouhaitez-vous_continuer_?"),
209: Language.getInstance().getText(
210: "Attention_!"),
211: JOptionPane.WARNING_MESSAGE,
212: options);
213:
214: /*int choice = JOptionPane.showOptionDialog(pComponent,
215: Language.getInstance().getText("Le_script_d'initialisation_de_l'environnement_ne_s'est_pas_déroulé_correctement._\nSouhaitez-vous_continuer_?"),
216: Language.getInstance().getText("Attention_!"),
217: JOptionPane.YES_NO_OPTION,
218: JOptionPane.WARNING_MESSAGE,
219: null,
220: options,
221: options[1]);
222: */
223: if (choice == JOptionPane.NO_OPTION) {
224: try {
225: Api.unLockTestPlan();
226: } catch (Exception e) {
227: if (Api.isDEBUG()) {
228: Util
229: .log("[CallExecThread->preExec] : Exception "
230: + e);
231: e.printStackTrace();
232: }
233: }
234: this .do_stop();
235: }
236: }
237: }
238:
239: }
240: // On lance le script PRE_SCRIPT de l'ex?cution
241: if (exec.getPreScriptFromModel() != null) {
242: ProgressBar pProgressBar = new ProgressBar(
243: "Pre script execution", "", 2);
244: pProgressBar.Show();
245: try {
246: pProgressBar.doTask("execute "
247: + exec.getPreScriptFromModel()
248: .getNameFromModel());
249: TestMethods.callScript(exec.getDataSetFromModel()
250: .getParametersHashMapFromModel(), exec
251: .getPreScriptFromModel(), null, pCampaign,
252: DataModel.getCurrentProject(), exec
253: .getEnvironmentFromModel(),
254: finalExecResult, exec, null);
255: pProgressBar.finishAllTask();
256: } catch (ScriptExecutionException see) {
257: pProgressBar.finishAllTask();
258: if (stopOnError) {
259: this .do_stop();
260: } else {
261: int choice = SalomeTMFContext
262: .getInstance()
263: .askQuestion(
264: Language
265: .getInstance()
266: .getText(
267: "Le_script_d'initialisation_de_l'exécution_ne_s'est_pas_déroulé_correctement._\nSouhaitez-vous_continuer_?"),
268: Language.getInstance().getText(
269: "Attention_!"),
270: JOptionPane.WARNING_MESSAGE,
271: options);
272: /*int choice = JOptionPane.showOptionDialog(pComponent,
273: Language.getInstance().getText("Le_script_d'initialisation_de_l'exécution_ne_s'est_pas_déroulé_correctement._\nSouhaitez-vous_continuer_?"),
274: Language.getInstance().getText("Attention_!"),
275: JOptionPane.YES_NO_OPTION,
276: JOptionPane.WARNING_MESSAGE,
277: null,
278: options,
279: options[1]);
280: */
281: if (choice == JOptionPane.NO_OPTION) {
282: try {
283: Api.unLockTestPlan();
284: } catch (Exception e) {
285: if (Api.isDEBUG()) {
286: Util
287: .log("[CallExecThread->preExec] : Exception "
288: + e);
289: e.printStackTrace();
290: }
291: }
292:
293: //this.stop();
294: this .do_stop();
295: }
296: }
297: }
298:
299: }
300:
301: }
302:
303: private void postexec() {
304:
305: finalExecResult.setNumberOfFailInModel(0);
306: finalExecResult.setNumberOfSuccessInModel(0);
307: finalExecResult.setNumberOfUnknowInModel(0);
308: if (!finalExecResult.allTestsHaveStatusInModel())
309: finalExecResult.setExecutionStatusInModel(INCOMPLETED);
310: else
311: finalExecResult.setExecutionStatusInModel(FINISHED);
312: ProgressBar pProgressBar = new ProgressBar("Data Saving", "",
313: ptrTestToBeExec.length + 2);
314: int transNumber = -1;
315: try {
316: /* S */
317: pProgressBar.Show();
318: /* BdD */
319: transNumber = Api.beginTransaction(110,
320: ApiConstants.INSERT_EXECUTION_RESULT);
321: boolean isOnlyAssigned = false;
322: if (finalExecResult.isInBase()) {
323: if (newLaunch) {
324: isOnlyAssigned = true;
325: }
326: newLaunch = false;
327: } else {
328: newLaunch = true;
329: }
330:
331: if (newLaunch) {
332: pProgressBar.doTask("Save execution");
333: //finalExecResult.addInDB(exec, DataModel.currentUser);
334: exec.addExecutionResultInDB(finalExecResult,
335: DataModel.currentUser);
336: } else {
337: pProgressBar.doTask("update execution");
338: finalExecResult.updateInDB(exec, DataModel.currentUser);
339: }
340:
341: for (int index_test = 0; index_test < ptrTestToBeExec.length; index_test++) {
342: pProgressBar.doTask("save test result");
343: Test testKey = (Test) ptrTestToBeExec[index_test];
344: Util.log("[CallExecThread] Set status for : "
345: + testKey.getNameFromModel());
346: if (!newLaunch) {
347: finalExecResult.updateExecTestResultInDB(testKey);
348: } else {
349: finalExecResult.addExecTestResultInDB(testKey);
350: }
351: if (testKey instanceof ManualTest) {
352: ArrayList actionList = ((ManualTest) testKey)
353: .getActionListFromModel(false);
354: for (int j = 0; j < actionList.size(); j++) {
355: Action act = (Action) actionList.get(j);
356: if (newLaunch) {
357: finalExecResult.addExecActionResultInDB(
358: testKey, act);
359: }
360: finalExecResult
361: .updateEffectiveResForActionInDB(
362: testKey, act);
363: }
364: }
365: Collection col = finalExecResult
366: .getExecutionTestResultFromModel(testKey)
367: .getAttachmentMapFromModel().values();
368: for (Iterator iterator = col.iterator(); iterator
369: .hasNext();) {
370: Attachment attachToTest = (Attachment) iterator
371: .next();
372: if (!attachToTest.isInBase()) {
373: if (attachToTest instanceof FileAttachment) {
374: finalExecResult
375: .addAttachFileForExecTestInDB(
376: testKey.getIdBdd(),
377: (FileAttachment) attachToTest);
378: } else {
379: finalExecResult
380: .addAttachUrlForExecTestInDB(
381: testKey.getIdBdd(),
382: (UrlAttachment) attachToTest);
383: }
384: }
385: }
386:
387: Vector oldAttachFileToExecResult = finalExecResult
388: .getAttachFilesFromDB();
389: Vector oldAttachUrlToExecResult = finalExecResult
390: .getAttachUrlsFromDB();
391:
392: col = finalExecResult.getAttachmentMapFromModel()
393: .values();
394: for (Iterator iterAttach = col.iterator(); iterAttach
395: .hasNext();) {
396: Attachment attachToExecRes = (Attachment) iterAttach
397: .next();
398: if (!attachToExecRes.isInBase()) {
399: if (attachToExecRes instanceof FileAttachment) {
400: if (oldAttachFileToExecResult == null
401: || !oldAttachFileToExecResult
402: .contains(attachToExecRes
403: .getNameFromModel())) {
404: finalExecResult
405: .addAttachementInDB(attachToExecRes);
406: }
407: } else {
408: if (oldAttachUrlToExecResult == null
409: || !oldAttachUrlToExecResult
410: .contains(attachToExecRes
411: .getNameFromModel())) {
412: finalExecResult
413: .addAttachementInDB(attachToExecRes);
414: }
415: }
416: }
417: }
418: }
419: pProgressBar.doTask("commit in database");
420: exec.updateLastExecDateInDBAndModel(finalExecResult
421: .getExecutionDateFromModel());
422: Vector timeVector = exec.getResultsTimeFromDB();
423: finalExecResult.setTimeInModel((Time) timeVector
424: .get(timeVector.size() - 1));
425:
426: /* TEST COMIT/ROLBACK*/
427: /*int exp = 0;
428: if (exp ==0){
429: throw new Exception();
430: }*/
431:
432: /*SalomeTMFContext.getInstance().showMessage(
433: Language.getInstance().getText("Le_script_de_restitution_de_l'exécution_ne_s'est_pas_déroulé_correctement."),
434: Language.getInstance().getText("Attention_!"),
435: JOptionPane.WARNING_MESSAGE);*/
436: /*int exp = 0;
437: if (exp ==0){
438: throw new Exception();
439: }*/
440: Api.commitTrans(transNumber);
441:
442: /* EN Cas d'exécution paralle*/
443: try {
444: if (finalExecResult.getNameFromModel().startsWith(
445: "user")
446: && !onlySave) {
447: //finalExecResult.reloadFromDB(true);
448: String status = finalExecResult
449: .getExecutionStatusFromModel();
450: finalExecResult.reloadTestResult();
451: if (!finalExecResult.allTestsHaveStatusInModel()) {
452: finalExecResult
453: .setExecutionStatusInModel(INCOMPLETED);
454: //System.out.println("INCOMPLETED");
455: finalExecResult.updateInDB(exec,
456: DataModel.currentUser);
457: } else {
458: finalExecResult
459: .setExecutionStatusInModel(FINISHED);
460: //System.out.println("FINISHED");
461: finalExecResult.updateInDB(exec,
462: DataModel.currentUser);
463: }
464:
465: }
466: } catch (Exception e1) {
467:
468: }
469: /* EN Cas d'exécution paralle*/
470:
471: transNumber = -1;
472: // IHM
473: // On modifie l'affichage pour l'ex?cution sel?ctionn?e si besoin
474: if (pComponent != null
475: && pComponent instanceof ExecutionView) {
476: boolean realNewLauch = newLaunch || isOnlyAssigned;
477: //System.out.println("............." + newLaunch);
478: ((ExecutionView) pComponent).updateIHMTable(
479: realNewLauch, exec, finalExecResult);
480: } else {
481: Util.log("[CallExecThread] : no IHM update with "
482: + pComponent);
483: }
484: if (newLaunch || isOnlyAssigned) {
485: exec.addExecutionResultInModel(finalExecResult);
486: }
487:
488: // On modifie la date de derni?re ex?cution
489: //exec.setLastDate(finalExecResult.getExecutionDate());
490: pProgressBar.finishAllTask();
491: } catch (Exception exception) {
492: Api.forceRollBackTrans(transNumber);
493: pProgressBar.finishAllTask();
494: if (pComponent != null) {
495: Tools.ihmExceptionView(exception);
496: }
497:
498: }
499:
500: // On lance le script POST_SCRIPT de l'ex?cution
501: if (exec.getPostScriptFromModel() != null) {
502: Object[] options = { Language.getInstance().getText("Oui"),
503: Language.getInstance().getText("Non") };
504: int choice = -1;
505: try {
506: if (stoped) {
507: choice = SalomeTMFContext
508: .getInstance()
509: .askQuestion(
510: Language
511: .getInstance()
512: .getText(
513: "L'exécution_est_stoppée,_voulez-vous_exécuter_le_script_de_restitution_?"),
514: Language.getInstance().getText(
515: "Attention_!"),
516: JOptionPane.WARNING_MESSAGE,
517: options);
518: /*choice = JOptionPane.showOptionDialog(pComponent,
519: Language.getInstance().getText("L'exécution_est_stoppée,_voulez-vous_exécuter_le_script_de_restitution_?"),
520: Language.getInstance().getText("Attention_!"),
521: JOptionPane.YES_NO_OPTION,
522: JOptionPane.WARNING_MESSAGE,
523: null,
524: options,
525: options[1]);
526: */
527: if (choice == JOptionPane.YES_OPTION) {
528: TestMethods.callScript(exec
529: .getDataSetFromModel()
530: .getParametersHashMapFromModel(), exec
531: .getPostScriptFromModel(), null,
532: pCampaign, DataModel.currentProject,
533: null, null, exec, null);
534: }
535: } else {
536: pProgressBar = new ProgressBar(
537: "Post script execution", "", 2);
538: pProgressBar.Show();
539: pProgressBar.doTask("execute "
540: + exec.getPostScriptFromModel()
541: .getNameFromModel());
542: TestMethods.callScript(exec.getDataSetFromModel()
543: .getParametersHashMapFromModel(), exec
544: .getPostScriptFromModel(), null, pCampaign,
545: DataModel.currentProject, null, null, exec,
546: null);
547: pProgressBar.finishAllTask();
548: }
549: } catch (ScriptExecutionException see) {
550: pProgressBar.finishAllTask();
551: if (pComponent != null) {
552: SalomeTMFContext
553: .getInstance()
554: .showMessage(
555: Language
556: .getInstance()
557: .getText(
558: "Le_script_de_restitution_de_l'exécution_ne_s'est_pas_déroulé_correctement."),
559: Language.getInstance().getText(
560: "Attention_!"),
561: JOptionPane.WARNING_MESSAGE);
562: /*JOptionPane.showMessageDialog(pComponent,
563: Language.getInstance().getText("Le_script_de_restitution_de_l'exécution_ne_s'est_pas_déroulé_correctement."),
564: Language.getInstance().getText("Attention_!"),
565: JOptionPane.WARNING_MESSAGE);*/
566: }
567: }
568: }
569: // on unlock l'arbre des tests
570: try {
571: Api.unLockTestPlan();
572: } catch (Exception e) {
573: if (Api.isDEBUG()) {
574: Util.log("[CallExecThread->postexec] : Exception " + e);
575: e.printStackTrace();
576: }
577: }
578:
579: if (execToLaunch != null && !stoped)
580: execToLaunch.start();
581: } // Fin de la méthode postExec/0
582: }
|