Search in sources :

Example 1 with TestResult

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;
}
Also used : TestRunner(org.eclipse.che.api.testing.server.framework.TestRunner) TestResult(org.eclipse.che.api.testing.shared.TestResult) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with TestResult

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;
}
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 TestResult

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;
}
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 4 with TestResult

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)));
                            }
                        });
                    }
                });
            }
        });
    });
}
Also used : CommandImpl(org.eclipse.che.ide.api.command.CommandImpl) CommandManager(org.eclipse.che.ide.api.command.CommandManager) CommandOutputConsole(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole) AsyncRequestFactory(org.eclipse.che.ide.rest.AsyncRequestFactory) JsPromiseError(org.eclipse.che.api.promises.client.js.JsPromiseError) CommandImpl(org.eclipse.che.ide.api.command.CommandImpl) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) Executor(org.eclipse.che.api.promises.client.js.Executor) PromiseError(org.eclipse.che.api.promises.client.PromiseError) Promise(org.eclipse.che.api.promises.client.Promise) ExecutorBody(org.eclipse.che.api.promises.client.js.Executor.ExecutorBody) AppContext(org.eclipse.che.ide.api.app.AppContext) Map(java.util.Map) RejectFunction(org.eclipse.che.api.promises.client.js.RejectFunction) URL(com.google.gwt.http.client.URL) HTTPHeader(org.eclipse.che.ide.rest.HTTPHeader) Operation(org.eclipse.che.api.promises.client.Operation) CommandConsoleFactory(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory) DtoUnmarshallerFactory(org.eclipse.che.ide.rest.DtoUnmarshallerFactory) DtoFactory(org.eclipse.che.ide.dto.DtoFactory) OperationException(org.eclipse.che.api.promises.client.OperationException) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) PromiseProvider(org.eclipse.che.api.promises.client.PromiseProvider) MatchResult(com.google.gwt.regexp.shared.MatchResult) Machine(org.eclipse.che.api.core.model.machine.Machine) ExecAgentCommandManager(org.eclipse.che.ide.api.machine.ExecAgentCommandManager) List(java.util.List) MimeType(org.eclipse.che.ide.MimeType) TestResult(org.eclipse.che.api.testing.shared.TestResult) RegExp(com.google.gwt.regexp.shared.RegExp) ResolveFunction(org.eclipse.che.api.promises.client.js.ResolveFunction) MacroProcessor(org.eclipse.che.ide.api.macro.MacroProcessor) ProcessesPanelPresenter(org.eclipse.che.ide.extension.machine.client.processes.panel.ProcessesPanelPresenter) Singleton(com.google.inject.Singleton) TestResult(org.eclipse.che.api.testing.shared.TestResult) Operation(org.eclipse.che.api.promises.client.Operation) Machine(org.eclipse.che.api.core.model.machine.Machine) RejectFunction(org.eclipse.che.api.promises.client.js.RejectFunction) CommandOutputConsole(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 5 with TestResult

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);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) TestResult(org.eclipse.che.api.testing.shared.TestResult) TestClasspathProvider(org.eclipse.che.plugin.testing.classpath.server.TestClasspathProvider)

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