2015-10-19 17:15:16 +00:00
|
|
|
/*
|
2016-03-08 19:53:35 +00:00
|
|
|
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
2015-10-19 17:15:16 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
2016-09-01 02:07:00 +00:00
|
|
|
* @bug 8144095 8164825
|
2015-10-19 17:15:16 +00:00
|
|
|
* @summary Test Command Completion
|
2016-01-25 19:01:32 +00:00
|
|
|
* @modules jdk.compiler/com.sun.tools.javac.api
|
|
|
|
* jdk.compiler/com.sun.tools.javac.main
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Co-authored-by: Vicente Romero <vicente.romero@oracle.com>
Co-authored-by: Andreas Lundblad <andreas.lundblad@oracle.com>
Co-authored-by: Andrey Nazarov <andrey.x.nazarov@oracle.com>
Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: Kumar Srinivasan <kumar.x.srinivasan@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs
2016-03-17 19:04:28 +00:00
|
|
|
* jdk.jdeps/com.sun.tools.javap
|
2016-01-25 19:01:32 +00:00
|
|
|
* jdk.jshell/jdk.internal.jshell.tool
|
2015-10-19 17:15:16 +00:00
|
|
|
* @library /tools/lib
|
2016-03-31 22:20:50 +00:00
|
|
|
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
|
|
|
|
* @build ReplToolTesting TestingInputStream Compiler
|
2015-10-19 17:15:16 +00:00
|
|
|
* @run testng CommandCompletionTest
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.FileSystems;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2016-09-01 02:07:00 +00:00
|
|
|
import java.util.Arrays;
|
2015-10-19 17:15:16 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public class CommandCompletionTest extends ReplToolTesting {
|
|
|
|
|
|
|
|
public void testCommand() {
|
|
|
|
assertCompletion("/deb|", false);
|
2016-10-20 19:53:11 +00:00
|
|
|
assertCompletion("/re|", false, "/reload ", "/reset ");
|
2015-10-19 17:15:16 +00:00
|
|
|
assertCompletion("/h|", false, "/help ", "/history ");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testList() {
|
2016-08-23 02:31:37 +00:00
|
|
|
test(false, new String[] {"--no-startup"},
|
2015-12-11 07:27:06 +00:00
|
|
|
a -> assertCompletion(a, "/l|", false, "/list "),
|
2016-05-18 18:35:10 +00:00
|
|
|
a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start "),
|
|
|
|
a -> assertCompletion(a, "/list -h|", false, "-history "),
|
2015-12-11 07:27:06 +00:00
|
|
|
a -> assertCompletion(a, "/list q|", false),
|
|
|
|
a -> assertVariable(a, "int", "xray"),
|
2016-05-18 18:35:10 +00:00
|
|
|
a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start ", "1", "xray"),
|
2015-12-11 07:27:06 +00:00
|
|
|
a -> assertCompletion(a, "/list x|", false, "xray")
|
|
|
|
);
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testDrop() {
|
2016-08-23 02:31:37 +00:00
|
|
|
test(false, new String[] {"--no-startup"},
|
2015-12-11 07:27:06 +00:00
|
|
|
a -> assertCompletion(a, "/d|", false, "/drop "),
|
2015-10-19 17:15:16 +00:00
|
|
|
a -> assertClass(a, "class cTest {}", "class", "cTest"),
|
|
|
|
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
|
|
|
|
a -> assertVariable(a, "int", "fTest"),
|
|
|
|
a -> assertCompletion(a, "/drop |", false, "1", "2", "3", "cTest", "fTest", "mTest"),
|
|
|
|
a -> assertCompletion(a, "/drop f|", false, "fTest")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testEdit() {
|
2016-08-23 02:31:37 +00:00
|
|
|
test(false, new String[]{"--no-startup"},
|
2015-12-11 07:27:06 +00:00
|
|
|
a -> assertCompletion(a, "/e|", false, "/edit ", "/exit "),
|
|
|
|
a -> assertCompletion(a, "/ed|", false, "/edit "),
|
2015-10-19 17:15:16 +00:00
|
|
|
a -> assertClass(a, "class cTest {}", "class", "cTest"),
|
|
|
|
a -> assertMethod(a, "int mTest() { return 0; }", "()I", "mTest"),
|
|
|
|
a -> assertVariable(a, "int", "fTest"),
|
|
|
|
a -> assertCompletion(a, "/edit |", false, "1", "2", "3", "cTest", "fTest", "mTest"),
|
|
|
|
a -> assertCompletion(a, "/edit f|", false, "fTest")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testOpen() throws IOException {
|
|
|
|
Compiler compiler = new Compiler();
|
|
|
|
assertCompletion("/o|", false, "/open ");
|
|
|
|
List<String> p1 = listFiles(Paths.get(""));
|
|
|
|
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString()));
|
|
|
|
Collections.sort(p1);
|
|
|
|
assertCompletion("/open |", false, p1.toArray(new String[p1.size()]));
|
|
|
|
Path classDir = compiler.getClassDir();
|
|
|
|
List<String> p2 = listFiles(classDir);
|
|
|
|
assertCompletion("/open " + classDir + "/|", false, p2.toArray(new String[p2.size()]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSave() throws IOException {
|
|
|
|
Compiler compiler = new Compiler();
|
2016-03-08 19:53:35 +00:00
|
|
|
assertCompletion("/s|", false, "/save ", "/set ");
|
2015-10-19 17:15:16 +00:00
|
|
|
List<String> p1 = listFiles(Paths.get(""));
|
2016-05-18 18:35:10 +00:00
|
|
|
Collections.addAll(p1, "-all ", "-history ", "-start ");
|
2015-10-19 17:15:16 +00:00
|
|
|
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString()));
|
|
|
|
Collections.sort(p1);
|
|
|
|
assertCompletion("/save |", false, p1.toArray(new String[p1.size()]));
|
|
|
|
Path classDir = compiler.getClassDir();
|
|
|
|
List<String> p2 = listFiles(classDir);
|
|
|
|
assertCompletion("/save " + classDir + "/|",
|
|
|
|
false, p2.toArray(new String[p2.size()]));
|
2016-05-18 18:35:10 +00:00
|
|
|
assertCompletion("/save -all " + classDir + "/|",
|
2015-10-19 17:15:16 +00:00
|
|
|
false, p2.toArray(new String[p2.size()]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testClassPath() throws IOException {
|
|
|
|
assertCompletion("/classp|", false, "/classpath ");
|
|
|
|
Compiler compiler = new Compiler();
|
|
|
|
Path outDir = compiler.getPath("testClasspathCompletion");
|
|
|
|
Files.createDirectories(outDir);
|
|
|
|
Files.createDirectories(outDir.resolve("dir"));
|
|
|
|
createIfNeeded(outDir.resolve("test.jar"));
|
|
|
|
createIfNeeded(outDir.resolve("test.zip"));
|
|
|
|
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
|
|
|
|
String jarName = "test.jar";
|
|
|
|
compiler.jar(outDir, jarName, "pkg/A.class");
|
|
|
|
compiler.getPath(outDir).resolve(jarName);
|
|
|
|
List<String> paths = listFiles(outDir, CLASSPATH_FILTER);
|
|
|
|
assertCompletion("/classpath " + outDir + "/|", false, paths.toArray(new String[paths.size()]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testUserHome() throws IOException {
|
|
|
|
List<String> completions;
|
|
|
|
Path home = Paths.get(System.getProperty("user.home"));
|
|
|
|
try (Stream<Path> content = Files.list(home)) {
|
|
|
|
completions = content.filter(CLASSPATH_FILTER)
|
|
|
|
.map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : ""))
|
|
|
|
.sorted()
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
assertCompletion("/classpath ~/|", false, completions.toArray(new String[completions.size()]));
|
|
|
|
}
|
|
|
|
|
2016-09-01 02:07:00 +00:00
|
|
|
public void testSet() throws IOException {
|
|
|
|
List<String> p1 = listFiles(Paths.get(""));
|
|
|
|
FileSystems.getDefault().getRootDirectories().forEach(s -> p1.add(s.toString()));
|
|
|
|
Collections.sort(p1);
|
|
|
|
|
|
|
|
String[] modes = {"concise ", "normal ", "silent ", "verbose "};
|
|
|
|
String[] options = {"-command", "-delete", "-quiet"};
|
|
|
|
String[] modesWithOptions = Stream.concat(Arrays.stream(options), Arrays.stream(modes)).sorted().toArray(String[]::new);
|
|
|
|
test(false, new String[] {"--no-startup"},
|
|
|
|
a -> assertCompletion(a, "/se|", false, "/set "),
|
|
|
|
a -> assertCompletion(a, "/set |", false, "editor ", "feedback ", "format ", "mode ", "prompt ", "start ", "truncation "),
|
|
|
|
|
|
|
|
// /set editor
|
|
|
|
a -> assertCompletion(a, "/set e|", false, "editor "),
|
|
|
|
a -> assertCompletion(a, "/set editor |", false, p1.toArray(new String[p1.size()])),
|
|
|
|
|
|
|
|
// /set feedback
|
|
|
|
a -> assertCompletion(a, "/set fe|", false, "feedback "),
|
|
|
|
a -> assertCompletion(a, "/set fe |", false, modes),
|
|
|
|
|
|
|
|
// /set format
|
|
|
|
a -> assertCompletion(a, "/set fo|", false, "format "),
|
|
|
|
a -> assertCompletion(a, "/set fo |", false, modes),
|
|
|
|
|
|
|
|
// /set mode
|
|
|
|
a -> assertCompletion(a, "/set mo|", false, "mode "),
|
|
|
|
a -> assertCompletion(a, "/set mo |", false),
|
|
|
|
a -> assertCompletion(a, "/set mo newmode |", false, modesWithOptions),
|
|
|
|
a -> assertCompletion(a, "/set mo newmode -|", false, options),
|
|
|
|
a -> assertCompletion(a, "/set mo newmode -command |", false),
|
|
|
|
a -> assertCompletion(a, "/set mo newmode normal |", false, options),
|
|
|
|
|
|
|
|
// /set prompt
|
|
|
|
a -> assertCompletion(a, "/set pro|", false, "prompt "),
|
|
|
|
a -> assertCompletion(a, "/set pro |", false, modes),
|
|
|
|
|
|
|
|
// /set start
|
|
|
|
a -> assertCompletion(a, "/set st|", false, "start "),
|
|
|
|
a -> assertCompletion(a, "/set st |", false, p1.toArray(new String[p1.size()])),
|
|
|
|
|
|
|
|
// /set truncation
|
|
|
|
a -> assertCompletion(a, "/set tr|", false, "truncation "),
|
|
|
|
a -> assertCompletion(a, "/set tr |", false, modes)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:15:16 +00:00
|
|
|
private void createIfNeeded(Path file) throws IOException {
|
|
|
|
if (!Files.exists(file))
|
|
|
|
Files.createFile(file);
|
|
|
|
}
|
|
|
|
private List<String> listFiles(Path path) throws IOException {
|
|
|
|
return listFiles(path, ACCEPT_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException {
|
|
|
|
try (Stream<Path> stream = Files.list(path)) {
|
|
|
|
return stream.filter(filter)
|
|
|
|
.map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : ""))
|
|
|
|
.sorted()
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Predicate<? super Path> ACCEPT_ALL =
|
|
|
|
(file) -> !file.endsWith(".") && !file.endsWith("..");
|
|
|
|
|
|
|
|
private static final Predicate<? super Path> CLASSPATH_FILTER =
|
|
|
|
(file) -> ACCEPT_ALL.test(file) &&
|
|
|
|
(Files.isDirectory(file) ||
|
|
|
|
file.getFileName().toString().endsWith(".jar") ||
|
|
|
|
file.getFileName().toString().endsWith(".zip"));
|
|
|
|
}
|