1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
|
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304
* @summary Tests for Basic tests for REPL tool
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.javap
* jdk.jshell/jdk.internal.jshell.tool
* @library /tools/lib
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
* @build KullaTesting TestingInputStream Compiler
* @run testng/timeout=600 ToolBasicTest
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@Test
public class ToolBasicTest extends ReplToolTesting {
public void elideStartUpFromList() {
test(
(a) -> assertCommandOutputContains(a, "123", "==> 123"),
(a) -> assertCommandCheckOutput(a, "/list", (s) -> {
int cnt;
try (Scanner scanner = new Scanner(s)) {
cnt = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (!line.trim().isEmpty()) {
++cnt;
}
}
}
assertEquals(cnt, 1, "Expected only one listed line");
})
);
}
public void elideStartUpFromSave() throws IOException {
Compiler compiler = new Compiler();
Path path = compiler.getPath("myfile");
test(
(a) -> assertCommandOutputContains(a, "123", "==> 123"),
(a) -> assertCommand(a, "/save " + path.toString(), "")
);
try (Stream<String> lines = Files.lines(path)) {
assertEquals(lines.count(), 1, "Expected only one saved line");
}
}
public void testInterrupt() {
ReplTest interrupt = (a) -> assertCommand(a, "\u0003", "");
for (String s : new String[] { "", "\u0003" }) {
test(false, new String[]{"--no-startup"},
(a) -> assertCommand(a, "int a = 2 +" + s, ""),
interrupt,
(a) -> assertCommand(a, "int a\u0003", ""),
(a) -> assertCommand(a, "int a = 2 + 2\u0003", ""),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> evaluateExpression(a, "int", "2", "2"),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertCommand(a, "void f() {", ""),
(a) -> assertCommand(a, "int q = 10;" + s, ""),
interrupt,
(a) -> assertCommand(a, "void f() {}\u0003", ""),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertCommand(a, "class A {" + s, ""),
interrupt,
(a) -> assertCommand(a, "class A {}\u0003", ""),
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
(a) -> assertClass(a, "interface A {}", "interface", "A"),
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
(a) -> assertCommand(a, "import java.util.stream." + s, ""),
interrupt,
(a) -> assertCommand(a, "import java.util.stream.\u0003", ""),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertImport(a, "import java.util.stream.Stream", "", "java.util.stream.Stream"),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
}
private final Object lock = new Object();
private PrintWriter out;
private boolean isStopped;
private Thread t;
private void assertStop(boolean after, String cmd, String output) {
if (!after) {
isStopped = false;
StringWriter writer = new StringWriter();
out = new PrintWriter(writer);
setCommandInput(cmd + "\n");
t = new Thread(() -> {
try {
// no chance to know whether cmd is being evaluated
Thread.sleep(5000);
} catch (InterruptedException ignored) {
}
int i = 1;
int n = 30;
synchronized (lock) {
do {
setCommandInput("\u0003");
if (!isStopped) {
out.println("Not stopped. Try again: " + i);
try {
lock.wait(1000);
} catch (InterruptedException ignored) {
}
}
} while (i++ < n && !isStopped);
if (!isStopped) {
System.err.println(writer.toString());
fail("Evaluation was not stopped: '" + cmd + "'");
}
}
});
t.start();
} else {
synchronized (lock) {
out.println("Evaluation was stopped successfully: '" + cmd + "'");
isStopped = true;
lock.notify();
}
try {
t.join();
t = null;
} catch (InterruptedException ignored) {
}
assertOutput(getCommandOutput(), "", "command");
assertOutput(getCommandErrorOutput(), "", "command error");
assertOutput(getUserOutput().trim(), output, "user");
assertOutput(getUserErrorOutput(), "", "user error");
}
}
public void testStop() {
test(
(a) -> assertStop(a, "while (true) {}", ""),
(a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "")
);
}
public void testRerun() {
test(false, new String[] {"--no-startup"},
(a) -> assertCommand(a, "/0", "| No such command or snippet id: /0\n| Type /help for help."),
(a) -> assertCommand(a, "/5", "| No such command or snippet id: /5\n| Type /help for help.")
);
String[] codes = new String[] {
"int a = 0;", // var
"class A {}", // class
"void f() {}", // method
"bool b;", // active failed
"void g() { h(); }", // active corralled
};
List<ReplTest> tests = new ArrayList<>();
for (String s : codes) {
tests.add((a) -> assertCommand(a, s, null));
}
// Test /1 through /5 -- assure references are correct
for (int i = 0; i < codes.length; ++i) {
final int finalI = i;
Consumer<String> check = (s) -> {
String[] ss = s.split("\n");
assertEquals(ss[0], codes[finalI]);
assertTrue(ss.length > 1, s);
};
tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check));
}
// Test /-1 ... note that the snippets added by history must be stepped over
for (int i = 0; i < codes.length; ++i) {
final int finalI = i;
Consumer<String> check = (s) -> {
String[] ss = s.split("\n");
assertEquals(ss[0], codes[codes.length - finalI - 1]);
assertTrue(ss.length > 1, s);
};
tests.add((a) -> assertCommandCheckOutput(a, "/-" + (2 * finalI + 1), check));
}
tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("int a = 0;")));
test(false, new String[]{"--no-startup"},
tests.toArray(new ReplTest[tests.size()]));
}
public void test8142447() {
Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) ->
(a) -> assertCommandCheckOutput(a, cmd, s -> {
String[] ss = s.split("\n");
assertEquals(ss[0], code);
loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount));
});
ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables());
Compiler compiler = new Compiler();
Path startup = compiler.getPath("StartupFileOption/startup.txt");
compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1
"void add(int n) { assertionCount += n; }");
test(new String[]{"--startup", startup.toString()},
(a) -> assertCommand(a, "add(1)", ""), // id: 1
(a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "| Error:")), // id: e1
(a) -> assertVariable(a, "int", "ONE", "1", "1"),
assertRerun.apply("/1").apply("add(1)", 2), assertVariables,
assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables,
assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables
);
test(false, new String[] {"--no-startup"},
(a) -> assertCommand(a, "/s1", "| No such command or snippet id: /s1\n| Type /help for help."),
(a) -> assertCommand(a, "/1", "| No such command or snippet id: /1\n| Type /help for help."),
(a) -> assertCommand(a, "/e1", "| No such command or snippet id: /e1\n| Type /help for help.")
);
}
public void testClasspathDirectory() {
Compiler compiler = new Compiler();
Path outDir = Paths.get("testClasspathDirectory");
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
Path classpath = compiler.getPath(outDir);
test(
(a) -> assertCommand(a, "/env --class-path " + classpath,
"| Setting new options and restoring state."),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
test(new String[] { "--class-path", classpath.toString() },
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
}
public void testEnvInStartUp() {
Compiler compiler = new Compiler();
Path outDir = Paths.get("testClasspathDirectory");
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
Path classpath = compiler.getPath(outDir);
Path sup = compiler.getPath("startup.jsh");
compiler.writeToFile(sup,
"int xxx;\n" +
"/env -class-path " + classpath + "\n" +
"int aaa = 735;\n"
);
test(
(a) -> assertCommand(a, "/set start -retain " + sup, ""),
(a) -> assertCommand(a, "/reset",
"| Resetting state."),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A"),
(a) -> assertCommand(a, "aaa", "aaa ==> 735")
);
test(
(a) -> assertCommandOutputContains(a, "/env", "--class-path"),
(a) -> assertCommandOutputContains(a, "xxx", "cannot find symbol", "variable xxx"),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A"),
(a) -> assertCommand(a, "aaa", "aaa ==> 735")
);
}
private String makeSimpleJar() {
Compiler compiler = new Compiler();
Path outDir = Paths.get("testClasspathJar");
compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
String jarName = "test.jar";
compiler.jar(outDir, jarName, "pkg/A.class");
return compiler.getPath(outDir).resolve(jarName).toString();
}
public void testClasspathJar() {
String jarPath = makeSimpleJar();
test(
(a) -> assertCommand(a, "/env --class-path " + jarPath,
"| Setting new options and restoring state."),
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
test(new String[] { "--class-path", jarPath },
(a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
);
}
public void testClasspathUserHomeExpansion() {
String jarPath = makeSimpleJar();
String tilde = "~" + File.separator;
test(
(a) -> assertCommand(a, "/env --class-path " + tilde + "forblato",
"| File '" + System.getProperty("user.home") + File.separator
+ "forblato' for '--class-path' is not found."),
(a) -> assertCommand(a, "/env --class-path " + jarPath + File.pathSeparator
+ tilde + "forblato",
"| File '" + System.getProperty("user.home") + File.separator
+ "forblato' for '--class-path' is not found.")
);
}
public void testBadClasspath() {
String jarPath = makeSimpleJar();
Compiler compiler = new Compiler();
Path t1 = compiler.getPath("whatever/thing.zip");
compiler.writeToFile(t1, "");
Path t2 = compiler.getPath("whatever/thing.jmod");
compiler.writeToFile(t2, "");
test(
(a) -> assertCommand(a, "/env --class-path " + t1.toString(),
"| Invalid '--class-path' argument: " + t1.toString()),
(a) -> assertCommand(a, "/env --class-path " + jarPath + File.pathSeparator + t1.toString(),
"| Invalid '--class-path' argument: " + t1.toString()),
(a) -> assertCommand(a, "/env --class-path " + t2.toString(),
"| Invalid '--class-path' argument: " + t2.toString())
);
}
public void testModulePath() {
Compiler compiler = new Compiler();
Path modsDir = Paths.get("mods");
Path outDir = Paths.get("mods", "org.astro");
compiler.compile(outDir, "package org.astro; public class World { public static String name() { return \"world\"; } }");
compiler.compile(outDir, "module org.astro { exports org.astro; }");
Path modsPath = compiler.getPath(modsDir);
test(new String[] { "--module-path", modsPath.toString(), "--add-modules", "org.astro" },
(a) -> assertCommand(a, "import org.astro.World;", ""),
(a) -> evaluateExpression(a, "String",
"String.format(\"Greetings %s!\", World.name());",
"\"Greetings world!\"")
);
}
public void testModulePathUserHomeExpansion() {
String tilde = "~" + File.separatorChar;
test(
(a) -> assertCommand(a, "/env --module-path " + tilde + "snardugol",
"| File '" + System.getProperty("user.home")
+ File.separatorChar + "snardugol' for '--module-path' is not found.")
);
}
public void testBadModulePath() {
Compiler compiler = new Compiler();
Path t1 = compiler.getPath("whatever/thing.zip");
compiler.writeToFile(t1, "");
test(
(a) -> assertCommand(a, "/env --module-path " + t1.toString(),
"| Invalid '--module-path' argument: " + t1.toString())
);
}
public void testStartupFileOption() {
Compiler compiler = new Compiler();
Path startup = compiler.getPath("StartupFileOption/startup.txt");
compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }");
test(new String[]{"--startup", startup.toString()},
(a) -> evaluateExpression(a, "A", "new A()", "A")
);
test(new String[]{"--no-startup"},
(a) -> assertCommandCheckOutput(a, "Pattern.compile(\"x+\")", assertStartsWith("| Error:\n| cannot find symbol"))
);
test(
(a) -> assertCommand(a, "Pattern.compile(\"x+\")", "$1 ==> x+", "", null, "", "")
);
}
public void testLoadingFromArgs() {
Compiler compiler = new Compiler();
Path path = compiler.getPath("loading.repl");
compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;");
test(new String[] { path.toString() },
(a) -> assertCommand(a, "x", "x ==> 20.0"),
(a) -> assertCommand(a, "a", "a ==> 10.0")
);
Path unknown = compiler.getPath("UNKNOWN.jar");
test(Locale.ROOT, true, new String[]{unknown.toString()},
"| File '" + unknown
+ "' for 'jshell' is not found.");
}
public void testReset() {
test(
(a) -> assertReset(a, "/res"),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertVariable(a, "int", "x"),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertMethod(a, "void f() { }", "()void", "f"),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertClass(a, "class A { }", "class", "A"),
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
(a) -> assertReset(a, "/reset"),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
public void testOpen() {
Compiler compiler = new Compiler();
Path path = compiler.getPath("testOpen.repl");
compiler.writeToFile(path,
"int a = 10;\ndouble x = 20;\ndouble a = 10;\n" +
"class A { public String toString() { return \"A\"; } }\nimport java.util.stream.*;");
for (String s : new String[]{"/o", "/open"}) {
test(
(a) -> assertCommand(a, s + " " + path.toString(), ""),
(a) -> assertCommand(a, "a", "a ==> 10.0"),
(a) -> evaluateExpression(a, "A", "new A();", "A"),
(a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"),
(a) -> {
loadVariable(a, "double", "x", "20.0", "20.0");
loadVariable(a, "double", "a", "10.0", "10.0");
loadVariable(a, "A", "$7", "new A();", "A");
loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1");
loadClass(a, "class A { public String toString() { return \"A\"; } }",
"class", "A");
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
assertCommandCheckOutput(a, "/types", assertClasses());
},
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports())
);
Path unknown = compiler.getPath("UNKNOWN.repl");
test(
(a) -> assertCommand(a, s + " " + unknown,
"| File '" + unknown + "' for '/open' is not found.")
);
}
}
public void testOpenResource() {
test(
(a) -> assertCommand(a, "/open PRINTING", ""),
(a) -> assertCommandOutputContains(a, "/list",
"void println", "System.out.printf"),
(a) -> assertCommand(a, "printf(\"%4.2f\", Math.PI)",
"", "", null, "3.14", "")
);
}
public void testSave() throws IOException {
Compiler compiler = new Compiler();
Path path = compiler.getPath("testSave.repl");
List<String> list = Arrays.asList(
"int a;",
"class A { public String toString() { return \"A\"; } }"
);
test(
(a) -> assertVariable(a, "int", "a"),
(a) -> assertCommand(a, "()", null, null, null, "", ""),
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
(a) -> assertCommand(a, "/save " + path.toString(), "")
);
assertEquals(Files.readAllLines(path), list);
{
List<String> output = new ArrayList<>();
test(
(a) -> assertCommand(a, "int a;", null),
(a) -> assertCommand(a, "()", null, null, null, "", ""),
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
(a) -> assertCommandCheckOutput(a, "/list -all", (out) ->
output.addAll(Stream.of(out.split("\n"))
.filter(str -> !str.isEmpty())
.map(str -> str.substring(str.indexOf(':') + 2))
.filter(str -> !str.startsWith("/"))
.collect(Collectors.toList()))),
(a) -> assertCommand(a, "/save -all " + path.toString(), "")
);
assertEquals(Files.readAllLines(path), output);
}
List<String> output = new ArrayList<>();
test(
(a) -> assertVariable(a, "int", "a"),
(a) -> assertCommand(a, "()", null, null, null, "", ""),
(a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
(a) -> assertCommandCheckOutput(a, "/history", (out) ->
output.addAll(Stream.of(out.split("\n"))
.filter(str -> !str.isEmpty())
.collect(Collectors.toList()))),
(a) -> assertCommand(a, "/save -history " + path.toString(), "")
);
output.add("/save -history " + path.toString());
assertEquals(Files.readAllLines(path), output);
}
public void testStartRetain() {
Compiler compiler = new Compiler();
Path startUpFile = compiler.getPath("startUp.txt");
test(
(a) -> assertVariable(a, "int", "a"),
(a) -> assertVariable(a, "double", "b", "10", "10.0"),
(a) -> assertMethod(a, "void f() {}", "()V", "f"),
(a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
(a) -> assertCommand(a, "/save " + startUpFile.toString(), null),
(a) -> assertCommand(a, "/set start -retain " + startUpFile.toString(), null)
);
Path unknown = compiler.getPath("UNKNOWN");
test(
(a) -> assertCommandOutputStartsWith(a, "/set start -retain " + unknown.toString(),
"| File '" + unknown + "' for '/set start' is not found.")
);
test(false, new String[0],
(a) -> {
loadVariable(a, "int", "a");
loadVariable(a, "double", "b", "10.0", "10.0");
loadMethod(a, "void f() {}", "()void", "f");
loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
assertCommandCheckOutput(a, "/types", assertClasses());
},
(a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
(a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
(a) -> assertCommandCheckOutput(a, "/imports", assertImports())
);
}
public void testStartSave() throws IOException {
Compiler compiler = new Compiler();
Path startSave = compiler.getPath("startSave.txt");
test(a -> assertCommand(a, "/save -start " + startSave.toString(), null));
List<String> lines = Files.lines(startSave)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
assertEquals(lines, START_UP);
}
public void testConstrainedUpdates() {
test(
a -> assertClass(a, "class XYZZY { }", "class", "XYZZY"),
a -> assertVariable(a, "XYZZY", "xyzzy"),
a -> assertCommandCheckOutput(a, "import java.util.stream.*",
(out) -> assertTrue(out.trim().isEmpty(), "Expected no output, got: " + out))
);
}
public void testRemoteExit() {
test(
a -> assertVariable(a, "int", "x"),
a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a -> assertCommandOutputContains(a, "System.exit(5);", "terminated"),
a -> assertCommandCheckOutput(a, "/vars", s ->
assertTrue(s.trim().isEmpty(), s)),
a -> assertMethod(a, "void f() { }", "()void", "f"),
a -> assertCommandCheckOutput(a, "/methods", assertMethods())
);
}
public void testFeedbackNegative() {
test(a -> assertCommandCheckOutput(a, "/set feedback aaaa",
assertStartsWith("| Does not match any current feedback mode")));
}
public void testFeedbackSilent() {
for (String off : new String[]{"s", "silent"}) {
test(
a -> assertCommand(a, "/set feedback " + off, ""),
a -> assertCommand(a, "int a", ""),
a -> assertCommand(a, "void f() {}", ""),
a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("| Error:")),
a -> assertCommandCheckOutput(a, "static void f() {}", assertStartsWith("| Warning:"))
);
}
}
public void testFeedbackNormal() {
Compiler compiler = new Compiler();
Path testNormalFile = compiler.getPath("testConciseNormal");
String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"};
String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"};
String[] output = new String[] {
"a ==> 0",
"| created method f()",
"| created class A",
"a ==> 10"
};
compiler.writeToFile(testNormalFile, sources2);
for (String feedback : new String[]{"/set fe", "/set feedback"}) {
for (String feedbackState : new String[]{"n", "normal"}) {
test(
a -> assertCommand(a, feedback + " " + feedbackState, "| Feedback mode: normal"),
a -> assertCommand(a, sources[0], output[0]),
a -> assertCommand(a, sources[1], output[1]),
a -> assertCommand(a, sources[2], output[2]),
a -> assertCommand(a, sources[3], output[3]),
a -> assertCommand(a, "/o " + testNormalFile.toString(), "")
);
}
}
}
public void testVarsWithNotActive() {
test(
a -> assertVariable(a, "Blath", "x"),
a -> assertCommandOutputContains(a, "/var -all", "(not-active)")
);
}
public void testHistoryReference() {
test(false, new String[]{"--no-startup"},
a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"),
a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"),
a -> assertCommand(a, "/-2", "System.err.println(1)", "", null, "", "1\n"),
a -> assertCommand(a, "/history",
"/debug 0\n" +
"System.err.println(1)\n" +
"System.err.println(2)\n" +
"System.err.println(1)\n" +
"/history\n"),
a -> assertCommand(a, "/-2", "System.err.println(2)", "", null, "", "2\n"),
a -> assertCommand(a, "/!", "System.err.println(2)", "", null, "", "2\n"),
a -> assertCommand(a, "/2", "System.err.println(2)", "", null, "", "2\n"),
a -> assertCommand(a, "/1", "System.err.println(1)", "", null, "", "1\n")
);
}
@Test(enabled = false) // TODO 8158197
public void testHeadlessEditPad() {
String prevHeadless = System.getProperty("java.awt.headless");
try {
System.setProperty("java.awt.headless", "true");
test(
(a) -> assertCommandOutputStartsWith(a, "/edit printf", "| Cannot launch editor -- unexpected exception:")
);
} finally {
System.setProperty("java.awt.headless", prevHeadless==null? "false" : prevHeadless);
}
}
public void testAddExports() {
test(false, new String[]{"--no-startup"},
a -> assertCommandOutputStartsWith(a, "import jdk.internal.misc.VM;", "| Error:")
);
test(false, new String[]{"--no-startup",
"-R--add-exports", "-Rjava.base/jdk.internal.misc=ALL-UNNAMED",
"-C--add-exports", "-Cjava.base/jdk.internal.misc=ALL-UNNAMED"},
a -> assertImport(a, "import jdk.internal.misc.VM;", "", "jdk.internal.misc.VM"),
a -> assertCommand(a, "System.err.println(VM.isBooted())", "", "", null, "", "true\n")
);
test(false, new String[]{"--no-startup", "--add-exports", "java.base/jdk.internal.misc"},
a -> assertImport(a, "import jdk.internal.misc.VM;", "", "jdk.internal.misc.VM"),
a -> assertCommand(a, "System.err.println(VM.isBooted())", "", "", null, "", "true\n")
);
}
}
|