author | rfield |
Thu, 10 Dec 2015 23:27:06 -0800 | |
changeset 34570 | 8a8f52a733dd |
parent 34475 | 7af94fd75ede |
child 35359 | f04501964016 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
26 |
* @bug 8144095 |
33362 | 27 |
* @summary Test Command Completion |
28 |
* @library /tools/lib |
|
29 |
* @build ReplToolTesting TestingInputStream Compiler ToolBox |
|
30 |
* @run testng CommandCompletionTest |
|
31 |
*/ |
|
32 |
||
33 |
import java.io.IOException; |
|
34 |
import java.nio.file.FileSystems; |
|
35 |
import java.nio.file.Files; |
|
36 |
import java.nio.file.Path; |
|
37 |
import java.nio.file.Paths; |
|
38 |
import java.util.Collections; |
|
39 |
import java.util.List; |
|
40 |
import java.util.function.Predicate; |
|
41 |
import java.util.stream.Collectors; |
|
42 |
import java.util.stream.Stream; |
|
43 |
||
44 |
import org.testng.annotations.Test; |
|
45 |
||
46 |
@Test |
|
47 |
public class CommandCompletionTest extends ReplToolTesting { |
|
48 |
||
49 |
public void testCommand() { |
|
50 |
assertCompletion("/f|", false, "/feedback "); |
|
51 |
assertCompletion("/deb|", false); |
|
52 |
assertCompletion("/feedback v|", false, "verbose"); |
|
53 |
assertCompletion("/c|", false, "/classes ", "/classpath "); |
|
54 |
assertCompletion("/h|", false, "/help ", "/history "); |
|
55 |
assertCompletion("/feedback |", false, |
|
56 |
"?", "concise", "default", "normal", "off", "verbose"); |
|
57 |
} |
|
58 |
||
59 |
public void testList() { |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
60 |
test(false, new String[] {"-nostartup"}, |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
61 |
a -> assertCompletion(a, "/l|", false, "/list "), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
62 |
a -> assertCompletion(a, "/list |", false, "all ", "history ", "start "), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
63 |
a -> assertCompletion(a, "/list h|", false, "history "), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
64 |
a -> assertCompletion(a, "/list q|", false), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
65 |
a -> assertVariable(a, "int", "xray"), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
66 |
a -> assertCompletion(a, "/list |", false, "1", "all ", "history ", "start ", "xray"), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
67 |
a -> assertCompletion(a, "/list x|", false, "xray") |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
68 |
); |
33362 | 69 |
} |
70 |
||
71 |
public void testDrop() { |
|
72 |
test(false, new String[] {"-nostartup"}, |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
73 |
a -> assertCompletion(a, "/d|", false, "/drop "), |
33362 | 74 |
a -> assertClass(a, "class cTest {}", "class", "cTest"), |
75 |
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"), |
|
76 |
a -> assertVariable(a, "int", "fTest"), |
|
77 |
a -> assertCompletion(a, "/drop |", false, "1", "2", "3", "cTest", "fTest", "mTest"), |
|
78 |
a -> assertCompletion(a, "/drop f|", false, "fTest") |
|
79 |
); |
|
80 |
} |
|
81 |
||
82 |
public void testEdit() { |
|
83 |
test(false, new String[]{"-nostartup"}, |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
84 |
a -> assertCompletion(a, "/e|", false, "/edit ", "/exit "), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
85 |
a -> assertCompletion(a, "/ed|", false, "/edit "), |
33362 | 86 |
a -> assertClass(a, "class cTest {}", "class", "cTest"), |
87 |
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"), |
|
88 |
a -> assertVariable(a, "int", "fTest"), |
|
89 |
a -> assertCompletion(a, "/edit |", false, "1", "2", "3", "cTest", "fTest", "mTest"), |
|
90 |
a -> assertCompletion(a, "/edit f|", false, "fTest") |
|
91 |
); |
|
92 |
} |
|
93 |
||
94 |
public void testOpen() throws IOException { |
|
95 |
Compiler compiler = new Compiler(); |
|
96 |
assertCompletion("/o|", false, "/open "); |
|
97 |
List<String> p1 = listFiles(Paths.get("")); |
|
98 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
|
99 |
Collections.sort(p1); |
|
100 |
assertCompletion("/open |", false, p1.toArray(new String[p1.size()])); |
|
101 |
Path classDir = compiler.getClassDir(); |
|
102 |
List<String> p2 = listFiles(classDir); |
|
103 |
assertCompletion("/open " + classDir + "/|", false, p2.toArray(new String[p2.size()])); |
|
104 |
} |
|
105 |
||
106 |
public void testSave() throws IOException { |
|
107 |
Compiler compiler = new Compiler(); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33362
diff
changeset
|
108 |
assertCompletion("/s|", false, "/save ", "/seteditor ", "/setstart "); |
33362 | 109 |
List<String> p1 = listFiles(Paths.get("")); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33362
diff
changeset
|
110 |
Collections.addAll(p1, "all ", "history ", "start "); |
33362 | 111 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
112 |
Collections.sort(p1); |
|
113 |
assertCompletion("/save |", false, p1.toArray(new String[p1.size()])); |
|
114 |
Path classDir = compiler.getClassDir(); |
|
115 |
List<String> p2 = listFiles(classDir); |
|
116 |
assertCompletion("/save " + classDir + "/|", |
|
117 |
false, p2.toArray(new String[p2.size()])); |
|
118 |
assertCompletion("/save all " + classDir + "/|", |
|
119 |
false, p2.toArray(new String[p2.size()])); |
|
120 |
} |
|
121 |
||
122 |
public void testClassPath() throws IOException { |
|
123 |
assertCompletion("/classp|", false, "/classpath "); |
|
124 |
Compiler compiler = new Compiler(); |
|
125 |
Path outDir = compiler.getPath("testClasspathCompletion"); |
|
126 |
Files.createDirectories(outDir); |
|
127 |
Files.createDirectories(outDir.resolve("dir")); |
|
128 |
createIfNeeded(outDir.resolve("test.jar")); |
|
129 |
createIfNeeded(outDir.resolve("test.zip")); |
|
130 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
131 |
String jarName = "test.jar"; |
|
132 |
compiler.jar(outDir, jarName, "pkg/A.class"); |
|
133 |
compiler.getPath(outDir).resolve(jarName); |
|
134 |
List<String> paths = listFiles(outDir, CLASSPATH_FILTER); |
|
135 |
assertCompletion("/classpath " + outDir + "/|", false, paths.toArray(new String[paths.size()])); |
|
136 |
} |
|
137 |
||
138 |
public void testUserHome() throws IOException { |
|
139 |
List<String> completions; |
|
140 |
Path home = Paths.get(System.getProperty("user.home")); |
|
141 |
try (Stream<Path> content = Files.list(home)) { |
|
142 |
completions = content.filter(CLASSPATH_FILTER) |
|
143 |
.map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : "")) |
|
144 |
.sorted() |
|
145 |
.collect(Collectors.toList()); |
|
146 |
} |
|
147 |
assertCompletion("/classpath ~/|", false, completions.toArray(new String[completions.size()])); |
|
148 |
} |
|
149 |
||
150 |
private void createIfNeeded(Path file) throws IOException { |
|
151 |
if (!Files.exists(file)) |
|
152 |
Files.createFile(file); |
|
153 |
} |
|
154 |
private List<String> listFiles(Path path) throws IOException { |
|
155 |
return listFiles(path, ACCEPT_ALL); |
|
156 |
} |
|
157 |
||
158 |
private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException { |
|
159 |
try (Stream<Path> stream = Files.list(path)) { |
|
160 |
return stream.filter(filter) |
|
161 |
.map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : "")) |
|
162 |
.sorted() |
|
163 |
.collect(Collectors.toList()); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
private static final Predicate<? super Path> ACCEPT_ALL = |
|
168 |
(file) -> !file.endsWith(".") && !file.endsWith(".."); |
|
169 |
||
170 |
private static final Predicate<? super Path> CLASSPATH_FILTER = |
|
171 |
(file) -> ACCEPT_ALL.test(file) && |
|
172 |
(Files.isDirectory(file) || |
|
173 |
file.getFileName().toString().endsWith(".jar") || |
|
174 |
file.getFileName().toString().endsWith(".zip")); |
|
175 |
} |