/*
+----------------------------------------------------------------------+
| Class: CLauncherimpl |
| |
| Developper: Eric Gavaldo (eric.gavaldo@xqual.com) |
| Version: 1.7 |
+----------------------------------------------------------------------+
*/
package com.xqual.xlauncher.visualstudio;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.xqual.xagent.launcher.CExecutionStep;
import com.xqual.xagent.launcher.CLauncher;
import com.xqual.xagent.launcher.CParamParsingException;
import com.xqual.xagent.launcher.CReturnStatus;
import com.xqual.xagent.launcher.runner.CRunner;
import com.xqual.xagent.launcher.runner.IRunner;
import com.xqual.xcommon.CAttribute;
import com.xqual.xcommon.CXmlDocumentFactory;
import com.xqual.xcommon.IConstantsResults;
import com.xqual.xcommon.utils.CFileUtils;
/**
* The CLauncherImpl implementation of ILauncher for Microsoft VisualStudio.
* * @author egavaldo
*/
public class CLauncherImpl extends CLauncher implements IConstantsResults {
// +==============================================================+
// | Attributes |
// +==============================================================+
static final String TRACE_HEADER = "{visual studio } ";
// parameters impacting executing at run time set by the test operator
private String testRootPath;
private String runnerPath;
private String dllName;
private String resultsPath;
private File workingDir;
// +==============================================================+
// | Constructors |
// +==============================================================+
public CLauncherImpl() {
super(TRACE_HEADER);
}
// +==============================================================+
// | Methods |
// +==============================================================+
public CReturnStatus initialize(int sutId, String sutName, String sutVersion) {
setDefaultTestcaseMustBeCreated(true); // if there is not testcase, let the systems create a default one
// check the configuration sent by the manager
printConfiguration();
try {
// retrieve the parameters we need
// section related to the SUT, independant from VisualStudio
testRootPath = getStringParamValue("General", "Test root path"); // i.e. C:/my_test_repository
dllName = getStringParamValue("General", "Library name"); // i.e. my_test.dll
// section related to VisualStudio
runnerPath = getStringParamValue("VisualStudio", "Runner path"); // i.e. C:/Program Files/Microsoft Visual Studio/common7/IDE/MSTest.exe
resultsPath = getStringParamValue("VisualStudio", "Results file path"); // i.e. C:/my_test_repository/testResults.trx
return new CReturnStatus(RESULT_SUCCESS, null);
} catch (CParamParsingException e) {
traceln(LOG_PRIORITY_SEVERE, "parsing error during initialization");
Vector executionSteps = new Vector();
executionSteps.add(new CExecutionStep(RESULT_FAILURE, e.getMessage()));
return new CReturnStatus(RESULT_FAILURE, executionSteps);
}
}
public CReturnStatus preRun(int testId, String testPath, String testName, Vector attributes, String additionalInfo) {
traceln(LOG_PRIORITY_INFO, "preRun testId=" + testId + " testPath=" + testPath + " [" + testName + "]...");
printAttributes(attributes);
return new CReturnStatus(RESULT_SUCCESS, null);
}
public CReturnStatus run(int testId, String testPath, String testName, int testcaseIndex, String testcaseName, String additionalInfo) {
traceln(LOG_PRIORITY_INFO, "run testId=" + testId + " testPath=" + testPath + ":" + testName + " testcaseIndex=" + testcaseIndex + "...");
Vector executionSteps = new Vector();
workingDir = new File(testRootPath);
// +------------------------------------+
// | Interpret the script
// +------------------------------------+
File visualStudioTraceFile = new File("visualstudio_traces.txt");
if (visualStudioTraceFile.exists())
visualStudioTraceFile.delete();
try {
visualStudioTraceFile.createNewFile();
} catch (IOException e) {
traceln(LOG_PRIORITY_SEVERE, "Could not create file \"visualstudio_traces.txt\"...");
}
// +------------------------------------+
// | run the visual studio script |
// +------------------------------------+
// C:/Program Files/Microsoft Visual Studio/common7/IDE/MSTest.exe /testcontainer:my_test.dll /test:folder1.test1 /resultsfile:C:/my_test_repository/testResults.trx
// <----------------------- runnerPath --------------------------> <- dllName -> <----------- resultsPath ----------->
CRunner visualStudioRunner = new CRunner("[" + testId + "] "+ testPath + ":" + testName + "." + testcaseIndex,
CFileUtils.quoteFilePath(runnerPath) + " " +
"/testcontainer:" + dllName + " " +
"/test:" + testPath + "." + testName + " " +
"/resultsfile:" + resultsPath,
workingDir);
short result = visualStudioRunner.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION, visualStudioTraceFile);
// +------------------------------------+
// | Add the output console to the test
// +------------------------------------+
addAttachment(visualStudioTraceFile);
addAttachment(new File(resultsPath));
if (result == RESULT_FAILURE) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "execution process failed"));
parseResultFile(executionSteps); // to have the details anyway
return new CReturnStatus(RESULT_FAILURE, executionSteps);
} else {
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "execution process succeeded"));
return parseResultFile(executionSteps);
}
}
public CReturnStatus postRun(int testId, String testPath, String testName) {
traceln(LOG_PRIORITY_INFO, "postRun testId=" + testId + " testPath=" + testPath + ":" + testName + "...");
return new CReturnStatus(RESULT_SUCCESS, null);
}
public CReturnStatus terminate() {
return new CReturnStatus(RESULT_SUCCESS, null);
}
// +--------------------------+
// ¦ Utilities ¦
// +--------------------------+
/*
private String convertPathSeparator(String input) {
return input.replaceAll("/", "\\\\");
}
*/
private CReturnStatus parseResultFile(Vector executionSteps) {
// parse the result file to get the result and the execution steps
File resultFile = new File(resultsPath);
if (!resultFile.exists()) {
traceln(LOG_PRIORITY_SEVERE, "Result file not found!");
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "run: result file not found!"));
return new CReturnStatus(RESULT_FAILURE, executionSteps);
} else {
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "run: result file found"));
}
boolean errorDetected = false;
Document xmlDocument = CXmlDocumentFactory.createXMLDoc(resultFile);
//Node rootXmlNode = xmlDocument.getFirstChild();
// unknown
int nbTests = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@total");
int nbTestsExecuted = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@executed");
int nbTestsNotExecuted = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@notExecuted");
int nbTestsInProgress = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@inProgress");
int nbTestsPending = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@pending");
// success
int nbTestsPassed = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@passed");
int nbTestsCompleted = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@completed");
// failure
int nbTestsError = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@error");
int nbTestsFailed = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@failed");
int nbTestsTimeout = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@timeout");
int nbTestsAborted = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@aborted");
int nbTestsInconclusive = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@inconclusive");
int nbTestsPassedButRunAborted = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@passedButRunAborted");
int nbTestsNotRunnable = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@notRunnable");
int nbTestsDisconnected = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@disconnected");
int nbTestsWarning = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Counters/@warning");
int timeToExecute = CXmlDocumentFactory.getNodeIntegerValueFromXPath(xmlDocument, "//Times/@finish");
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "timeToExecute:" + timeToExecute));
executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, "nbTests:" + nbTests + " " +
"nbTestsExecuted:" + nbTestsExecuted + " " +
"nbTestsNotExecuted:" + nbTestsNotExecuted + " " +
"nbTestsInProgress:" + nbTestsInProgress + " " +
"nbTestsPending:" + nbTestsPending));
if (nbTestsPassed+nbTestsCompleted>0) {
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "nbTestsPassed:" + nbTestsPassed + " " +
"nbTestsCompleted:" + nbTestsCompleted));
}
if (nbTestsError+nbTestsFailed+nbTestsTimeout+nbTestsAborted+nbTestsInconclusive+nbTestsPassedButRunAborted+nbTestsNotRunnable+nbTestsDisconnected+nbTestsWarning>0) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "nbTestsError:" + nbTestsError + " " +
"nbTestsFailed:" + nbTestsFailed + " " +
"nbTestsTimeout:" + nbTestsTimeout + " " +
"nbTestsAborted:" + nbTestsAborted + " " +
"nbTestsInconclusive:" + nbTestsInconclusive + " " +
"nbTestsPassedButRunAborted:" + nbTestsPassedButRunAborted + " " +
"nbTestsNotRunnable:" + nbTestsNotRunnable + " " +
"nbTestsDisconnected:" + nbTestsDisconnected + " " +
"nbTestsWarning:" + nbTestsWarning));
errorDetected = true;
}
// grab each test
NodeList testNodelist = CXmlDocumentFactory.getNodeObjListFromXPath(xmlDocument, "//ManualTest");
for (int i=0; i< testNodelist.getLength(); i++) {
Node TestNode = testNodelist.item(i);
String id = TestNode.getAttributes().getNamedItem("id").getNodeValue();
String name = TestNode.getAttributes().getNamedItem("name").getNodeValue();
String description = CXmlDocumentFactory.getNodeStringValueFromXPath(TestNode, "Description");
String result = CXmlDocumentFactory.getNodeStringValueFromXPath(xmlDocument, "//ManualTestResult[@testId='" + id + "']/@outcome");
executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, "id:" + id + " " +
"name:" + name + " " +
"description:" + description + " " +
"result:" + result));
}
if (errorDetected) {
return new CReturnStatus(RESULT_FAILURE, executionSteps);
} else {
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
}
}