author | rfield |
Thu, 02 Jun 2016 14:05:13 -0700 | |
changeset 38836 | b09d1cfbf28c |
parent 38613 | 2a8e50869b08 |
child 40516 | 9e0e107c39dd |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36494 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
|
38613 | 26 |
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36718
diff
changeset
|
27 |
* @summary Tests for Basic tests for REPL tool |
37389 | 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 |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
32 |
* @library /tools/lib |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36718
diff
changeset
|
33 |
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36718
diff
changeset
|
34 |
* @build KullaTesting TestingInputStream Compiler |
35002 | 35 |
* @run testng/timeout=600 ToolBasicTest |
33362 | 36 |
*/ |
37 |
||
38 |
import java.io.IOException; |
|
39 |
import java.io.PrintWriter; |
|
40 |
import java.io.StringWriter; |
|
41 |
import java.nio.file.Files; |
|
42 |
import java.nio.file.Path; |
|
43 |
import java.nio.file.Paths; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Arrays; |
|
46 |
import java.util.List; |
|
36990 | 47 |
import java.util.Locale; |
33362 | 48 |
import java.util.Scanner; |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
49 |
import java.util.function.BiFunction; |
33362 | 50 |
import java.util.function.Consumer; |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
51 |
import java.util.function.Function; |
33362 | 52 |
import java.util.prefs.BackingStoreException; |
53 |
import java.util.prefs.Preferences; |
|
54 |
import java.util.stream.Collectors; |
|
55 |
import java.util.stream.Stream; |
|
56 |
||
57 |
import org.testng.annotations.Test; |
|
58 |
||
59 |
import static org.testng.Assert.assertEquals; |
|
60 |
import static org.testng.Assert.assertTrue; |
|
61 |
import static org.testng.Assert.fail; |
|
62 |
||
63 |
@Test |
|
64 |
public class ToolBasicTest extends ReplToolTesting { |
|
65 |
||
66 |
public void elideStartUpFromList() { |
|
67 |
test( |
|
38613 | 68 |
(a) -> assertCommandOutputContains(a, "123", "==> 123"), |
33362 | 69 |
(a) -> assertCommandCheckOutput(a, "/list", (s) -> { |
70 |
int cnt; |
|
71 |
try (Scanner scanner = new Scanner(s)) { |
|
72 |
cnt = 0; |
|
73 |
while (scanner.hasNextLine()) { |
|
74 |
String line = scanner.nextLine(); |
|
75 |
if (!line.trim().isEmpty()) { |
|
76 |
++cnt; |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
assertEquals(cnt, 1, "Expected only one listed line"); |
|
81 |
}) |
|
82 |
); |
|
83 |
} |
|
84 |
||
85 |
public void elideStartUpFromSave() throws IOException { |
|
86 |
Compiler compiler = new Compiler(); |
|
87 |
Path path = compiler.getPath("myfile"); |
|
88 |
test( |
|
38613 | 89 |
(a) -> assertCommandOutputContains(a, "123", "==> 123"), |
33362 | 90 |
(a) -> assertCommand(a, "/save " + path.toString(), "") |
91 |
); |
|
92 |
try (Stream<String> lines = Files.lines(path)) { |
|
93 |
assertEquals(lines.count(), 1, "Expected only one saved line"); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
public void testInterrupt() { |
|
98 |
ReplTest interrupt = (a) -> assertCommand(a, "\u0003", ""); |
|
99 |
for (String s : new String[] { "", "\u0003" }) { |
|
100 |
test(false, new String[]{"-nostartup"}, |
|
101 |
(a) -> assertCommand(a, "int a = 2 +" + s, ""), |
|
102 |
interrupt, |
|
103 |
(a) -> assertCommand(a, "int a\u0003", ""), |
|
104 |
(a) -> assertCommand(a, "int a = 2 + 2\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
105 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 106 |
(a) -> evaluateExpression(a, "int", "2", "2"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
107 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 108 |
(a) -> assertCommand(a, "void f() {", ""), |
109 |
(a) -> assertCommand(a, "int q = 10;" + s, ""), |
|
110 |
interrupt, |
|
111 |
(a) -> assertCommand(a, "void f() {}\u0003", ""), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
112 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 113 |
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
114 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 115 |
(a) -> assertCommand(a, "class A {" + s, ""), |
116 |
interrupt, |
|
117 |
(a) -> assertCommand(a, "class A {}\u0003", ""), |
|
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
37389
diff
changeset
|
118 |
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()), |
33362 | 119 |
(a) -> assertClass(a, "interface A {}", "interface", "A"), |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
37389
diff
changeset
|
120 |
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()), |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
121 |
(a) -> assertCommand(a, "import java.util.stream." + s, ""), |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
122 |
interrupt, |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
123 |
(a) -> assertCommand(a, "import java.util.stream.\u0003", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
124 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
125 |
(a) -> assertImport(a, "import java.util.stream.Stream", "", "java.util.stream.Stream"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
126 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 127 |
); |
128 |
} |
|
129 |
} |
|
130 |
||
131 |
private final Object lock = new Object(); |
|
132 |
private PrintWriter out; |
|
133 |
private boolean isStopped; |
|
134 |
private Thread t; |
|
135 |
private void assertStop(boolean after, String cmd, String output) { |
|
136 |
if (!after) { |
|
137 |
isStopped = false; |
|
138 |
StringWriter writer = new StringWriter(); |
|
139 |
out = new PrintWriter(writer); |
|
140 |
setCommandInput(cmd + "\n"); |
|
141 |
t = new Thread(() -> { |
|
142 |
try { |
|
143 |
// no chance to know whether cmd is being evaluated |
|
144 |
Thread.sleep(5000); |
|
145 |
} catch (InterruptedException ignored) { |
|
146 |
} |
|
147 |
int i = 1; |
|
148 |
int n = 30; |
|
149 |
synchronized (lock) { |
|
150 |
do { |
|
151 |
setCommandInput("\u0003"); |
|
152 |
if (!isStopped) { |
|
153 |
out.println("Not stopped. Try again: " + i); |
|
154 |
try { |
|
155 |
lock.wait(1000); |
|
156 |
} catch (InterruptedException ignored) { |
|
157 |
} |
|
158 |
} |
|
159 |
} while (i++ < n && !isStopped); |
|
160 |
if (!isStopped) { |
|
161 |
System.err.println(writer.toString()); |
|
162 |
fail("Evaluation was not stopped: '" + cmd + "'"); |
|
163 |
} |
|
164 |
} |
|
165 |
}); |
|
166 |
t.start(); |
|
167 |
} else { |
|
168 |
synchronized (lock) { |
|
169 |
out.println("Evaluation was stopped successfully: '" + cmd + "'"); |
|
170 |
isStopped = true; |
|
171 |
lock.notify(); |
|
172 |
} |
|
173 |
try { |
|
174 |
t.join(); |
|
175 |
t = null; |
|
176 |
} catch (InterruptedException ignored) { |
|
177 |
} |
|
178 |
assertOutput(getCommandOutput(), "", "command"); |
|
179 |
assertOutput(getCommandErrorOutput(), "", "command error"); |
|
37389 | 180 |
assertOutput(getUserOutput().trim(), output, "user"); |
33362 | 181 |
assertOutput(getUserErrorOutput(), "", "user error"); |
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public void testStop() { |
|
186 |
test( |
|
38613 | 187 |
(a) -> assertStop(a, "while (true) {}", ""), |
188 |
(a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "") |
|
33362 | 189 |
); |
190 |
} |
|
191 |
||
192 |
public void testRerun() { |
|
193 |
test(false, new String[] {"-nostartup"}, |
|
37389 | 194 |
(a) -> assertCommand(a, "/0", "| No such command or snippet id: /0\n| Type /help for help."), |
195 |
(a) -> assertCommand(a, "/5", "| No such command or snippet id: /5\n| Type /help for help.") |
|
33362 | 196 |
); |
197 |
String[] codes = new String[] { |
|
198 |
"int a = 0;", // var |
|
199 |
"class A {}", // class |
|
200 |
"void f() {}", // method |
|
201 |
"bool b;", // active failed |
|
202 |
"void g() { h(); }", // active corralled |
|
203 |
}; |
|
204 |
List<ReplTest> tests = new ArrayList<>(); |
|
205 |
for (String s : codes) { |
|
206 |
tests.add((a) -> assertCommand(a, s, null)); |
|
207 |
} |
|
38613 | 208 |
// Test /1 through /5 -- assure references are correct |
33362 | 209 |
for (int i = 0; i < codes.length; ++i) { |
210 |
final int finalI = i; |
|
211 |
Consumer<String> check = (s) -> { |
|
212 |
String[] ss = s.split("\n"); |
|
213 |
assertEquals(ss[0], codes[finalI]); |
|
214 |
assertTrue(ss.length > 1, s); |
|
215 |
}; |
|
216 |
tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check)); |
|
217 |
} |
|
38613 | 218 |
// Test /-1 ... note that the snippets added by history must be stepped over |
33362 | 219 |
for (int i = 0; i < codes.length; ++i) { |
220 |
final int finalI = i; |
|
221 |
Consumer<String> check = (s) -> { |
|
222 |
String[] ss = s.split("\n"); |
|
223 |
assertEquals(ss[0], codes[codes.length - finalI - 1]); |
|
224 |
assertTrue(ss.length > 1, s); |
|
225 |
}; |
|
38613 | 226 |
tests.add((a) -> assertCommandCheckOutput(a, "/-" + (2 * finalI + 1), check)); |
33362 | 227 |
} |
38613 | 228 |
tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("int a = 0;"))); |
33362 | 229 |
test(false, new String[]{"-nostartup"}, |
230 |
tests.toArray(new ReplTest[tests.size()])); |
|
231 |
} |
|
232 |
||
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
233 |
public void test8142447() { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
234 |
Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) -> |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
235 |
(a) -> assertCommandCheckOutput(a, cmd, s -> { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
236 |
String[] ss = s.split("\n"); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
237 |
assertEquals(ss[0], code); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
238 |
loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount)); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
239 |
}); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
240 |
ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables()); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
241 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
242 |
Compiler compiler = new Compiler(); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
243 |
Path startup = compiler.getPath("StartupFileOption/startup.txt"); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
244 |
compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1 |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
245 |
"void add(int n) { assertionCount += n; }"); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
246 |
test(new String[]{"-startup", startup.toString()}, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
247 |
(a) -> assertCommand(a, "add(1)", ""), // id: 1 |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
248 |
(a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "| Error:")), // id: e1 |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
249 |
(a) -> assertVariable(a, "int", "ONE", "1", "1"), |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
250 |
assertRerun.apply("/1").apply("add(1)", 2), assertVariables, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
251 |
assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables, |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
252 |
assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
253 |
); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
254 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
255 |
test(false, new String[] {"-nostartup"}, |
37389 | 256 |
(a) -> assertCommand(a, "/s1", "| No such command or snippet id: /s1\n| Type /help for help."), |
257 |
(a) -> assertCommand(a, "/1", "| No such command or snippet id: /1\n| Type /help for help."), |
|
258 |
(a) -> assertCommand(a, "/e1", "| No such command or snippet id: /e1\n| Type /help for help.") |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
259 |
); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
260 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
261 |
|
33362 | 262 |
public void testClasspathDirectory() { |
263 |
Compiler compiler = new Compiler(); |
|
264 |
Path outDir = Paths.get("testClasspathDirectory"); |
|
265 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
266 |
Path classpath = compiler.getPath(outDir); |
|
267 |
test( |
|
37389 | 268 |
(a) -> assertCommand(a, "/classpath " + classpath, String.format("| Path '%s' added to classpath", classpath)), |
269 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A") |
|
33362 | 270 |
); |
271 |
test(new String[] { "-cp", classpath.toString() }, |
|
37389 | 272 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A") |
33362 | 273 |
); |
274 |
test(new String[] { "-classpath", classpath.toString() }, |
|
37389 | 275 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A") |
33362 | 276 |
); |
277 |
} |
|
278 |
||
279 |
public void testClasspathJar() { |
|
280 |
Compiler compiler = new Compiler(); |
|
281 |
Path outDir = Paths.get("testClasspathJar"); |
|
282 |
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }"); |
|
283 |
String jarName = "test.jar"; |
|
284 |
compiler.jar(outDir, jarName, "pkg/A.class"); |
|
285 |
Path jarPath = compiler.getPath(outDir).resolve(jarName); |
|
286 |
test( |
|
37389 | 287 |
(a) -> assertCommand(a, "/classpath " + jarPath, String.format("| Path '%s' added to classpath", jarPath)), |
288 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A") |
|
33362 | 289 |
); |
290 |
test(new String[] { "-cp", jarPath.toString() }, |
|
37389 | 291 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A") |
33362 | 292 |
); |
293 |
test(new String[] { "-classpath", jarPath.toString() }, |
|
37389 | 294 |
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A") |
33362 | 295 |
); |
296 |
} |
|
297 |
||
298 |
public void testStartupFileOption() { |
|
299 |
try { |
|
300 |
Compiler compiler = new Compiler(); |
|
301 |
Path startup = compiler.getPath("StartupFileOption/startup.txt"); |
|
302 |
compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }"); |
|
303 |
test(new String[]{"-startup", startup.toString()}, |
|
37389 | 304 |
(a) -> evaluateExpression(a, "A", "new A()", "A") |
33362 | 305 |
); |
306 |
test(new String[]{"-nostartup"}, |
|
307 |
(a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("| Error:\n| cannot find symbol")) |
|
308 |
); |
|
38613 | 309 |
test( |
310 |
(a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", "") |
|
311 |
); |
|
33362 | 312 |
} finally { |
313 |
removeStartup(); |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
public void testLoadingFromArgs() { |
|
318 |
Compiler compiler = new Compiler(); |
|
319 |
Path path = compiler.getPath("loading.repl"); |
|
320 |
compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;"); |
|
321 |
test(new String[] { path.toString() }, |
|
37389 | 322 |
(a) -> assertCommand(a, "x", "x ==> 20.0"), |
323 |
(a) -> assertCommand(a, "a", "a ==> 10.0") |
|
33362 | 324 |
); |
325 |
Path unknown = compiler.getPath("UNKNOWN.jar"); |
|
36990 | 326 |
test(Locale.ROOT, true, new String[]{unknown.toString()}, |
38613 | 327 |
"| File '" + unknown |
328 |
+ "' for 'jshell' is not found."); |
|
33362 | 329 |
} |
330 |
||
331 |
public void testReset() { |
|
332 |
test( |
|
37389 | 333 |
(a) -> assertReset(a, "/res"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
334 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 335 |
(a) -> assertVariable(a, "int", "x"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
336 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
33362 | 337 |
(a) -> assertMethod(a, "void f() { }", "()void", "f"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
338 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
33362 | 339 |
(a) -> assertClass(a, "class A { }", "class", "A"), |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
37389
diff
changeset
|
340 |
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()), |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
341 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
342 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()), |
33362 | 343 |
(a) -> assertReset(a, "/reset"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
344 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
345 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
37389
diff
changeset
|
346 |
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
347 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 348 |
); |
349 |
} |
|
350 |
||
351 |
public void testOpen() { |
|
352 |
Compiler compiler = new Compiler(); |
|
353 |
Path path = compiler.getPath("testOpen.repl"); |
|
354 |
compiler.writeToFile(path, |
|
355 |
"int a = 10;\ndouble x = 20;\ndouble a = 10;\n" + |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
356 |
"class A { public String toString() { return \"A\"; } }\nimport java.util.stream.*;"); |
33362 | 357 |
for (String s : new String[]{"/o", "/open"}) { |
358 |
test( |
|
359 |
(a) -> assertCommand(a, s + " " + path.toString(), ""), |
|
37389 | 360 |
(a) -> assertCommand(a, "a", "a ==> 10.0"), |
361 |
(a) -> evaluateExpression(a, "A", "new A();", "A"), |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
362 |
(a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"), |
33362 | 363 |
(a) -> { |
364 |
loadVariable(a, "double", "x", "20.0", "20.0"); |
|
365 |
loadVariable(a, "double", "a", "10.0", "10.0"); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
366 |
loadVariable(a, "A", "$7", "new A();", "A"); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
367 |
loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1"); |
33362 | 368 |
loadClass(a, "class A { public String toString() { return \"A\"; } }", |
369 |
"class", "A"); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
370 |
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*"); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
37389
diff
changeset
|
371 |
assertCommandCheckOutput(a, "/types", assertClasses()); |
33362 | 372 |
}, |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
373 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
374 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
375 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 376 |
); |
377 |
Path unknown = compiler.getPath("UNKNOWN.repl"); |
|
378 |
test( |
|
379 |
(a) -> assertCommand(a, s + " " + unknown, |
|
37389 | 380 |
"| File '" + unknown + "' for '/open' is not found.") |
33362 | 381 |
); |
382 |
} |
|
383 |
} |
|
384 |
||
385 |
public void testSave() throws IOException { |
|
386 |
Compiler compiler = new Compiler(); |
|
387 |
Path path = compiler.getPath("testSave.repl"); |
|
388 |
List<String> list = Arrays.asList( |
|
389 |
"int a;", |
|
390 |
"class A { public String toString() { return \"A\"; } }" |
|
391 |
); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
392 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
393 |
(a) -> assertVariable(a, "int", "a"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
394 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
395 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
396 |
(a) -> assertCommand(a, "/save " + path.toString(), "") |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
397 |
); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
398 |
assertEquals(Files.readAllLines(path), list); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
399 |
{ |
33362 | 400 |
List<String> output = new ArrayList<>(); |
401 |
test( |
|
402 |
(a) -> assertCommand(a, "int a;", null), |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
403 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
33362 | 404 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
405 |
(a) -> assertCommandCheckOutput(a, "/list -all", (out) -> |
33362 | 406 |
output.addAll(Stream.of(out.split("\n")) |
407 |
.filter(str -> !str.isEmpty()) |
|
408 |
.map(str -> str.substring(str.indexOf(':') + 2)) |
|
409 |
.filter(str -> !str.startsWith("/")) |
|
410 |
.collect(Collectors.toList()))), |
|
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
411 |
(a) -> assertCommand(a, "/save -all " + path.toString(), "") |
33362 | 412 |
); |
413 |
assertEquals(Files.readAllLines(path), output); |
|
414 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
415 |
List<String> output = new ArrayList<>(); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
416 |
test( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
417 |
(a) -> assertVariable(a, "int", "a"), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
418 |
(a) -> assertCommand(a, "()", null, null, null, "", ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
419 |
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
420 |
(a) -> assertCommandCheckOutput(a, "/history", (out) -> |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
421 |
output.addAll(Stream.of(out.split("\n")) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
422 |
.filter(str -> !str.isEmpty()) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
423 |
.collect(Collectors.toList()))), |
38613 | 424 |
(a) -> assertCommand(a, "/save -history " + path.toString(), "") |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
425 |
); |
38613 | 426 |
output.add("/save -history " + path.toString()); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
427 |
assertEquals(Files.readAllLines(path), output); |
33362 | 428 |
} |
429 |
||
38613 | 430 |
public void testStartRetain() throws BackingStoreException { |
33362 | 431 |
try { |
432 |
Compiler compiler = new Compiler(); |
|
433 |
Path startUpFile = compiler.getPath("startUp.txt"); |
|
434 |
test( |
|
435 |
(a) -> assertVariable(a, "int", "a"), |
|
436 |
(a) -> assertVariable(a, "double", "b", "10", "10.0"), |
|
437 |
(a) -> assertMethod(a, "void f() {}", "()V", "f"), |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
438 |
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
439 |
(a) -> assertCommand(a, "/save " + startUpFile.toString(), null), |
38613 | 440 |
(a) -> assertCommand(a, "/retain start " + startUpFile.toString(), null) |
33362 | 441 |
); |
442 |
Path unknown = compiler.getPath("UNKNOWN"); |
|
443 |
test( |
|
38613 | 444 |
(a) -> assertCommandOutputStartsWith(a, "/retain start " + unknown.toString(), |
445 |
"| File '" + unknown + "' for '/retain start' is not found.") |
|
33362 | 446 |
); |
447 |
test(false, new String[0], |
|
448 |
(a) -> { |
|
449 |
loadVariable(a, "int", "a"); |
|
450 |
loadVariable(a, "double", "b", "10.0", "10.0"); |
|
451 |
loadMethod(a, "void f() {}", "()void", "f"); |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
452 |
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*"); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
37389
diff
changeset
|
453 |
assertCommandCheckOutput(a, "/types", assertClasses()); |
33362 | 454 |
}, |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
455 |
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
456 |
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
457 |
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()) |
33362 | 458 |
); |
459 |
} finally { |
|
460 |
removeStartup(); |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
private void removeStartup() { |
|
36494 | 465 |
Preferences preferences = Preferences.userRoot().node("tool/JShell"); |
33362 | 466 |
if (preferences != null) { |
467 |
preferences.remove("STARTUP"); |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
public void testStartSave() throws IOException { |
|
472 |
Compiler compiler = new Compiler(); |
|
473 |
Path startSave = compiler.getPath("startSave.txt"); |
|
38520
17e72b872ffd
8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents:
38514
diff
changeset
|
474 |
test(a -> assertCommand(a, "/save -start " + startSave.toString(), null)); |
33362 | 475 |
List<String> lines = Files.lines(startSave) |
476 |
.filter(s -> !s.isEmpty()) |
|
477 |
.collect(Collectors.toList()); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34481
diff
changeset
|
478 |
assertEquals(lines, START_UP); |
33362 | 479 |
} |
480 |
||
481 |
public void testConstrainedUpdates() { |
|
482 |
test( |
|
483 |
a -> assertClass(a, "class XYZZY { }", "class", "XYZZY"), |
|
484 |
a -> assertVariable(a, "XYZZY", "xyzzy"), |
|
485 |
a -> assertCommandCheckOutput(a, "import java.util.stream.*", |
|
486 |
(out) -> assertTrue(out.trim().isEmpty(), "Expected no output, got: " + out)) |
|
487 |
); |
|
488 |
} |
|
489 |
||
490 |
public void testRemoteExit() { |
|
491 |
test( |
|
492 |
a -> assertVariable(a, "int", "x"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
493 |
a -> assertCommandCheckOutput(a, "/vars", assertVariables()), |
36494 | 494 |
a -> assertCommandOutputContains(a, "System.exit(5);", "terminated"), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
495 |
a -> assertCommandCheckOutput(a, "/vars", s -> |
33362 | 496 |
assertTrue(s.trim().isEmpty(), s)), |
497 |
a -> assertMethod(a, "void f() { }", "()void", "f"), |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
498 |
a -> assertCommandCheckOutput(a, "/methods", assertMethods()) |
33362 | 499 |
); |
500 |
} |
|
501 |
||
502 |
public void testFeedbackNegative() { |
|
36494 | 503 |
test(a -> assertCommandCheckOutput(a, "/set feedback aaaa", |
504 |
assertStartsWith("| Does not match any current feedback mode"))); |
|
33362 | 505 |
} |
506 |
||
37389 | 507 |
public void testFeedbackSilent() { |
508 |
for (String off : new String[]{"s", "silent"}) { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
509 |
test( |
36494 | 510 |
a -> assertCommand(a, "/set feedback " + off, ""), |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
511 |
a -> assertCommand(a, "int a", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
512 |
a -> assertCommand(a, "void f() {}", ""), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
513 |
a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("| Error:")), |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
514 |
a -> assertCommandCheckOutput(a, "public void f() {}", assertStartsWith("| Warning:")) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
515 |
); |
33362 | 516 |
} |
517 |
} |
|
518 |
||
519 |
public void testFeedbackNormal() { |
|
520 |
Compiler compiler = new Compiler(); |
|
521 |
Path testNormalFile = compiler.getPath("testConciseNormal"); |
|
522 |
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"}; |
|
523 |
String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"}; |
|
524 |
String[] output = new String[] { |
|
37389 | 525 |
"a ==> 0", |
526 |
"| created method f()", |
|
527 |
"| created class A", |
|
528 |
"a ==> 10" |
|
33362 | 529 |
}; |
530 |
compiler.writeToFile(testNormalFile, sources2); |
|
37389 | 531 |
for (String feedback : new String[]{"/set fe", "/set feedback"}) { |
532 |
for (String feedbackState : new String[]{"n", "normal"}) { |
|
33362 | 533 |
test( |
37389 | 534 |
a -> assertCommand(a, feedback + " " + feedbackState, "| Feedback mode: normal"), |
33362 | 535 |
a -> assertCommand(a, sources[0], output[0]), |
536 |
a -> assertCommand(a, sources[1], output[1]), |
|
537 |
a -> assertCommand(a, sources[2], output[2]), |
|
538 |
a -> assertCommand(a, sources[3], output[3]), |
|
36494 | 539 |
a -> assertCommand(a, "/o " + testNormalFile.toString(), "") |
33362 | 540 |
); |
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
public void testHistoryReference() { |
|
546 |
test(false, new String[]{"-nostartup"}, |
|
547 |
a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"), |
|
548 |
a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"), |
|
37389 | 549 |
a -> assertCommand(a, "/-2", "System.err.println(1)", "", null, "", "1\n"), |
550 |
a -> assertCommand(a, "/history", |
|
33362 | 551 |
"/debug 0\n" + |
552 |
"System.err.println(1)\n" + |
|
553 |
"System.err.println(2)\n" + |
|
554 |
"System.err.println(1)\n" + |
|
555 |
"/history\n"), |
|
37389 | 556 |
a -> assertCommand(a, "/-2", "System.err.println(2)", "", null, "", "2\n"), |
557 |
a -> assertCommand(a, "/!", "System.err.println(2)", "", null, "", "2\n"), |
|
558 |
a -> assertCommand(a, "/2", "System.err.println(2)", "", null, "", "2\n"), |
|
559 |
a -> assertCommand(a, "/1", "System.err.println(1)", "", null, "", "1\n") |
|
33362 | 560 |
); |
561 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33918
diff
changeset
|
562 |
|
38836
b09d1cfbf28c
8131029: JShell: recover from VMConnection launch failure
rfield
parents:
38613
diff
changeset
|
563 |
@Test(enabled = false) // TODO 8158197 |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
564 |
public void testHeadlessEditPad() { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
565 |
String prevHeadless = System.getProperty("java.awt.headless"); |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
566 |
try { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
567 |
System.setProperty("java.awt.headless", "true"); |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
568 |
test( |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
569 |
(a) -> assertCommandOutputStartsWith(a, "/edit printf", "| Cannot launch editor -- unexpected exception:") |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
570 |
); |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
571 |
} finally { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
572 |
System.setProperty("java.awt.headless", prevHeadless==null? "false" : prevHeadless); |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
573 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
574 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
575 |
|
33362 | 576 |
} |