/* +----------------------------------------------------------------------+ | Class: CLauncher | | | | Developper: Eric Gavaldo (eric.gavaldo@xqual.com) | | Version: 1.5 | +----------------------------------------------------------------------+ */ package com.xqual.xlauncher.testng; 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; import com.xqual.xcommon.utils.CUtils; /** * The CLauncherImpl implementation of ILauncher for TestNG. * @author egavaldo */ public class CLauncherImpl extends CLauncher implements IConstantsResults { // +==============================================================+ // | Attributes | // +==============================================================+ static final String TRACE_HEADER = "{testng } "; // parameters impacting executing at run time set by the test operator private String testRootPath; private String additionalClasspath; private String javaInstallPath; private String testngJarPath; private String testngInterpreter; private File workingDir; private static final String TESTNG_RUNNER_CLASS = "org.testng.TestNG"; private static final String JAVA_INTERPRETER_EXE = CUtils.getExecutableName("java"); private static final char CLASSPATH_DELIMITER = CUtils.getClasspathDelimiterChar(); // +==============================================================+ // | Constructors | // +==============================================================+ public CLauncherImpl() { super(TRACE_HEADER); } // +==============================================================+ // | Methods | // +==============================================================+ @Override public CReturnStatus initialize(int sutId, String sutName, String sutVersion) { setSutDetails(sutId, sutName, sutVersion); setDefaultTestcaseMustBeCreated(true); // if there is not testcase, let the systems create a default one // check the configuration sent by the manager printConfiguration(); Vector executionSteps = new Vector(); try { // retrieve the parameters we need testRootPath = getStringParamValue("General", "Test root path"); // i.e. Y:/XStudio/build/tmp/eclipse_classes additionalClasspath = getStringParamValue("General", "Additional classpath"); // i.e. Y:/XStudio/build/lib/javaws-1_2-dev/jnlp.jar javaInstallPath = getStringParamValue("TestNG", "Java install path"); // i.e. C:/Progra~1/Java/jdk1.6.0_17 testngJarPath = getStringParamValue("TestNG", "TestNG jar path"); // i.e. Y:/Externals/testng-5.10/testng-5.10-jdk15.jar testngInterpreter = CFileUtils.quoteFilePath(javaInstallPath + "/bin/" + JAVA_INTERPRETER_EXE) + " " + "-classpath " + CFileUtils.quoteFilePath(testngJarPath + CLASSPATH_DELIMITER + additionalClasspath + CLASSPATH_DELIMITER + testRootPath) + " " + TESTNG_RUNNER_CLASS + " "; } catch (CParamParsingException e) { traceln(LOG_PRIORITY_SEVERE, "parsing error during initialization"); executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception during initialize: " + e.getMessage())); return new CReturnStatus(RESULT_FAILURE, executionSteps); } return new CReturnStatus(RESULT_SUCCESS, executionSteps); } @Override public CReturnStatus preRun(int testId, String testPath, String testName, Vector attributes, String additionalInfo) { traceln(LOG_PRIORITY_INFO, "preRun testId=" + testId + " testPath=" + testPath + " [" + testName + "]..."); Vector executionSteps = new Vector(); return new CReturnStatus(RESULT_SUCCESS, executionSteps); } @Override public CReturnStatus run(int testId, String testPath, String testName, int testcaseIndex, String testcaseName, String additionalInfo) { traceln(LOG_PRIORITY_INFO, "run testId=" + testId + " testPath=" + testRootPath + "/" + testPath + "/" + testName + " testcaseIndex=" + testcaseIndex + "..."); Vector executionSteps = new Vector(); workingDir = new File(testRootPath); // parent folder of all the test classes // +------------------------------------+ // | Interpret the script // +------------------------------------+ File testNGTraceFile = new File("testng_traces.txt"); if (testNGTraceFile.exists()) testNGTraceFile.delete(); try { testNGTraceFile.createNewFile(); } catch (IOException e) { traceln(LOG_PRIORITY_SEVERE, "Could not create file \"testng_traces.txt\"..."); } // +------------------------------------+ // | Run the TestNG test class // +------------------------------------+ CRunner testngRunner = new CRunner("[" + testId + "] "+ testPath + "." + testName, testngInterpreter.toString() + " -testclass " + testPath + "." + testName, workingDir); // here we redirect the output to a file for later parsing short result = testngRunner.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION, testNGTraceFile); // +------------------------------------+ // | Add the output console to the test // +------------------------------------+ addAttachment(testNGTraceFile); addAttachment(new File(testRootPath + "/test-output/emailable-report.html")); addAttachment(new File(testRootPath + "/test-output/index.html")); addAttachment(new File(testRootPath + "/test-output/testng-results.xml")); 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); } } @Override public CReturnStatus postRun(int testId, String testPath, String testName) { traceln(LOG_PRIORITY_INFO, "postRun testId=" + testId + " testPath=" + testPath + ":" + testName + "..."); Vector executionSteps = new Vector(); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "postRun: succeeded")); return new CReturnStatus(RESULT_SUCCESS, executionSteps); } @Override public CReturnStatus terminate() { Vector executionSteps = new Vector(); executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "Terminate")); return new CReturnStatus(RESULT_SUCCESS, executionSteps); } // +--------------------------+ // ¦ Utilities ¦ // +--------------------------+ private CReturnStatus parseResultFile(Vector executionSteps) { // parse the result file to get the result and the execution steps File resultFile = new File(testRootPath + "/test-output/testng-results.xml"); 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")); } // parse the result file to get the result and the execution steps Document xmlDocument = CXmlDocumentFactory.createXMLDoc(resultFile); Node rootXmlNode = xmlDocument.getFirstChild(); int nbSkipped; int nbFails; int nbSuccess; try { nbSkipped = CXmlDocumentFactory.getNodeIntegerValueFromXPath(rootXmlNode, "count(//test-method[@status='SKIP'])"); nbFails = CXmlDocumentFactory.getNodeIntegerValueFromXPath(rootXmlNode, "count(//test-method[@status='FAIL'])"); nbSuccess = CXmlDocumentFactory.getNodeIntegerValueFromXPath(rootXmlNode, "count(//test-method[@status='PASS'])"); } catch (NumberFormatException e) { traceln(LOG_PRIORITY_SEVERE, "result file parsing failed !"); return new CReturnStatus(RESULT_FAILURE, executionSteps); } short result = RESULT_FAILURE; if (nbFails == 0 && nbSkipped == 0 && nbSuccess > 0) { result = RESULT_SUCCESS; } else if (nbFails == 0 && nbSuccess == 0) { // whatever nbSkipped result = RESULT_NOT_EXECUTED; } NodeList resultNodeList = CXmlDocumentFactory.getNodeObjListFromXPath(rootXmlNode, "//test-method"); for (int j=0; j