summaryrefslogtreecommitdiff
path: root/src/test/java/org/javacs
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2017-03-19 13:38:40 -0700
committerGeorge Fraser <george@fivetran.com>2017-03-19 13:38:40 -0700
commit69328dca64e15d6acea74866555de7feb462db5b (patch)
treef4e882432695eacdeb0b32545575c56ad6e6d84e /src/test/java/org/javacs
parent139784693f4bef6078fdc58e7ce17a9416e1a6c8 (diff)
downloadjava-language-server-69328dca64e15d6acea74866555de7feb462db5b.zip
Fixed most tests
Diffstat (limited to 'src/test/java/org/javacs')
-rw-r--r--src/test/java/org/javacs/AutocompleteTest.java23
-rw-r--r--src/test/java/org/javacs/CompilerProfiling.java27
-rw-r--r--src/test/java/org/javacs/ContextPrinter.java218
-rw-r--r--src/test/java/org/javacs/FindResource.java24
-rw-r--r--src/test/java/org/javacs/GotoTest.java23
-rw-r--r--src/test/java/org/javacs/JavaCompilerTest.java15
-rw-r--r--src/test/java/org/javacs/JavacHolderTest.java30
-rw-r--r--src/test/java/org/javacs/LanguageServerFixture.java (renamed from src/test/java/org/javacs/Fixtures.java)10
-rw-r--r--src/test/java/org/javacs/LinterTest.java127
-rw-r--r--src/test/java/org/javacs/ParserTest.java22
-rw-r--r--src/test/java/org/javacs/RecompileTest.java99
-rw-r--r--src/test/java/org/javacs/SymbolIndexTest.java42
-rw-r--r--src/test/java/org/javacs/SymbolUnderCursorTest.java24
13 files changed, 418 insertions, 266 deletions
diff --git a/src/test/java/org/javacs/AutocompleteTest.java b/src/test/java/org/javacs/AutocompleteTest.java
index 1f0a3eb..4640f25 100644
--- a/src/test/java/org/javacs/AutocompleteTest.java
+++ b/src/test/java/org/javacs/AutocompleteTest.java
@@ -1,12 +1,13 @@
package org.javacs;
-import org.eclipse.lsp4j.*;
+import org.eclipse.lsp4j.CompletionItem;
+import org.eclipse.lsp4j.Position;
+import org.eclipse.lsp4j.TextDocumentIdentifier;
+import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
@@ -16,7 +17,7 @@ import java.util.stream.Stream;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
-public class AutocompleteTest extends Fixtures {
+public class AutocompleteTest {
private static final Logger LOG = Logger.getLogger("main");
@Test
@@ -417,6 +418,8 @@ public class AutocompleteTest extends Fixtures {
.collect(Collectors.toSet());
}
+ private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
+
private List<? extends CompletionItem> items(String file, int row, int column) {
TextDocumentPositionParams position = new TextDocumentPositionParams();
@@ -424,18 +427,8 @@ public class AutocompleteTest extends Fixtures {
position.getPosition().setLine(row);
position.getPosition().setCharacter(column);
position.setTextDocument(new TextDocumentIdentifier());
- position.getTextDocument().setUri(uri(file).toString());
-
- JavaLanguageServer server = getJavaLanguageServer();
+ position.getTextDocument().setUri(FindResource.uri(file).toString());
return server.autocomplete(position).getItems();
}
-
- private URI uri(String file) {
- try {
- return AutocompleteTest.class.getResource(file).toURI();
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
}
diff --git a/src/test/java/org/javacs/CompilerProfiling.java b/src/test/java/org/javacs/CompilerProfiling.java
index 5056a88..dc2edc4 100644
--- a/src/test/java/org/javacs/CompilerProfiling.java
+++ b/src/test/java/org/javacs/CompilerProfiling.java
@@ -6,26 +6,24 @@ import com.sun.tools.javac.util.Context;
import org.junit.Ignore;
import org.junit.Test;
-import javax.tools.DiagnosticCollector;
-import javax.tools.JavaFileObject;
import java.io.IOException;
+import java.net.URI;
import java.net.URISyntaxException;
-import java.nio.file.Files;
-import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collections;
+import java.util.Optional;
import java.util.logging.Logger;
import static org.junit.Assert.assertNotNull;
@Ignore
-public class CompilerProfiling extends Fixtures {
+public class CompilerProfiling {
private static final Logger LOG = Logger.getLogger("main");
@Test
public void parsingSpeed() throws IOException, URISyntaxException {
- StringFileObject file = fromResource("/org/javacs/example/LargeFile.java");
+ URI file = FindResource.uri("/org/javacs/example/LargeFile.java");
for (int i = 0; i < 10; i++) {
Duration duration = compileLargeFile(file);
@@ -34,19 +32,14 @@ public class CompilerProfiling extends Fixtures {
}
}
- private Duration compileLargeFile(StringFileObject file) {
+ private Duration compileLargeFile(URI file) {
long start = System.nanoTime();
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- JavacHolder compiler = new JavacHolder(Collections.emptySet(), Collections.emptySet(), Paths.get("out"));
+ JavacHolder compiler = new JavacHolder(Collections.emptySet(), Collections.emptySet(), Paths.get("out"), false);
GetCompilationUnit compilationUnit = new GetCompilationUnit(compiler.context);
- compiler.onError(errors);
-
try {
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- tree.accept(compilationUnit);
+ compiler.compile(Collections.singletonMap(file, Optional.empty())).trees.forEach(tree -> tree.accept(compilationUnit));
} catch (RuntimeException e) {
if (e.getCause() instanceof AbortCompilation)
LOG.info("Aborted further stages");
@@ -61,12 +54,6 @@ public class CompilerProfiling extends Fixtures {
return Duration.ofNanos(finish - start);
}
- private StringFileObject fromResource(String file) throws URISyntaxException, IOException {
- Path path = Paths.get(LinterTest.class.getResource(file).toURI());
- String content = new String(Files.readAllBytes(path));
- return new StringFileObject(content, path);
- }
-
private static class GetCompilationUnit extends BaseScanner {
private CompilationUnitTree result;
diff --git a/src/test/java/org/javacs/ContextPrinter.java b/src/test/java/org/javacs/ContextPrinter.java
new file mode 100644
index 0000000..c61827c
--- /dev/null
+++ b/src/test/java/org/javacs/ContextPrinter.java
@@ -0,0 +1,218 @@
+package org.javacs;
+
+import com.sun.tools.javac.util.Context;
+
+import java.lang.reflect.Field;
+import java.util.*;
+
+public class ContextPrinter {
+ public static Tree tree(Context context, int depth) throws NoSuchFieldException, IllegalAccessException {
+ Field ht = Context.class.getDeclaredField("ht");
+
+ ht.setAccessible(true);
+
+ Object value = ht.get(context);
+ TreeConverter treeConverter = new TreeConverter();
+
+ treeConverter.seen.add(context);
+
+ return treeConverter.convert(value, depth);
+ }
+}
+
+class TreeConverter {
+ Set<Object> seen = new HashSet<>();
+
+ Tree convert(Object o, int depth) throws IllegalAccessException {
+ if (o == null)
+ return new Leaf("null");
+ else if (seen.contains(o))
+ return new Leaf("<" + o.getClass().getSimpleName() + ">");
+ else {
+ seen.add(o);
+
+ if (o instanceof Map) {
+ Node node = Node.anonymous();
+ Map map = (Map) o;
+
+ for (Object key : map.keySet()) {
+ Object value = map.get(key);
+
+ node.children.put(keyString(key, value, node.children.keySet()), convert(value, depth));
+ }
+
+ return node;
+ } else if (o instanceof Iterable) {
+ Seq seq = new Seq();
+ Iterable it = (Iterable) o;
+
+ for (Object each : it)
+ seq.children.add(convert(each, depth));
+
+ return seq;
+ } else if (depth > 0) {
+ Class<?> klass = o.getClass();
+ Node node = Node.named(klass.getSimpleName());
+
+ for (Field field : klass.getDeclaredFields()) {
+ field.setAccessible(true);
+
+ Object value = field.get(o);
+
+ node.children.put(field.getName(), convert(value, depth - 1));
+ }
+
+ return node;
+ }
+ else return new Leaf("<" + o.getClass().getSimpleName() + ">");
+ }
+ }
+
+ private String keyString(Object key, Object value, Set<String> used) {
+ if (key instanceof Context.Key)
+ return "Context.Key<" + value.getClass().getSimpleName() + ">";
+ else
+ return key.toString();
+ }
+}
+
+class IndentPrinter {
+ private final StringBuilder out = new StringBuilder();
+ private int indent = 0;
+ private boolean startOfLine = true;
+
+ IndentPrinter increaseIndent() {
+ indent++;
+
+ return this;
+ }
+
+ IndentPrinter decreaseIndent() {
+ indent--;
+
+ return this;
+ }
+
+ IndentPrinter append(String content) {
+ if (startOfLine) {
+ for (int i = 0; i < indent; i++)
+ out.append(" ");
+
+ startOfLine = false;
+ }
+
+ out.append(content);
+
+ return this;
+ }
+
+ IndentPrinter newline() {
+ out.append("\n");
+ startOfLine = true;
+
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return out.toString();
+ }
+}
+
+abstract class Tree {
+ protected abstract void print(IndentPrinter out);
+
+ @Override
+ public String toString() {
+ IndentPrinter printer = new IndentPrinter();
+
+ print(printer);
+
+ return printer.toString();
+ }
+}
+
+class Node extends Tree {
+ final Optional<String> name;
+ final Map<String, Tree> children = new HashMap<>();
+
+ private Node(Optional<String> name) {
+ this.name = name;
+ }
+
+ static Node anonymous() {
+ return new Node(Optional.empty());
+ }
+
+ static Node named(String name) {
+ return new Node(Optional.of(name));
+ }
+
+ @Override
+ public void print(IndentPrinter out) {
+ name.ifPresent(out::append);
+
+ out.append("{").newline().increaseIndent();
+
+ children.forEach((key, value) -> {
+ out.append(key).append(": ");
+ value.print(out);
+ out.newline();
+ });
+
+ out.decreaseIndent().append("}");
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Node node = (Node) o;
+ return Objects.equals(name, node.name) &&
+ Objects.equals(children, node.children);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, children);
+ }
+}
+
+class Seq extends Tree {
+ final List<Tree> children = new ArrayList<>();
+
+ @Override
+ public void print(IndentPrinter out) {
+ out.append("[").newline().increaseIndent();
+
+ children.forEach(child -> child.print(out));
+
+ out.decreaseIndent().append("]");
+ }
+}
+
+class Leaf extends Tree {
+ final String value;
+
+ Leaf(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public void print(IndentPrinter out) {
+ out.append(value);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Leaf leaf = (Leaf) o;
+ return Objects.equals(value, leaf.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(value);
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/javacs/FindResource.java b/src/test/java/org/javacs/FindResource.java
new file mode 100644
index 0000000..355223a
--- /dev/null
+++ b/src/test/java/org/javacs/FindResource.java
@@ -0,0 +1,24 @@
+package org.javacs;
+
+import java.io.File;
+import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * Represents a java source on the system resource path.
+ */
+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();
+ File file = path.toFile();
+
+ if (!file.exists())
+ throw new RuntimeException(file + " does not exist");
+
+ return file.toURI();
+ }
+}
diff --git a/src/test/java/org/javacs/GotoTest.java b/src/test/java/org/javacs/GotoTest.java
index afda170..66f3820 100644
--- a/src/test/java/org/javacs/GotoTest.java
+++ b/src/test/java/org/javacs/GotoTest.java
@@ -6,20 +6,16 @@ import org.junit.Test;
import java.io.IOException;
import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Logger;
import static org.hamcrest.Matchers.contains;
-import static org.hamcrest.Matchers.hasItems;
-import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
-public class GotoTest extends Fixtures {
+public class GotoTest {
private static final Logger LOG = Logger.getLogger("main");
private static final String file = "/org/javacs/example/Goto.java";
- private static final URI uri = uri(file);
+ private static final URI uri = FindResource.uri(file);
@Test
public void localVariable() throws IOException {
@@ -68,6 +64,8 @@ public class GotoTest extends Fixtures {
public void staticMethod() throws IOException {
List<? extends Location> suggestions = doGoto(file, 15, 13);
+ System.out.println(suggestions.size());
+
assertThat(suggestions, contains(location(uri, 37, 25, 37, 37)));
}
@@ -124,10 +122,12 @@ public class GotoTest extends Fixtures {
return location;
}
+ private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
+
private List<? extends Location> doGoto(String file, int row, int column) throws IOException {
TextDocumentIdentifier document = new TextDocumentIdentifier();
- document.setUri(uri(file).toString());
+ document.setUri(FindResource.uri(file).toString());
Position position = new Position();
@@ -139,16 +139,7 @@ public class GotoTest extends Fixtures {
p.setTextDocument(document);
p.setPosition(position);
- JavaLanguageServer server = Fixtures.getJavaLanguageServer();
-
return server.gotoDefinition(p);
}
- private static URI uri(String file) {
- try {
- return GotoTest.class.getResource(file).toURI();
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
}
diff --git a/src/test/java/org/javacs/JavaCompilerTest.java b/src/test/java/org/javacs/JavaCompilerTest.java
index 4c90cf2..b0dc956 100644
--- a/src/test/java/org/javacs/JavaCompilerTest.java
+++ b/src/test/java/org/javacs/JavaCompilerTest.java
@@ -4,18 +4,21 @@ import com.google.common.collect.ImmutableList;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.JavacTool;
-import com.sun.tools.javac.tree.JCTree;
import org.junit.Ignore;
import org.junit.Test;
import javax.lang.model.element.Element;
-import javax.tools.*;
+import javax.tools.Diagnostic;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import java.util.logging.Logger;
// TODO java compiler can fail badly, handle somehow
@@ -41,13 +44,9 @@ public class JavaCompilerTest {
@Test
public void javacHolder() {
- JavacHolder javac = new JavacHolder(Collections.emptySet(), Collections.singleton(Paths.get("src/test/resources")), Paths.get("target"));
+ JavacHolder javac = new JavacHolder(Collections.emptySet(), Collections.singleton(Paths.get("src/test/resources")), Paths.get("target"), false);
File file = Paths.get("src/test/resources/org/javacs/example/Bad.java").toFile();
- JCTree.JCCompilationUnit parsed = javac.parse(javac.fileManager.getRegularFile(file));
-
- javac.compile(Collections.singleton(parsed));
-
- LOG.info(parsed.toString());
+ CompilationResult compile = javac.compile(Collections.singletonMap(file.toURI(), Optional.empty()));
}
private void reportError(Diagnostic<? extends JavaFileObject> error) {
diff --git a/src/test/java/org/javacs/JavacHolderTest.java b/src/test/java/org/javacs/JavacHolderTest.java
new file mode 100644
index 0000000..2f6f5e7
--- /dev/null
+++ b/src/test/java/org/javacs/JavacHolderTest.java
@@ -0,0 +1,30 @@
+package org.javacs;
+
+import org.junit.Test;
+
+import java.net.URI;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+public class JavacHolderTest {
+ @Test
+ public void clear() throws NoSuchFieldException, IllegalAccessException {
+ JavacHolder javac = new JavacHolder(Collections.emptySet(), Collections.singleton(Paths.get("src/test/resources")), Paths.get("target"), false);
+ URI file = FindResource.uri("org/javacs/example/FooString.java");
+ CompilationResult compile = javac.compile(Collections.singletonMap(file, Optional.empty()));
+ Tree tree = ContextPrinter.tree(javac.context, 3);
+
+ assertThat(tree.toString(), containsString("FooString"));
+
+ javac.clear(file);
+
+ System.out.println(tree.toString());
+
+ assertThat(tree.toString(), not(containsString("FooString")));
+ }
+}
diff --git a/src/test/java/org/javacs/Fixtures.java b/src/test/java/org/javacs/LanguageServerFixture.java
index f3c6db6..3c24acb 100644
--- a/src/test/java/org/javacs/Fixtures.java
+++ b/src/test/java/org/javacs/LanguageServerFixture.java
@@ -1,23 +1,19 @@
package org.javacs;
import org.eclipse.lsp4j.InitializeParams;
-import org.junit.BeforeClass;
-import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Set;
-public class Fixtures {
+public class LanguageServerFixture {
- public static void init() { }
-
- static JavaLanguageServer getJavaLanguageServer() {
+ public static JavaLanguageServer getJavaLanguageServer() {
Set<Path> classPath = Collections.emptySet();
Set<Path> sourcePath = Collections.singleton(Paths.get("src/test/resources").toAbsolutePath());
Path outputDirectory = Paths.get("out").toAbsolutePath();
- JavacHolder javac = new JavacHolder(classPath, sourcePath, outputDirectory);
+ JavacHolder javac = new JavacHolder(classPath, sourcePath, outputDirectory, true);
JavaLanguageServer server = new JavaLanguageServer(javac);
InitializeParams init = new InitializeParams();
diff --git a/src/test/java/org/javacs/LinterTest.java b/src/test/java/org/javacs/LinterTest.java
index 09d3422..88b0680 100644
--- a/src/test/java/org/javacs/LinterTest.java
+++ b/src/test/java/org/javacs/LinterTest.java
@@ -11,6 +11,7 @@ import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
import java.io.IOException;
+import java.net.URI;
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Logger;
@@ -18,133 +19,90 @@ import java.util.logging.Logger;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
-public class LinterTest extends Fixtures {
+public class LinterTest {
private static final Logger LOG = Logger.getLogger("main");
@Test
public void compile() throws IOException {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/HelloWorld.java");
- JavacHolder compiler = newCompiler();
- compiler.onError(errors);
- compiler.compile(Collections.singleton(compiler.parse(file)));
+ URI file = FindResource.uri("/org/javacs/example/HelloWorld.java");
+ DiagnosticCollector<JavaFileObject> errors = newCompiler().compile(Collections.singletonMap(file, Optional.empty())).errors;
assertThat(errors.getDiagnostics(), empty());
}
@Test
public void inspectTree() throws IOException {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/HelloWorld.java");
+ URI file = FindResource.uri("/org/javacs/example/HelloWorld.java");
JavacHolder compiler = newCompiler();
CollectMethods scanner = new CollectMethods(compiler.context);
- compiler.onError(errors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
-
- tree.accept(scanner);
+ compiler.compile(Collections.singletonMap(file, Optional.empty())).trees.forEach(tree -> tree.accept(scanner));
assertThat(scanner.methodNames, hasItem("main"));
}
@Test
public void missingMethodBody() throws IOException {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/MissingMethodBody.java");
+ URI file = FindResource.uri("/org/javacs/example/MissingMethodBody.java");
JavacHolder compiler = newCompiler();
CollectMethods scanner = new CollectMethods(compiler.context);
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.onError(errors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
-
- tree.accept(scanner);
+ compile.trees.forEach(tree -> tree.accept(scanner));
assertThat(scanner.methodNames, hasItem("test"));
- assertThat(errors.getDiagnostics(), not(empty()));
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
// Lint again
- errors = new DiagnosticCollector<>();
-
- compiler.onError(errors);
-
- tree = compiler.parse(file);
+ compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.compile(Collections.singleton(tree));
-
- assertThat(errors.getDiagnostics(), not(empty()));
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
}
@Test
public void incompleteAssignment() throws IOException {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/IncompleteAssignment.java");
+ URI file = FindResource.uri("/org/javacs/example/IncompleteAssignment.java");
JavacHolder compiler = newCompiler();
- CollectMethods parsed = new CollectMethods(compiler.context);
CollectMethods compiled = new CollectMethods(compiler.context);
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.onError(errors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
+ compile.trees.forEach(tree -> tree.accept(compiled));
- tree.accept(parsed);
-
- compiler.compile(Collections.singleton(tree));
-
- tree.accept(compiled);
-
- assertThat(parsed.methodNames, hasItem("test")); // Error recovery should have worked
assertThat(compiled.methodNames, hasItem("test")); // Type error recovery should have worked
- assertThat(errors.getDiagnostics(), not(empty()));
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
}
@Test
public void undefinedSymbol() throws IOException {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/UndefinedSymbol.java");
+ URI file = FindResource.uri("/org/javacs/example/UndefinedSymbol.java");
JavacHolder compiler = newCompiler();
CollectMethods scanner = new CollectMethods(compiler.context);
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.onError(errors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
-
- tree.accept(scanner);
+ compile.trees.forEach(tree -> tree.accept(scanner));
assertThat(scanner.methodNames, hasItem("test")); // Type error, so parse tree is present
- Diagnostic<? extends JavaFileObject> d = errors.getDiagnostics().get(0);
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
+
+ Diagnostic<? extends JavaFileObject> d = compile.errors.getDiagnostics().get(0);
// Error position should span entire 'foo' symbol
assertThat(d.getLineNumber(), greaterThan(0L));
assertThat(d.getStartPosition(), greaterThan(0L));
assertThat(d.getEndPosition(), greaterThan(d.getStartPosition() + 1));
- assertThat(errors.getDiagnostics(), not(empty()));
}
@Test
public void getType() {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/FooString.java");
+ URI file = FindResource.uri("/org/javacs/example/FooString.java");
JavacHolder compiler = newCompiler();
MethodTypes scanner = new MethodTypes(compiler.context);
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.onError(errors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
+ compile.trees.forEach(tree -> tree.accept(scanner));
- tree.accept(scanner);
-
- assertThat(errors.getDiagnostics(), empty());
+ assertThat(compile.errors.getDiagnostics(), empty());
assertThat(scanner.methodTypes, hasKey("test"));
Type.MethodType type = scanner.methodTypes.get("test");
@@ -156,35 +114,29 @@ public class LinterTest extends Fixtures {
@Test
public void notJava() {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/NotJava.java.txt");
+ URI file = FindResource.uri("/org/javacs/example/NotJava.java.txt");
JavacHolder compiler = newCompiler();
- compiler.onError(errors);
- compiler.compile(Collections.singleton(compiler.parse(file)));
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- assertThat(errors.getDiagnostics(), not(empty()));
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
}
@Test
public void errorInDependency() {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/ErrorInDependency.java");
+ URI file = FindResource.uri("/org/javacs/example/ErrorInDependency.java");
JavacHolder compiler = newCompiler();
- compiler.onError(errors);
- compiler.compile(Collections.singleton(compiler.parse(file)));
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- assertThat(errors.getDiagnostics(), not(empty()));
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
}
@Test
public void deprecationWarning() {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/DeprecationWarning.java");
+ URI file = FindResource.uri("/org/javacs/example/DeprecationWarning.java");
JavacHolder compiler = newCompiler();
- compiler.onError(errors);
- compiler.compile(Collections.singleton(compiler.parse(file)));
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- assertThat(errors.getDiagnostics(), not(empty()));
+ assertThat(compile.errors.getDiagnostics(), not(empty()));
}
public static class MethodTypes extends BaseScanner {
@@ -222,8 +174,11 @@ public class LinterTest extends Fixtures {
}
private static JavacHolder newCompiler() {
- return new JavacHolder(Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("out"));
+ return new JavacHolder(
+ Collections.emptySet(),
+ Collections.singleton(Paths.get("src/test/resources")),
+ Paths.get("out"),
+ false
+ );
}
}
diff --git a/src/test/java/org/javacs/ParserTest.java b/src/test/java/org/javacs/ParserTest.java
index 2a3074d..63a93f6 100644
--- a/src/test/java/org/javacs/ParserTest.java
+++ b/src/test/java/org/javacs/ParserTest.java
@@ -5,39 +5,41 @@ import com.sun.tools.javac.tree.TreeScanner;
import org.junit.Test;
import java.io.IOException;
+import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
-public class ParserTest extends Fixtures {
+public class ParserTest {
@Test
public void missingSemicolon() throws IOException, URISyntaxException {
JavacHolder compiler = newCompiler();
List<String> methods = new ArrayList<>();
+ URI file = FindResource.uri("/org/javacs/example/MissingSemicolon.java");
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/MissingSemicolon.java");
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- tree.accept(new TreeScanner() {
+ compiler.compile(Collections.singletonMap(file, Optional.empty())).trees.forEach(t -> t.accept(new TreeScanner() {
@Override
public void visitMethodDef(JCTree.JCMethodDecl node) {
methods.add(node.getName().toString());
}
- });
+ }));
assertThat(methods, hasItem("methodWithMissingSemicolon"));
assertThat(methods, hasItem("methodAfterMissingSemicolon"));
}
private JavacHolder newCompiler() {
- return new JavacHolder(Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("out"));
+ return new JavacHolder(
+ Collections.emptySet(),
+ Collections.singleton(Paths.get("src/test/resources")),
+ Paths.get("out"),
+ false
+ );
}
}
diff --git a/src/test/java/org/javacs/RecompileTest.java b/src/test/java/org/javacs/RecompileTest.java
index 15da1b5..bb7d123 100644
--- a/src/test/java/org/javacs/RecompileTest.java
+++ b/src/test/java/org/javacs/RecompileTest.java
@@ -7,135 +7,102 @@ import org.junit.Test;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
import java.io.IOException;
-import java.nio.file.Path;
+import java.net.URI;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
-public class RecompileTest extends Fixtures {
- static {
- Fixtures.init();
- }
-
+public class RecompileTest {
@Test
public void compileTwice() {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/CompileTwice.java");
+ URI file = FindResource.uri("/org/javacs/example/CompileTwice.java");
JavacHolder compiler = newCompiler();
List<String> visits = new ArrayList<>();
GetClass getClass = new GetClass(compiler.context, visits);
+ CompilationResult compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.onError(errors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
-
- tree.accept(getClass);
+ compile.trees.forEach(tree -> tree.accept(getClass));
- assertThat(errors.getDiagnostics(), empty());
+ assertThat(compile.errors.getDiagnostics(), empty());
assertThat(visits, hasItems("CompileTwice", "NestedStaticClass", "NestedClass"));
// Compile again
- tree = compiler.parse(file);
+ compile = compiler.compile(Collections.singletonMap(file, Optional.empty()));
- compiler.compile(Collections.singleton(tree));
+ compile.trees.forEach(tree -> tree.accept(getClass));
- tree.accept(getClass);
-
- assertThat(errors.getDiagnostics(), empty());
+ assertThat(compile.errors.getDiagnostics(), empty());
assertThat(visits, hasItems("CompileTwice", "NestedStaticClass", "NestedClass",
"CompileTwice", "NestedStaticClass", "NestedClass"));
}
@Test
public void fixParseError() {
- Path path = Paths.get("org/javacs/example/FixParseError.java");
- StringFileObject bad = new StringFileObject("public class FixParseError { public String foo() { return \"foo\"; }", path);
- StringFileObject good = new StringFileObject("public class FixParseError { public String foo() { return \"foo\"; } }", path);
+ URI bad = FindResource.uri("/org/javacs/example/FixParseErrorBefore.java");
+ URI good = FindResource.uri("/org/javacs/example/FixParseErrorAfter.java");
JavacHolder compiler = newCompiler();
- DiagnosticCollector<JavaFileObject> badErrors = new DiagnosticCollector<>();
-
- compiler.onError(badErrors);
- compiler.parse(bad);
-
- DiagnosticCollector<JavaFileObject> goodErrors = new DiagnosticCollector<>();
+ DiagnosticCollector<JavaFileObject> badErrors = compiler.compile(Collections.singletonMap(bad, Optional.empty())).errors;
assertThat(badErrors.getDiagnostics(), not(empty()));
// Parse again
+ CompilationResult goodCompile = compiler.compile(Collections.singletonMap(good, Optional.empty()));
+ DiagnosticCollector<JavaFileObject> goodErrors = goodCompile.errors;
List<String> parsedClassNames = new ArrayList<>();
GetClass getClass = new GetClass(compiler.context, parsedClassNames);
- compiler.onError(goodErrors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(good);
-
- tree.accept(getClass);
+ goodCompile.trees.forEach(tree -> tree.accept(getClass));
assertThat(goodErrors.getDiagnostics(), empty());
- assertThat(parsedClassNames, contains("FixParseError"));
+ assertThat(parsedClassNames, contains("FixParseErrorAfter"));
}
@Test
public void fixTypeError() {
- Path path = Paths.get("org/javacs/example/FixTypeError.java");
- StringFileObject bad = new StringFileObject("public class FixTypeError { public String foo() { return 1; } }", path);
- StringFileObject good = new StringFileObject("public class FixTypeError { public String foo() { return \"foo\"; } }", path);
+ URI bad = FindResource.uri("/org/javacs/example/FixTypeErrorBefore.java");
+ URI good = FindResource.uri("/org/javacs/example/FixTypeErrorAfter.java");
JavacHolder compiler = newCompiler();
- DiagnosticCollector<JavaFileObject> badErrors = new DiagnosticCollector<>();
-
- compiler.onError(badErrors);
- compiler.compile(Collections.singleton(compiler.parse(bad)));
-
- DiagnosticCollector<JavaFileObject> goodErrors = new DiagnosticCollector<>();
+ DiagnosticCollector<JavaFileObject> badErrors = compiler.compile(Collections.singletonMap(bad, Optional.empty())).errors;
assertThat(badErrors.getDiagnostics(), not(empty()));
// Parse again
+ CompilationResult goodCompile = compiler.compile(Collections.singletonMap(good, Optional.empty()));
+ DiagnosticCollector<JavaFileObject> goodErrors = goodCompile.errors;
List<String> parsedClassNames = new ArrayList<>();
-
GetClass getClass = new GetClass(compiler.context, parsedClassNames);
- compiler.onError(goodErrors);
-
- JCTree.JCCompilationUnit tree = compiler.parse(good);
-
- compiler.compile(Collections.singleton(tree));
-
- tree.accept(getClass);
+ goodCompile.trees.forEach(tree -> tree.accept(getClass));
assertThat(goodErrors.getDiagnostics(), empty());
- assertThat(parsedClassNames, contains("FixTypeError"));
+ assertThat(parsedClassNames, contains("FixTypeErrorAfter"));
}
private static JavacHolder newCompiler() {
- return new JavacHolder(Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("out"));
+ return new JavacHolder(
+ Collections.emptySet(),
+ Collections.singleton(Paths.get("src/test/resources")),
+ Paths.get("out"),
+ false
+ );
}
@Test
public void keepTypeError() throws IOException {
- DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>();
- GetResourceFileObject file = new GetResourceFileObject("/org/javacs/example/UndefinedSymbol.java");
+ URI file = FindResource.uri("/org/javacs/example/UndefinedSymbol.java");
JavacHolder compiler = newCompiler();
// Compile once
- compiler.onError(errors);
- compiler.compile(Collections.singleton(compiler.parse(file)));
-
+ DiagnosticCollector<JavaFileObject> errors = compiler.compile(Collections.singletonMap(file, Optional.empty())).errors;
assertThat(errors.getDiagnostics(), not(empty()));
// Compile twice
- errors = new DiagnosticCollector<>();
- compiler.onError(errors);
-
- compiler.compile(Collections.singleton(compiler.parse(file)));
+ errors = compiler.compile(Collections.singletonMap(file, Optional.empty())).errors;
assertThat(errors.getDiagnostics(), not(empty()));
}
diff --git a/src/test/java/org/javacs/SymbolIndexTest.java b/src/test/java/org/javacs/SymbolIndexTest.java
index cedbc5d..a6f35b7 100644
--- a/src/test/java/org/javacs/SymbolIndexTest.java
+++ b/src/test/java/org/javacs/SymbolIndexTest.java
@@ -1,14 +1,16 @@
package org.javacs;
import com.sun.tools.javac.code.Symbol;
-
-import javax.tools.*;
-import com.sun.tools.javac.tree.*;
+import com.sun.tools.javac.tree.JCTree;
import org.junit.Test;
+import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.*;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -62,7 +64,7 @@ public class SymbolIndexTest {
compile(path);
- List<String> all = index.allInFile(new GetResourceFileObject(path).toUri())
+ List<String> all = index.allInFile(FindResource.uri(path))
.map(s -> s.getName())
.collect(Collectors.toList());
@@ -82,7 +84,7 @@ public class SymbolIndexTest {
compile(path);
- List<String> all = index.allInFile(new GetResourceFileObject(path).toUri())
+ List<String> all = index.allInFile(FindResource.uri(path))
.map(s -> s.getName())
.collect(Collectors.toList());
@@ -90,17 +92,14 @@ public class SymbolIndexTest {
}
private Symbol symbol(String path, int line, int character) {
- return new JavaLanguageServer(compiler).findSymbol(compile(path), line, character).orElse(null);
+ return new JavaLanguageServer(compiler)
+ .findSymbol(FindResource.uri(path), line, character).orElse(null);
}
private JCTree.JCCompilationUnit compile(String path) {
- JavaFileObject file = new GetResourceFileObject(path);
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
- index.update(tree, compiler.context);
+ URI file = FindResource.uri(path);
- return tree;
+ return compiler.compile(Collections.singletonMap(file, Optional.empty())).trees.stream().findFirst().get();
}
private Set<String> search(String query) {
@@ -111,20 +110,19 @@ public class SymbolIndexTest {
private SymbolIndex index = getIndex();
private static SymbolIndex getIndex() {
- Set<Path> sourcePath = Collections.singleton(Paths.get("src/main/java").toAbsolutePath());
- Path outputDirectory = Paths.get("out").toAbsolutePath();
- SymbolIndex index = new SymbolIndex(classPath, sourcePath, outputDirectory);
-
- index.initialIndexComplete.join();
+ compiler.initialIndexComplete.join();
- return index;
+ return compiler.index;
}
private static JavacHolder compiler = newCompiler();
private static JavacHolder newCompiler() {
- return new JavacHolder(Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("out"));
+ return new JavacHolder(
+ Collections.emptySet(),
+ Collections.singleton(Paths.get("src/test/resources")),
+ Paths.get("out"),
+ true
+ );
}
} \ No newline at end of file
diff --git a/src/test/java/org/javacs/SymbolUnderCursorTest.java b/src/test/java/org/javacs/SymbolUnderCursorTest.java
index 0de053b..187372d 100644
--- a/src/test/java/org/javacs/SymbolUnderCursorTest.java
+++ b/src/test/java/org/javacs/SymbolUnderCursorTest.java
@@ -1,18 +1,16 @@
package org.javacs;
import com.sun.tools.javac.code.Symbol;
-import com.sun.tools.javac.tree.JCTree;
import org.junit.Ignore;
import org.junit.Test;
-import javax.tools.JavaFileObject;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
-public class SymbolUnderCursorTest extends Fixtures {
+public class SymbolUnderCursorTest {
@Test
public void classDeclaration() {
@@ -81,25 +79,19 @@ public class SymbolUnderCursorTest extends Fixtures {
}
private String symbolAt(String file, int line, int character) {
- Optional<Symbol> symbol = new JavaLanguageServer(compiler).findSymbol(compile(file), line, character);
+ Optional<Symbol> symbol = new JavaLanguageServer(compiler).findSymbol(FindResource.uri(file), line, character);
return symbol.map(s -> s.getSimpleName().toString()).orElse(null);
}
- private JCTree.JCCompilationUnit compile(String path) {
- JavaFileObject file = new GetResourceFileObject(path);
- JCTree.JCCompilationUnit tree = compiler.parse(file);
-
- compiler.compile(Collections.singleton(tree));
-
- return tree;
- }
-
private static JavacHolder compiler = newCompiler();
private static JavacHolder newCompiler() {
- return new JavacHolder(Collections.emptySet(),
- Collections.singleton(Paths.get("src/test/resources")),
- Paths.get("out"));
+ return new JavacHolder(
+ Collections.emptySet(),
+ Collections.singleton(Paths.get("src/test/resources")),
+ Paths.get("out"),
+ false
+ );
}
}