test/jdk/jshell/ToolBasicTest.java

Print this page




  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(
  89                 (a) -> assertCommandOutputContains(a, "123", "==> 123"),
  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", ""),
 105                     (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 106                     (a) -> evaluateExpression(a, "int", "2", "2"),
 107                     (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 108                     (a) -> assertCommand(a, "void f() {", ""),
 109                     (a) -> assertCommand(a, "int q = 10;" + s, ""),
 110                     interrupt,
 111                     (a) -> assertCommand(a, "void f() {}\u0003", ""),
 112                     (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 113                     (a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
 114                     (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 115                     (a) -> assertCommand(a, "class A {" + s, ""),
 116                     interrupt,
 117                     (a) -> assertCommand(a, "class A {}\u0003", ""),
 118                     (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
 119                     (a) -> assertClass(a, "interface A {}", "interface", "A"),
 120                     (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),


 173             try {
 174                 t.join();
 175                 t = null;
 176             } catch (InterruptedException ignored) {
 177             }
 178             assertOutput(getCommandOutput(), "", "command");
 179             assertOutput(getCommandErrorOutput(), "", "command error");
 180             assertOutput(getUserOutput().trim(), output, "user");
 181             assertOutput(getUserErrorOutput(), "", "user error");
 182         }
 183     }
 184 
 185     public void testStop() {
 186         test(
 187                 (a) -> assertStop(a, "while (true) {}", ""),
 188                 (a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "")
 189         );
 190     }
 191 
 192     public void testRerun() {
 193         test(false, new String[] {"-nostartup"},
 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.")
 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         }
 208         // Test /1 through /5 -- assure references are correct
 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         }
 218         // Test /-1 ... note that the snippets added by history must be stepped over
 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             };
 226             tests.add((a) -> assertCommandCheckOutput(a, "/-" + (2 * finalI + 1), check));
 227         }
 228         tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("int a = 0;")));
 229         test(false, new String[]{"-nostartup"},
 230                 tests.toArray(new ReplTest[tests.size()]));
 231     }
 232 
 233     public void test8142447() {
 234         Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) ->
 235                 (a) -> assertCommandCheckOutput(a, cmd, s -> {
 236                             String[] ss = s.split("\n");
 237                             assertEquals(ss[0], code);
 238                             loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount));
 239                         });
 240         ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables());
 241 
 242         Compiler compiler = new Compiler();
 243         Path startup = compiler.getPath("StartupFileOption/startup.txt");
 244         compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1
 245                 "void add(int n) { assertionCount += n; }");
 246         test(new String[]{"-startup", startup.toString()},
 247                 (a) -> assertCommand(a, "add(1)", ""), // id: 1
 248                 (a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "|  Error:")), // id: e1
 249                 (a) -> assertVariable(a, "int", "ONE", "1", "1"),
 250                 assertRerun.apply("/1").apply("add(1)", 2), assertVariables,
 251                 assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables,
 252                 assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables
 253         );
 254 
 255         test(false, new String[] {"-nostartup"},
 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.")
 259         );
 260     }
 261 
 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(
 268                 (a) -> assertCommand(a, "/classpath " + classpath, String.format("|  Path '%s' added to classpath", classpath)),
 269                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 270         );
 271         test(new String[] { "-cp", classpath.toString() },
 272                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 273         );
 274         test(new String[] { "-classpath", classpath.toString() },
 275                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 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(
 287                 (a) -> assertCommand(a, "/classpath " + jarPath, String.format("|  Path '%s' added to classpath", jarPath)),
 288                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 289         );
 290         test(new String[] { "-cp", jarPath.toString() },
 291                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 292         );
 293         test(new String[] { "-classpath", jarPath.toString() },
 294                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 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()},
 304                     (a) -> evaluateExpression(a, "A", "new A()", "A")
 305             );
 306             test(new String[]{"-nostartup"},
 307                     (a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("|  Error:\n|  cannot find symbol"))
 308             );
 309             test(
 310                     (a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", "")
 311             );
 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() },
 322                 (a) -> assertCommand(a, "x", "x ==> 20.0"),
 323                 (a) -> assertCommand(a, "a", "a ==> 10.0")
 324         );
 325         Path unknown = compiler.getPath("UNKNOWN.jar");
 326         test(Locale.ROOT, true, new String[]{unknown.toString()},


 526                 "|  created method f()",
 527                 "|  created class A",
 528                 "a ==> 10"
 529         };
 530         compiler.writeToFile(testNormalFile, sources2);
 531         for (String feedback : new String[]{"/set fe", "/set feedback"}) {
 532             for (String feedbackState : new String[]{"n", "normal"}) {
 533                 test(
 534                         a -> assertCommand(a, feedback + " " + feedbackState, "|  Feedback mode: normal"),
 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]),
 539                         a -> assertCommand(a, "/o " + testNormalFile.toString(), "")
 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"),
 549                 a -> assertCommand(a, "/-2", "System.err.println(1)", "", null, "", "1\n"),
 550                 a -> assertCommand(a, "/history",
 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"),
 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")
 560         );
 561     }
 562 
 563     @Test(enabled = false) // TODO 8158197
 564     public void testHeadlessEditPad() {
 565         String prevHeadless = System.getProperty("java.awt.headless");
 566         try {


  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(
  89                 (a) -> assertCommandOutputContains(a, "123", "==> 123"),
  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[]{"--no-startup"},
 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", ""),
 105                     (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 106                     (a) -> evaluateExpression(a, "int", "2", "2"),
 107                     (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 108                     (a) -> assertCommand(a, "void f() {", ""),
 109                     (a) -> assertCommand(a, "int q = 10;" + s, ""),
 110                     interrupt,
 111                     (a) -> assertCommand(a, "void f() {}\u0003", ""),
 112                     (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 113                     (a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
 114                     (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 115                     (a) -> assertCommand(a, "class A {" + s, ""),
 116                     interrupt,
 117                     (a) -> assertCommand(a, "class A {}\u0003", ""),
 118                     (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
 119                     (a) -> assertClass(a, "interface A {}", "interface", "A"),
 120                     (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),


 173             try {
 174                 t.join();
 175                 t = null;
 176             } catch (InterruptedException ignored) {
 177             }
 178             assertOutput(getCommandOutput(), "", "command");
 179             assertOutput(getCommandErrorOutput(), "", "command error");
 180             assertOutput(getUserOutput().trim(), output, "user");
 181             assertOutput(getUserErrorOutput(), "", "user error");
 182         }
 183     }
 184 
 185     public void testStop() {
 186         test(
 187                 (a) -> assertStop(a, "while (true) {}", ""),
 188                 (a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "")
 189         );
 190     }
 191 
 192     public void testRerun() {
 193         test(false, new String[] {"--no-startup"},
 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.")
 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         }
 208         // Test /1 through /5 -- assure references are correct
 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         }
 218         // Test /-1 ... note that the snippets added by history must be stepped over
 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             };
 226             tests.add((a) -> assertCommandCheckOutput(a, "/-" + (2 * finalI + 1), check));
 227         }
 228         tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("int a = 0;")));
 229         test(false, new String[]{"--no-startup"},
 230                 tests.toArray(new ReplTest[tests.size()]));
 231     }
 232 
 233     public void test8142447() {
 234         Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) ->
 235                 (a) -> assertCommandCheckOutput(a, cmd, s -> {
 236                             String[] ss = s.split("\n");
 237                             assertEquals(ss[0], code);
 238                             loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount));
 239                         });
 240         ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables());
 241 
 242         Compiler compiler = new Compiler();
 243         Path startup = compiler.getPath("StartupFileOption/startup.txt");
 244         compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1
 245                 "void add(int n) { assertionCount += n; }");
 246         test(new String[]{"--startup", startup.toString()},
 247                 (a) -> assertCommand(a, "add(1)", ""), // id: 1
 248                 (a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "|  Error:")), // id: e1
 249                 (a) -> assertVariable(a, "int", "ONE", "1", "1"),
 250                 assertRerun.apply("/1").apply("add(1)", 2), assertVariables,
 251                 assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables,
 252                 assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables
 253         );
 254 
 255         test(false, new String[] {"--no-startup"},
 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.")
 259         );
 260     }
 261 
 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(
 268                 (a) -> assertCommand(a, "/classpath " + classpath, String.format("|  Path '%s' added to classpath", classpath)),
 269                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 270         );
 271         test(new String[] { "--class-path", classpath.toString() },



 272                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 273         );
 274     }
 275 
 276     public void testClasspathJar() {
 277         Compiler compiler = new Compiler();
 278         Path outDir = Paths.get("testClasspathJar");
 279         compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
 280         String jarName = "test.jar";
 281         compiler.jar(outDir, jarName, "pkg/A.class");
 282         Path jarPath = compiler.getPath(outDir).resolve(jarName);
 283         test(
 284                 (a) -> assertCommand(a, "/classpath " + jarPath, String.format("|  Path '%s' added to classpath", jarPath)),
 285                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 286         );
 287         test(new String[] { "--class-path", jarPath.toString() },



 288                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
 289         );
 290     }
 291 
 292     public void testStartupFileOption() {
 293         try {
 294             Compiler compiler = new Compiler();
 295             Path startup = compiler.getPath("StartupFileOption/startup.txt");
 296             compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }");
 297             test(new String[]{"--startup", startup.toString()},
 298                     (a) -> evaluateExpression(a, "A", "new A()", "A")
 299             );
 300             test(new String[]{"--no-startup"},
 301                     (a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("|  Error:\n|  cannot find symbol"))
 302             );
 303             test(
 304                     (a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", "")
 305             );
 306         } finally {
 307             removeStartup();
 308         }
 309     }
 310 
 311     public void testLoadingFromArgs() {
 312         Compiler compiler = new Compiler();
 313         Path path = compiler.getPath("loading.repl");
 314         compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;");
 315         test(new String[] { path.toString() },
 316                 (a) -> assertCommand(a, "x", "x ==> 20.0"),
 317                 (a) -> assertCommand(a, "a", "a ==> 10.0")
 318         );
 319         Path unknown = compiler.getPath("UNKNOWN.jar");
 320         test(Locale.ROOT, true, new String[]{unknown.toString()},


 520                 "|  created method f()",
 521                 "|  created class A",
 522                 "a ==> 10"
 523         };
 524         compiler.writeToFile(testNormalFile, sources2);
 525         for (String feedback : new String[]{"/set fe", "/set feedback"}) {
 526             for (String feedbackState : new String[]{"n", "normal"}) {
 527                 test(
 528                         a -> assertCommand(a, feedback + " " + feedbackState, "|  Feedback mode: normal"),
 529                         a -> assertCommand(a, sources[0], output[0]),
 530                         a -> assertCommand(a, sources[1], output[1]),
 531                         a -> assertCommand(a, sources[2], output[2]),
 532                         a -> assertCommand(a, sources[3], output[3]),
 533                         a -> assertCommand(a, "/o " + testNormalFile.toString(), "")
 534                 );
 535             }
 536         }
 537     }
 538 
 539     public void testHistoryReference() {
 540         test(false, new String[]{"--no-startup"},
 541                 a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"),
 542                 a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"),
 543                 a -> assertCommand(a, "/-2", "System.err.println(1)", "", null, "", "1\n"),
 544                 a -> assertCommand(a, "/history",
 545                                                     "/debug 0\n" +
 546                                                     "System.err.println(1)\n" +
 547                                                     "System.err.println(2)\n" +
 548                                                     "System.err.println(1)\n" +
 549                                                     "/history\n"),
 550                 a -> assertCommand(a, "/-2", "System.err.println(2)", "", null, "", "2\n"),
 551                 a -> assertCommand(a, "/!", "System.err.println(2)", "", null, "", "2\n"),
 552                 a -> assertCommand(a, "/2", "System.err.println(2)", "", null, "", "2\n"),
 553                 a -> assertCommand(a, "/1", "System.err.println(1)", "", null, "", "1\n")
 554         );
 555     }
 556 
 557     @Test(enabled = false) // TODO 8158197
 558     public void testHeadlessEditPad() {
 559         String prevHeadless = System.getProperty("java.awt.headless");
 560         try {