diff --git a/.gitignore b/.gitignore index e9a1f2ddf..cb58cf4c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.class -**/.classpath **/classpath.txt **/.idea **/*.iml diff --git a/vscode-extensions/commons/project-test-harness/.classpath b/vscode-extensions/commons/project-test-harness/.classpath new file mode 100644 index 000000000..8620f6a2b --- /dev/null +++ b/vscode-extensions/commons/project-test-harness/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/vscode-extensions/commons/project-test-harness/.gitignore b/vscode-extensions/commons/project-test-harness/.gitignore index 36dc59cb6..ca0618fbd 100644 --- a/vscode-extensions/commons/project-test-harness/.gitignore +++ b/vscode-extensions/commons/project-test-harness/.gitignore @@ -1,5 +1,5 @@ -/.classpath /.project +**/.classpath **/classpath.txt **/.project **/.settings/*.* diff --git a/vscode-extensions/commons/project-test-harness/pom.xml b/vscode-extensions/commons/project-test-harness/pom.xml index 75b41d4fd..6706409e1 100644 --- a/vscode-extensions/commons/project-test-harness/pom.xml +++ b/vscode-extensions/commons/project-test-harness/pom.xml @@ -37,4 +37,11 @@ ${junit-version} + + + + test-projects + + + \ No newline at end of file diff --git a/vscode-extensions/commons/project-test-harness/src/main/java/org/springframework/ide/vscode/project/harness/ProjectsHarness.java b/vscode-extensions/commons/project-test-harness/src/main/java/org/springframework/ide/vscode/project/harness/ProjectsHarness.java index 02b9efb6b..81c7ba423 100644 --- a/vscode-extensions/commons/project-test-harness/src/main/java/org/springframework/ide/vscode/project/harness/ProjectsHarness.java +++ b/vscode-extensions/commons/project-test-harness/src/main/java/org/springframework/ide/vscode/project/harness/ProjectsHarness.java @@ -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 cache = CacheBuilder.newBuilder().build(); + public Cache 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 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() { + + @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 { diff --git a/vscode-extensions/commons/properties-editor-test-harness/.classpath b/vscode-extensions/commons/properties-editor-test-harness/.classpath new file mode 100644 index 000000000..af1430be1 --- /dev/null +++ b/vscode-extensions/commons/properties-editor-test-harness/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +