author | rfield |
Thu, 20 Oct 2016 12:53:11 -0700 | |
changeset 41635 | cb3d04878117 |
parent 40765 | 6f9556cf4404 |
child 42265 | b36ad5a64e75 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36494 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
|
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
26 |
* @bug 8144095 8164825 |
33362 | 27 |
* @summary Test Command Completion |
35359 | 28 |
* @modules jdk.compiler/com.sun.tools.javac.api |
29 |
* jdk.compiler/com.sun.tools.javac.main |
|
36526 | 30 |
* jdk.jdeps/com.sun.tools.javap |
35359 | 31 |
* jdk.jshell/jdk.internal.jshell.tool |
33362 | 32 |
* @library /tools/lib |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
33 |
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
34 |
* @build ReplToolTesting TestingInputStream Compiler |
33362 | 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; |
|
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
43 |
import java.util.Arrays; |
33362 | 44 |
import java.util.Collections; |
45 |
import java.util.List; |
|
46 |
import java.util.function.Predicate; |
|
47 |
import java.util.stream.Collectors; |
|
48 |
import java.util.stream.Stream; |
|
49 |
||
50 |
import org.testng.annotations.Test; |
|
51 |
||
52 |
@Test |
|
53 |
public class CommandCompletionTest extends ReplToolTesting { |
|
54 |
||
55 |
public void testCommand() { |
|
56 |
assertCompletion("/deb|", false); |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
40765
diff
changeset
|
57 |
assertCompletion("/re|", false, "/reload ", "/reset "); |
33362 | 58 |
assertCompletion("/h|", false, "/help ", "/history "); |
59 |
} |
|
60 |
||
61 |
public void testList() { |
|
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
38531
diff
changeset
|
62 |
test(false, new String[] {"--no-startup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
63 |
a -> assertCompletion(a, "/l|", false, "/list "), |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
64 |
a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start "), |
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
65 |
a -> assertCompletion(a, "/list -h|", false, "-history "), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
66 |
a -> assertCompletion(a, "/list q|", false), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
67 |
a -> assertVariable(a, "int", "xray"), |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
68 |
a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start ", "1", "xray"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
69 |
a -> assertCompletion(a, "/list x|", false, "xray") |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
70 |
); |
33362 | 71 |
} |
72 |
||
73 |
public void testDrop() { |
|
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
38531
diff
changeset
|
74 |
test(false, new String[] {"--no-startup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
75 |
a -> assertCompletion(a, "/d|", false, "/drop "), |
33362 | 76 |
a -> assertClass(a, "class cTest {}", "class", "cTest"), |
77 |
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"), |
|
78 |
a -> assertVariable(a, "int", "fTest"), |
|
79 |
a -> assertCompletion(a, "/drop |", false, "1", "2", "3", "cTest", "fTest", "mTest"), |
|
80 |
a -> assertCompletion(a, "/drop f|", false, "fTest") |
|
81 |
); |
|
82 |
} |
|
83 |
||
84 |
public void testEdit() { |
|
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
38531
diff
changeset
|
85 |
test(false, new String[]{"--no-startup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
86 |
a -> assertCompletion(a, "/e|", false, "/edit ", "/exit "), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
87 |
a -> assertCompletion(a, "/ed|", false, "/edit "), |
33362 | 88 |
a -> assertClass(a, "class cTest {}", "class", "cTest"), |
89 |
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"), |
|
90 |
a -> assertVariable(a, "int", "fTest"), |
|
91 |
a -> assertCompletion(a, "/edit |", false, "1", "2", "3", "cTest", "fTest", "mTest"), |
|
92 |
a -> assertCompletion(a, "/edit f|", false, "fTest") |
|
93 |
); |
|
94 |
} |
|
95 |
||
96 |
public void testOpen() throws IOException { |
|
97 |
Compiler compiler = new Compiler(); |
|
98 |
assertCompletion("/o|", false, "/open "); |
|
99 |
List<String> p1 = listFiles(Paths.get("")); |
|
100 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
|
101 |
Collections.sort(p1); |
|
102 |
assertCompletion("/open |", false, p1.toArray(new String[p1.size()])); |
|
103 |
Path classDir = compiler.getClassDir(); |
|
104 |
List<String> p2 = listFiles(classDir); |
|
105 |
assertCompletion("/open " + classDir + "/|", false, p2.toArray(new String[p2.size()])); |
|
106 |
} |
|
107 |
||
108 |
public void testSave() throws IOException { |
|
109 |
Compiler compiler = new Compiler(); |
|
36494 | 110 |
assertCompletion("/s|", false, "/save ", "/set "); |
33362 | 111 |
List<String> p1 = listFiles(Paths.get("")); |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
112 |
Collections.addAll(p1, "-all ", "-history ", "-start "); |
33362 | 113 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
114 |
Collections.sort(p1); |
|
115 |
assertCompletion("/save |", false, p1.toArray(new String[p1.size()])); |
|
116 |
Path classDir = compiler.getClassDir(); |
|
117 |
List<String> p2 = listFiles(classDir); |
|
118 |
assertCompletion("/save " + classDir + "/|", |
|
119 |
false, p2.toArray(new String[p2.size()])); |
|
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
120 |
assertCompletion("/save -all " + classDir + "/|", |
33362 | 121 |
false, p2.toArray(new String[p2.size()])); |
122 |
} |
|
123 |
||
124 |
public void testClassPath() throws IOException { |
|
125 |
assertCompletion("/classp|", false, "/classpath "); |
|
126 |
Compiler compiler = new Compiler(); |
|
127 |
Path outDir = compiler.getPath("testClasspathCompletion"); |
|
128 |
Files.createDirectories(outDir); |
|
129 |
Files.createDirectories(outDir.resolve("dir")); |
|
130 |
createIfNeeded(outDir.resolve("test.jar")); |
|
131 |
createIfNeeded(outDir.resolve("test.zip")); |
|
132 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
133 |
String jarName = "test.jar"; |
|
134 |
compiler.jar(outDir, jarName, "pkg/A.class"); |
|
135 |
compiler.getPath(outDir).resolve(jarName); |
|
136 |
List<String> paths = listFiles(outDir, CLASSPATH_FILTER); |
|
137 |
assertCompletion("/classpath " + outDir + "/|", false, paths.toArray(new String[paths.size()])); |
|
138 |
} |
|
139 |
||
140 |
public void testUserHome() throws IOException { |
|
141 |
List<String> completions; |
|
142 |
Path home = Paths.get(System.getProperty("user.home")); |
|
143 |
try (Stream<Path> content = Files.list(home)) { |
|
144 |
completions = content.filter(CLASSPATH_FILTER) |
|
145 |
.map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : "")) |
|
146 |
.sorted() |
|
147 |
.collect(Collectors.toList()); |
|
148 |
} |
|
149 |
assertCompletion("/classpath ~/|", false, completions.toArray(new String[completions.size()])); |
|
150 |
} |
|
151 |
||
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
152 |
public void testSet() throws IOException { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
153 |
List<String> p1 = listFiles(Paths.get("")); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
154 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
155 |
Collections.sort(p1); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
156 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
157 |
String[] modes = {"concise ", "normal ", "silent ", "verbose "}; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
158 |
String[] options = {"-command", "-delete", "-quiet"}; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
159 |
String[] modesWithOptions = Stream.concat(Arrays.stream(options), Arrays.stream(modes)).sorted().toArray(String[]::new); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
160 |
test(false, new String[] {"--no-startup"}, |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
161 |
a -> assertCompletion(a, "/se|", false, "/set "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
162 |
a -> assertCompletion(a, "/set |", false, "editor ", "feedback ", "format ", "mode ", "prompt ", "start ", "truncation "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
163 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
164 |
// /set editor |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
165 |
a -> assertCompletion(a, "/set e|", false, "editor "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
166 |
a -> assertCompletion(a, "/set editor |", false, p1.toArray(new String[p1.size()])), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
167 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
168 |
// /set feedback |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
169 |
a -> assertCompletion(a, "/set fe|", false, "feedback "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
170 |
a -> assertCompletion(a, "/set fe |", false, modes), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
171 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
172 |
// /set format |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
173 |
a -> assertCompletion(a, "/set fo|", false, "format "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
174 |
a -> assertCompletion(a, "/set fo |", false, modes), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
175 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
176 |
// /set mode |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
177 |
a -> assertCompletion(a, "/set mo|", false, "mode "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
178 |
a -> assertCompletion(a, "/set mo |", false), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
179 |
a -> assertCompletion(a, "/set mo newmode |", false, modesWithOptions), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
180 |
a -> assertCompletion(a, "/set mo newmode -|", false, options), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
181 |
a -> assertCompletion(a, "/set mo newmode -command |", false), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
182 |
a -> assertCompletion(a, "/set mo newmode normal |", false, options), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
183 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
184 |
// /set prompt |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
185 |
a -> assertCompletion(a, "/set pro|", false, "prompt "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
186 |
a -> assertCompletion(a, "/set pro |", false, modes), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
187 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
188 |
// /set start |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
189 |
a -> assertCompletion(a, "/set st|", false, "start "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
190 |
a -> assertCompletion(a, "/set st |", false, p1.toArray(new String[p1.size()])), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
191 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
192 |
// /set truncation |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
193 |
a -> assertCompletion(a, "/set tr|", false, "truncation "), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
194 |
a -> assertCompletion(a, "/set tr |", false, modes) |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
195 |
); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
196 |
} |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40588
diff
changeset
|
197 |
|
33362 | 198 |
private void createIfNeeded(Path file) throws IOException { |
199 |
if (!Files.exists(file)) |
|
200 |
Files.createFile(file); |
|
201 |
} |
|
202 |
private List<String> listFiles(Path path) throws IOException { |
|
203 |
return listFiles(path, ACCEPT_ALL); |
|
204 |
} |
|
205 |
||
206 |
private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException { |
|
207 |
try (Stream<Path> stream = Files.list(path)) { |
|
208 |
return stream.filter(filter) |
|
209 |
.map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : "")) |
|
210 |
.sorted() |
|
211 |
.collect(Collectors.toList()); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
private static final Predicate<? super Path> ACCEPT_ALL = |
|
216 |
(file) -> !file.endsWith(".") && !file.endsWith(".."); |
|
217 |
||
218 |
private static final Predicate<? super Path> CLASSPATH_FILTER = |
|
219 |
(file) -> ACCEPT_ALL.test(file) && |
|
220 |
(Files.isDirectory(file) || |
|
221 |
file.getFileName().toString().endsWith(".jar") || |
|
222 |
file.getFileName().toString().endsWith(".zip")); |
|
223 |
} |