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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.junit.Ignore;
import org.junit.Test;
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 = FindResource.uri(file),
other = FindResource.uri("/org/javacs/example/GotoOther.java");
private static final String defaultConstructorFile = "/org/javacs/example/GotoDefaultConstructor.java";
private static final URI defaultConstructorUri = FindResource.uri(defaultConstructorFile);
@Test
public void localVariable() {
List<? extends Location> suggestions = doGoto(file, 9, 8);
assertThat(suggestions, contains(location(uri, 4, 8, 4, 21)));
}
@Test
public void defaultConstructor() {
List<? extends Location> suggestions = doGoto(defaultConstructorFile, 4, 45);
assertThat(suggestions, contains(location(defaultConstructorUri, 2, 0, 6, 1)));
}
@Test
public void constructor() {
List<? extends Location> suggestions = doGoto(file, 10, 20);
assertThat(suggestions, contains(location(uri, 2, 0, 47, 1)));
}
@Test
public void className() {
List<? extends Location> suggestions = doGoto(file, 15, 8);
assertThat(suggestions, contains(location(uri, 2, 0, 47, 1)));
}
@Test
public void staticField() {
List<? extends Location> suggestions = doGoto(file, 12, 21);
assertThat(suggestions, contains(location(uri, 35, 4, 35, 37)));
}
@Test
public void field() {
List<? extends Location> suggestions = doGoto(file, 13, 21);
assertThat(suggestions, contains(location(uri, 36, 4, 36, 24)));
}
@Test
public void staticMethod() {
List<? extends Location> suggestions = doGoto(file, 15, 13);
assertThat(suggestions, contains(location(uri, 37, 4, 39, 5)));
}
@Test
public void method() {
List<? extends Location> suggestions = doGoto(file, 16, 13);
assertThat(suggestions, contains(location(uri, 40, 4, 42, 5)));
}
@Test
public void staticMethodReference() {
List<? extends Location> suggestions = doGoto(file, 18, 26);
assertThat(suggestions, contains(location(uri, 37, 4, 39, 5)));
}
@Test
public void methodReference() {
List<? extends Location> suggestions = doGoto(file, 19, 26);
assertThat(suggestions, contains(location(uri, 40, 4, 42, 5)));
}
@Test
public void otherStaticMethod() {
List<? extends Location> suggestions = doGoto(file, 28, 24);
assertThat(suggestions, contains(hasProperty("uri", equalTo(other.toString()))));
}
@Test
public void otherMethod() {
List<? extends Location> suggestions = doGoto(file, 29, 17);
assertThat(suggestions, contains(hasProperty("uri", equalTo(other.toString()))));
}
@Test
public void otherCompiledFile() {
List<? extends Location> suggestions = doGoto(file, 28, 24);
assertThat(suggestions, contains(hasProperty("uri", equalTo(other.toString()))));
}
@Test
@Ignore // TODO
public void typeParam() {
List<? extends Location> suggestions = doGoto(file, 45, 11);
assertThat(suggestions, contains(location(uri, 2, 18, 2, 23)));
}
@Test
public void gotoEnum() {
String file = "/org/javacs/example/GotoEnum.java";
assertThat(doGoto(file, 5, 30), not(empty()));
assertThat(doGoto(file, 5, 35), not(empty()));
}
private Location location(URI uri, int startRow, int startColumn, int endRow, int endColumn) {
Position start = new Position();
start.setLine(startRow);
start.setCharacter(startColumn);
Position end = new Position();
end.setLine(endRow);
end.setCharacter(endColumn);
Range range = new Range();
range.setStart(start);
range.setEnd(end);
Location location = new Location();
location.setUri(uri.toString());
location.setRange(range);
return location;
}
private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
private List<? extends Location> doGoto(String file, int row, int column) {
TextDocumentIdentifier document = new TextDocumentIdentifier();
document.setUri(FindResource.uri(file).toString());
Position position = new Position();
position.setLine(row);
position.setCharacter(column);
TextDocumentPositionParams p = new TextDocumentPositionParams();
p.setTextDocument(document);
p.setPosition(position);
try {
return server.getTextDocumentService().definition(p).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}
|