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