author | rfield |
Mon, 22 Aug 2016 19:31:37 -0700 | |
changeset 40588 | b5c32bfa9710 |
parent 38531 | c449daa25b45 |
child 40765 | 6f9556cf4404 |
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 |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
26 |
* @bug 8144095 |
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; |
|
43 |
import java.util.Collections; |
|
44 |
import java.util.List; |
|
45 |
import java.util.function.Predicate; |
|
46 |
import java.util.stream.Collectors; |
|
47 |
import java.util.stream.Stream; |
|
48 |
||
49 |
import org.testng.annotations.Test; |
|
50 |
||
51 |
@Test |
|
52 |
public class CommandCompletionTest extends ReplToolTesting { |
|
53 |
||
54 |
public void testCommand() { |
|
55 |
assertCompletion("/deb|", false); |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
56 |
assertCompletion("/re|", false, "/reload ", "/reset ", "/retain "); |
33362 | 57 |
assertCompletion("/h|", false, "/help ", "/history "); |
58 |
} |
|
59 |
||
60 |
public void testList() { |
|
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
38531
diff
changeset
|
61 |
test(false, new String[] {"--no-startup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
62 |
a -> assertCompletion(a, "/l|", false, "/list "), |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
63 |
a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start "), |
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
64 |
a -> assertCompletion(a, "/list -h|", false, "-history "), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
65 |
a -> assertCompletion(a, "/list q|", false), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
66 |
a -> assertVariable(a, "int", "xray"), |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
67 |
a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start ", "1", "xray"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
68 |
a -> assertCompletion(a, "/list x|", false, "xray") |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
69 |
); |
33362 | 70 |
} |
71 |
||
72 |
public void testDrop() { |
|
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
38531
diff
changeset
|
73 |
test(false, new String[] {"--no-startup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
74 |
a -> assertCompletion(a, "/d|", false, "/drop "), |
33362 | 75 |
a -> assertClass(a, "class cTest {}", "class", "cTest"), |
76 |
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"), |
|
77 |
a -> assertVariable(a, "int", "fTest"), |
|
78 |
a -> assertCompletion(a, "/drop |", false, "1", "2", "3", "cTest", "fTest", "mTest"), |
|
79 |
a -> assertCompletion(a, "/drop f|", false, "fTest") |
|
80 |
); |
|
81 |
} |
|
82 |
||
83 |
public void testEdit() { |
|
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
38531
diff
changeset
|
84 |
test(false, new String[]{"--no-startup"}, |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
85 |
a -> assertCompletion(a, "/e|", false, "/edit ", "/exit "), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34475
diff
changeset
|
86 |
a -> assertCompletion(a, "/ed|", false, "/edit "), |
33362 | 87 |
a -> assertClass(a, "class cTest {}", "class", "cTest"), |
88 |
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"), |
|
89 |
a -> assertVariable(a, "int", "fTest"), |
|
90 |
a -> assertCompletion(a, "/edit |", false, "1", "2", "3", "cTest", "fTest", "mTest"), |
|
91 |
a -> assertCompletion(a, "/edit f|", false, "fTest") |
|
92 |
); |
|
93 |
} |
|
94 |
||
95 |
public void testOpen() throws IOException { |
|
96 |
Compiler compiler = new Compiler(); |
|
97 |
assertCompletion("/o|", false, "/open "); |
|
98 |
List<String> p1 = listFiles(Paths.get("")); |
|
99 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
|
100 |
Collections.sort(p1); |
|
101 |
assertCompletion("/open |", false, p1.toArray(new String[p1.size()])); |
|
102 |
Path classDir = compiler.getClassDir(); |
|
103 |
List<String> p2 = listFiles(classDir); |
|
104 |
assertCompletion("/open " + classDir + "/|", false, p2.toArray(new String[p2.size()])); |
|
105 |
} |
|
106 |
||
107 |
public void testSave() throws IOException { |
|
108 |
Compiler compiler = new Compiler(); |
|
36494 | 109 |
assertCompletion("/s|", false, "/save ", "/set "); |
33362 | 110 |
List<String> p1 = listFiles(Paths.get("")); |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
111 |
Collections.addAll(p1, "-all ", "-history ", "-start "); |
33362 | 112 |
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString())); |
113 |
Collections.sort(p1); |
|
114 |
assertCompletion("/save |", false, p1.toArray(new String[p1.size()])); |
|
115 |
Path classDir = compiler.getClassDir(); |
|
116 |
List<String> p2 = listFiles(classDir); |
|
117 |
assertCompletion("/save " + classDir + "/|", |
|
118 |
false, p2.toArray(new String[p2.size()])); |
|
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
119 |
assertCompletion("/save -all " + classDir + "/|", |
33362 | 120 |
false, p2.toArray(new String[p2.size()])); |
121 |
} |
|
122 |
||
123 |
public void testClassPath() throws IOException { |
|
124 |
assertCompletion("/classp|", false, "/classpath "); |
|
125 |
Compiler compiler = new Compiler(); |
|
126 |
Path outDir = compiler.getPath("testClasspathCompletion"); |
|
127 |
Files.createDirectories(outDir); |
|
128 |
Files.createDirectories(outDir.resolve("dir")); |
|
129 |
createIfNeeded(outDir.resolve("test.jar")); |
|
130 |
createIfNeeded(outDir.resolve("test.zip")); |
|
131 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
132 |
String jarName = "test.jar"; |
|
133 |
compiler.jar(outDir, jarName, "pkg/A.class"); |
|
134 |
compiler.getPath(outDir).resolve(jarName); |
|
135 |
List<String> paths = listFiles(outDir, CLASSPATH_FILTER); |
|
136 |
assertCompletion("/classpath " + outDir + "/|", false, paths.toArray(new String[paths.size()])); |
|
137 |
} |
|
138 |
||
139 |
public void testUserHome() throws IOException { |
|
140 |
List<String> completions; |
|
141 |
Path home = Paths.get(System.getProperty("user.home")); |
|
142 |
try (Stream<Path> content = Files.list(home)) { |
|
143 |
completions = content.filter(CLASSPATH_FILTER) |
|
144 |
.map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : "")) |
|
145 |
.sorted() |
|
146 |
.collect(Collectors.toList()); |
|
147 |
} |
|
148 |
assertCompletion("/classpath ~/|", false, completions.toArray(new String[completions.size()])); |
|
149 |
} |
|
150 |
||
151 |
private void createIfNeeded(Path file) throws IOException { |
|
152 |
if (!Files.exists(file)) |
|
153 |
Files.createFile(file); |
|
154 |
} |
|
155 |
private List<String> listFiles(Path path) throws IOException { |
|
156 |
return listFiles(path, ACCEPT_ALL); |
|
157 |
} |
|
158 |
||
159 |
private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException { |
|
160 |
try (Stream<Path> stream = Files.list(path)) { |
|
161 |
return stream.filter(filter) |
|
162 |
.map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : "")) |
|
163 |
.sorted() |
|
164 |
.collect(Collectors.toList()); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
private static final Predicate<? super Path> ACCEPT_ALL = |
|
169 |
(file) -> !file.endsWith(".") && !file.endsWith(".."); |
|
170 |
||
171 |
private static final Predicate<? super Path> CLASSPATH_FILTER = |
|
172 |
(file) -> ACCEPT_ALL.test(file) && |
|
173 |
(Files.isDirectory(file) || |
|
174 |
file.getFileName().toString().endsWith(".jar") || |
|
175 |
file.getFileName().toString().endsWith(".zip")); |
|
176 |
} |