diff options
author | George Fraser <george@fivetran.com> | 2018-06-26 10:49:17 -0700 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-06-26 10:49:17 -0700 |
commit | 9c0b5d6757804ef3b8dc43358ed43c90993a2183 (patch) | |
tree | d8ef606f76f6048aa0e6036b55cc520abe538415 /src/test | |
parent | f9136b43f39d3827a22a54709a2eb77e713289e9 (diff) | |
download | java-language-server-9c0b5d6757804ef3b8dc43358ed43c90993a2183.zip |
Delete old code
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/org/javacs/CodeActionsTest.java | 105 | ||||
-rw-r--r-- | src/test/java/org/javacs/CompilerProfiling.java | 43 | ||||
-rw-r--r-- | src/test/java/org/javacs/CompletionsTest.java | 10 | ||||
-rw-r--r-- | src/test/java/org/javacs/IncrementalFileManagerTest.java | 92 | ||||
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerTest.java | 84 | ||||
-rw-r--r-- | src/test/java/org/javacs/LinterTest.java | 112 | ||||
-rw-r--r-- | src/test/java/org/javacs/RecompileTest.java | 86 | ||||
-rw-r--r-- | src/test/java/org/javacs/RefactorFileTest.java | 211 | ||||
-rw-r--r-- | src/test/java/org/javacs/SymbolIndexTest.java | 20 | ||||
-rw-r--r-- | src/test/java/org/javacs/TreePrunerTest.java | 80 |
10 files changed, 0 insertions, 843 deletions
diff --git a/src/test/java/org/javacs/CodeActionsTest.java b/src/test/java/org/javacs/CodeActionsTest.java deleted file mode 100644 index b457d85..0000000 --- a/src/test/java/org/javacs/CodeActionsTest.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.junit.Assert.assertThat; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URI; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.eclipse.lsp4j.CodeActionContext; -import org.eclipse.lsp4j.CodeActionParams; -import org.eclipse.lsp4j.Command; -import org.eclipse.lsp4j.Diagnostic; -import org.eclipse.lsp4j.DidOpenTextDocumentParams; -import org.eclipse.lsp4j.DidSaveTextDocumentParams; -import org.eclipse.lsp4j.Range; -import org.eclipse.lsp4j.TextDocumentIdentifier; -import org.eclipse.lsp4j.TextDocumentItem; -import org.junit.Before; -import org.junit.Test; - -public class CodeActionsTest { - - private static List<Diagnostic> diagnostics = new ArrayList<>(); - - @Before - public void before() { - diagnostics.clear(); - } - - private static final JavaLanguageServer server = - LanguageServerFixture.getJavaLanguageServer(LanguageServerFixture.DEFAULT_WORKSPACE_ROOT, diagnostics::add); - - @Test - public void addImport() { - List<String> titles = - commands("/org/javacs/example/MissingImport.java", 5, 14) - .stream() - .map(c -> c.getTitle()) - .collect(Collectors.toList()); - - assertThat(titles, hasItem("Import java.util.ArrayList")); - } - - @Test - public void missingImport() { - String message = - "cannot find symbol\n" + " symbol: class ArrayList\n" + " location: class org.javacs.MissingImport"; - - assertThat(CodeActions.cannotFindSymbolClassName(message), equalTo(Optional.of("ArrayList"))); - } - - private List<? extends Command> commands(String file, int row, int column) { - URI uri = FindResource.uri(file); - TextDocumentIdentifier document = new TextDocumentIdentifier(uri.toString()); - - 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) { - boolean startCondition = - range.getStart().getLine() < line - || (range.getStart().getLine() == line && range.getStart().getCharacter() <= character); - boolean endCondition = - line < range.getEnd().getLine() - || (line == range.getEnd().getLine() && character <= range.getEnd().getCharacter()); - - return startCondition && endCondition; - } - - private Stream<? extends Command> codeActionsAt(TextDocumentIdentifier documentId, Diagnostic diagnostic) { - CodeActionParams params = - new CodeActionParams(documentId, diagnostic.getRange(), new CodeActionContext(diagnostics)); - - return server.getTextDocumentService().codeAction(params).join().stream(); - } -} diff --git a/src/test/java/org/javacs/CompilerProfiling.java b/src/test/java/org/javacs/CompilerProfiling.java deleted file mode 100644 index 01308ea..0000000 --- a/src/test/java/org/javacs/CompilerProfiling.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.javacs; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Paths; -import java.time.Duration; -import java.util.Collections; -import java.util.Optional; -import java.util.logging.Logger; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaFileObject; -import org.junit.Ignore; -import org.junit.Test; - -@Ignore -public class CompilerProfiling { - private static final Logger LOG = Logger.getLogger("main"); - - @Test - public void parsingSpeed() throws IOException, URISyntaxException { - URI file = FindResource.uri("/org/javacs/example/LargeFile.java"); - - for (int i = 0; i < 10; i++) { - Duration duration = compileLargeFile(file); - - LOG.info(duration.toString()); - } - } - - private Duration compileLargeFile(URI file) { - long start = System.nanoTime(); - JavacHolder compiler = - JavacHolder.create( - Collections.singleton(Paths.get("src/test/test-project/workspace/src")), - Collections.emptySet()); - DiagnosticCollector<JavaFileObject> result = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - long finish = System.nanoTime(); - - return Duration.ofNanos(finish - start); - } -} diff --git a/src/test/java/org/javacs/CompletionsTest.java b/src/test/java/org/javacs/CompletionsTest.java index 12181fd..50b14f6 100644 --- a/src/test/java/org/javacs/CompletionsTest.java +++ b/src/test/java/org/javacs/CompletionsTest.java @@ -691,16 +691,6 @@ public class CompletionsTest extends CompletionsBase { } @Test - public void containsCharactersInOrder() { - assertTrue(Completions.containsCharactersInOrder("FooBar", "FooBar", false)); - assertTrue(Completions.containsCharactersInOrder("FooBar", "foobar", false)); - assertTrue(Completions.containsCharactersInOrder("FooBar", "FB", false)); - assertTrue(Completions.containsCharactersInOrder("FooBar", "fb", false)); - assertFalse(Completions.containsCharactersInOrder("FooBar", "FooBar1", false)); - assertFalse(Completions.containsCharactersInOrder("FooBar", "FB1", false)); - } - - @Test public void withinConstructor() throws IOException { String file = "/org/javacs/example/AutocompleteContext.java"; diff --git a/src/test/java/org/javacs/IncrementalFileManagerTest.java b/src/test/java/org/javacs/IncrementalFileManagerTest.java deleted file mode 100644 index f48e847..0000000 --- a/src/test/java/org/javacs/IncrementalFileManagerTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; - -import com.google.common.collect.ImmutableList; -import com.sun.tools.javac.api.JavacTool; -import com.sun.tools.javac.file.JavacFileManager; -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Paths; -import javax.tools.StandardLocation; -import org.javacs.pubapi.PubApi; -import org.junit.Before; -import org.junit.Test; - -public class IncrementalFileManagerTest { - private JavacFileManager delegate = - JavacTool.create().getStandardFileManager(__ -> {}, null, Charset.defaultCharset()); - private IncrementalFileManager test = new IncrementalFileManager(delegate); - private File sourcePath = Paths.get("./src/test/test-project/workspace/src").toFile(); - private File classPath = Paths.get("./src/test/test-project/workspace/target/classes").toFile(); - - @Before - public void setPaths() throws IOException { - delegate.setLocation(StandardLocation.SOURCE_PATH, ImmutableList.of(sourcePath)); - delegate.setLocation(StandardLocation.CLASS_PATH, ImmutableList.of(classPath)); - } - - @Test - public void sourceFileSignature() { - PubApi sig = test.sourceSignature("com.example.Signatures").get(); - - assertThat( - sig.types.get("com.example.Signatures").pubApi.methods.keySet(), - hasItems("void voidMethod()", "java.lang.String stringMethod()")); - assertThat( - sig.types.get("com.example.Signatures").pubApi.methods.keySet(), not(hasItems("void privateMethod()"))); - assertThat( - sig.types.get("com.example.Signatures").pubApi.types.keySet(), - hasItems("com.example.Signatures$RegularInnerClass", "com.example.Signatures$StaticInnerClass")); - } - - @Test - public void classFileSignature() { - PubApi sig = test.classSignature("com.example.Signatures").get(); - - assertThat( - sig.types.get("com.example.Signatures").pubApi.methods.keySet(), - hasItems("void voidMethod()", "java.lang.String stringMethod()")); - assertThat( - sig.types.get("com.example.Signatures").pubApi.methods.keySet(), not(hasItems("void privateMethod()"))); - assertThat( - sig.types.get("com.example.Signatures").pubApi.types.keySet(), - hasItems("com.example.Signatures$RegularInnerClass", "com.example.Signatures$StaticInnerClass")); - } - - @Test - public void simpleSignatureEquals() { - PubApi classSig = test.classSignature("com.example.Signatures").get(), - sourceSig = test.sourceSignature("com.example.Signatures").get(); - - assertThat(classSig, equalTo(sourceSig)); - } - - @Test - public void packagePrivateSourceSignature() { - PubApi sig = test.sourceSignature("com.example.PackagePrivate").get(); - - assertThat( - sig.types.get("com.example.PackagePrivate").pubApi.methods.keySet(), - hasItem("void packagePrivateMethod()")); - } - - @Test - public void packagePrivateClassSignature() { - PubApi sig = test.classSignature("com.example.PackagePrivate").get(); - - assertThat( - sig.types.get("com.example.PackagePrivate").pubApi.methods.keySet(), - hasItem("void packagePrivateMethod()")); - } - - @Test - public void packagePrivateEquals() { - PubApi classSig = test.classSignature("com.example.PackagePrivate").get(), - sourceSig = test.sourceSignature("com.example.PackagePrivate").get(); - - assertThat(classSig, equalTo(sourceSig)); - } -} diff --git a/src/test/java/org/javacs/JavaCompilerTest.java b/src/test/java/org/javacs/JavaCompilerTest.java deleted file mode 100644 index d5f8b44..0000000 --- a/src/test/java/org/javacs/JavaCompilerTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.javacs; - -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 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; -import javax.lang.model.element.Element; -import javax.tools.Diagnostic; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; -import org.junit.Test; - -public class JavaCompilerTest { - private static final Logger LOG = Logger.getLogger("main"); - - @Test - public void javacTool() throws IOException { - JavaCompiler javaCompiler = JavacTool.create(); - StandardJavaFileManager fileManager = - javaCompiler.getStandardFileManager(this::reportError, null, Charset.defaultCharset()); - List<String> options = - ImmutableList.of("-sourcepath", Paths.get("src/test/resources").toAbsolutePath().toString()); - List<String> classes = Collections.emptyList(); - File file = Paths.get("src/test/resources/org/javacs/example/Bad.java").toFile(); - Iterable<? extends JavaFileObject> compilationUnits = - fileManager.getJavaFileObjectsFromFiles(Collections.singleton(file)); - JavacTask task = - (JavacTask) - javaCompiler.getTask(null, fileManager, this::reportError, options, classes, compilationUnits); - - Iterable<? extends CompilationUnitTree> parsed = task.parse(); - Iterable<? extends Element> typed = task.analyze(); - - LOG.info(typed.toString()); - } - - @Test - public void javacHolder() { - JavacHolder javac = - JavacHolder.create( - Collections.singleton(Paths.get("src/test/test-project/workspace/src")), - Collections.emptySet()); - File file = Paths.get("src/test/test-project/workspace/src/org/javacs/example/Bad.java").toFile(); - DiagnosticCollector<JavaFileObject> compile = - javac.compileBatch(Collections.singletonMap(file.toURI(), Optional.empty())); - } - - /* It's too hard to keep this test working - @Test - public void incremental() { - JavacHolder javac = JavacHolder.create(Collections.emptySet(), Collections.singleton(Paths.get("src/test/resources")), Paths.get("target/test-output")); - - // Compile Target to a .class file - File target = Paths.get("src/test/resources/org/javacs/example/Target.java").toFile(); - DiagnosticCollector<JavaFileObject> batch = javac.compileBatch(Collections.singletonMap(target.toURI(), Optional.empty())); - - // Incremental compilation should use Target.class, not Target.java - File dependsOnTarget = Paths.get("src/test/resources/org/javacs/example/DependsOnTarget.java").toFile(); - int[] count = {0}; - FocusedResult incremental = javac.compileFocused(dependsOnTarget.toURI(), Optional.empty(), 5, 27, true); - - assertThat(counts.get(TaskEvent.Kind.PARSE), equalTo(1)); - - // Check that we can find org.javacs.example.Target - TypeElement targetClass = incremental.task.getElements().getTypeElement("org.javacs.example.Target"); - - assertThat(targetClass, not(nullValue())); - } - */ - - private void reportError(Diagnostic<? extends JavaFileObject> error) { - LOG.severe(error.getMessage(null)); - } -} diff --git a/src/test/java/org/javacs/LinterTest.java b/src/test/java/org/javacs/LinterTest.java deleted file mode 100644 index 4ac7108..0000000 --- a/src/test/java/org/javacs/LinterTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; - -import java.net.URI; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.Optional; -import java.util.logging.Logger; -import javax.tools.Diagnostic; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaFileObject; -import org.junit.Test; - -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()); - - @Test - public void compile() { - URI file = FindResource.uri("/org/javacs/example/HelloWorld.java"); - DiagnosticCollector<JavaFileObject> errors = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(errors.getDiagnostics(), empty()); - } - - @Test - public void missingMethodBody() { - URI file = FindResource.uri("/org/javacs/example/MissingMethodBody.java"); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), not(empty())); - } - - @Test - public void incompleteAssignment() { - URI file = FindResource.uri("/org/javacs/example/IncompleteAssignment.java"); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), not(empty())); - } - - @Test - public void undefinedSymbol() { - URI file = FindResource.uri("/org/javacs/example/UndefinedSymbol.java"); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), not(empty())); - - Diagnostic<? extends JavaFileObject> d = compile.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)); - } - - @Test(expected = IllegalArgumentException.class) - public void notJava() { - URI file = FindResource.uri("/org/javacs/example/NotJava.java.txt"); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - } - - @Test - public void errorInDependency() { - URI file = FindResource.uri("/org/javacs/example/ErrorInDependency.java"); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), not(empty())); - } - - @Test - public void deprecationWarning() { - URI file = FindResource.uri("/org/javacs/example/DeprecationWarning.java"); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), not(empty())); - } - - @Test - public void parseError() { - URI file = URI.create("/org/javacs/example/ArrowTry.java"); - String source = - "package org.javacs.example;\n" - + "\n" - + "class Example {\n" - + " private static <In, Out> Function<In, Stream<Out>> catchClasspathErrors(Function<In, Stream<Out>> f) {\n" - + " return in -> try {\n" - + " return f.apply(in);\n" - + " } catch (Symbol.CompletionFailure failed) {\n" - + " LOG.warning(failed.getMessage());\n" - + " return Stream.empty();\n" - + " };\n" - + " }\n" - + "}"; - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.of(source))); - - assertThat(compile.getDiagnostics(), not(empty())); - } -} diff --git a/src/test/java/org/javacs/RecompileTest.java b/src/test/java/org/javacs/RecompileTest.java deleted file mode 100644 index cc16a1f..0000000 --- a/src/test/java/org/javacs/RecompileTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertThat; - -import java.io.IOException; -import java.net.URI; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.Optional; -import javax.tools.DiagnosticCollector; -import javax.tools.JavaFileObject; -import org.junit.Test; - -public class RecompileTest { - @Test - public void compileTwice() { - URI file = FindResource.uri("/org/javacs/example/CompileTwice.java"); - JavacHolder compiler = newCompiler(); - DiagnosticCollector<JavaFileObject> compile = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), empty()); - - // Compile again - compile = compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(compile.getDiagnostics(), empty()); - } - - @Test - public void fixParseError() { - URI bad = FindResource.uri("/org/javacs/example/FixParseErrorBefore.java"); - URI good = FindResource.uri("/org/javacs/example/FixParseErrorAfter.java"); - JavacHolder compiler = newCompiler(); - DiagnosticCollector<JavaFileObject> badErrors = - compiler.compileBatch(Collections.singletonMap(bad, Optional.empty())); - - assertThat(badErrors.getDiagnostics(), not(empty())); - - // Parse again - DiagnosticCollector<JavaFileObject> goodErrors = - compiler.compileBatch(Collections.singletonMap(good, Optional.empty())); - - assertThat(goodErrors.getDiagnostics(), empty()); - } - - @Test - public void fixTypeError() { - URI bad = FindResource.uri("/org/javacs/example/FixTypeErrorBefore.java"); - URI good = FindResource.uri("/org/javacs/example/FixTypeErrorAfter.java"); - JavacHolder compiler = newCompiler(); - DiagnosticCollector<JavaFileObject> badErrors = - compiler.compileBatch(Collections.singletonMap(bad, Optional.empty())); - - assertThat(badErrors.getDiagnostics(), not(empty())); - - // Parse again - DiagnosticCollector<JavaFileObject> goodErrors = - compiler.compileBatch(Collections.singletonMap(good, Optional.empty())); - - assertThat(goodErrors.getDiagnostics(), empty()); - } - - private static JavacHolder newCompiler() { - return JavacHolder.create( - Collections.singleton(Paths.get("src/test/test-project/workspace/src")), Collections.emptySet()); - } - - @Test - public void keepTypeError() throws IOException { - URI file = FindResource.uri("/org/javacs/example/UndefinedSymbol.java"); - JavacHolder compiler = newCompiler(); - - // Compile once - DiagnosticCollector<JavaFileObject> errors = - compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - assertThat(errors.getDiagnostics(), not(empty())); - - // Compile twice - errors = compiler.compileBatch(Collections.singletonMap(file, Optional.empty())); - - assertThat(errors.getDiagnostics(), not(empty())); - } -} diff --git a/src/test/java/org/javacs/RefactorFileTest.java b/src/test/java/org/javacs/RefactorFileTest.java deleted file mode 100644 index 65c4c70..0000000 --- a/src/test/java/org/javacs/RefactorFileTest.java +++ /dev/null @@ -1,211 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.util.JavacTask; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; -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 java.util.function.BiConsumer; -import java.util.logging.Logger; -import org.eclipse.lsp4j.Position; -import org.eclipse.lsp4j.TextEdit; -import org.junit.Test; - -public class RefactorFileTest { - - private static final Logger LOG = Logger.getLogger("main"); - private static final URI FAKE_FILE = URI.create("test/imaginary-resources/org/javacs/Example.java"); - - @Test - public void addImportToEmpty() { - String before = "package org.javacs;\n" + "\n" + "public class Example { void main() { } }"; - List<TextEdit> edits = addImport(before, "org.javacs", "Foo"); - String after = applyEdits(before, edits); - - assertThat( - after, - equalTo( - "package org.javacs;\n" - + "\n" - + "import org.javacs.Foo;\n" - + "\n" - + "public class Example { void main() { } }")); - } - - @Test - public void addImportToExisting() { - String before = - "package org.javacs;\n" - + "\n" - + "import java.util.List;\n" - + "\n" - + "public class Example { void main() { } }"; - List<TextEdit> edits = addImport(before, "org.javacs", "Foo"); - String after = applyEdits(before, edits); - - assertThat( - after, - equalTo( - "package org.javacs;\n" - + "\n" - + "import java.util.List;\n" - + "import org.javacs.Foo;\n" - + "\n" - + "public class Example { void main() { } }")); - } - - @Test - public void addImportAtBeginning() { - String before = - "package org.javacs;\n" - + "\n" - + "import org.javacs.Foo;\n" - + "\n" - + "public class Example { void main() { } }"; - List<TextEdit> edits = addImport(before, "java.util", "List"); - String after = applyEdits(before, edits); - - assertThat( - after, - equalTo( - "package org.javacs;\n" - + "\n" - + "import java.util.List;\n" - + "import org.javacs.Foo;\n" - + "\n" - + "public class Example { void main() { } }")); - } - - @Test - public void importAlreadyExists() { - String before = - "package org.javacs;\n" - + "\n" - + "import java.util.List;\n" - + "\n" - + "public class Example { void main() { } }"; - List<TextEdit> edits = addImport(before, "java.util", "List"); - String after = applyEdits(before, edits); - - assertThat( - after, - equalTo( - "package org.javacs;\n" - + "\n" - + "import java.util.List;\n" - + "\n" - + "public class Example { void main() { } }")); - } - - @Test - public void noPackage() { - String before = "import java.util.List;\n" + "\n" + "public class Example { void main() { } }"; - List<TextEdit> edits = addImport(before, "org.javacs", "Foo"); - String after = applyEdits(before, edits); - - assertThat( - after, - equalTo( - "import java.util.List;\n" - + "import org.javacs.Foo;\n" - + "\n" - + "public class Example { void main() { } }")); - } - - @Test - public void noPackageNoImports() { - String before = "public class Example { void main() { } }"; - List<TextEdit> edits = addImport(before, "org.javacs", "Foo"); - String after = applyEdits(before, edits); - - assertThat(after, equalTo("import org.javacs.Foo;\n" + "\n" + "public class Example { void main() { } }")); - } - - private List<TextEdit> addImport(String content, String packageName, String className) { - List<TextEdit> result = new ArrayList<>(); - JavacHolder compiler = - JavacHolder.create( - Collections.singleton(Paths.get("src/test/test-project/workspace/src")), - Collections.emptySet()); - BiConsumer<JavacTask, CompilationUnitTree> doRefactor = - (task, tree) -> { - List<TextEdit> edits = new RefactorFile(task, tree).addImport(packageName, className); - - result.addAll(edits); - }; - - compiler.compileBatch(Collections.singletonMap(FAKE_FILE, Optional.of(content)), doRefactor); - - return result; - } - - private String applyEdits(String before, List<TextEdit> edits) { - StringBuffer buffer = new StringBuffer(before); - - edits.stream().sorted(this::compareEdits).forEach(edit -> applyEdit(buffer, edit)); - - return buffer.toString(); - } - - private int compareEdits(TextEdit left, TextEdit right) { - int compareLines = -Integer.compare(left.getRange().getEnd().getLine(), right.getRange().getEnd().getLine()); - - if (compareLines != 0) return compareLines; - else - return -Integer.compare( - left.getRange().getStart().getCharacter(), right.getRange().getEnd().getCharacter()); - } - - private void applyEdit(StringBuffer buffer, TextEdit edit) { - buffer.replace( - offset(edit.getRange().getStart(), buffer), - offset(edit.getRange().getEnd(), buffer), - edit.getNewText()); - } - - private int offset(Position pos, StringBuffer buffer) { - return (int) findOffset(buffer.toString(), pos.getLine(), pos.getCharacter()); - } - - public static long findOffset(String content, int targetLine, int targetCharacter) { - try (Reader in = new StringReader(content)) { - long offset = 0; - int line = 0; - int character = 0; - - while (line < targetLine) { - int next = in.read(); - - if (next < 0) return offset; - else { - offset++; - - if (next == '\n') line++; - } - } - - while (character < targetCharacter) { - int next = in.read(); - - if (next < 0) return offset; - else { - offset++; - character++; - } - } - - return offset; - } catch (IOException e) { - throw ShowMessageException.error(e.getMessage(), e); - } - } -} diff --git a/src/test/java/org/javacs/SymbolIndexTest.java b/src/test/java/org/javacs/SymbolIndexTest.java deleted file mode 100644 index 679daab..0000000 --- a/src/test/java/org/javacs/SymbolIndexTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.Optional; -import org.junit.Test; - -public class SymbolIndexTest { - private Path workspaceRoot = Paths.get("src/test/test-project/workspace"); - private SymbolIndex index = new SymbolIndex(workspaceRoot, () -> Collections.emptySet(), __ -> Optional.empty()); - - @Test - public void workspaceSourcePath() { - assertThat(index.sourcePath(), contains(workspaceRoot.resolve("src").toAbsolutePath())); - } -} diff --git a/src/test/java/org/javacs/TreePrunerTest.java b/src/test/java/org/javacs/TreePrunerTest.java deleted file mode 100644 index 674a6de..0000000 --- a/src/test/java/org/javacs/TreePrunerTest.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.javacs; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace; -import static org.junit.Assert.assertThat; - -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.util.JavacTask; -import com.sun.tools.javac.api.JavacTool; -import java.io.IOException; -import java.net.URI; -import java.util.Collections; -import java.util.logging.Logger; -import javax.tools.JavaFileObject; -import org.junit.Test; - -public class TreePrunerTest { - private static final Logger LOG = Logger.getLogger("main"); - - private JavacTask task(String source) { - return JavacTool.create() - .getTask( - null, - null, - err -> LOG.warning(err.getMessage(null)), - Collections.emptyList(), - Collections.emptyList(), - Collections.singleton(new StringFileObject(source, URI.create("FakePath.java")))); - } - - @Test - public void putSemicolonAfterCursor() throws IOException { - String source = "public class Example {\n" + " void main() {\n" + " this.m\n" + " }\n" + "}\n"; - JavaFileObject after = - TreePruner.putSemicolonAfterCursor(new StringFileObject(source, URI.create("Example.java")), 3, 11); - - assertThat( - after.getCharContent(true).toString(), - equalTo("public class Example {\n" + " void main() {\n" + " this.m;\n" + " }\n" + "}\n")); - } - - @Test - public void putSemicolonAtEndOfLine() throws IOException { - String source = "public class Example {\n" + " void main() {\n" + " this.main\n" + " }\n" + "}\n"; - JavaFileObject after = - TreePruner.putSemicolonAfterCursor(new StringFileObject(source, URI.create("Example.java")), 3, 11); - - assertThat( - after.getCharContent(true).toString(), - equalTo("public class Example {\n" + " void main() {\n" + " this.main;\n" + " }\n" + "}\n")); - } - - @Test - public void removeStatementsAfterCursor() throws IOException { - String source = - "public class Example {\n" - + " void main() {\n" - + " foo()\n" - + " bar()\n" - + " doh()\n" - + " }\n" - + "}\n"; - JavacTask task = task(source); - CompilationUnitTree tree = task.parse().iterator().next(); - - new TreePruner(task).removeStatementsAfterCursor(tree, 4, 6); - - assertThat( - tree.toString(), - equalToIgnoringWhiteSpace( - "\n" - + "public class Example {\n" - + " \n" - + " void main() {\n" - + " foo();\n" - + " bar();\n" - + " }\n" - + "}")); - } -} |