summaryrefslogtreecommitdiff
path: root/src/test/java/org/javacs/SearchTest.java
blob: b92b0e164eb8801c8a5ea10e2d5057ad9463b919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.javacs;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import com.google.common.base.Joiner;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.*;
import org.junit.BeforeClass;
import org.junit.Test;

public class SearchTest {
    private static final Logger LOG = Logger.getLogger("main");

    private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();

    @BeforeClass
    public static void openSource() throws URISyntaxException, IOException {
        URI uri = FindResource.uri("/org/javacs/example/AutocompleteBetweenLines.java");
        String textContent = Joiner.on("\n").join(Files.readAllLines(Paths.get(uri)));
        TextDocumentItem document = new TextDocumentItem();

        document.setUri(uri.toString());
        document.setText(textContent);

        server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(document, null));
    }

    private static Set<String> searchWorkspace(String query) {
        try {
            return server.getWorkspaceService()
                    .symbol(new WorkspaceSymbolParams(query))
                    .get()
                    .stream()
                    .map(result -> result.getName())
                    .collect(Collectors.toSet());
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private static Set<String> searchFile(URI uri) {
        try {
            return server.getTextDocumentService()
                    .documentSymbol(
                            new DocumentSymbolParams(new TextDocumentIdentifier(uri.toString())))
                    .get()
                    .stream()
                    .map(result -> result.getName())
                    .collect(Collectors.toSet());
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void all() {
        Set<String> all = searchWorkspace("");

        assertThat(all, not(empty()));
    }

    @Test
    public void searchClasses() {
        Set<String> all = searchWorkspace("ABetweenLines");

        assertThat(all, hasItem("AutocompleteBetweenLines"));
    }

    @Test
    public void searchMethods() {
        Set<String> all = searchWorkspace("mStatic");

        assertThat(all, hasItem("methodStatic"));
    }

    @Test
    public void symbolsInFile() {
        String path = "/org/javacs/example/AutocompleteMemberFixed.java";
        Set<String> all = searchFile(FindResource.uri(path));

        assertThat(
                all,
                hasItems(
                        "methodStatic", "method",
                        "methodStaticPrivate", "methodPrivate"));

        assertThat(
                all,
                hasItems(
                        "fieldStatic", "field",
                        "fieldStaticPrivate", "fieldPrivate"));
    }

    @Test
    public void explicitConstructor() {
        String path = "/org/javacs/example/ReferenceConstructor.java";
        Set<String> all = searchFile(FindResource.uri(path));

        assertThat("includes explicit constructor", all, hasItem("ReferenceConstructor"));
    }
}