summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2018-12-30 21:56:10 -0800
committerGeorge Fraser <george@fivetran.com>2018-12-30 21:56:10 -0800
commitbab1f5a754c72d6b10c6c43af2a17d25bd93cdef (patch)
tree8c0e4619a010b4d978cbe0420c20367cbdb8dda9 /src/test/java
parentf903f5dec011e52189664d75012e9ca47318cd62 (diff)
downloadjava-language-server-bab1f5a754c72d6b10c6c43af2a17d25bd93cdef.zip
Source path classes in the same dir are accessible
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/javacs/CompletionsBase.java4
-rw-r--r--src/test/java/org/javacs/CompletionsTest.java27
-rw-r--r--src/test/java/org/javacs/DocsTest.java4
-rw-r--r--src/test/java/org/javacs/FindResource.java12
-rw-r--r--src/test/java/org/javacs/JavaCompilerServiceTest.java29
-rw-r--r--src/test/java/org/javacs/LanguageServerFixture.java2
-rw-r--r--src/test/java/org/javacs/ParserFixImportsTest.java2
7 files changed, 61 insertions, 19 deletions
diff --git a/src/test/java/org/javacs/CompletionsBase.java b/src/test/java/org/javacs/CompletionsBase.java
index 30af123..1a6b161 100644
--- a/src/test/java/org/javacs/CompletionsBase.java
+++ b/src/test/java/org/javacs/CompletionsBase.java
@@ -10,6 +10,8 @@ import org.javacs.lsp.*;
public class CompletionsBase {
protected static final Logger LOG = Logger.getLogger("main");
+ protected static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
+
protected Set<String> insertTemplate(String file, int row, int column) {
var items = items(file, row, column);
@@ -81,8 +83,6 @@ public class CompletionsBase {
.collect(Collectors.toSet());
}
- protected static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
-
protected List<? extends CompletionItem> items(String file, int row, int column) {
var uri = FindResource.uri(file);
var position =
diff --git a/src/test/java/org/javacs/CompletionsTest.java b/src/test/java/org/javacs/CompletionsTest.java
index b09ae92..37e46f4 100644
--- a/src/test/java/org/javacs/CompletionsTest.java
+++ b/src/test/java/org/javacs/CompletionsTest.java
@@ -3,6 +3,10 @@ package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+import java.util.List;
import java.util.stream.Collectors;
import org.javacs.lsp.*;
import org.junit.Ignore;
@@ -796,4 +800,27 @@ public class CompletionsTest extends CompletionsBase {
assertThat(suggestions, hasItem(containsString("util")));
}
+
+ @Test
+ public void newlyCreatedClass() throws IOException {
+ var file = FindResource.path("/org/javacs/example/NewlyCreatedFile.java");
+ try {
+ // Create a file that didn't exist when we created the server
+ try (var writer = Files.newBufferedWriter(file, StandardOpenOption.CREATE_NEW)) {
+ writer.write("package org.javacs.example;\nclass NewlyCreatedFile { }");
+ }
+ // Send a 'file created' notification
+ var created = new FileEvent();
+ created.uri = file.toUri();
+ created.type = FileChangeType.Created;
+ var changes = new DidChangeWatchedFilesParams();
+ changes.changes = List.of(created);
+ server.didChangeWatchedFiles(changes);
+ // Autocomplete `New`
+ var suggestions = insertText("/org/javacs/example/AutocompleteNewFile.java", 5, 12);
+ assertThat(suggestions, hasItem(containsString("NewlyCreatedFile")));
+ } finally {
+ Files.delete(file);
+ }
+ }
}
diff --git a/src/test/java/org/javacs/DocsTest.java b/src/test/java/org/javacs/DocsTest.java
index 583dea0..dd89e46 100644
--- a/src/test/java/org/javacs/DocsTest.java
+++ b/src/test/java/org/javacs/DocsTest.java
@@ -9,7 +9,7 @@ import org.junit.Test;
public class DocsTest {
@Test
public void classDoc() {
- var sourcePath = Set.of(JavaCompilerServiceTest.resourcesDir());
+ var sourcePath = Set.of(JavaCompilerServiceTest.simpleProjectSrc());
var docs = new Docs(sourcePath);
var tree = docs.classDoc("ClassDoc");
assertTrue(tree.isPresent());
@@ -18,7 +18,7 @@ public class DocsTest {
@Test
public void memberDoc() {
- var sourcePath = Set.of(JavaCompilerServiceTest.resourcesDir());
+ var sourcePath = Set.of(JavaCompilerServiceTest.simpleProjectSrc());
var docs = new Docs(sourcePath);
var tree = docs.memberDoc("LocalMethodDoc", "targetMethod");
assertTrue(tree.isPresent());
diff --git a/src/test/java/org/javacs/FindResource.java b/src/test/java/org/javacs/FindResource.java
index 897f85b..ce07a04 100644
--- a/src/test/java/org/javacs/FindResource.java
+++ b/src/test/java/org/javacs/FindResource.java
@@ -1,18 +1,20 @@
package org.javacs;
import java.net.URI;
+import java.nio.file.Path;
import java.nio.file.Paths;
/** Find java sources in test-project/workspace/src */
public class FindResource {
public static URI uri(String resourcePath) {
- if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1);
+ var path = path(resourcePath);
- var path = Paths.get("./src/test/test-project/workspace/src").resolve(resourcePath).normalize();
- var file = path.toAbsolutePath().toFile();
+ return path.toUri();
+ }
- if (!file.exists()) throw new RuntimeException(file + " does not exist");
+ public static Path path(String resourcePath) {
+ if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1);
- return URI.create("file://" + file);
+ return Paths.get("./src/test/test-project/workspace/src").resolve(resourcePath).normalize();
}
}
diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java
index c211014..8c67cdf 100644
--- a/src/test/java/org/javacs/JavaCompilerServiceTest.java
+++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java
@@ -30,22 +30,22 @@ public class JavaCompilerServiceTest {
private JavaCompilerService compiler =
new JavaCompilerService(
- Collections.singleton(resourcesDir()),
+ Collections.singleton(simpleProjectSrc()),
JavaCompilerServiceTest::allJavaFiles,
Collections.emptySet(),
Collections.emptySet());
- static Path resourcesDir() {
- try {
- return Paths.get(JavaCompilerServiceTest.class.getResource("/HelloWorld.java").toURI()).getParent();
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
+ static Path mavenProjectSrc() {
+ return Paths.get("src/test/test-project/workspace/src").normalize();
+ }
+
+ static Path simpleProjectSrc() {
+ return Paths.get("src/test/test-project/simple").normalize();
}
static Set<Path> allJavaFiles() {
try {
- return Files.walk(resourcesDir())
+ return Files.walk(simpleProjectSrc())
.filter(f -> f.getFileName().toString().endsWith(".java"))
.collect(Collectors.toSet());
} catch (IOException e) {
@@ -319,4 +319,17 @@ public class JavaCompilerServiceTest {
assertTrue(CompileFocus.matchesPartialName("foobar", "foo"));
assertFalse(CompileFocus.matchesPartialName("foo", "foobar"));
}
+
+ @Test
+ public void packageName() {
+ var compiler =
+ new JavaCompilerService(
+ Collections.singleton(mavenProjectSrc()),
+ JavaCompilerServiceTest::allJavaFiles,
+ Collections.emptySet(),
+ Collections.emptySet());
+ assertThat(
+ compiler.pathBasedPackageName(FindResource.path("/org/javacs/example/Goto.java")),
+ equalTo("org.javacs.example"));
+ }
}
diff --git a/src/test/java/org/javacs/LanguageServerFixture.java b/src/test/java/org/javacs/LanguageServerFixture.java
index 2c93283..f2da3b7 100644
--- a/src/test/java/org/javacs/LanguageServerFixture.java
+++ b/src/test/java/org/javacs/LanguageServerFixture.java
@@ -9,7 +9,7 @@ import org.javacs.lsp.*;
class LanguageServerFixture {
- public static Path DEFAULT_WORKSPACE_ROOT = Paths.get("src/test/test-project/workspace").toAbsolutePath();
+ public static Path DEFAULT_WORKSPACE_ROOT = Paths.get("src/test/test-project/workspace").normalize();
static {
Main.setRootFormat();
diff --git a/src/test/java/org/javacs/ParserFixImportsTest.java b/src/test/java/org/javacs/ParserFixImportsTest.java
index e935962..8eb1fcb 100644
--- a/src/test/java/org/javacs/ParserFixImportsTest.java
+++ b/src/test/java/org/javacs/ParserFixImportsTest.java
@@ -12,7 +12,7 @@ public class ParserFixImportsTest {
@Test
public void findExistingImports() throws IOException {
var allJavaFiles =
- Files.walk(JavaCompilerServiceTest.resourcesDir())
+ Files.walk(JavaCompilerServiceTest.simpleProjectSrc())
.filter(f -> f.getFileName().toString().endsWith(".java"))
.collect(Collectors.toSet());
assertThat(allJavaFiles, not(empty()));