diff options
author | George Fraser <george@fivetran.com> | 2018-06-29 20:08:14 -0700 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-06-29 20:08:14 -0700 |
commit | 0297056b17b1ac2808e3ac501aee9129fe3b06e7 (patch) | |
tree | be84cd222bc0edd5c205b066e8e4999c122769f9 /src/test/java/org | |
parent | 9b66c6aea292d1aeec178ccf69f9605cdf3b4801 (diff) | |
download | java-language-server-0297056b17b1ac2808e3ac501aee9129fe3b06e7.zip |
FIx class path indexing
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/javacs/ClassPathIndexTest.java | 62 | ||||
-rw-r--r-- | src/test/java/org/javacs/ClassesTest.java | 84 | ||||
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerServiceTest.java | 2 | ||||
-rw-r--r-- | src/test/java/org/javacs/ParserFixImportsTest.java | 4 |
4 files changed, 87 insertions, 65 deletions
diff --git a/src/test/java/org/javacs/ClassPathIndexTest.java b/src/test/java/org/javacs/ClassPathIndexTest.java deleted file mode 100644 index b981021..0000000 --- a/src/test/java/org/javacs/ClassPathIndexTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.google.common.reflect.ClassPath; -import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.Optional; -import org.junit.Test; - -public class ClassPathIndexTest { - - @Test - public void createEmptyLoader() throws ClassNotFoundException { - ClassLoader emptyClassLoader = ClassPathIndex.parentClassLoader(); - - assertThat(emptyClassLoader.loadClass("java.util.ArrayList"), not(nullValue())); - - try { - Class<?> found = emptyClassLoader.loadClass("com.google.common.collect.Lists"); - - fail("Found " + found); - } catch (ClassNotFoundException e) { - // OK - } - } - - @Test - public void java8Platform() throws IOException { - String javaHome = Paths.get("./src/test/test-platforms/jdk8-home").toAbsolutePath().toString(); - URL[] resources = ClassPathIndex.java8Platform(javaHome); - assertThat("found example.jar", resources, hasItemInArray(hasToString(containsString("rt.jar")))); - ClassPath classPath = ClassPath.from(new URLClassLoader(resources, null)); - assertThat(classPath.getTopLevelClasses(), hasItem(hasProperty("simpleName", equalTo("HelloWorld")))); - } - - @Test - public void java9Platform() throws IOException { - String javaHome = Paths.get("./src/test/test-platforms/jdk9-home").toAbsolutePath().toString(); - URL[] resources = ClassPathIndex.java9Platform(javaHome); - assertThat( - "found java.compiler.jmod", - resources, - hasItemInArray(hasToString(containsString("java.compiler.jmod")))); - ClassPath classPath = ClassPath.from(new URLClassLoader(resources, null)); - assertThat(classPath.getTopLevelClasses(), hasItem(hasProperty("simpleName", equalTo("JavaCompiler")))); - } - - @Test - public void topLevelClasses() { - ClassPathIndex index = new ClassPathIndex(Collections.emptySet()); - Optional<ClassPath.ClassInfo> arrayList = - index.topLevelClasses().filter(c -> c.getName().equals("java.util.ArrayList")).findFirst(); - assertTrue("java.util.ArrayList is on the classpath", arrayList.isPresent()); - } -} diff --git a/src/test/java/org/javacs/ClassesTest.java b/src/test/java/org/javacs/ClassesTest.java new file mode 100644 index 0000000..f5e6d5f --- /dev/null +++ b/src/test/java/org/javacs/ClassesTest.java @@ -0,0 +1,84 @@ +package org.javacs; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import com.google.common.collect.*; +import com.google.common.reflect.*; +import java.net.*; +import java.nio.file.*; +import java.util.*; +import org.junit.*; + +public class ClassesTest { + + @Test + public void list() { + var jdk = Classes.jdkTopLevelClasses(); + assertThat(jdk.classes(), hasItem("java.util.List")); + assertThat(jdk.load("java.util.List"), not(nullValue())); + + var empty = Classes.classPathTopLevelClasses(Collections.emptySet()); + assertThat(empty.classes(), not(hasItem("java.util.List"))); + } + + @Test + public void arrayList() { + var jdk = Classes.jdkTopLevelClasses(); + assertThat(jdk.classes(), hasItem("java.util.ArrayList")); + assertThat(jdk.load("java.util.ArrayList"), not(nullValue())); + } + + @Test + public void platformClassPath() throws Exception { + var fs = FileSystems.getFileSystem(URI.create("jrt:/")); + var path = fs.getPath("/"); + Files.walk(path).forEach(p -> System.out.println(p)); + } + + @Test + public void loadMain() throws Exception { + var classes = ClassPath.from(this.getClass().getClassLoader()); + var found = classes.getTopLevelClasses("org.javacs"); + assertThat(found, hasItem(hasToString("org.javacs.Main"))); + + var main = found.stream().filter(c -> c.getName().equals("org.javacs.Main")).findFirst(); + assertTrue(main.isPresent()); + + var load = main.get().load(); + assertNotNull(load); + } + + @Test + public void loadList() throws Exception { + var classes = ClassPath.from(ClassLoader.getPlatformClassLoader()); + var found = classes.getTopLevelClasses("java.util"); + assertThat(found, not(empty())); + assertThat(found, hasItem(hasToString("java.util.List"))); + + var list = found.stream().filter(c -> c.getName().equals("java.util.List")).findFirst(); + assertTrue(list.isPresent()); + + var load = list.get().load(); + assertNotNull(load); + } + + void ancestors(ClassLoader classLoader) { + while (classLoader != null) { + System.out.println(classLoader.toString()); + classLoader = classLoader.getParent(); + } + } + + @Test + public void printAncestors() throws Exception { + System.out.println("This:"); + ancestors(this.getClass().getClassLoader()); + System.out.println("List:"); + ancestors(java.util.List.class.getClassLoader()); + System.out.println("System:"); + ancestors(ClassLoader.getSystemClassLoader()); + System.out.println("Platform:"); + ancestors(ClassLoader.getPlatformClassLoader()); + } +} diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index 0da2acc..f912ec4 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -97,7 +97,7 @@ public class JavaCompilerServiceTest { if (c.element != null) result.add(c.element.getSimpleName().toString()); else if (c.packagePart != null) result.add(c.packagePart.name); else if (c.classSymbol != null) result.add("class"); - else if (c.notImportedClass != null) result.add(c.notImportedClass.getSimpleName()); + else if (c.notImportedClass != null) result.add(Parser.lastName(c.notImportedClass)); } return result; } diff --git a/src/test/java/org/javacs/ParserFixImportsTest.java b/src/test/java/org/javacs/ParserFixImportsTest.java index 2bdb2fe..b754fe1 100644 --- a/src/test/java/org/javacs/ParserFixImportsTest.java +++ b/src/test/java/org/javacs/ParserFixImportsTest.java @@ -8,12 +8,12 @@ import java.util.Map; import org.junit.Test; public class ParserFixImportsTest { - ClassPathIndex classPath = new ClassPathIndex(Collections.emptySet()); ExistingImports emptyImports = new ExistingImports(Collections.emptySet(), Collections.emptySet()); @Test public void findJavaUtilList() { - Map<String, String> resolved = Parser.resolveSymbols(Collections.singleton("List"), emptyImports, classPath); + Map<String, String> resolved = + Parser.resolveSymbols(Collections.singleton("List"), emptyImports, Collections.emptySet()); assertThat(resolved, hasEntry("List", "java.util.List")); } |