Search in sources :

Example 11 with TestResult

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

the class RunTestXMLAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    final StatusNotification notification = new StatusNotification("Running Tests...", PROGRESS, FLOAT_MODE);
    notificationManager.notify(notification);
    final Project project = appContext.getRootProject();
    Map<String, String> parameters = new HashMap<>();
    parameters.put("updateClasspath", "true");
    parameters.put("testngXML", project.getPath() + "/" + MavenAttributes.DEFAULT_TEST_RESOURCES_FOLDER + "/testng.xml");
    Promise<TestResult> testResultPromise = service.getTestResult(project.getPath(), "testng", parameters);
    testResultPromise.then(new Operation<TestResult>() {

        @Override
        public void apply(TestResult result) throws OperationException {
            notification.setStatus(SUCCESS);
            if (result.isSuccess()) {
                notification.setTitle("Test runner executed successfully");
                notification.setContent("All tests are passed");
            } else {
                notification.setTitle("Test runner executed successfully with test failures.");
                notification.setContent(result.getFailureCount() + " test(s) failed.\n");
            }
            presenter.handleResponse(result);
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError exception) throws OperationException {
            final String errorMessage = (exception.getMessage() != null) ? exception.getMessage() : "Failed to run test cases";
            notification.setContent(errorMessage);
            notification.setStatus(FAIL);
        }
    });
}
Also used : Project(org.eclipse.che.ide.api.resources.Project) PromiseError(org.eclipse.che.api.promises.client.PromiseError) HashMap(java.util.HashMap) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) TestResult(org.eclipse.che.api.testing.shared.TestResult) Operation(org.eclipse.che.api.promises.client.Operation) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 12 with TestResult

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

the class TestNGRunner method execute.

/**
     * {@inheritDoc}
     */
@Override
public TestResult execute(Map<String, String> testParameters) throws Exception {
    String projectAbsolutePath = testParameters.get("absoluteProjectPath");
    String xmlPath = testParameters.get("testngXML");
    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();
    }
    TestClasspathProvider classpathProvider = classpathRegistry.getTestClasspathProvider(projectType);
    projectClassLoader = classpathProvider.getClassLoader(projectAbsolutePath, projectPath, updateClasspath);
    TestResult testResult;
    if (runClass) {
        String fqn = testParameters.get("fqn");
        testResult = run(projectAbsolutePath, fqn);
    } else {
        if (xmlPath == null) {
            testResult = runAll(projectAbsolutePath);
        } else {
            testResult = runTestXML(projectAbsolutePath, ResourcesPlugin.getPathToWorkspace() + xmlPath);
        }
    }
    return testResult;
}
Also used : TestResult(org.eclipse.che.api.testing.shared.TestResult) TestClasspathProvider(org.eclipse.che.plugin.testing.classpath.server.TestClasspathProvider)

Example 13 with TestResult

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

the class RunTestActionDelegate method doRunTests.

public void doRunTests(ActionEvent e, Map<String, String> parameters) {
    final StatusNotification notification = new StatusNotification("Running Tests...", PROGRESS, FLOAT_MODE);
    source.getNotificationManager().notify(notification);
    final Project project = source.getAppContext().getRootProject();
    parameters.put("updateClasspath", "true");
    Promise<TestResult> testResultPromise = source.getService().getTestResult(project.getPath(), "junit", parameters, notification);
    testResultPromise.then(new Operation<TestResult>() {

        @Override
        public void apply(TestResult result) throws OperationException {
            notification.setStatus(SUCCESS);
            if (result.isSuccess()) {
                notification.setTitle("Test runner executed successfully");
                notification.setContent("All tests are passed");
            } else {
                notification.setTitle("Test runner executed successfully with test failures.");
                notification.setContent(result.getFailureCount() + " test(s) failed.\n");
            }
            source.getPresenter().handleResponse(result);
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError exception) throws OperationException {
            final String errorMessage = (exception.getMessage() != null) ? exception.getMessage() : "Failed to run test cases";
            notification.setContent(errorMessage);
            notification.setStatus(FAIL);
        }
    });
}
Also used : Project(org.eclipse.che.ide.api.resources.Project) PromiseError(org.eclipse.che.api.promises.client.PromiseError) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) TestResult(org.eclipse.che.api.testing.shared.TestResult) Operation(org.eclipse.che.api.promises.client.Operation) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 14 with TestResult

use of org.eclipse.che.api.testing.shared.TestResult 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)

Example 15 with TestResult

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

the class RunAllTestAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    final StatusNotification notification = new StatusNotification("Running Tests...", PROGRESS, FLOAT_MODE);
    notificationManager.notify(notification);
    final Project project = appContext.getRootProject();
    Map<String, String> parameters = new HashMap<>();
    parameters.put("updateClasspath", "true");
    Promise<TestResult> testResultPromise = service.getTestResult(project.getPath(), "testng", parameters);
    testResultPromise.then(new Operation<TestResult>() {

        @Override
        public void apply(TestResult result) throws OperationException {
            notification.setStatus(SUCCESS);
            if (result.isSuccess()) {
                notification.setTitle("Test runner executed successfully");
                notification.setContent("All tests are passed");
            } else {
                notification.setTitle("Test runner executed successfully with test failures.");
                notification.setContent(result.getFailureCount() + " test(s) failed.\n");
            }
            presenter.handleResponse(result);
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError exception) throws OperationException {
            final String errorMessage = (exception.getMessage() != null) ? exception.getMessage() : "Failed to run test cases";
            notification.setContent(errorMessage);
            notification.setStatus(FAIL);
        }
    });
}
Also used : Project(org.eclipse.che.ide.api.resources.Project) PromiseError(org.eclipse.che.api.promises.client.PromiseError) HashMap(java.util.HashMap) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) TestResult(org.eclipse.che.api.testing.shared.TestResult) Operation(org.eclipse.che.api.promises.client.Operation) OperationException(org.eclipse.che.api.promises.client.OperationException)

Aggregations

TestResult (org.eclipse.che.api.testing.shared.TestResult)18 OperationException (org.eclipse.che.api.promises.client.OperationException)9 PromiseError (org.eclipse.che.api.promises.client.PromiseError)9 Operation (org.eclipse.che.api.promises.client.Operation)7 StatusNotification (org.eclipse.che.ide.api.notification.StatusNotification)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 List (java.util.List)5 CommandImpl (org.eclipse.che.ide.api.command.CommandImpl)5 Matchers.anyString (org.mockito.Matchers.anyString)5 Failure (org.eclipse.che.api.testing.shared.Failure)4 Project (org.eclipse.che.ide.api.resources.Project)4 URLClassLoader (java.net.URLClassLoader)3 Test (org.junit.Test)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Map (java.util.Map)2 Machine (org.eclipse.che.api.core.model.machine.Machine)2 DtoWithPid (org.eclipse.che.api.machine.shared.dto.execagent.event.DtoWithPid)2 Promise (org.eclipse.che.api.promises.client.Promise)2