Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/langtools/jdk/jshell/CommandCompletionTest.java
64440 views
1
/*
2
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8144095 8164825 8169818 8153402 8165405 8177079 8178013 8167554 8166232 8277328
27
* @summary Test Command Completion
28
* @modules jdk.compiler/com.sun.tools.javac.api
29
* jdk.compiler/com.sun.tools.javac.main
30
* jdk.jdeps/com.sun.tools.javap
31
* jdk.jshell/jdk.internal.jshell.tool
32
* @library /tools/lib
33
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
34
* @build ReplToolTesting TestingInputStream Compiler
35
* @run testng CommandCompletionTest
36
*/
37
38
import java.io.IOException;
39
import java.nio.file.FileSystems;
40
import java.nio.file.Files;
41
import java.nio.file.Path;
42
import java.nio.file.Paths;
43
import java.util.Arrays;
44
import java.util.Collections;
45
import java.util.List;
46
import java.util.Locale;
47
import java.util.function.Predicate;
48
import java.util.stream.Collectors;
49
import java.util.stream.Stream;
50
import java.util.stream.StreamSupport;
51
52
import org.testng.SkipException;
53
import org.testng.annotations.Test;
54
55
import jdk.internal.jshell.tool.JShellTool;
56
import jdk.internal.jshell.tool.JShellToolBuilder;
57
import jdk.jshell.SourceCodeAnalysis.Suggestion;
58
59
import static org.testng.Assert.assertEquals;
60
import static org.testng.Assert.assertTrue;
61
import static org.testng.Assert.fail;
62
63
public class CommandCompletionTest extends ReplToolTesting {
64
65
66
private JShellTool repl;
67
68
@Override
69
protected void testRawRun(Locale locale, String[] args) {
70
repl = ((JShellToolBuilder) builder(locale))
71
.rawTool();
72
try {
73
repl.start(args);
74
} catch (Exception ex) {
75
fail("Repl tool died with exception", ex);
76
}
77
}
78
79
public void assertCompletion(boolean after, String code, boolean isSmart, String... expected) {
80
if (!after) {
81
setCommandInput("\n");
82
} else {
83
assertCompletion(code, isSmart, expected);
84
}
85
}
86
87
public void assertCompletion(String code, boolean isSmart, String... expected) {
88
List<String> completions = computeCompletions(code, isSmart);
89
assertEquals(completions, Arrays.asList(expected), "Command: " + code + ", output: " +
90
completions.toString());
91
}
92
93
private List<String> computeCompletions(String code, boolean isSmart) {
94
int cursor = code.indexOf('|');
95
code = code.replace("|", "");
96
assertTrue(cursor > -1, "'|' not found: " + code);
97
List<Suggestion> completions =
98
repl.commandCompletionSuggestions(code, cursor, new int[] {-1}); //XXX: ignoring anchor for now
99
return completions.stream()
100
.filter(s -> isSmart == s.matchesType())
101
.map(s -> s.continuation())
102
.distinct()
103
.collect(Collectors.toList());
104
}
105
106
@Test
107
public void testCommand() {
108
testNoStartUp(
109
a -> assertCompletion(a, "/deb|", false),
110
a -> assertCompletion(a, "/re|", false, "/reload ", "/reset "),
111
a -> assertCompletion(a, "/h|", false, "/help ", "/history ")
112
);
113
}
114
115
@Test
116
public void testList() {
117
test(false, new String[] {"--no-startup"},
118
a -> assertCompletion(a, "/l|", false, "/list "),
119
a -> assertCompletion(a, "/list |", false, "-all", "-history", "-start "),
120
a -> assertCompletion(a, "/list -h|", false, "-history"),
121
a -> assertCompletion(a, "/list q|", false),
122
a -> assertVariable(a, "int", "xray"),
123
a -> assertCompletion(a, "/list |", false, "-all", "-history", "-start ", "1 ", "xray "),
124
a -> assertCompletion(a, "/list x|", false, "xray "),
125
a -> assertCompletion(a, "/list xray |", false)
126
);
127
}
128
129
@Test
130
public void testHistory() {
131
test(false, new String[] {"--no-startup"},
132
a -> assertCompletion(a, "/hi|", false, "/history "),
133
a -> assertCompletion(a, "/history |", false, "-all")
134
);
135
}
136
137
@Test
138
public void testDrop() {
139
test(false, new String[] {"--no-startup"},
140
a -> assertCompletion(a, "/d|", false, "/drop "),
141
a -> assertClass(a, "class cTest {}", "class", "cTest"),
142
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
143
a -> assertVariable(a, "int", "fTest"),
144
a -> assertCompletion(a, "/drop |", false, "1 ", "2 ", "3 ", "cTest ", "fTest ", "mTest "),
145
a -> assertCompletion(a, "/drop f|", false, "fTest ")
146
);
147
}
148
149
@Test
150
public void testEdit() {
151
test(false, new String[]{"--no-startup"},
152
a -> assertCompletion(a, "/e|", false, "/edit ", "/env ", "/exit "),
153
a -> assertCompletion(a, "/ed|", false, "/edit "),
154
a -> assertClass(a, "class cTest {}", "class", "cTest"),
155
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
156
a -> assertVariable(a, "int", "fTest"),
157
a -> assertCompletion(a, "/edit |", false,
158
"-all" , "-start " , "1 ", "2 ", "3 ", "cTest ", "fTest ", "mTest "),
159
a -> assertCompletion(a, "/edit cTest |", false,
160
"2 ", "3 ", "fTest ", "mTest "),
161
a -> assertCompletion(a, "/edit 1 fTest |", false,
162
"2 ", "mTest "),
163
a -> assertCompletion(a, "/edit f|", false, "fTest "),
164
a -> assertCompletion(a, "/edit mTest f|", false, "fTest ")
165
);
166
}
167
168
@Test
169
public void testHelp() {
170
testNoStartUp(
171
a -> assertCompletion(a, "/help |", false,
172
"/! ", "/-<n> ", "/<id> ", "/? ", "/drop ",
173
"/edit ", "/env ", "/exit ",
174
"/help ", "/history ", "/imports ",
175
"/list ", "/methods ", "/open ", "/reload ", "/reset ",
176
"/save ", "/set ", "/types ", "/vars ", "context ",
177
"id ", "intro ", "keys ", "rerun ", "shortcuts "),
178
a -> assertCompletion(a, "/? |", false,
179
"/! ", "/-<n> ", "/<id> ", "/? ", "/drop ",
180
"/edit ", "/env ", "/exit ",
181
"/help ", "/history ", "/imports ",
182
"/list ", "/methods ", "/open ", "/reload ", "/reset ",
183
"/save ", "/set ", "/types ", "/vars ", "context ",
184
"id ", "intro ", "keys ", "rerun ", "shortcuts "),
185
a -> assertCompletion(a, "/help /s|", false,
186
"/save ", "/set "),
187
a -> assertCompletion(a, "/help /set |", false,
188
"editor", "feedback", "format", "indent", "mode", "prompt", "start", "truncation"),
189
a -> assertCompletion(a, "/help set |", false,
190
"editor", "feedback", "format", "indent", "mode", "prompt", "start", "truncation"),
191
a -> assertCompletion(a, "/help /edit |", false),
192
a -> assertCompletion(a, "/help dr|", false,
193
"drop ")
194
);
195
}
196
197
@Test
198
public void testReload() {
199
String[] ropts = new String[] { "-add-exports ", "-add-modules ",
200
"-class-path ", "-module-path ", "-quiet ", "-restore " };
201
String[] dropts = new String[] { "--add-exports ", "--add-modules ",
202
"--class-path ", "--module-path ", "--quiet ", "--restore " };
203
testNoStartUp(
204
a -> assertCompletion(a, "/reloa |", false, ropts),
205
a -> assertCompletion(a, "/relo |", false, ropts),
206
a -> assertCompletion(a, "/reload -|", false, ropts),
207
a -> assertCompletion(a, "/reload --|", false, dropts),
208
a -> assertCompletion(a, "/reload -restore |", false, ropts),
209
a -> assertCompletion(a, "/reload -restore --|", false, dropts),
210
a -> assertCompletion(a, "/reload -rest|", false, "-restore "),
211
a -> assertCompletion(a, "/reload --r|", false, "--restore "),
212
a -> assertCompletion(a, "/reload -q|", false, "-quiet "),
213
a -> assertCompletion(a, "/reload -add|", false, "-add-exports ", "-add-modules "),
214
a -> assertCompletion(a, "/reload -class-path . -quiet |", false, ropts)
215
);
216
}
217
218
@Test
219
public void testEnv() {
220
String[] ropts = new String[] { "-add-exports ", "-add-modules ",
221
"-class-path ", "-module-path " };
222
String[] dropts = new String[] { "--add-exports ", "--add-modules ",
223
"--class-path ", "--module-path " };
224
testNoStartUp(
225
a -> assertCompletion(a, "/env |", false, ropts),
226
a -> assertCompletion(a, "/env -|", false, ropts),
227
a -> assertCompletion(a, "/env --|", false, dropts),
228
a -> assertCompletion(a, "/env --a|", false, "--add-exports ", "--add-modules "),
229
a -> assertCompletion(a, "/env -add-|", false, "-add-exports ", "-add-modules "),
230
a -> assertCompletion(a, "/env -class-path . |", false, ropts),
231
a -> assertCompletion(a, "/env -class-path . --|", false, dropts)
232
);
233
}
234
235
@Test
236
public void testReset() {
237
String[] ropts = new String[] { "-add-exports ", "-add-modules ",
238
"-class-path ", "-module-path " };
239
String[] dropts = new String[] { "--add-exports ", "--add-modules ",
240
"--class-path ", "--module-path " };
241
testNoStartUp(
242
a -> assertCompletion(a, "/reset |", false, ropts),
243
a -> assertCompletion(a, "/res -m|", false, "-module-path "),
244
a -> assertCompletion(a, "/res -module-|", false, "-module-path "),
245
a -> assertCompletion(a, "/res --m|", false, "--module-path "),
246
a -> assertCompletion(a, "/res --module-|", false, "--module-path "),
247
a -> assertCompletion(a, "/reset -add|", false, "-add-exports ", "-add-modules "),
248
a -> assertCompletion(a, "/rese -class-path . |", false, ropts),
249
a -> assertCompletion(a, "/rese -class-path . --|", false, dropts)
250
);
251
}
252
253
@Test
254
public void testVarsMethodsTypes() {
255
testNoStartUp(
256
a -> assertCompletion(a, "/v|", false, "/vars "),
257
a -> assertCompletion(a, "/m|", false, "/methods "),
258
a -> assertCompletion(a, "/t|", false, "/types "),
259
a -> assertClass(a, "class cTest {}", "class", "cTest"),
260
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
261
a -> assertVariable(a, "int", "fTest"),
262
a -> assertCompletion(a, "/vars |", false, "-all", "-start ", "3 ", "fTest "),
263
a -> assertCompletion(a, "/meth |", false, "-all", "-start ", "2 ", "mTest "),
264
a -> assertCompletion(a, "/typ |", false, "-all", "-start ", "1 ", "cTest "),
265
a -> assertCompletion(a, "/var f|", false, "fTest ")
266
);
267
}
268
269
@Test
270
public void testOpen() throws IOException {
271
Compiler compiler = new Compiler();
272
testNoStartUp(
273
a -> assertCompletion(a, "/o|", false, "/open ")
274
);
275
List<String> p1 = listFiles(Paths.get(""));
276
getRootDirectories().forEach(s -> p1.add(s.toString()));
277
Collections.sort(p1);
278
testNoStartUp(
279
a -> assertCompletion(a, "/open |", false, p1.toArray(new String[p1.size()]))
280
);
281
Path classDir = compiler.getClassDir();
282
List<String> p2 = listFiles(classDir);
283
testNoStartUp(
284
a -> assertCompletion(a, "/open " + classDir + "/|", false, p2.toArray(new String[p2.size()]))
285
);
286
}
287
288
@Test
289
public void testSave() throws IOException {
290
Compiler compiler = new Compiler();
291
testNoStartUp(
292
a -> assertCompletion(a, "/s|", false, "/save ", "/set ")
293
);
294
List<String> p1 = listFiles(Paths.get(""));
295
Collections.addAll(p1, "-all ", "-history ", "-start ");
296
getRootDirectories().forEach(s -> p1.add(s.toString()));
297
Collections.sort(p1);
298
testNoStartUp(
299
a -> assertCompletion(a, "/save |", false, p1.toArray(new String[p1.size()]))
300
);
301
Path classDir = compiler.getClassDir();
302
List<String> p2 = listFiles(classDir);
303
testNoStartUp(
304
a -> assertCompletion(a, "/save " + classDir + "/|",
305
false, p2.toArray(new String[p2.size()])),
306
a -> assertCompletion(a, "/save -all " + classDir + "/|",
307
false, p2.toArray(new String[p2.size()]))
308
);
309
}
310
311
@Test
312
public void testClassPath() throws IOException {
313
Compiler compiler = new Compiler();
314
Path outDir = compiler.getPath("testClasspathCompletion");
315
Files.createDirectories(outDir);
316
Files.createDirectories(outDir.resolve("dir"));
317
createIfNeeded(outDir.resolve("test.jar"));
318
createIfNeeded(outDir.resolve("test.zip"));
319
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
320
String jarName = "test.jar";
321
compiler.jar(outDir, jarName, "pkg/A.class");
322
compiler.getPath(outDir).resolve(jarName);
323
List<String> paths = listFiles(outDir, CLASSPATH_FILTER);
324
String[] pathArray = paths.toArray(new String[paths.size()]);
325
testNoStartUp(
326
a -> assertCompletion(a, "/env -class-path " + outDir + "/|", false, pathArray),
327
a -> assertCompletion(a, "/env --class-path " + outDir + "/|", false, pathArray),
328
a -> assertCompletion(a, "/env -clas " + outDir + "/|", false, pathArray),
329
a -> assertCompletion(a, "/env --class-p " + outDir + "/|", false, pathArray),
330
a -> assertCompletion(a, "/env --module-path . --class-p " + outDir + "/|", false, pathArray)
331
);
332
}
333
334
@Test
335
public void testClassPathWithSpace() throws IOException {
336
Compiler compiler = new Compiler();
337
Path outDir = compiler.getPath("testClassPathWithSpace");
338
Path dirWithSpace = Files.createDirectories(outDir.resolve("dir with space"));
339
Files.createDirectories(dirWithSpace.resolve("nested with space"));
340
String[] pathArray = new String[] {"dir\\ with\\ space/"};
341
String[] pathArray2 = new String[] {"nested\\ with\\ space/"};
342
testNoStartUp(
343
a -> assertCompletion(a, "/env -class-path " + outDir + "/|", false, pathArray),
344
a -> assertCompletion(a, "/env -class-path " + outDir + "/dir|", false, pathArray),
345
a -> assertCompletion(a, "/env -class-path " + outDir + "/dir\\ with|", false, pathArray),
346
a -> assertCompletion(a, "/env -class-path " + outDir + "/dir\\ with\\ space/|", false, pathArray2)
347
);
348
}
349
350
@Test
351
public void testUserHome() throws IOException {
352
List<String> completions;
353
Path home = Paths.get(System.getProperty("user.home"));
354
String selectedFile;
355
try (Stream<Path> content = Files.list(home)) {
356
selectedFile = content.filter(CLASSPATH_FILTER)
357
.filter(file -> file.getFileName().toString().contains(" "))
358
.findAny()
359
.map(file -> file.getFileName().toString().replace(" ", "\\ "))
360
.orElse(null);
361
}
362
if (selectedFile == null) {
363
throw new SkipException("No suitable file(s) found for this test in " + home);
364
}
365
try (Stream<Path> content = Files.list(home)) {
366
completions = content.filter(CLASSPATH_FILTER)
367
.filter(file -> file.getFileName().toString().startsWith(selectedFile.replace("\\ ", " ")))
368
.map(file -> file.getFileName().toString().replace(" ", "\\ ") + (Files.isDirectory(file) ? "/" : ""))
369
.sorted()
370
.collect(Collectors.toList());
371
}
372
testNoStartUp(
373
a -> assertCompletion(a, "/env --class-path ~/" + selectedFile + "|", false, completions.toArray(new String[completions.size()]))
374
);
375
}
376
377
@Test
378
public void testSet() throws IOException {
379
List<String> p1 = listFiles(Paths.get(""));
380
getRootDirectories().forEach(s -> p1.add(s.toString()));
381
Collections.sort(p1);
382
383
String[] modes = {"concise ", "normal ", "silent ", "verbose "};
384
String[] options = {"-command", "-delete", "-quiet"};
385
String[] modesWithOptions = Stream.concat(Arrays.stream(options), Arrays.stream(modes)).sorted().toArray(String[]::new);
386
test(false, new String[] {"--no-startup"},
387
a -> assertCompletion(a, "/se|", false, "/set "),
388
a -> assertCompletion(a, "/set |", false, "editor ", "feedback ", "format ", "indent ", "mode ", "prompt ", "start ", "truncation "),
389
390
// /set editor
391
a -> assertCompletion(a, "/set e|", false, "editor "),
392
a -> assertCompletion(a, "/set editor |", false, p1.toArray(new String[p1.size()])),
393
394
// /set feedback
395
a -> assertCompletion(a, "/set fe|", false, "feedback "),
396
a -> assertCompletion(a, "/set fe |", false, modes),
397
398
// /set format
399
a -> assertCompletion(a, "/set fo|", false, "format "),
400
a -> assertCompletion(a, "/set fo |", false, modes),
401
402
// /set mode
403
a -> assertCompletion(a, "/set mo|", false, "mode "),
404
a -> assertCompletion(a, "/set mo |", false),
405
a -> assertCompletion(a, "/set mo newmode |", false, modesWithOptions),
406
a -> assertCompletion(a, "/set mo newmode -|", false, options),
407
a -> assertCompletion(a, "/set mo newmode -command |", false),
408
a -> assertCompletion(a, "/set mo newmode normal |", false, options),
409
410
// /set prompt
411
a -> assertCompletion(a, "/set pro|", false, "prompt "),
412
a -> assertCompletion(a, "/set pro |", false, modes),
413
414
// /set start
415
a -> assertCompletion(a, "/set st|", false, "start "),
416
a -> assertCompletion(a, "/set st |", false, p1.toArray(new String[p1.size()])),
417
418
// /set truncation
419
a -> assertCompletion(a, "/set tr|", false, "truncation "),
420
a -> assertCompletion(a, "/set tr |", false, modes)
421
);
422
}
423
424
private void createIfNeeded(Path file) throws IOException {
425
if (!Files.exists(file))
426
Files.createFile(file);
427
}
428
private List<String> listFiles(Path path) throws IOException {
429
return listFiles(path, ACCEPT_ALL);
430
}
431
432
private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException {
433
try (Stream<Path> stream = Files.list(path)) {
434
return stream.filter(filter)
435
.map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : ""))
436
.sorted()
437
.collect(Collectors.toList());
438
}
439
}
440
441
private static final Predicate<? super Path> ACCEPT_ALL =
442
(file) -> !file.endsWith(".") && !file.endsWith("..");
443
444
private static final Predicate<? super Path> CLASSPATH_FILTER =
445
(file) -> ACCEPT_ALL.test(file) &&
446
(Files.isDirectory(file) ||
447
file.getFileName().toString().endsWith(".jar") ||
448
file.getFileName().toString().endsWith(".zip"));
449
450
private static Iterable<? extends Path> getRootDirectories() {
451
return StreamSupport.stream(FileSystems.getDefault()
452
.getRootDirectories()
453
.spliterator(),
454
false)
455
.filter(p -> Files.exists(p))
456
.collect(Collectors.toList());
457
}
458
}
459
460