/*
+----------------------------------------------------------------------+
| Class: CLauncher |
| |
| Developper: Eric Gavaldo (eric.gavaldo@xqual.com) |
| Version: 1.7 |
+----------------------------------------------------------------------+
*/
package com.xqual.xlauncher.junit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
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.IConstantsResults;
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 = "{junit } ";
// parameters impacting executing at run time set by the test operator
private String testRootPath;
private String additionalClasspath;
private String javaInstallPath;
private String junitJarPath;
private String junitVersion;
private String junitInterpreter;
private File workingDir;
private static final String JUNIT3_RUNNER_CLASS = "junit.textui.TestRunner";
private static final String JUNIT4_RUNNER_CLASS = "org.junit.runner.JUnitCore";
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
// Y:/XStudio/build/lib/junit-4.7
additionalClasspath = getStringParamValue("General", "Additional classpath"); // i.e. Y:/XStudio/build/lib/javaws-1_2-dev/jnlp.jar
junitVersion = getStringParamValue("JUnit", "JUnit version"); // i.e. JUnit4
javaInstallPath = getStringParamValue("JUnit", "Java install path"); // i.e. C:/Program Files/Java/jdk1.6.0_17
junitJarPath = getStringParamValue("JUnit", "JUnit jar path"); // i.e. Y:/XStudio/build/lib/junit-4.7/junit-4.7.jar
junitInterpreter = CFileUtils.quoteFilePath(javaInstallPath + "/bin/" + JAVA_INTERPRETER_EXE) + " " +
"-classpath " + CFileUtils.quoteFilePath(junitJarPath + CLASSPATH_DELIMITER + additionalClasspath + CLASSPATH_DELIMITER + testRootPath) + " " +
(junitVersion.equalsIgnoreCase("JUnit4") ? JUNIT4_RUNNER_CLASS : JUNIT3_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 jUnitTraceFile = new File("junit_traces.txt");
if (jUnitTraceFile.exists())
jUnitTraceFile.delete();
try {
jUnitTraceFile.createNewFile();
} catch (IOException e) {
traceln(LOG_PRIORITY_SEVERE, "Could not create file \"junit_traces.txt\"...");
}
// +------------------------------------+
// | Run the JUnit test class
// +------------------------------------+
CRunner junitRunner = new CRunner("[" + testId + "] "+ testPath + "." + testName,
junitInterpreter.toString() + testPath + "." + testName,
workingDir);
// here we redirect the output to a file for later parsing
short result = junitRunner.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION, jUnitTraceFile);
// +------------------------------------+
// | Add the output console to the test
// +------------------------------------+
addAttachment(jUnitTraceFile);
// return code of org.junit.runner.JUnitCore: if all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
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("junit_traces.txt");
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"));
}
String line, message;
boolean errorDetected = false;
int errorIndex = 1;
try {
FileInputStream fileInputStream = new FileInputStream(resultFile);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
while ((line = bufferedReader.readLine()) != null) {
line = line.trim();
System.out.println(">" + line);
if (line.startsWith("JUnit version ")) {
executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, line));
} else if (line.startsWith("Could not find class")) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, line));
errorDetected = true;
} else if (line.startsWith("Exception in thread \"main\"")) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, line));
errorDetected = true;
} else if (line.startsWith("Time: ")) {
executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, line));
} else if (line.startsWith("There was") && line.endsWith("failure:")
|| line.startsWith("There were") && line.endsWith("failures:")) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, line));
errorDetected = true;
} else if (line.startsWith(errorIndex + ")")) {
errorDetected = true;
message = line.substring((errorIndex + ")").length() + 1, line.length());
String nextLine = bufferedReader.readLine();
if (nextLine != null)
message = message + nextLine;
executionSteps.add(new CExecutionStep(RESULT_FAILURE, message));
errorIndex++;
} else if (line.startsWith("OK ")) {
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, line));
} else if (line.startsWith("Tests run")) {
executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, line));
} else {
//traceln(LOG_PRIORITY_SEVERE, "unknown tag!");
}
}
} catch (Exception e) {
traceln(LOG_PRIORITY_SEVERE, "exception whle parsing the result file: " + e);
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception while parsing the result file: " + e));
errorDetected = true;
}
if (errorDetected) {
return new CReturnStatus(RESULT_FAILURE, executionSteps);
} else {
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
}
}