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