Fix test-projects finding logic.

This commit is contained in:
BoykoAlex
2016-11-14 14:03:05 -05:00
parent 73378402d3
commit 2ee1220fbf
6 changed files with 165 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,4 @@
*.class
**/.classpath
**/classpath.txt
**/.idea
**/*.iml

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="test-projects">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -1,5 +1,5 @@
/.classpath
/.project
**/.classpath
**/classpath.txt
**/.project
**/.settings/*.*

View File

@@ -37,4 +37,11 @@
<version>${junit-version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>test-projects</directory>
</resource>
</resources>
</build>
</project>

View File

@@ -10,10 +10,21 @@
*******************************************************************************/
package org.springframework.ide.vscode.project.harness;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.CopyOption;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.maven.MavenCore;
@@ -33,7 +44,7 @@ public class ProjectsHarness {
public static final ProjectsHarness INSTANCE = new ProjectsHarness();;
public Cache<String, IJavaProject> cache = CacheBuilder.newBuilder().build();
public Cache<String, IJavaProject> cache = CacheBuilder.newBuilder().concurrencyLevel(1).build();
private enum ProjectType {
MAVEN,
@@ -59,10 +70,94 @@ public class ProjectsHarness {
});
}
protected Path getProjectPath(String name) throws URISyntaxException {
URL sourceLocation = ProjectsHarness.class.getProtectionDomain().getCodeSource().getLocation();
protected Path getProjectPath(String name) throws URISyntaxException, IOException {
URI sourceLocation = ProjectsHarness.class.getProtectionDomain().getCodeSource().getLocation().toURI();
// file:/Users/aboyko/git/sts4/vscode-extensions/commons/project-test-harness/target/project-test-harness-0.0.1-SNAPSHOT.jar
return Paths.get(sourceLocation.toURI()).getParent().getParent().resolve("test-projects").resolve(name);
Path testProjectsPath = Paths.get(sourceLocation).getParent().getParent().resolve("test-projects").resolve(name);
if (Files.exists(testProjectsPath)) {
return testProjectsPath;
} else {
/*
* If "test-projects" folder is not found then extract test project
* from the jar's "test-projects" folder and copy it in the temp
* folder
*/
return getProjectPathFromClasspath(name);
}
}
private Path getProjectPathFromClasspath(String name) throws URISyntaxException, IOException {
URI resource = ProjectsHarness.class.getResource("/" + name).toURI();
if (resource.getScheme().equalsIgnoreCase("jar")) {
return getProjectPathFromJar(resource);
} else {
return Paths.get(resource);
}
}
private Path getProjectPathFromJar(URI jar) throws IOException {
final String[] array = jar.toString().split("!");
URI firstHalf = URI.create(array[0]);
Path tempFolderPath = Paths.get(new File(System.getProperty(MavenCore.JAVA_IO_TMPDIR)).toURI());
FileSystem fs = FileSystems.newFileSystem(firstHalf, Collections.emptyMap());
try {
Path path = fs.getPath(array[1]);
Path projectCopyPath = tempFolderPath.resolve(path.getFileName().toString());
if (Files.exists(projectCopyPath)) {
recursiveDelete(projectCopyPath);
}
recursiveCopy(path, tempFolderPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Copied test project to: " + projectCopyPath);
return projectCopyPath;
} finally {
fs.close();
}
}
private static void recursiveCopy(Path source, Path target, CopyOption... options) throws IOException {
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
Path destination = target;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
destination = destination.resolve(dir.getFileName().toString());
Files.copy(dir, destination, options);
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path newFile = destination.resolve(file.getFileName().toString());
Files.copy(file, newFile, options);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
destination = destination.getParent();
return super.postVisitDirectory(dir, exc);
}
});
}
private static void recursiveDelete(Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
});
}
public MavenJavaProject mavenProject(String name) throws Exception {

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>