Skip to content

Commit

Permalink
JUnit testing support Bug fixes and Enhancements - Step 1 (#4303)
Browse files Browse the repository at this point in the history
Provide  JUnit test real-time output in a `Tests` output view
  • Loading branch information
davidfestal authored and l0rd committed Mar 10, 2017
1 parent 85d3578 commit 440f92d
Show file tree
Hide file tree
Showing 29 changed files with 2,129 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
*******************************************************************************/
package org.eclipse.che.plugin.java.server.rest;

import static java.util.Collections.emptyList;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.eclipse.che.dto.server.DtoFactory;
import org.eclipse.che.ide.ext.java.shared.dto.classpath.ClasspathEntryDto;
import org.eclipse.jdt.core.IClasspathEntry;
Expand All @@ -19,35 +30,24 @@
import org.eclipse.jdt.internal.core.JavaModel;
import org.eclipse.jdt.internal.core.JavaModelManager;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.emptyList;

/**
* Service for getting information about classpath.
*
* @author Valeriy Svydenko
*/
@Path("java/classpath/")
public class ClasspathService {
public class ClasspathService implements ClasspathServiceInterface {

private static final JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();

/**
* Returns information about classpath.
*
* @param projectPath
* path to the current project
* @param projectPath path to the current project
* @return list of classpath entries
* @throws JavaModelException
* when JavaModel has a failure
* @throws JavaModelException when JavaModel has a failure
*/
@Override
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ClasspathEntryDto> getClasspath(@QueryParam("projectpath") String projectPath) throws JavaModelException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2012-2017 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.plugin.java.server.rest;

import java.util.List;

import org.eclipse.che.ide.ext.java.shared.dto.classpath.ClasspathEntryDto;
import org.eclipse.jdt.core.JavaModelException;

import com.google.inject.ImplementedBy;


/**
* Interface for the service which gets information about classpath.
*
* @author Valeriy Svydenko
*/
@ImplementedBy(ClasspathService.class)
public interface ClasspathServiceInterface {
List<ClasspathEntryDto> getClasspath(String projectPath) throws JavaModelException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,85 @@
<artifactId>guice-multibindings</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-core</artifactId>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.equinox.common</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-inject</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.lib</groupId>
<artifactId>org-eclipse-jdt-core-repack</artifactId>
<exclusions>
<exclusion>
<artifactId>org.eclipse.jdt.core</artifactId>
<groupId>org.eclipse.tycho</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-java-ext-lang-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-java-ext-lang-shared</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>che-plugin-testing-classpath-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.plugin</groupId>
<artifactId>org.eclipse.core.resources</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>target/classes</outputDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/**/MavenTestClasspathProviderTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${version.surefire.plugin}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,50 @@
*******************************************************************************/
package org.eclipse.che.plugin.testing.classpath.maven.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.che.api.core.util.CommandLine;
import org.eclipse.che.api.core.util.LineConsumer;
import org.eclipse.che.api.core.util.ProcessUtil;
import org.eclipse.che.ide.ext.java.shared.dto.classpath.ClasspathEntryDto;
import org.eclipse.che.plugin.java.server.rest.ClasspathServiceInterface;
import org.eclipse.che.plugin.testing.classpath.server.TestClasspathProvider;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaModelException;

import com.google.inject.Inject;

/**
* Maven implementation for the test classpath provider.
*
* @author Mirage Abeysekara
* @author David Festal
*/
public class MavenTestClasspathProvider implements TestClasspathProvider {
private ClasspathServiceInterface classpathService;

@Inject
public MavenTestClasspathProvider(ClasspathServiceInterface classpathService) {
this.classpathService = classpathService;
}

/**
* {@inheritDoc}
*/
@Override
public ClassLoader getClassLoader(String projectPath, boolean updateClasspath) throws Exception {
List<URL> classUrls;
public ClassLoader getClassLoader(String projectAbsolutePath, String projectRelativePath, boolean updateClasspath) throws Exception {
try {
if (updateClasspath) {
buildClasspath(projectPath);
}
classUrls = getProjectClasspath(projectPath);
} catch (IOException | InterruptedException e) {
throw new Exception("Failed to build Maven classpath.", e);
return new URLClassLoader(getProjectClasspath(projectAbsolutePath, projectRelativePath, getWorkspaceRoot()), null);
} catch (JavaModelException e) {
throw new Exception("Failed to build the classpath for testing project: " + projectRelativePath, e);
}
return new URLClassLoader(classUrls.toArray(new URL[classUrls.size()]), null);
}

/**
Expand All @@ -57,35 +64,57 @@ public String getProjectType() {
return "maven";
}

private boolean buildClasspath(String projectPath) throws IOException, InterruptedException {
final CommandLine commandLineClassPath = new CommandLine("mvn", "clean", "dependency:build-classpath",
"-Dmdep.outputFile=target/test.classpath.maven");
Process processBuildClassPath = new ProcessBuilder().redirectErrorStream(true).directory(new File(projectPath))
.command(commandLineClassPath.toShellCommand()).start();
ProcessUtil.process(processBuildClassPath, LineConsumer.DEV_NULL, LineConsumer.DEV_NULL);
processBuildClassPath.waitFor();
final CommandLine commandLineTestCompile = new CommandLine("mvn", "test-compile");
Process processTestCompile = new ProcessBuilder().redirectErrorStream(true).directory(new File(projectPath))
.command(commandLineTestCompile.toShellCommand()).start();
ProcessUtil.process(processTestCompile, LineConsumer.DEV_NULL, LineConsumer.DEV_NULL);
return processTestCompile.waitFor() == 0;

private Stream<ClasspathEntryDto> toResolvedClassPath(Stream<ClasspathEntryDto> rawClasspath) {
return rawClasspath.flatMap(dto -> {
if (dto.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
return toResolvedClassPath(dto.getExpandedEntries().stream());
} else {
return Stream.of(dto);
}
});
}

private List<URL> getProjectClasspath(String projectPath) throws IOException {
List<URL> classUrls = new ArrayList<>();
File cpFile = Paths.get(projectPath, "target", "test.classpath.maven").toFile();
FileReader fileReader = new FileReader(cpFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = bufferedReader.readLine();
String[] paths = line.split(":");
for (String path : paths) {
classUrls.add(new File(path).toURI().toURL());
}
bufferedReader.close();
fileReader.close();
classUrls.add(Paths.get(projectPath, "target", "classes").toUri().toURL());
classUrls.add(Paths.get(projectPath, "target", "test-classes").toUri().toURL());
return classUrls;
private IWorkspaceRoot getWorkspaceRoot() {
return ResourcesPlugin.getWorkspace().getRoot();
}

public URL[] getProjectClasspath(String projectAbsolutePath, String projectRelativePath, IWorkspaceRoot root) throws JavaModelException {
Stream<ClasspathEntryDto> rawClasspath = classpathService.getClasspath(projectRelativePath).stream();
Stream<ClasspathEntryDto> resolvedClasspath = toResolvedClassPath(rawClasspath);
return resolvedClasspath.map(dto -> {
try {
String dtoPath = dto.getPath();
IResource res = root.findMember(new Path(dtoPath));
File path;
switch (dto.getEntryKind()) {
case IClasspathEntry.CPE_LIBRARY:
if (res == null) {
path = new File(dtoPath);
} else {
path = res.getLocation().toFile();
}
break;
case IClasspathEntry.CPE_SOURCE:
IPath relativePathFromProjectRoot = new Path(dtoPath).removeFirstSegments(1);
String relativePathFromProjectRootStr = relativePathFromProjectRoot.toString();
switch (relativePathFromProjectRootStr) {
case "src/main/java":
path = Paths.get(projectAbsolutePath, "target", "classes").toFile();
break;
case "src/test/java":
path = Paths.get(projectAbsolutePath, "target", "test-classes").toFile();
break;
default:
path = Paths.get(projectAbsolutePath, "target", "classes").toFile();
}
break;
default:
path = new File(dtoPath);
}
return path.toURI().toURL();
} catch (MalformedURLException e) {
return null;
}
}).filter(url -> url != null).distinct().toArray(URL[]::new);
}
}
Loading

0 comments on commit 440f92d

Please sign in to comment.