/* +----------------------------------------------------------------------+ | Class: CLauncher | | | | Developper: Eric Gavaldo (eric.gavaldo@xqual.com) | | Version: 1.7 | | License: LGPL (http://www.gnu.org/licenses/lgpl.html) | +----------------------------------------------------------------------+ */ package com.xqual.xlauncher.marathon; 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.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.type.CParam; import com.xqual.xcommon.type.CParamParsingException; import com.xqual.xcommon.utils.CFileUtils; import com.xqual.xcommon.utils.CUtils; /** * The CLauncherImpl implementation of ILauncher for JUnit. * @author egavaldo */ public class CLauncherImpl extends CLauncher implements IConstantsResults { // +==============================================================+ // | Attributes | // +==============================================================+ static final String TRACE_HEADER = "{marathon } "; // parameters impacting executing at run time set by the test operator private String testRootPath; private String marathonHome; private String javaInstallPath; private String marathonClassPath; private String marathonInterpreter; private File workingDir; private static final String JAVA_INTERPRETER_EXE = CUtils.getExecutableName("java"); private static final String MARATHON_RUNNER_CLASS = "net.sourceforge.marathon.Main"; // +==============================================================+ // | Constructors | // +==============================================================+ public CLauncherImpl() { super(TRACE_HEADER); } // +==============================================================+ // | Methods | // +==============================================================+ @Override public CReturnStatus initialize(int sutId, String sutName, String sutVersion) { setSutDetails(sutId, sutName, sutVersion); // 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/lib/marathon-1.2.1.1/examples marathonHome = getStringParamValue("Marathon", "Marathon home"); // i.e. Y:/XStudio/build/lib/marathon-1.2.1.1 marathonClassPath = getStringParamValue("Marathon", "Marathon classpath"); // i.e. Y:/XStudio/build/lib/marathon-1.2.1.1/marathon.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/vldocking_2.1.1.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/rmi-lite.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/forms-1.0.7/forms-1.0.7.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/jaccess-1.3/jaccess.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/junit3.8.2/junit.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/jython-2.2/jython.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/looks-2.0.4/looks-2.0.4.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/jedit-textArea.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/jline-0.9.93.jar;Y:/XStudio/build/lib/marathon-1.2.1.1/Support/vldocking_2.1.5C.jar javaInstallPath = getStringParamValue("Marathon", "Java install path"); // i.e. C:/Progra~1/Java/jdk1.6.0_17 marathonInterpreter = CFileUtils.quoteFilePath(javaInstallPath + "/bin/" + JAVA_INTERPRETER_EXE) + " " + "-classpath " + CFileUtils.quoteFilePath(marathonClassPath) + " " + "-Dmarathon.home=\"" + marathonHome + ".\" " + "-Dpython.home=\"" + marathonHome + ".\" " + MARATHON_RUNNER_CLASS + " " + "-batch " + "-xml " + CFileUtils.quoteFilePath(testRootPath + "/marathon_report.xml") + " " + testRootPath; // project folder } 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 testcaseId, int testcaseIndex, String testcaseName, Vector params, 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 marathonTraceFile = new File("marathon_traces.txt"); if (marathonTraceFile.exists()) marathonTraceFile.delete(); try { marathonTraceFile.createNewFile(); } catch (IOException e) { traceln(LOG_PRIORITY_SEVERE, "Could not create file \"marathon_traces.txt\"..."); } // +------------------------------------+ // | Run the JUnit test class // +------------------------------------+ CRunner marathonRunner = new CRunner("[" + testId + "] "+ testPath + "." + testName, marathonInterpreter.toString() + " " + testPath + "." + testName, workingDir); // here we redirect the output to a file for later parsing short result = marathonRunner.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION, marathonTraceFile); // +------------------------------------+ // | Add the output console to the test // +------------------------------------+ addAttachment(marathonTraceFile); addAttachment(new File(testRootPath + "/marathon_report.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) { short result = RESULT_FAILURE; int nbSkipped = 0; int nbFails = 0; int nbSuccess = 0; // parse the result file to get the result and the execution steps File resultFile = new File(testRootPath + "/marathon_report.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(); NodeList resultNodeList = CXmlDocumentFactory.getNodeObjListFromXPath(rootXmlNode, "//testcase"); for (int j=0; j 0) { result = RESULT_SUCCESS; } else if (nbFails == 0 && nbSuccess == 0) { // whatever nbSkipped result = RESULT_NOT_EXECUTED; } return new CReturnStatus(result, executionSteps); } }