diff options
Diffstat (limited to 'src/test/java/org/javacs/JavaCompilerServiceTest.java')
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerServiceTest.java | 93 |
1 files changed, 41 insertions, 52 deletions
diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index 563fbb8..d776078 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -3,14 +3,8 @@ package org.javacs; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.tree.LineMap; -import com.sun.source.util.SourcePositions; -import com.sun.source.util.TreePath; -import com.sun.source.util.Trees; import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; @@ -19,11 +13,9 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Set; import java.util.logging.Logger; import java.util.stream.Collectors; import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic; import javax.tools.JavaFileObject; import org.junit.Test; @@ -44,7 +36,7 @@ public class JavaCompilerServiceTest { } private String contents(String resourceFile) { - try (InputStream in = JavaCompilerServiceTest.class.getResourceAsStream(resourceFile)) { + try (var in = JavaCompilerServiceTest.class.getResourceAsStream(resourceFile)) { return new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")); } catch (IOException e) { throw new RuntimeException(e); @@ -61,37 +53,37 @@ public class JavaCompilerServiceTest { @Test public void element() { - Element found = compiler.element(URI.create("/HelloWorld.java"), contents("/HelloWorld.java"), 3, 24); + var found = compiler.element(URI.create("/HelloWorld.java"), contents("/HelloWorld.java"), 3, 24); assertThat(found.getSimpleName(), hasToString(containsString("println"))); } @Test public void elementWithError() { - Element found = compiler.element(URI.create("/CompleteMembers.java"), contents("/CompleteMembers.java"), 3, 12); + var found = compiler.element(URI.create("/CompleteMembers.java"), contents("/CompleteMembers.java"), 3, 12); assertThat(found, notNullValue()); } @Test public void pruneMethods() { - Pruner pruner = new Pruner(URI.create("/PruneMethods.java"), contents("/PruneMethods.java")); + var pruner = new Pruner(URI.create("/PruneMethods.java"), contents("/PruneMethods.java")); pruner.prune(6, 19); - String expected = contents("/PruneMethods_erased.java"); + var expected = contents("/PruneMethods_erased.java"); assertThat(pruner.contents(), equalToIgnoringWhiteSpace(expected)); } @Test public void pruneToEndOfBlock() { - Pruner pruner = new Pruner(URI.create("/PruneToEndOfBlock.java"), contents("/PruneToEndOfBlock.java")); + var pruner = new Pruner(URI.create("/PruneToEndOfBlock.java"), contents("/PruneToEndOfBlock.java")); pruner.prune(4, 18); - String expected = contents("/PruneToEndOfBlock_erased.java"); + var expected = contents("/PruneToEndOfBlock_erased.java"); assertThat(pruner.contents(), equalToIgnoringWhiteSpace(expected)); } private List<String> completionNames(List<Completion> found) { - List<String> result = new ArrayList<>(); - for (Completion c : found) { + var result = new ArrayList<String>(); + for (var c : found) { if (c.element != null) result.add(c.element.getSimpleName().toString()); else if (c.packagePart != null) result.add(c.packagePart.name); else if (c.keyword != null) result.add(c.keyword); @@ -106,10 +98,10 @@ public class JavaCompilerServiceTest { @Test public void identifiers() { - List<Element> found = + var found = compiler.scopeMembers( URI.create("/CompleteIdentifiers.java"), contents("/CompleteIdentifiers.java"), 13, 21); - List<String> names = elementNames(found); + var names = elementNames(found); assertThat(names, hasItem("completeLocal")); assertThat(names, hasItem("completeParam")); assertThat(names, hasItem("super")); @@ -123,9 +115,9 @@ public class JavaCompilerServiceTest { @Test public void identifiersInMiddle() { - List<Element> found = + var found = compiler.scopeMembers(URI.create("/CompleteInMiddle.java"), contents("/CompleteInMiddle.java"), 13, 21); - List<String> names = elementNames(found); + var names = elementNames(found); assertThat(names, hasItem("completeLocal")); assertThat(names, hasItem("completeParam")); assertThat(names, hasItem("super")); @@ -139,7 +131,7 @@ public class JavaCompilerServiceTest { @Test public void completeIdentifiers() { - List<Completion> found = + var found = compiler.completions( URI.create("/CompleteIdentifiers.java"), contents("/CompleteIdentifiers.java"), @@ -147,7 +139,7 @@ public class JavaCompilerServiceTest { 21, Integer.MAX_VALUE) .items; - List<String> names = completionNames(found); + var names = completionNames(found); assertThat(names, hasItem("completeLocal")); assertThat(names, hasItem("completeParam")); assertThat(names, hasItem("super")); @@ -161,9 +153,8 @@ public class JavaCompilerServiceTest { @Test public void members() { - List<Completion> found = - compiler.members(URI.create("/CompleteMembers.java"), contents("/CompleteMembers.java"), 3, 14); - List<String> names = completionNames(found); + var found = compiler.members(URI.create("/CompleteMembers.java"), contents("/CompleteMembers.java"), 3, 14); + var names = completionNames(found); assertThat(names, hasItem("subMethod")); assertThat(names, hasItem("superMethod")); assertThat(names, hasItem("equals")); @@ -171,7 +162,7 @@ public class JavaCompilerServiceTest { @Test public void completeMembers() { - List<Completion> found = + var found = compiler.completions( URI.create("/CompleteMembers.java"), contents("/CompleteMembers.java"), @@ -179,7 +170,7 @@ public class JavaCompilerServiceTest { 15, Integer.MAX_VALUE) .items; - List<String> names = completionNames(found); + var names = completionNames(found); assertThat(names, hasItem("subMethod")); assertThat(names, hasItem("superMethod")); assertThat(names, hasItem("equals")); @@ -187,7 +178,7 @@ public class JavaCompilerServiceTest { @Test public void completeExpression() { - List<Completion> found = + var found = compiler.completions( URI.create("/CompleteExpression.java"), contents("/CompleteExpression.java"), @@ -195,7 +186,7 @@ public class JavaCompilerServiceTest { 37, Integer.MAX_VALUE) .items; - List<String> names = completionNames(found); + var names = completionNames(found); assertThat(names, hasItem("instanceMethod")); assertThat(names, not(hasItem("create"))); assertThat(names, hasItem("equals")); @@ -203,7 +194,7 @@ public class JavaCompilerServiceTest { @Test public void completeClass() { - List<Completion> found = + var found = compiler.completions( URI.create("/CompleteClass.java"), contents("/CompleteClass.java"), @@ -211,7 +202,7 @@ public class JavaCompilerServiceTest { 23, Integer.MAX_VALUE) .items; - List<String> names = completionNames(found); + var names = completionNames(found); assertThat(names, hasItems("staticMethod", "staticField")); assertThat(names, hasItems("class")); assertThat(names, not(hasItem("instanceMethod"))); @@ -220,7 +211,7 @@ public class JavaCompilerServiceTest { @Test public void completeImports() { - List<Completion> found = + var found = compiler.completions( URI.create("/CompleteImports.java"), contents("/CompleteImports.java"), @@ -228,7 +219,7 @@ public class JavaCompilerServiceTest { 18, Integer.MAX_VALUE) .items; - List<String> names = completionNames(found); + var names = completionNames(found); assertThat(names, hasItem("List")); assertThat(names, hasItem("concurrent")); } @@ -236,17 +227,17 @@ public class JavaCompilerServiceTest { /* @Test public void gotoDefinition() { - Optional<TreePath> def = + var def = compiler.definition(URI.create("/GotoDefinition.java"), 3, 12, uri -> Files.readAllText(uri)); assertTrue(def.isPresent()); - TreePath t = def.get(); - CompilationUnitTree unit = t.getCompilationUnit(); + var t = def.get(); + var unit = t.getCompilationUnit(); assertThat(unit.getSourceFile().getName(), endsWith("GotoDefinition.java")); - Trees trees = compiler.trees(); - SourcePositions pos = trees.getSourcePositions(); - LineMap lines = unit.getLineMap(); + var trees = compiler.trees(); + var pos = trees.getSourcePositions(); + var lines = unit.getLineMap(); long start = pos.getStartPosition(unit, t.getLeaf()); long line = lines.getLineNumber(start); assertThat(line, equalTo(6L)); @@ -255,15 +246,14 @@ public class JavaCompilerServiceTest { @Test public void references() { - List<TreePath> refs = - compiler.references(URI.create("/GotoDefinition.java"), contents("/GotoDefinition.java"), 6, 13); + var refs = compiler.references(URI.create("/GotoDefinition.java"), contents("/GotoDefinition.java"), 6, 13); boolean found = false; - for (TreePath t : refs) { - CompilationUnitTree unit = t.getCompilationUnit(); - String name = unit.getSourceFile().getName(); - Trees trees = compiler.trees(); - SourcePositions pos = trees.getSourcePositions(); - LineMap lines = unit.getLineMap(); + for (var t : refs) { + var unit = t.getCompilationUnit(); + var name = unit.getSourceFile().getName(); + var trees = compiler.trees(); + var pos = trees.getSourcePositions(); + var lines = unit.getLineMap(); long start = pos.getStartPosition(unit, t.getLeaf()); long line = lines.getLineNumber(start); if (name.endsWith("GotoDefinition.java") && line == 3) found = true; @@ -274,8 +264,7 @@ public class JavaCompilerServiceTest { @Test public void overloads() { - MethodInvocation found = - compiler.methodInvocation(URI.create("/Overloads.java"), contents("/Overloads.java"), 3, 15).get(); + var found = compiler.methodInvocation(URI.create("/Overloads.java"), contents("/Overloads.java"), 3, 15).get(); assertThat(found.overloads, hasItem(hasToString("print(int)"))); assertThat(found.overloads, hasToString("print(java.lang.String)")); @@ -290,7 +279,7 @@ public class JavaCompilerServiceTest { @Test public void localDoc() { - ExecutableElement method = + var method = compiler.methodInvocation(URI.create("/LocalMethodDoc.java"), contents("/LocalMethodDoc.java"), 3, 21) .get() .activeMethod @@ -302,7 +291,7 @@ public class JavaCompilerServiceTest { @Test public void fixImports() { - Set<String> qualifiedNames = + var qualifiedNames = compiler.fixImports(resourceUri("/MissingImport.java"), contents("/MissingImport.java")).fixedImports; assertThat(qualifiedNames, hasItem("java.util.List")); } |