use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.
the class TestingService method run.
/**
* Execute the Java test cases and return the test result.
*
* <pre>
* Required request parameters.
* <em>projectPath</em> : Relative path to the project directory.
* <em>testFramework</em> : Name of the test framework where the tests should be run on. This should match with
* the name returned by {@link TestRunner#getName()} implementation.
* </pre>
*
* @param uriInfo
* JAX-RS implementation of UrlInfo with set of query parameters.
* @return the test result of test case
* @throws Exception
* when the test runner failed to execute test cases.
*/
@GET
@Path("run")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Execute Java tests and return results", notes = "The GET parameters are passed to the test framework implementation.")
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Server error") })
public TestResult run(@Context UriInfo uriInfo) throws Exception {
Map<String, String> queryParameters = getMap(uriInfo.getQueryParameters());
String projectPath = queryParameters.get("projectPath");
String absoluteProjectPath = ResourcesPlugin.getPathToWorkspace() + projectPath;
queryParameters.put("absoluteProjectPath", absoluteProjectPath);
String testFramework = queryParameters.get("testFramework");
TestRunner runner = frameworkRegistry.getTestRunner(testFramework);
if (runner == null) {
throw new Exception("No test frameworks found: " + testFramework);
}
TestResult result = frameworkRegistry.getTestRunner(testFramework).execute(queryParameters);
return result;
}
use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.
the class TestNGRunner method runTestXML.
private TestResult runTestXML(String projectAbsolutePath, String xmlPath) throws Exception {
ClassLoader classLoader = projectClassLoader;
Class<?> clsTestNG = Class.forName("org.testng.TestNG", true, classLoader);
Class<?> clsTestListner = Class.forName("org.testng.TestListenerAdapter", true, classLoader);
Class<?> clsITestListner = Class.forName("org.testng.ITestListener", true, classLoader);
Class<?> clsResult = Class.forName("org.testng.ITestResult", true, classLoader);
Class<?> clsIClass = Class.forName("org.testng.IClass", true, classLoader);
Class<?> clsThrowable = Class.forName("java.lang.Throwable", true, classLoader);
Class<?> clsStackTraceElement = Class.forName("java.lang.StackTraceElement", true, classLoader);
Object testNG = clsTestNG.newInstance();
Object testListner = clsTestListner.newInstance();
clsTestNG.getMethod("addListener", clsITestListner).invoke(testNG, testListner);
List<String> testSuites = new ArrayList<>();
testSuites.add(xmlPath);
clsTestNG.getMethod("setTestSuites", List.class).invoke(testNG, testSuites);
clsTestNG.getMethod("setOutputDirectory", String.class).invoke(testNG, Paths.get(projectAbsolutePath, "target", "testng-out").toString());
clsTestNG.getMethod("run").invoke(testNG);
List<?> failures = (List<?>) clsTestListner.getMethod("getFailedTests").invoke(testListner);
TestResult dtoResult = DtoFactory.getInstance().createDto(TestResult.class);
boolean isSuccess = (failures.size() == 0);
List<Failure> testNGFailures = new ArrayList<>();
for (Object failure : failures) {
Failure dtoFailure = DtoFactory.getInstance().createDto(Failure.class);
Object throwable = clsResult.getMethod("getThrowable").invoke(failure);
String message = (String) clsThrowable.getMethod("getMessage").invoke(throwable);
Object failingClass = clsResult.getMethod("getTestClass").invoke(failure);
String failClassName = (String) clsIClass.getMethod("getName").invoke(failingClass);
Object stackTrace = clsThrowable.getMethod("getStackTrace").invoke(throwable);
String failMethod = "";
Integer failLine = null;
if (stackTrace.getClass().isArray()) {
int length = Array.getLength(stackTrace);
for (int i = 0; i < length; i++) {
Object arrayElement = Array.get(stackTrace, i);
String failClass = (String) clsStackTraceElement.getMethod("getClassName").invoke(arrayElement);
if (failClass.equals(failClassName)) {
failMethod = (String) clsStackTraceElement.getMethod("getMethodName").invoke(arrayElement);
failLine = (Integer) clsStackTraceElement.getMethod("getLineNumber").invoke(arrayElement);
break;
}
}
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
clsThrowable.getMethod("printStackTrace", PrintWriter.class).invoke(throwable, pw);
String trace = sw.toString();
dtoFailure.setFailingClass(failClassName);
dtoFailure.setFailingMethod(failMethod);
dtoFailure.setFailingLine(failLine);
dtoFailure.setMessage(message);
dtoFailure.setTrace(trace);
testNGFailures.add(dtoFailure);
}
dtoResult.setTestFramework("TestNG");
dtoResult.setSuccess(isSuccess);
dtoResult.setFailureCount(testNGFailures.size());
dtoResult.setFailures(testNGFailures);
return dtoResult;
}
use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.
the class TestNGRunner method runTestClasses.
private TestResult runTestClasses(String projectAbsolutePath, Class<?>... classes) throws Exception {
ClassLoader classLoader = projectClassLoader;
Class<?> clsTestNG = Class.forName("org.testng.TestNG", true, classLoader);
Class<?> clsTestListner = Class.forName("org.testng.TestListenerAdapter", true, classLoader);
Class<?> clsITestListner = Class.forName("org.testng.ITestListener", true, classLoader);
Class<?> clsResult = Class.forName("org.testng.ITestResult", true, classLoader);
Class<?> clsIClass = Class.forName("org.testng.IClass", true, classLoader);
Class<?> clsThrowable = Class.forName("java.lang.Throwable", true, classLoader);
Class<?> clsStackTraceElement = Class.forName("java.lang.StackTraceElement", true, classLoader);
Object testNG = clsTestNG.newInstance();
Object testListner = clsTestListner.newInstance();
clsTestNG.getMethod("addListener", clsITestListner).invoke(testNG, testListner);
clsTestNG.getMethod("setTestClasses", Class[].class).invoke(testNG, new Object[] { classes });
clsTestNG.getMethod("setOutputDirectory", String.class).invoke(testNG, Paths.get(projectAbsolutePath, "target", "testng-out").toString());
clsTestNG.getMethod("run").invoke(testNG);
List<?> failures = (List<?>) clsTestListner.getMethod("getFailedTests").invoke(testListner);
TestResult dtoResult = DtoFactory.getInstance().createDto(TestResult.class);
boolean isSuccess = (failures.size() == 0);
List<Failure> testNGFailures = new ArrayList<>();
for (Object failure : failures) {
Failure dtoFailure = DtoFactory.getInstance().createDto(Failure.class);
Object throwable = clsResult.getMethod("getThrowable").invoke(failure);
String message = (String) clsThrowable.getMethod("getMessage").invoke(throwable);
Object failingClass = clsResult.getMethod("getTestClass").invoke(failure);
String failClassName = (String) clsIClass.getMethod("getName").invoke(failingClass);
Object stackTrace = clsThrowable.getMethod("getStackTrace").invoke(throwable);
String failMethod = "";
Integer failLine = null;
if (stackTrace.getClass().isArray()) {
int length = Array.getLength(stackTrace);
for (int i = 0; i < length; i++) {
Object arrayElement = Array.get(stackTrace, i);
String failClass = (String) clsStackTraceElement.getMethod("getClassName").invoke(arrayElement);
if (failClass.equals(failClassName)) {
failMethod = (String) clsStackTraceElement.getMethod("getMethodName").invoke(arrayElement);
failLine = (Integer) clsStackTraceElement.getMethod("getLineNumber").invoke(arrayElement);
break;
}
}
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
clsThrowable.getMethod("printStackTrace", PrintWriter.class).invoke(throwable, pw);
String trace = sw.toString();
dtoFailure.setFailingClass(failClassName);
dtoFailure.setFailingMethod(failMethod);
dtoFailure.setFailingLine(failLine);
dtoFailure.setMessage(message);
dtoFailure.setTrace(trace);
testNGFailures.add(dtoFailure);
}
dtoResult.setTestFramework("TestNG");
dtoResult.setSuccess(isSuccess);
dtoResult.setFailureCount(testNGFailures.size());
dtoResult.setFailures(testNGFailures);
return dtoResult;
}
use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.
the class TestServiceClient method runTestsAfterCompilation.
Promise<TestResult> runTestsAfterCompilation(String projectPath, String testFramework, Map<String, String> parameters, StatusNotification statusNotification, Promise<CommandImpl> compileCommand) {
return compileCommand.thenPromise(command -> {
final Machine machine;
if (command == null) {
machine = null;
} else {
machine = appContext.getDevMachine().getDescriptor();
}
if (machine == null) {
if (statusNotification != null) {
statusNotification.setContent("Executing the tests without preliminary compilation.");
}
return sendTests(projectPath, testFramework, parameters);
}
if (statusNotification != null) {
statusNotification.setContent("Compiling the project before starting the test session.");
}
return promiseFromExecutorBody(new ExecutorBody<TestResult>() {
boolean compiled = false;
@Override
public void apply(final ResolveFunction<TestResult> resolve, RejectFunction reject) {
macroProcessor.expandMacros(command.getCommandLine()).then(new Operation<String>() {
@Override
public void apply(String expandedCommandLine) throws OperationException {
CommandImpl expandedCommand = new CommandImpl(command.getName(), expandedCommandLine, command.getType(), command.getAttributes());
final CommandOutputConsole console = commandConsoleFactory.create(expandedCommand, machine);
final String machineId = machine.getId();
processesPanelPresenter.addCommandOutput(machineId, console);
execAgentCommandManager.startProcess(machineId, expandedCommand).then(startResonse -> {
if (!startResonse.getAlive()) {
reject.apply(promiseFromThrowable(new Throwable(PROJECT_BUILD_NOT_STARTED_MESSAGE)));
}
}).thenIfProcessStartedEvent(console.getProcessStartedOperation()).thenIfProcessStdErrEvent(evt -> {
if (evt.getText().contains("BUILD SUCCESS")) {
compiled = true;
}
console.getStdErrOperation().apply(evt);
}).thenIfProcessStdOutEvent(evt -> {
if (evt.getText().contains("BUILD SUCCESS")) {
compiled = true;
}
console.getStdOutOperation().apply(evt);
}).thenIfProcessDiedEvent(evt -> {
console.getProcessDiedOperation().apply(evt);
if (compiled) {
if (statusNotification != null) {
statusNotification.setContent(EXECUTING_TESTS_MESSAGE);
}
sendTests(projectPath, testFramework, parameters).then(new Operation<TestResult>() {
@Override
public void apply(TestResult result) throws OperationException {
resolve.apply(result);
}
});
} else {
reject.apply(promiseFromThrowable(new Throwable(PROJECT_BUILD_FAILED_MESSAGE)));
}
});
}
});
}
});
});
}
use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.
the class JUnitTestRunner method execute.
/**
* {@inheritDoc}
*/
@Override
public TestResult execute(Map<String, String> testParameters) throws Exception {
String projectAbsolutePath = testParameters.get("absoluteProjectPath");
boolean updateClasspath = Boolean.valueOf(testParameters.get("updateClasspath"));
boolean runClass = Boolean.valueOf(testParameters.get("runClass"));
String projectPath = testParameters.get("projectPath");
String projectType = "";
if (projectManager != null) {
projectType = projectManager.getProject(projectPath).getType();
}
ClassLoader currentClassLoader = this.getClass().getClassLoader();
TestClasspathProvider classpathProvider = classpathRegistry.getTestClasspathProvider(projectType);
URLClassLoader providedClassLoader = (URLClassLoader) classpathProvider.getClassLoader(projectAbsolutePath, projectPath, updateClasspath);
projectClassLoader = new URLClassLoader(providedClassLoader.getURLs(), null) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if (name.startsWith("javassist.")) {
return currentClassLoader.loadClass(name);
}
return super.findClass(name);
}
};
boolean isJUnit4Compatible = false;
boolean isJUnit3Compatible = false;
try {
Class.forName(JUNIT4X_RUNNER_CLASS, true, projectClassLoader);
isJUnit4Compatible = true;
} catch (Exception ignored) {
}
try {
Class.forName(JUNIT3X_RUNNER_CLASS, true, projectClassLoader);
isJUnit3Compatible = true;
} catch (Exception ignored) {
}
boolean useJUnitV3API = false;
if (!isJUnit4Compatible) {
if (!isJUnit3Compatible) {
throw new ClassNotFoundException("JUnit classes not found in the following project classpath: " + Arrays.asList(providedClassLoader.getURLs()));
} else {
useJUnitV3API = true;
}
}
String currentWorkingDir = System.getProperty("user.dir");
try {
System.setProperty("user.dir", projectAbsolutePath);
TestResult testResult;
if (runClass) {
String fqn = testParameters.get("fqn");
testResult = useJUnitV3API ? run3x(fqn) : run4x(fqn);
} else {
testResult = useJUnitV3API ? runAll3x(projectAbsolutePath) : runAll4x(projectAbsolutePath);
}
return testResult;
} finally {
System.setProperty("user.dir", currentWorkingDir);
}
}
Aggregations