summaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2017-05-06 10:31:20 -0400
committerGeorge Fraser <george@fivetran.com>2017-05-06 10:31:20 -0400
commit630df6ba27dcb88e2997c0897766ea1b6c7913ed (patch)
treebd0b05d24f244dd876384f1c9904b8915dd462ee /src/test/java/org
parent748fe64c7f4fd0bb7308a481699cb318d8de2b6c (diff)
downloadjava-language-server-630df6ba27dcb88e2997c0897766ea1b6c7913ed.zip
Figure out sourcePath automatically
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/javacs/CodeActionsTest.java38
-rw-r--r--src/test/java/org/javacs/CompilerProfiling.java6
-rw-r--r--src/test/java/org/javacs/FindResource.java4
-rw-r--r--src/test/java/org/javacs/InferConfigTest.java25
-rw-r--r--src/test/java/org/javacs/JavaCompilerTest.java16
-rw-r--r--src/test/java/org/javacs/JavadocsTest.java23
-rw-r--r--src/test/java/org/javacs/LanguageServerFixture.java12
-rw-r--r--src/test/java/org/javacs/LinterTest.java2
-rw-r--r--src/test/java/org/javacs/RecompileTest.java2
-rw-r--r--src/test/java/org/javacs/RefactorFileTest.java6
-rw-r--r--src/test/java/org/javacs/SearchTest.java2
-rw-r--r--src/test/java/org/javacs/SymbolUnderCursorTest.java16
12 files changed, 70 insertions, 82 deletions
diff --git a/src/test/java/org/javacs/CodeActionsTest.java b/src/test/java/org/javacs/CodeActionsTest.java
index 36924c8..71bf488 100644
--- a/src/test/java/org/javacs/CodeActionsTest.java
+++ b/src/test/java/org/javacs/CodeActionsTest.java
@@ -5,9 +5,9 @@ import org.eclipse.lsp4j.services.LanguageClient;
import org.junit.Before;
import org.junit.Test;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
+import java.io.*;
import java.net.URI;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -80,21 +80,27 @@ public class CodeActionsTest {
private List<? extends Command> commands(String file, int row, int column) {
URI uri = FindResource.uri(file);
TextDocumentIdentifier document = new TextDocumentIdentifier(uri.toString());
- String content = new BufferedReader(new InputStreamReader(FindResource.class.getResourceAsStream(file))).lines()
- .collect(Collectors.joining("\n"));
- TextDocumentItem open = new TextDocumentItem();
- open.setText(content);
- open.setUri(uri.toString());
- open.setLanguageId("java");
-
- server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(open, content));
- server.getTextDocumentService().didSave(new DidSaveTextDocumentParams(document, content));
-
- return diagnostics.stream()
- .filter(diagnostic -> includes(diagnostic.getRange(), row - 1, column - 1))
- .flatMap(diagnostic -> codeActionsAt(document, diagnostic))
- .collect(Collectors.toList());
+ try {
+ InputStream in = Files.newInputStream(new File(uri).toPath());
+ String content = new BufferedReader(new InputStreamReader(in)).lines()
+ .collect(Collectors.joining("\n"));
+ TextDocumentItem open = new TextDocumentItem();
+
+ open.setText(content);
+ open.setUri(uri.toString());
+ open.setLanguageId("java");
+
+ server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(open, content));
+ server.getTextDocumentService().didSave(new DidSaveTextDocumentParams(document, content));
+
+ return diagnostics.stream()
+ .filter(diagnostic -> includes(diagnostic.getRange(), row - 1, column - 1))
+ .flatMap(diagnostic -> codeActionsAt(document, diagnostic))
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
private boolean includes(Range range, int line, int character) {
diff --git a/src/test/java/org/javacs/CompilerProfiling.java b/src/test/java/org/javacs/CompilerProfiling.java
index 1114b3e..020bbf7 100644
--- a/src/test/java/org/javacs/CompilerProfiling.java
+++ b/src/test/java/org/javacs/CompilerProfiling.java
@@ -29,7 +29,11 @@ public class CompilerProfiling {
private Duration compileLargeFile(URI file) {
long start = System.nanoTime();
- JavacHolder compiler = JavacHolder.create(Collections.emptySet(), Collections.emptySet(), Paths.get("target/test-output"));
+ JavacHolder compiler = JavacHolder.create(
+ Collections.singleton(Paths.get("src/test/test-project/workspace/src")),
+ Collections.emptySet(),
+ Paths.get("target/test-output")
+ );
BatchResult result = compiler.compileBatch(Collections.singletonMap(file, Optional.empty()));
long finish = System.nanoTime();
diff --git a/src/test/java/org/javacs/FindResource.java b/src/test/java/org/javacs/FindResource.java
index 355223a..0f26115 100644
--- a/src/test/java/org/javacs/FindResource.java
+++ b/src/test/java/org/javacs/FindResource.java
@@ -6,14 +6,14 @@ import java.nio.file.Path;
import java.nio.file.Paths;
/**
- * Represents a java source on the system resource path.
+ * Find java sources in test-project/workspace/src
*/
public class FindResource {
public static URI uri(String resourcePath) {
if (resourcePath.startsWith("/"))
resourcePath = resourcePath.substring(1);
- Path path = Paths.get("./src/test/resources").resolve(resourcePath).normalize();
+ Path path = Paths.get("./src/test/test-project/workspace/src").resolve(resourcePath).normalize();
File file = path.toFile();
if (!file.exists())
diff --git a/src/test/java/org/javacs/InferConfigTest.java b/src/test/java/org/javacs/InferConfigTest.java
index 46be918..0c42e2d 100644
--- a/src/test/java/org/javacs/InferConfigTest.java
+++ b/src/test/java/org/javacs/InferConfigTest.java
@@ -1,28 +1,27 @@
package org.javacs;
import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
-import org.junit.Test;
-import static org.junit.Assert.*;
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.contains;
+import static org.junit.Assert.assertThat;
public class InferConfigTest {
- Path workspaceRoot = Paths.get("src/test/test-project/workspace");
- Path mavenHome = Paths.get("src/test/test-project/home/.m2");
- Path gradleHome = Paths.get("src/test/test-project/home/.gradle");
- Path outputDirectory = createOutputDir();
- List<Artifact> externalDependencies = ImmutableList.of(new Artifact("com.external", "external-library", "1.2"));
- InferConfig both = new InferConfig(workspaceRoot, externalDependencies, mavenHome, gradleHome, outputDirectory),
+ private Path workspaceRoot = Paths.get("src/test/test-project/workspace");
+ private Path mavenHome = Paths.get("src/test/test-project/home/.m2");
+ private Path gradleHome = Paths.get("src/test/test-project/home/.gradle");
+ private Path outputDirectory = createOutputDir();
+ private List<Artifact> externalDependencies = ImmutableList.of(new Artifact("com.external", "external-library", "1.2"));
+ private InferConfig both = new InferConfig(workspaceRoot, externalDependencies, mavenHome, gradleHome, outputDirectory),
gradle = new InferConfig(workspaceRoot, externalDependencies, Paths.get("nowhere"), gradleHome, outputDirectory);
- Path createOutputDir() {
+ private Path createOutputDir() {
try {
return Files.createTempDirectory("output").toAbsolutePath();
} catch (IOException e) {
@@ -33,7 +32,7 @@ public class InferConfigTest {
@Test
public void workspaceSourcePath() {
assertThat(
- both.workspaceSourcePath(),
+ InferConfig.workspaceSourcePath(workspaceRoot),
contains(workspaceRoot.resolve("src"))
);
}
diff --git a/src/test/java/org/javacs/JavaCompilerTest.java b/src/test/java/org/javacs/JavaCompilerTest.java
index 657b125..3575afc 100644
--- a/src/test/java/org/javacs/JavaCompilerTest.java
+++ b/src/test/java/org/javacs/JavaCompilerTest.java
@@ -3,15 +3,10 @@ package org.javacs;
import com.google.common.collect.ImmutableList;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.JavacTask;
-import com.sun.source.util.TaskEvent;
-import com.sun.source.util.TaskEvent.Kind;
-import com.sun.source.util.TaskListener;
import com.sun.tools.javac.api.JavacTool;
-import java.util.EnumMap;
import org.junit.Test;
import javax.lang.model.element.Element;
-import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
@@ -25,9 +20,6 @@ import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertThat;
-
public class JavaCompilerTest {
private static final Logger LOG = Logger.getLogger("main");
@@ -49,8 +41,12 @@ public class JavaCompilerTest {
@Test
public void javacHolder() {
- JavacHolder javac = JavacHolder.create(Collections.emptySet(), Collections.singleton(Paths.get("src/test/resources")), Paths.get("target/test-output"));
- File file = Paths.get("src/test/resources/org/javacs/example/Bad.java").toFile();
+ JavacHolder javac = JavacHolder.create(
+ Collections.singleton(Paths.get("src/test/test-project/workspace/src")),
+ Collections.emptySet(),
+ Paths.get("target/test-output")
+ );
+ File file = Paths.get("src/test/test-project/workspace/src/org/javacs/example/Bad.java").toFile();
BatchResult compile = javac.compileBatch(Collections.singletonMap(file.toURI(), Optional.empty()));
}
diff --git a/src/test/java/org/javacs/JavadocsTest.java b/src/test/java/org/javacs/JavadocsTest.java
index ae20f42..ce248f7 100644
--- a/src/test/java/org/javacs/JavadocsTest.java
+++ b/src/test/java/org/javacs/JavadocsTest.java
@@ -2,31 +2,25 @@ package org.javacs;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.RootDoc;
-import com.sun.source.util.Trees;
import org.junit.Ignore;
import org.junit.Test;
-import javax.lang.model.element.ExecutableElement;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Optional;
import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
public class JavadocsTest {
- private final Javadocs docs = new Javadocs(Collections.singleton(Paths.get("src/test/resources")));
- private final JavacHolder compiler = newCompiler();
-
- private static JavacHolder newCompiler() {
- return JavacHolder.create(
- Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("target/test-output")
- );
- }
+ private final Javadocs docs = new Javadocs(
+ Collections.singleton(Paths.get("src/test/test-project/workspace/src")),
+ Collections.emptySet(),
+ __ -> Optional.empty()
+ );
@Test
public void findSystemDoc() throws IOException {
@@ -45,7 +39,8 @@ public class JavadocsTest {
assertTrue("Found method", docs.methodDoc("org.javacs.docs.TrickyDocstring#parameterized(java.lang.Object)").isPresent());
}
- @Test
+ @Test
+ @Ignore // Blocked by emptyFileManager
public void findInheritedDoc() {
Optional<MethodDoc> found = docs.methodDoc("org.javacs.docs.SubDoc#method()");
diff --git a/src/test/java/org/javacs/LanguageServerFixture.java b/src/test/java/org/javacs/LanguageServerFixture.java
index e803bdb..656ed59 100644
--- a/src/test/java/org/javacs/LanguageServerFixture.java
+++ b/src/test/java/org/javacs/LanguageServerFixture.java
@@ -5,8 +5,6 @@ import org.eclipse.lsp4j.services.LanguageClient;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.Collections;
-import java.util.Set;
import java.util.concurrent.CompletableFuture;
class LanguageServerFixture {
@@ -45,16 +43,12 @@ class LanguageServerFixture {
}
static JavaLanguageServer getJavaLanguageServer(LanguageClient client) {
- Set<Path> classPath = Collections.emptySet();
- Set<Path> sourcePath = Collections.singleton(Paths.get("src/test/resources").toAbsolutePath());
- Path outputDirectory = Paths.get("target/test-output").toAbsolutePath();
- JavacHolder javac = JavacHolder.create(classPath, sourcePath, outputDirectory);
- JavaLanguageServer server = new JavaLanguageServer(javac);
+ Path workspaceRoot = Paths.get("src/test/test-project/workspace").toAbsolutePath();
+ JavaLanguageServer server = new JavaLanguageServer();
InitializeParams init = new InitializeParams();
- String workspaceRoot = Paths.get(".").toAbsolutePath().toString();
- init.setRootPath(workspaceRoot);
+ init.setRootPath(workspaceRoot.toString());
server.initialize(init);
server.installClient(client);
diff --git a/src/test/java/org/javacs/LinterTest.java b/src/test/java/org/javacs/LinterTest.java
index ddad1a8..76d37e1 100644
--- a/src/test/java/org/javacs/LinterTest.java
+++ b/src/test/java/org/javacs/LinterTest.java
@@ -19,8 +19,8 @@ public class LinterTest {
private static final Logger LOG = Logger.getLogger("main");
private static final JavacHolder compiler = JavacHolder.create(
+ Collections.singleton(Paths.get("src/test/test-project/workspace/src")),
Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
Paths.get("target/test-output")
);
diff --git a/src/test/java/org/javacs/RecompileTest.java b/src/test/java/org/javacs/RecompileTest.java
index 5c02b3c..05b3aa2 100644
--- a/src/test/java/org/javacs/RecompileTest.java
+++ b/src/test/java/org/javacs/RecompileTest.java
@@ -63,8 +63,8 @@ public class RecompileTest {
private static JavacHolder newCompiler() {
return JavacHolder.create(
+ Collections.singleton(Paths.get("src/test/test-project/workspace/src")),
Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
Paths.get("target/test-output")
);
}
diff --git a/src/test/java/org/javacs/RefactorFileTest.java b/src/test/java/org/javacs/RefactorFileTest.java
index 258a5f6..8d8bc62 100644
--- a/src/test/java/org/javacs/RefactorFileTest.java
+++ b/src/test/java/org/javacs/RefactorFileTest.java
@@ -200,7 +200,11 @@ public class RefactorFileTest {
}
private ParseResult file(String content) {
- JavacHolder compiler = JavacHolder.create(Collections.emptySet(), Collections.emptySet(), Paths.get("test-output"));
+ JavacHolder compiler = JavacHolder.create(
+ Collections.singleton(Paths.get("src/test/test-project/workspace/src")),
+ Collections.emptySet(),
+ Paths.get("target/test-output")
+ );
return compiler.parse(FAKE_FILE, Optional.of(content), error -> LOG.warning(error.toString()));
}
diff --git a/src/test/java/org/javacs/SearchTest.java b/src/test/java/org/javacs/SearchTest.java
index 75fe289..7e2b4cd 100644
--- a/src/test/java/org/javacs/SearchTest.java
+++ b/src/test/java/org/javacs/SearchTest.java
@@ -25,7 +25,7 @@ public class SearchTest {
@BeforeClass
public static void openSource() throws URISyntaxException, IOException {
- URI uri = SearchTest.class.getResource("/org/javacs/example/AutocompleteBetweenLines.java").toURI();
+ URI uri = FindResource.uri("/org/javacs/example/AutocompleteBetweenLines.java");
String textContent = Joiner.on("\n").join(Files.readAllLines(Paths.get(uri)));
TextDocumentItem document = new TextDocumentItem();
diff --git a/src/test/java/org/javacs/SymbolUnderCursorTest.java b/src/test/java/org/javacs/SymbolUnderCursorTest.java
index fa68618..5608fbf 100644
--- a/src/test/java/org/javacs/SymbolUnderCursorTest.java
+++ b/src/test/java/org/javacs/SymbolUnderCursorTest.java
@@ -4,8 +4,6 @@ import org.junit.Ignore;
import org.junit.Test;
import javax.lang.model.element.Element;
-import java.nio.file.Paths;
-import java.util.Collections;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
@@ -78,19 +76,11 @@ public class SymbolUnderCursorTest {
assertEquals("localVariable", symbolAt("/org/javacs/example/SymbolUnderCursor.java", 10, 16));
}
+ private final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
+
private String symbolAt(String file, int line, int character) {
- Optional<Element> symbol = new JavaLanguageServer(compiler).findSymbol(FindResource.uri(file), line, character);
+ Optional<Element> symbol = server.findSymbol(FindResource.uri(file), line, character);
return symbol.map(s -> s.getSimpleName().toString()).orElse(null);
}
-
- private static JavacHolder compiler = newCompiler();
-
- private static JavacHolder newCompiler() {
- return JavacHolder.create(
- Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("target/test-output")
- );
- }
}