/*
* SalomeTMF is a Test Management Framework
* Copyright (C) 2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @author Fayal SOUGRATI, Vincent Pautret, Marche Mikael
*
* Contact: mikael.marche@rd.francetelecom.com
*/
package org.objectweb.salome_tmf.ihm.datawrapper;
import java.awt.Component;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.objectweb.salome_tmf.api.Api;
import org.objectweb.salome_tmf.api.ApiConstants;
import org.objectweb.salome_tmf.data.Action;
import org.objectweb.salome_tmf.data.Attachment;
import org.objectweb.salome_tmf.data.Campaign;
import org.objectweb.salome_tmf.data.ConnectionData;
import org.objectweb.salome_tmf.data.Execution;
import org.objectweb.salome_tmf.data.ExecutionResult;
import org.objectweb.salome_tmf.data.FileAttachment;
import org.objectweb.salome_tmf.data.ManualTest;
import org.objectweb.salome_tmf.data.ProjectData;
import org.objectweb.salome_tmf.data.Test;
import org.objectweb.salome_tmf.data.UrlAttachment;
import org.objectweb.salome_tmf.ihm.AutomaticExecution;
import org.objectweb.salome_tmf.ihm.ExecutionView;
import org.objectweb.salome_tmf.ihm.ManualExecution;
import org.objectweb.salome_tmf.ihm.languages.Language;
import org.objectweb.salome_tmf.ihm.models.ScriptExecutionException;
import org.objectweb.salome_tmf.ihm.tools.Tools;
public class CallExecThread extends Thread implements ApiConstants {
Vector listeExec;
//ArrayList execResultList;
Execution exec;
ArrayList splittedListOfTest;
//int selectedRowIndex;
//HashMap attachToBeSuppress;
boolean newLaunch;
ExecutionResult finalExecResult;
CallExecThread execToLaunch;
boolean stoped;
Object[] ptrTestToBeExec = null;
Campaign pCampaign;
Component pComponent = null;
boolean stopOnError;
public CallExecThread(Vector listeExec, Execution exec, ArrayList splittedListOfTest,
boolean newLaunch, ExecutionResult finalExecResult,
CallExecThread execToLaunch, Object[] ptrTestToBeExec, Component pComponent,
boolean stopOnError) {
pCampaign = exec.getCampagne();
this.stopOnError = stopOnError;
this.finalExecResult = finalExecResult;
this.listeExec = listeExec;
this.exec = exec;
this.splittedListOfTest = splittedListOfTest;
//this.selectedRowIndex = selectedRowIndex;
//this.attachToBeSuppress = attachToBeSuppress;
this.newLaunch = newLaunch;
this.execToLaunch = execToLaunch;
this.pComponent = pComponent;
stoped = false;
this.ptrTestToBeExec = ptrTestToBeExec;
}
public void run(){
// on lock l'arbre des tests
if (ConnectionData.isConnected()) {
ConnectionData.getCampTest().lockTestPlan();
}
Api.log("CallExecThread : start");
AutomaticExecution automaticExecution;
ManualExecution manualExecution;
preExec();
while (!listeExec.isEmpty() && !stoped){
Api.log("CallExecThread : exec un test ");
Object o = listeExec.remove(0);
if (o instanceof AutomaticExecution){
Api.log("Auto Test");
automaticExecution = (AutomaticExecution)o;
automaticExecution.lauchAutomaticExecution();
Api.log("CallExecThread : wait begin " + exec.getName());
while (!automaticExecution.isFinished()){
//wait(500);
yield();
}
Api.log("CallExecThread : wait end " + exec.getName());
automaticExecution.setFinished(false);
} else {
Api.log("Manual Test");
manualExecution = (ManualExecution) o;
manualExecution.lauchManualExecution();
Api.log("CallExecThread : wait begin " + exec.getName());
while (!manualExecution.isFinished()){
//wait(500);
yield();
}
Api.log("CallExecThread : wait end " + exec.getName());
manualExecution.setFinished(false);
}
if (finalExecResult.getExecutionStatus() != null){
stoped = finalExecResult.getExecutionStatus().equals(INTERRUPT);
}
}
Api.log("CallExecThread : do postexec");
postexec();
}
public void do_stop() {
stoped = true;
}
private void preExec() {
Object[] options = {Language.getInstance().getText("Oui"), Language.getInstance().getText("Non")};
// On lance le script de l'environnement
if (exec.getEnvironment().getInitScript() != null) {
try {
HashMap map = new HashMap();
map.putAll(exec.getEnvironment().getParametersHashTable());
TestMethods.callScript(map, exec.getEnvironment().getInitScript(), null, pCampaign, ProjectData.getCurrentProject(), exec.getEnvironment(), finalExecResult, exec, null);
} catch (ScriptExecutionException see) {
if (stopOnError) {
this.do_stop();
} else {
int choice = JOptionPane.showOptionDialog(pComponent,
Language.getInstance().getText("Le_script_d'initialisation_de_l'environnement_ne_s'est_pas_droul_correctement._\nSouhaitez-vous_continuer_?"),
Language.getInstance().getText("Attention_!"),
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[1]);
if (choice == JOptionPane.NO_OPTION) {
if (ConnectionData.isConnected()) {
ConnectionData.getCampTest().unLockTestPlan();
}
this.do_stop();
}
}
}
}
// On lance le script PRE_SCRIPT de l'ex?cution
if (exec.getInitScript() != null) {
try {
TestMethods.callScript(exec.getDataSet().getParametersHashMap(), exec.getInitScript(), null, pCampaign, ProjectData.getCurrentProject(), exec.getEnvironment(), finalExecResult, exec, null);
} catch (ScriptExecutionException see) {
if (stopOnError) {
this.do_stop();
} else {
int choice = JOptionPane.showOptionDialog(pComponent,
Language.getInstance().getText("Le_script_d'initialisation_de_l'excution_ne_s'est_pas_droul_correctement._\nSouhaitez-vous_continuer_?"),
Language.getInstance().getText("Attention_!"),
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[1]);
if (choice == JOptionPane.NO_OPTION) {
if (ConnectionData.isConnected()) {
ConnectionData.getCampTest().unLockTestPlan();
}
//this.stop();
this.do_stop();
}
}
}
}
}
private void postexec() {
finalExecResult.setNumberOfFail(0);
finalExecResult.setNumberOfSuccess(0);
finalExecResult.setNumberOfUnknow(0);
if (!finalExecResult.allTestsHaveStatus())
finalExecResult.setExecutionStatus(INCOMPLETED);
else
finalExecResult.setExecutionStatus(FINISHED);
int transNumber = -1;
try {
// BdD
transNumber = Api.beginTransaction(Api.INSERT_EXECUTION_RESULT);
if (newLaunch) {
finalExecResult.add2DB(exec, ProjectData.getCurrentUser());
} else {
finalExecResult.update2DB(exec, ProjectData.getCurrentUser());
}
for (int index_test = 0; index_test < ptrTestToBeExec.length; index_test++){
Test testKey = (Test)ptrTestToBeExec[index_test];
Api.log("Set status for : " + testKey.getName());
if (!newLaunch) {
finalExecResult.updateExecTestResult2DB(testKey);
} else {
finalExecResult.addExecTestResult2DB(testKey);
}
if (testKey instanceof ManualTest) {
for (int j =0; j < ((ManualTest)testKey).getActionList().size(); j++) {
Action act = (Action)((ManualTest)testKey).getActionList().get(j);
if (newLaunch) {
finalExecResult.addExecActionResult2DB(testKey, act);
}
finalExecResult.updateEffectiveResForActionInDB(testKey.getIdBDD(),act);
}
}
Collection col = finalExecResult.getExecutionTestResult(testKey).getAttachmentMap().values();
for (Iterator iterator = col.iterator(); iterator.hasNext();) {
Attachment attachToTest = (Attachment)iterator.next();
if (attachToTest instanceof FileAttachment) {
finalExecResult.addAttachFile2ExecTestResInDB(testKey.getIdBDD(), (FileAttachment)attachToTest);
} else {
finalExecResult.addAttachUrl2ExecTestResInDB(testKey.getIdBDD(), (UrlAttachment)attachToTest);
}
}
Vector oldAttachFileToExecResult = finalExecResult.getAttachFilesFromDB();
Vector oldAttachUrlToExecResult = finalExecResult.getAttachUrlsFromDB();
col = finalExecResult.getAttachmentMap().values();
for (Iterator iterAttach = col.iterator(); iterAttach.hasNext();) {
Attachment attachToExecRes = (Attachment)iterAttach.next();
if (attachToExecRes instanceof FileAttachment) {
if (oldAttachFileToExecResult == null || !oldAttachFileToExecResult.contains(attachToExecRes.getName())) {
finalExecResult.addAttachFile2DB((FileAttachment)attachToExecRes);
}
} else {
if (oldAttachUrlToExecResult == null || !oldAttachUrlToExecResult.contains(attachToExecRes.getName())) {
finalExecResult.addAttachUrl2DB((UrlAttachment)attachToExecRes);
}
}
}
}
exec.updateLastExecDateInBddAndModel(finalExecResult.getExecutionDate());
Vector timeVector = exec.getResultsTimeFromDB();
finalExecResult.setTime((Time)timeVector.get(timeVector.size()-1));
Api.commitTrans(transNumber);
// IHM
// On modifie l'affichage pour l'ex?cution sel?ctionn?e si besoin
if (pComponent != null && pComponent instanceof ExecutionView) {
((ExecutionView)pComponent).updateIHMTable(newLaunch, exec, finalExecResult);
} else {
Api.log("[CallExecThread] : no IHM update with " + pComponent);
}
if (newLaunch) {
exec.addExecutionResult(finalExecResult);
}
// On modifie la date de derni?re ex?cution
//exec.setLastDate(finalExecResult.getExecutionDate());
} catch (Exception exception) {
Api.forceRollBackTrans(transNumber);
if (pComponent != null)
Tools.ihmExceptionView(exception.toString());
}
// On lance le script POST_SCRIPT de l'ex?cution
if (exec.getPostScript() != null) {
Object[] options = {Language.getInstance().getText("Oui"), Language.getInstance().getText("Non")};
int choice = -1;
try {
if (stoped) {
choice = JOptionPane.showOptionDialog(pComponent,
Language.getInstance().getText("L'excution_est_stoppe,_voulez-vous_excuter_le_script_de_restitution_?"),
Language.getInstance().getText("Attention_!"),
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[1]);
if (choice == JOptionPane.YES_OPTION) {
TestMethods.callScript(exec.getDataSet().getParametersHashMap(), exec.getPostScript(), null, pCampaign, ProjectData.getCurrentProject(), null, null, exec, null);
}
} else {
TestMethods.callScript(exec.getDataSet().getParametersHashMap(), exec.getPostScript(), null, pCampaign, ProjectData.getCurrentProject(), null, null, exec, null);
}
}
catch (ScriptExecutionException see) {
if (pComponent != null) {
JOptionPane.showMessageDialog(pComponent,
Language.getInstance().getText("Le_script_de_restitution_de_l'excution_ne_s'est_pas_droul_correctement."),
Language.getInstance().getText("Attention_!"),
JOptionPane.WARNING_MESSAGE);
}
}
}
// on unlock l'arbre des tests
if (ConnectionData.isConnected()) {
ConnectionData.getCampTest().unLockTestPlan();
}
if (execToLaunch != null && !stoped)
execToLaunch.start();
} // Fin de la mthode postExec/0
}
|