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