|
1 /* |
|
2 * Copyright (c) 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 8153716 |
|
27 * @summary Simple jshell tool tests |
|
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 * @build KullaTesting TestingInputStream |
|
33 * @run testng ToolSimpleTest |
|
34 */ |
|
35 import java.util.Arrays; |
|
36 import java.util.ArrayList; |
|
37 import java.util.List; |
|
38 import java.util.function.Consumer; |
|
39 import java.util.stream.Collectors; |
|
40 import java.util.stream.Stream; |
|
41 |
|
42 import org.testng.annotations.Test; |
|
43 |
|
44 import static org.testng.Assert.assertEquals; |
|
45 import static org.testng.Assert.assertTrue; |
|
46 |
|
47 @Test |
|
48 public class ToolSimpleTest extends ReplToolTesting { |
|
49 |
|
50 public void defineVar() { |
|
51 test( |
|
52 (a) -> assertCommand(a, "int x = 72", "| Added variable x of type int with initial value 72"), |
|
53 (a) -> assertCommand(a, "x", "| Variable x of type int has value 72"), |
|
54 (a) -> assertCommand(a, "/vars", "| int x = 72") |
|
55 ); |
|
56 } |
|
57 |
|
58 @Test(enabled = false) // TODO 8153897 |
|
59 public void defineUnresolvedVar() { |
|
60 test( |
|
61 (a) -> assertCommand(a, "undefined x", |
|
62 "| Added variable x, however, it cannot be referenced until class undefined is declared"), |
|
63 (a) -> assertCommand(a, "/vars", "| undefined x = (not-active)") |
|
64 ); |
|
65 } |
|
66 |
|
67 public void testUnresolved() { |
|
68 test( |
|
69 (a) -> assertCommand(a, "int f() { return g() + x + new A().a; }", |
|
70 "| Added method f(), however, it cannot be invoked until method g(), variable x, and class A are declared"), |
|
71 (a) -> assertCommand(a, "f()", |
|
72 "| Attempted to call method f() which cannot be invoked until method g(), variable x, and class A are declared"), |
|
73 (a) -> assertCommandOutputStartsWith(a, "int g() { return x; }", |
|
74 "| Added method g(), however, it cannot be invoked until variable x is declared"), |
|
75 (a) -> assertCommand(a, "g()", "| Attempted to call method g() which cannot be invoked until variable x is declared") |
|
76 ); |
|
77 } |
|
78 |
|
79 public void testDebug() { |
|
80 test( |
|
81 (a) -> assertCommand(a, "/deb", "| Debugging on"), |
|
82 (a) -> assertCommand(a, "/debug", "| Debugging off"), |
|
83 (a) -> assertCommand(a, "/debug", "| Debugging on"), |
|
84 (a) -> assertCommand(a, "/deb", "| Debugging off") |
|
85 ); |
|
86 } |
|
87 |
|
88 public void testHelpLength() { |
|
89 Consumer<String> testOutput = (s) -> { |
|
90 List<String> ss = Stream.of(s.split("\n")) |
|
91 .filter(l -> !l.isEmpty()) |
|
92 .collect(Collectors.toList()); |
|
93 assertTrue(ss.size() >= 10, "Help does not print enough lines:" + s); |
|
94 }; |
|
95 test( |
|
96 (a) -> assertCommandCheckOutput(a, "/?", testOutput), |
|
97 (a) -> assertCommandCheckOutput(a, "/help", testOutput), |
|
98 (a) -> assertCommandCheckOutput(a, "/help /list", testOutput) |
|
99 ); |
|
100 } |
|
101 |
|
102 public void testHelp() { |
|
103 test( |
|
104 (a) -> assertHelp(a, "/?", "/list", "/help", "/exit", "intro"), |
|
105 (a) -> assertHelp(a, "/help", "/list", "/help", "/exit", "intro"), |
|
106 (a) -> assertHelp(a, "/help short", "shortcuts", "<tab>"), |
|
107 (a) -> assertHelp(a, "/? /li", "/list all", "snippets"), |
|
108 (a) -> assertHelp(a, "/help /help", "/help <command>") |
|
109 ); |
|
110 } |
|
111 |
|
112 private void assertHelp(boolean a, String command, String... find) { |
|
113 assertCommandCheckOutput(a, command, s -> { |
|
114 for (String f : find) { |
|
115 assertTrue(s.contains(f), "Expected output of " + command + " to contain: " + f); |
|
116 } |
|
117 }); |
|
118 } |
|
119 |
|
120 // Check that each line of output contains the corresponding string from the list |
|
121 private void checkLineToList(String in, List<String> match) { |
|
122 String[] res = in.trim().split("\n"); |
|
123 assertEquals(res.length, match.size(), "Got: " + Arrays.asList(res)); |
|
124 for (int i = 0; i < match.size(); ++i) { |
|
125 assertTrue(res[i].contains(match.get(i))); |
|
126 } |
|
127 } |
|
128 |
|
129 public void testListArgs() { |
|
130 String arg = "qqqq"; |
|
131 List<String> startVarList = new ArrayList<>(START_UP); |
|
132 startVarList.add("int aardvark"); |
|
133 test( |
|
134 a -> assertCommandCheckOutput(a, "/list all", |
|
135 s -> checkLineToList(s, START_UP)), |
|
136 a -> assertCommandOutputStartsWith(a, "/list " + arg, |
|
137 "| No definition or id found named: " + arg), |
|
138 a -> assertVariable(a, "int", "aardvark"), |
|
139 a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"), |
|
140 a -> assertCommandCheckOutput(a, "/list start", |
|
141 s -> checkLineToList(s, START_UP)), |
|
142 a -> assertCommandCheckOutput(a, "/list all", |
|
143 s -> checkLineToList(s, startVarList)), |
|
144 a -> assertCommandCheckOutput(a, "/list printf", |
|
145 s -> assertTrue(s.contains("void printf"))), |
|
146 a -> assertCommandOutputStartsWith(a, "/list " + arg, |
|
147 "| No definition or id found named: " + arg) |
|
148 ); |
|
149 } |
|
150 |
|
151 public void testHeadlessEditPad() { |
|
152 String prevHeadless = System.getProperty("java.awt.headless"); |
|
153 try { |
|
154 System.setProperty("java.awt.headless", "true"); |
|
155 test( |
|
156 (a) -> assertCommandOutputStartsWith(a, "/edit printf", "| Cannot launch editor -- unexpected exception:") |
|
157 ); |
|
158 } finally { |
|
159 System.setProperty("java.awt.headless", prevHeadless==null? "false" : prevHeadless); |
|
160 } |
|
161 } |
|
162 } |