Search in sources :

Example 1 with Failure

use of org.eclipse.che.api.testing.shared.Failure 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;
}
Also used : ArrayList(java.util.ArrayList) TestResult(org.eclipse.che.api.testing.shared.TestResult) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.eclipse.che.api.testing.shared.Failure) PrintWriter(java.io.PrintWriter)

Example 2 with Failure

use of org.eclipse.che.api.testing.shared.Failure 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;
}
Also used : ArrayList(java.util.ArrayList) TestResult(org.eclipse.che.api.testing.shared.TestResult) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.eclipse.che.api.testing.shared.Failure) PrintWriter(java.io.PrintWriter)

Example 3 with Failure

use of org.eclipse.che.api.testing.shared.Failure 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;
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) TestResult(org.eclipse.che.api.testing.shared.TestResult) OutputTestListener(org.eclipse.che.plugin.testing.junit.server.listener.OutputTestListener) URLClassLoader(java.net.URLClassLoader) Failure(org.eclipse.che.api.testing.shared.Failure)

Example 4 with Failure

use of org.eclipse.che.api.testing.shared.Failure in project che by eclipse.

the class TestResultViewImpl method buildTree.

private void buildTree() {
    resultTree.getNodeStorage().clear();
    outputResult.setText("");
    TestResultGroupNode root = nodeFactory.getTestResultGroupNode(lastTestResult);
    HashMap<String, List<Node>> classNodeHashMap = new HashMap<>();
    for (Failure failure : lastTestResult.getFailures()) {
        if (!classNodeHashMap.containsKey(failure.getFailingClass())) {
            List<Node> methodNodes = new ArrayList<>();
            classNodeHashMap.put(failure.getFailingClass(), methodNodes);
        }
        classNodeHashMap.get(failure.getFailingClass()).add(nodeFactory.getTestResultMethodNodeNode(failure.getFailingMethod(), failure.getTrace(), failure.getMessage(), failure.getFailingLine(), this));
    }
    List<Node> classNodes = new ArrayList<>();
    for (Map.Entry<String, List<Node>> entry : classNodeHashMap.entrySet()) {
        TestResultClassNode classNode = nodeFactory.getTestResultClassNodeNode(entry.getKey());
        classNode.setChildren(entry.getValue());
        classNodes.add(classNode);
    }
    root.setChildren(classNodes);
    resultTree.getNodeStorage().add(root);
}
Also used : TestResultGroupNode(org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultGroupNode) HashMap(java.util.HashMap) TestResultGroupNode(org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultGroupNode) TestResultMethodNode(org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultMethodNode) TestResultClassNode(org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultClassNode) Node(org.eclipse.che.ide.api.data.tree.Node) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TestResultClassNode(org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultClassNode) Map(java.util.Map) HashMap(java.util.HashMap) Failure(org.eclipse.che.api.testing.shared.Failure)

Example 5 with Failure

use of org.eclipse.che.api.testing.shared.Failure 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;
}
Also used : ArrayList(java.util.ArrayList) TestResult(org.eclipse.che.api.testing.shared.TestResult) OutputTestListener(org.eclipse.che.plugin.testing.junit.server.listener.OutputTestListener) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.eclipse.che.api.testing.shared.Failure)

Aggregations

ArrayList (java.util.ArrayList)5 Failure (org.eclipse.che.api.testing.shared.Failure)5 List (java.util.List)4 TestResult (org.eclipse.che.api.testing.shared.TestResult)4 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 URLClassLoader (java.net.URLClassLoader)2 OutputTestListener (org.eclipse.che.plugin.testing.junit.server.listener.OutputTestListener)2 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Node (org.eclipse.che.ide.api.data.tree.Node)1 TestResultClassNode (org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultClassNode)1 TestResultGroupNode (org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultGroupNode)1 TestResultMethodNode (org.eclipse.che.plugin.testing.ide.view.navigation.nodes.TestResultMethodNode)1