-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JUnit testing support Bug fixes and Enhancements - Step 1 #4303
Changes from 10 commits
02ae5f2
cc4469a
a2316bf
6cdbd06
c2a7241
b36c5ec
c7a7fc8
0f430e4
239c490
8780bfb
3eda7ef
022a430
22bab77
8c718b2
131bf87
4f0c8e3
f87c3c7
8c00de8
d6900e6
ba8a293
5a698ac
0bf39a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,43 +10,51 @@ | |
*******************************************************************************/ | ||
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.ClasspathService; | ||
import org.eclipse.che.plugin.testing.classpath.server.TestClasspathProvider; | ||
import org.eclipse.core.resources.IContainer; | ||
import org.eclipse.core.resources.IResource; | ||
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 ClasspathService classpathService; | ||
|
||
@Inject | ||
public MavenTestClasspathProvider(ClasspathService classpathService) { | ||
this.classpathService = classpathService; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public ClassLoader getClassLoader(String projectPath, boolean updateClasspath) throws Exception { | ||
List<URL> classUrls; | ||
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(projectPath).toArray(URL[]::new), null); | ||
} catch (JavaModelException e) { | ||
throw new Exception("Failed to build the classpath for testing project: " + projectPath, e); | ||
} | ||
return new URLClassLoader(classUrls.toArray(new URL[classUrls.size()]), null); | ||
} | ||
|
||
/** | ||
|
@@ -57,35 +65,56 @@ 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and there. so I'm not sure it's up-to-date ? |
||
} 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 Stream<URL> getProjectClasspath(String projectPath) throws JavaModelException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to have unit tests for that part ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will be, surely, but is it required now, or could I push the tests in a next pull request ? |
||
String relativeProject = projectPath.substring(ResourcesPlugin.getPathToWorkspace().length()); | ||
IContainer root = ResourcesPlugin.getWorkspace().getRoot(); | ||
return toResolvedClassPath(classpathService.getClasspath(relativeProject).stream()) | ||
.map(dto -> { | ||
try { | ||
String dtoPath = dto.getPath(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems there is usage of tab characters and indent is not correct |
||
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 = new File(root.getLocation().toFile(), dtoPath); | ||
} | ||
break; | ||
case IClasspathEntry.CPE_SOURCE: | ||
IPath projectRelativePath = new Path(dtoPath).removeFirstSegments(1); | ||
String projectRelativePathStr = projectRelativePath.toString(); | ||
switch(projectRelativePathStr) { | ||
case "src/main/java": | ||
path = Paths.get(projectPath, "target", "classes").toFile(); | ||
break; | ||
case "src/test/java": | ||
path = Paths.get(projectPath, "target", "test-classes").toFile(); | ||
break; | ||
default: | ||
path = new File(root.getLocation().toFile(), dtoPath); | ||
} | ||
break; | ||
default: | ||
path = new File(dtoPath); | ||
} | ||
return path.toURI().toURL(); | ||
} catch (MalformedURLException e) { | ||
return null; | ||
} | ||
}) | ||
.filter(url -> url != null) | ||
.distinct(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidfestal I still see here a tab character ?