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.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.data.Action;
039: import org.objectweb.salome_tmf.data.Attachment;
040: import org.objectweb.salome_tmf.data.Campaign;
041: import org.objectweb.salome_tmf.data.ConnectionData;
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.ProjectData;
047: import org.objectweb.salome_tmf.data.Test;
048: import org.objectweb.salome_tmf.data.UrlAttachment;
049: import org.objectweb.salome_tmf.ihm.AutomaticExecution;
050: import org.objectweb.salome_tmf.ihm.ExecutionView;
051: import org.objectweb.salome_tmf.ihm.ManualExecution;
052: import org.objectweb.salome_tmf.ihm.languages.Language;
053: import org.objectweb.salome_tmf.ihm.models.ScriptExecutionException;
054: import org.objectweb.salome_tmf.ihm.tools.Tools;
055:
056: public class CallExecThread extends Thread implements ApiConstants {
057: Vector listeExec;
058: //ArrayList execResultList;
059: Execution exec;
060: ArrayList splittedListOfTest;
061: //int selectedRowIndex;
062: //HashMap attachToBeSuppress;
063: boolean newLaunch;
064: ExecutionResult finalExecResult;
065: CallExecThread execToLaunch;
066: boolean stoped;
067: Object[] ptrTestToBeExec = null;
068:
069: Campaign pCampaign;
070: Component pComponent = null;
071: boolean stopOnError;
072:
073: public CallExecThread(Vector listeExec, Execution exec,
074: ArrayList splittedListOfTest, boolean newLaunch,
075: ExecutionResult finalExecResult,
076: CallExecThread execToLaunch, Object[] ptrTestToBeExec,
077: Component pComponent, boolean stopOnError) {
078:
079: pCampaign = exec.getCampagne();
080: this .stopOnError = stopOnError;
081: this .finalExecResult = finalExecResult;
082: this .listeExec = listeExec;
083: this .exec = exec;
084: this .splittedListOfTest = splittedListOfTest;
085: //this.selectedRowIndex = selectedRowIndex;
086: //this.attachToBeSuppress = attachToBeSuppress;
087: this .newLaunch = newLaunch;
088: this .execToLaunch = execToLaunch;
089: this .pComponent = pComponent;
090: stoped = false;
091: this .ptrTestToBeExec = ptrTestToBeExec;
092: }
093:
094: public void run() {
095: // on lock l'arbre des tests
096: if (ConnectionData.isConnected()) {
097: ConnectionData.getCampTest().lockTestPlan();
098: }
099:
100: Api.log("CallExecThread : start");
101: AutomaticExecution automaticExecution;
102: ManualExecution manualExecution;
103: preExec();
104:
105: while (!listeExec.isEmpty() && !stoped) {
106:
107: Api.log("CallExecThread : exec un test ");
108: Object o = listeExec.remove(0);
109: if (o instanceof AutomaticExecution) {
110: Api.log("Auto Test");
111: automaticExecution = (AutomaticExecution) o;
112: automaticExecution.lauchAutomaticExecution();
113: Api
114: .log("CallExecThread : wait begin "
115: + exec.getName());
116: while (!automaticExecution.isFinished()) {
117: //wait(500);
118: yield();
119: }
120: Api.log("CallExecThread : wait end " + exec.getName());
121: automaticExecution.setFinished(false);
122: } else {
123: Api.log("Manual Test");
124: manualExecution = (ManualExecution) o;
125: manualExecution.lauchManualExecution();
126: Api
127: .log("CallExecThread : wait begin "
128: + exec.getName());
129: while (!manualExecution.isFinished()) {
130: //wait(500);
131: yield();
132: }
133: Api.log("CallExecThread : wait end " + exec.getName());
134: manualExecution.setFinished(false);
135:
136: }
137: if (finalExecResult.getExecutionStatus() != null) {
138: stoped = finalExecResult.getExecutionStatus().equals(
139: INTERRUPT);
140: }
141: }
142: Api.log("CallExecThread : do postexec");
143: postexec();
144: }
145:
146: public void do_stop() {
147: stoped = true;
148: }
149:
150: private void preExec() {
151: Object[] options = { Language.getInstance().getText("Oui"),
152: Language.getInstance().getText("Non") };
153: // On lance le script de l'environnement
154: if (exec.getEnvironment().getInitScript() != null) {
155: try {
156: HashMap map = new HashMap();
157: map.putAll(exec.getEnvironment()
158: .getParametersHashTable());
159: TestMethods.callScript(map, exec.getEnvironment()
160: .getInitScript(), null, pCampaign, ProjectData
161: .getCurrentProject(), exec.getEnvironment(),
162: finalExecResult, exec, null);
163: } catch (ScriptExecutionException see) {
164: if (stopOnError) {
165: this .do_stop();
166: } else {
167: int choice = JOptionPane
168: .showOptionDialog(
169: pComponent,
170: Language
171: .getInstance()
172: .getText(
173: "Le_script_d'initialisation_de_l'environnement_ne_s'est_pas_déroulé_correctement._\nSouhaitez-vous_continuer_?"),
174: Language.getInstance().getText(
175: "Attention_!"),
176: JOptionPane.YES_NO_OPTION,
177: JOptionPane.WARNING_MESSAGE, null,
178: options, options[1]);
179: if (choice == JOptionPane.NO_OPTION) {
180: if (ConnectionData.isConnected()) {
181: ConnectionData.getCampTest()
182: .unLockTestPlan();
183: }
184: this .do_stop();
185: }
186: }
187: }
188:
189: }
190: // On lance le script PRE_SCRIPT de l'ex?cution
191: if (exec.getInitScript() != null) {
192: try {
193: TestMethods.callScript(exec.getDataSet()
194: .getParametersHashMap(), exec.getInitScript(),
195: null, pCampaign, ProjectData
196: .getCurrentProject(), exec
197: .getEnvironment(), finalExecResult,
198: exec, null);
199: } catch (ScriptExecutionException see) {
200: if (stopOnError) {
201: this .do_stop();
202: } else {
203: int choice = JOptionPane
204: .showOptionDialog(
205: pComponent,
206: Language
207: .getInstance()
208: .getText(
209: "Le_script_d'initialisation_de_l'exécution_ne_s'est_pas_déroulé_correctement._\nSouhaitez-vous_continuer_?"),
210: Language.getInstance().getText(
211: "Attention_!"),
212: JOptionPane.YES_NO_OPTION,
213: JOptionPane.WARNING_MESSAGE, null,
214: options, options[1]);
215: if (choice == JOptionPane.NO_OPTION) {
216: if (ConnectionData.isConnected()) {
217: ConnectionData.getCampTest()
218: .unLockTestPlan();
219: }
220: //this.stop();
221: this .do_stop();
222: }
223: }
224: }
225:
226: }
227:
228: }
229:
230: private void postexec() {
231:
232: finalExecResult.setNumberOfFail(0);
233: finalExecResult.setNumberOfSuccess(0);
234: finalExecResult.setNumberOfUnknow(0);
235: if (!finalExecResult.allTestsHaveStatus())
236: finalExecResult.setExecutionStatus(INCOMPLETED);
237: else
238: finalExecResult.setExecutionStatus(FINISHED);
239:
240: int transNumber = -1;
241: try {
242: // BdD
243: transNumber = Api
244: .beginTransaction(Api.INSERT_EXECUTION_RESULT);
245:
246: if (newLaunch) {
247: finalExecResult.add2DB(exec, ProjectData
248: .getCurrentUser());
249: } else {
250: finalExecResult.update2DB(exec, ProjectData
251: .getCurrentUser());
252: }
253:
254: for (int index_test = 0; index_test < ptrTestToBeExec.length; index_test++) {
255: Test testKey = (Test) ptrTestToBeExec[index_test];
256: Api.log("Set status for : " + testKey.getName());
257: if (!newLaunch) {
258: finalExecResult.updateExecTestResult2DB(testKey);
259: } else {
260: finalExecResult.addExecTestResult2DB(testKey);
261: }
262: if (testKey instanceof ManualTest) {
263: for (int j = 0; j < ((ManualTest) testKey)
264: .getActionList().size(); j++) {
265: Action act = (Action) ((ManualTest) testKey)
266: .getActionList().get(j);
267: if (newLaunch) {
268: finalExecResult.addExecActionResult2DB(
269: testKey, act);
270: }
271: finalExecResult
272: .updateEffectiveResForActionInDB(
273: testKey.getIdBDD(), act);
274: }
275: }
276: Collection col = finalExecResult
277: .getExecutionTestResult(testKey)
278: .getAttachmentMap().values();
279: for (Iterator iterator = col.iterator(); iterator
280: .hasNext();) {
281: Attachment attachToTest = (Attachment) iterator
282: .next();
283: if (attachToTest instanceof FileAttachment) {
284: finalExecResult.addAttachFile2ExecTestResInDB(
285: testKey.getIdBDD(),
286: (FileAttachment) attachToTest);
287: } else {
288: finalExecResult.addAttachUrl2ExecTestResInDB(
289: testKey.getIdBDD(),
290: (UrlAttachment) attachToTest);
291: }
292:
293: }
294:
295: Vector oldAttachFileToExecResult = finalExecResult
296: .getAttachFilesFromDB();
297: Vector oldAttachUrlToExecResult = finalExecResult
298: .getAttachUrlsFromDB();
299:
300: col = finalExecResult.getAttachmentMap().values();
301: for (Iterator iterAttach = col.iterator(); iterAttach
302: .hasNext();) {
303: Attachment attachToExecRes = (Attachment) iterAttach
304: .next();
305: if (attachToExecRes instanceof FileAttachment) {
306: if (oldAttachFileToExecResult == null
307: || !oldAttachFileToExecResult
308: .contains(attachToExecRes
309: .getName())) {
310: finalExecResult
311: .addAttachFile2DB((FileAttachment) attachToExecRes);
312: }
313: } else {
314: if (oldAttachUrlToExecResult == null
315: || !oldAttachUrlToExecResult
316: .contains(attachToExecRes
317: .getName())) {
318: finalExecResult
319: .addAttachUrl2DB((UrlAttachment) attachToExecRes);
320: }
321: }
322: }
323: }
324: exec.updateLastExecDateInBddAndModel(finalExecResult
325: .getExecutionDate());
326: Vector timeVector = exec.getResultsTimeFromDB();
327: finalExecResult.setTime((Time) timeVector.get(timeVector
328: .size() - 1));
329:
330: Api.commitTrans(transNumber);
331:
332: // IHM
333: // On modifie l'affichage pour l'ex?cution sel?ctionn?e si besoin
334: if (pComponent != null
335: && pComponent instanceof ExecutionView) {
336: ((ExecutionView) pComponent).updateIHMTable(newLaunch,
337: exec, finalExecResult);
338: } else {
339: Api.log("[CallExecThread] : no IHM update with "
340: + pComponent);
341: }
342: if (newLaunch) {
343: exec.addExecutionResult(finalExecResult);
344: }
345: // On modifie la date de derni?re ex?cution
346: //exec.setLastDate(finalExecResult.getExecutionDate());
347:
348: } catch (Exception exception) {
349: Api.forceRollBackTrans(transNumber);
350: if (pComponent != null)
351: Tools.ihmExceptionView(exception.toString());
352: }
353:
354: // On lance le script POST_SCRIPT de l'ex?cution
355: if (exec.getPostScript() != null) {
356: Object[] options = { Language.getInstance().getText("Oui"),
357: Language.getInstance().getText("Non") };
358: int choice = -1;
359: try {
360: if (stoped) {
361: choice = JOptionPane
362: .showOptionDialog(
363: pComponent,
364: Language
365: .getInstance()
366: .getText(
367: "L'exécution_est_stoppée,_voulez-vous_exécuter_le_script_de_restitution_?"),
368: Language.getInstance().getText(
369: "Attention_!"),
370: JOptionPane.YES_NO_OPTION,
371: JOptionPane.WARNING_MESSAGE, null,
372: options, options[1]);
373: if (choice == JOptionPane.YES_OPTION) {
374: TestMethods.callScript(exec.getDataSet()
375: .getParametersHashMap(), exec
376: .getPostScript(), null, pCampaign,
377: ProjectData.getCurrentProject(), null,
378: null, exec, null);
379: }
380: } else {
381: TestMethods.callScript(exec.getDataSet()
382: .getParametersHashMap(), exec
383: .getPostScript(), null, pCampaign,
384: ProjectData.getCurrentProject(), null,
385: null, exec, null);
386: }
387: } catch (ScriptExecutionException see) {
388: if (pComponent != null) {
389: JOptionPane
390: .showMessageDialog(
391: pComponent,
392: Language
393: .getInstance()
394: .getText(
395: "Le_script_de_restitution_de_l'exécution_ne_s'est_pas_déroulé_correctement."),
396: Language.getInstance().getText(
397: "Attention_!"),
398: JOptionPane.WARNING_MESSAGE);
399: }
400: }
401: }
402: // on unlock l'arbre des tests
403: if (ConnectionData.isConnected()) {
404: ConnectionData.getCampTest().unLockTestPlan();
405: }
406:
407: if (execToLaunch != null && !stoped)
408: execToLaunch.start();
409: } // Fin de la méthode postExec/0
410: }
|