use of org.eclipse.che.plugin.testing.junit.server.listener.OutputTestListener in project che by eclipse.
the class JUnitTestRunner method run3xTestClasses.
private TestResult run3xTestClasses(Class<?>... classes) throws Exception {
ClassLoader classLoader = projectClassLoader;
Class<?> clsTestSuite = Class.forName("junit.framework.TestSuite", true, classLoader);
Class<?> clsTestResult = Class.forName("junit.framework.TestResult", true, classLoader);
Class<?> clsThrowable = Class.forName("java.lang.Throwable", true, classLoader);
Class<?> clsStackTraceElement = Class.forName("java.lang.StackTraceElement", true, classLoader);
Class<?> clsFailure = Class.forName("junit.framework.TestFailure", true, classLoader);
Object testSuite = clsTestSuite.getConstructor().newInstance();
Object testResult = clsTestResult.getConstructor().newInstance();
Class<?> clsTestListener = Class.forName("junit.framework.TestListener", true, classLoader);
try (OutputTestListener outputListener = new OutputTestListener(this.getClass().getName() + ".run3xTestClasses")) {
Object testListener = create3xTestListener(classLoader, clsTestListener, outputListener);
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(projectClassLoader);
clsTestResult.getMethod("addListener", clsTestListener).invoke(testResult, testListener);
for (Class<?> testClass : classes) {
clsTestSuite.getMethod("addTestSuite", Class.class).invoke(testSuite, testClass);
}
clsTestSuite.getMethod("run", clsTestResult).invoke(testSuite, testResult);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
clsTestResult.getMethod("removeListener", clsTestListener).invoke(testResult, testListener);
}
}
TestResult dtoResult = DtoFactory.getInstance().createDto(TestResult.class);
boolean isSuccess = (Boolean) clsTestResult.getMethod("wasSuccessful").invoke(testResult);
Enumeration<?> failures = (Enumeration<?>) clsTestResult.getMethod("failures").invoke(testResult);
List<Failure> jUnitFailures = new ArrayList<>();
while (failures.hasMoreElements()) {
Failure dtoFailure = DtoFactory.getInstance().createDto(Failure.class);
Object failure = failures.nextElement();
String message = (String) clsFailure.getMethod("exceptionMessage").invoke(failure);
String trace = (String) clsFailure.getMethod("trace").invoke(failure);
Object failClassObject = clsFailure.getMethod("failedTest").invoke(failure);
String failClassName = failClassObject.getClass().getName();
Object exception = clsFailure.getMethod("thrownException").invoke(failure);
Object stackTrace = clsThrowable.getMethod("getStackTrace").invoke(exception);
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;
}
}
}
dtoFailure.setFailingClass(failClassName);
dtoFailure.setFailingMethod(failMethod);
dtoFailure.setFailingLine(failLine);
dtoFailure.setMessage(message);
dtoFailure.setTrace(trace);
jUnitFailures.add(dtoFailure);
}
dtoResult.setTestFramework("JUnit3x");
dtoResult.setSuccess(isSuccess);
dtoResult.setFailureCount(jUnitFailures.size());
dtoResult.setFailures(jUnitFailures);
return dtoResult;
}
use of org.eclipse.che.plugin.testing.junit.server.listener.OutputTestListener in project che by eclipse.
the class JUnitTestRunner method run4xTestClasses.
private TestResult run4xTestClasses(Class<?>... classes) throws Exception {
ClassLoader classLoader = projectClassLoader;
Class<?> clsJUnitCore = Class.forName("org.junit.runner.JUnitCore", true, classLoader);
Class<?> clsResult = Class.forName("org.junit.runner.Result", true, classLoader);
Class<?> clsFailure = Class.forName("org.junit.runner.notification.Failure", true, classLoader);
Class<?> clsDescription = Class.forName("org.junit.runner.Description", true, classLoader);
Class<?> clsThrowable = Class.forName("java.lang.Throwable", true, classLoader);
Class<?> clsStackTraceElement = Class.forName("java.lang.StackTraceElement", true, classLoader);
Class<?> clsTestRunner = Class.forName("org.junit.runner.notification.RunListener", true, classLoader);
Object jUnitCore = clsJUnitCore.getConstructor().newInstance();
Object result;
try (OutputTestListener outputListener = new OutputTestListener(this.getClass().getName() + ".run4xTestClasses")) {
Object testListener = create4xTestListener(classLoader, clsTestRunner, outputListener);
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(projectClassLoader);
clsJUnitCore.getMethod("addListener", clsTestRunner).invoke(jUnitCore, testListener);
result = clsJUnitCore.getMethod("run", Class[].class).invoke(jUnitCore, new Object[] { classes });
} finally {
Thread.currentThread().setContextClassLoader(tccl);
clsJUnitCore.getMethod("removeListener", clsTestRunner).invoke(jUnitCore, testListener);
}
}
TestResult dtoResult = DtoFactory.getInstance().createDto(TestResult.class);
boolean isSuccess = (Boolean) clsResult.getMethod("wasSuccessful").invoke(result);
List<?> failures = (List<?>) clsResult.getMethod("getFailures").invoke(result);
List<Failure> jUnitFailures = new ArrayList<>();
for (Object failure : failures) {
Failure dtoFailure = DtoFactory.getInstance().createDto(Failure.class);
String message = (String) clsFailure.getMethod("getMessage").invoke(failure);
Object description = clsFailure.getMethod("getDescription").invoke(failure);
String failClassName = (String) clsDescription.getMethod("getClassName").invoke(description);
Object exception = clsFailure.getMethod("getException").invoke(failure);
Object stackTrace = clsThrowable.getMethod("getStackTrace").invoke(exception);
String failMethod = "";
Integer failLine = null;
if (stackTrace.getClass().isArray()) {
int length = Array.getLength(stackTrace);
for (int i = 0; i < length; i++) {
Object stackElement = Array.get(stackTrace, i);
String failClass = (String) clsStackTraceElement.getMethod("getClassName").invoke(stackElement);
if (failClass.equals(failClassName)) {
failMethod = (String) clsStackTraceElement.getMethod("getMethodName").invoke(stackElement);
failLine = (Integer) clsStackTraceElement.getMethod("getLineNumber").invoke(stackElement);
break;
}
}
}
String trace = (String) clsFailure.getMethod("getTrace").invoke(failure);
dtoFailure.setFailingClass(failClassName);
dtoFailure.setFailingMethod(failMethod);
dtoFailure.setFailingLine(failLine);
dtoFailure.setMessage(message);
dtoFailure.setTrace(trace);
jUnitFailures.add(dtoFailure);
}
dtoResult.setTestFramework("JUnit4x");
dtoResult.setSuccess(isSuccess);
dtoResult.setFailureCount(jUnitFailures.size());
dtoResult.setFailures(jUnitFailures);
return dtoResult;
}
Aggregations