1 /* 2 * Copyright (c) 2015, 2016, 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 * @bug 8144095 27 * @summary Test Command Completion 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.main 30 * jdk.jdeps/com.sun.tools.javap 31 * jdk.jshell/jdk.internal.jshell.tool 32 * @library /tools/lib 33 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask 34 * @build ReplToolTesting TestingInputStream Compiler 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); 56 assertCompletion("/re|", false, "/reload ", "/reset ", "/retain "); 57 assertCompletion("/h|", false, "/help ", "/history "); 58 } 59 60 public void testList() { 61 test(false, new String[] {"--no-startup"}, 62 a -> assertCompletion(a, "/l|", false, "/list "), 63 a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start "), 64 a -> assertCompletion(a, "/list -h|", false, "-history "), 65 a -> assertCompletion(a, "/list q|", false), 66 a -> assertVariable(a, "int", "xray"), 67 a -> assertCompletion(a, "/list |", false, "-all ", "-history ", "-start ", "1", "xray"), 68 a -> assertCompletion(a, "/list x|", false, "xray") 69 ); 70 } 71 72 public void testDrop() { 73 test(false, new String[] {"--no-startup"}, 74 a -> assertCompletion(a, "/d|", false, "/drop "), 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() { 84 test(false, new String[]{"--no-startup"}, 85 a -> assertCompletion(a, "/e|", false, "/edit ", "/exit "), 86 a -> assertCompletion(a, "/ed|", false, "/edit "), 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(); 109 assertCompletion("/s|", false, "/save ", "/set "); 110 List<String> p1 = listFiles(Paths.get("")); 111 Collections.addAll(p1, "-all ", "-history ", "-start "); 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()])); 119 assertCompletion("/save -all " + classDir + "/|", 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 }