langtools/test/jdk/jshell/ToolBasicTest.java
changeset 36494 4175f47b2a50
parent 35812 6a6ca0bd3c14
child 36718 bf40906bf49d
equal deleted inserted replaced
36493:1e87cd35c980 36494:4175f47b2a50
     1 /*
     1 /*
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886
    26  * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317
    27  * @requires os.family != "solaris"
    27  * @requires os.family != "solaris"
    28  * @summary Tests for Basic tests for REPL tool
    28  * @summary Tests for Basic tests for REPL tool
    29  * @library /tools/lib
    29  * @library /tools/lib
    30  * @ignore 8139873
    30  * @ignore 8139873
    31  * @build KullaTesting TestingInputStream ToolBox Compiler
    31  * @build KullaTesting TestingInputStream ToolBox Compiler
    88         );
    88         );
    89     }
    89     }
    90 
    90 
    91     public void elideStartUpFromList() {
    91     public void elideStartUpFromList() {
    92         test(
    92         test(
    93                 (a) -> assertCommandCheckOutput(a, "123", (s) ->
    93                 (a) -> assertCommandOutputContains(a, "123", "type int"),
    94                         assertTrue(s.contains("type int"), s)),
       
    95                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
    94                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
    96                     int cnt;
    95                     int cnt;
    97                     try (Scanner scanner = new Scanner(s)) {
    96                     try (Scanner scanner = new Scanner(s)) {
    98                         cnt = 0;
    97                         cnt = 0;
    99                         while (scanner.hasNextLine()) {
    98                         while (scanner.hasNextLine()) {
   110 
   109 
   111     public void elideStartUpFromSave() throws IOException {
   110     public void elideStartUpFromSave() throws IOException {
   112         Compiler compiler = new Compiler();
   111         Compiler compiler = new Compiler();
   113         Path path = compiler.getPath("myfile");
   112         Path path = compiler.getPath("myfile");
   114         test(
   113         test(
   115                 (a) -> assertCommandCheckOutput(a, "123",
   114                 (a) -> assertCommandOutputContains(a, "123", "type int"),
   116                         (s) -> assertTrue(s.contains("type int"), s)),
       
   117                 (a) -> assertCommand(a, "/save " + path.toString(), "")
   115                 (a) -> assertCommand(a, "/save " + path.toString(), "")
   118         );
   116         );
   119         try (Stream<String> lines = Files.lines(path)) {
   117         try (Stream<String> lines = Files.lines(path)) {
   120             assertEquals(lines.count(), 1, "Expected only one saved line");
   118             assertEquals(lines.count(), 1, "Expected only one saved line");
   121         }
   119         }
   592                     (a) -> assertVariable(a, "int", "a"),
   590                     (a) -> assertVariable(a, "int", "a"),
   593                     (a) -> assertVariable(a, "double", "b", "10", "10.0"),
   591                     (a) -> assertVariable(a, "double", "b", "10", "10.0"),
   594                     (a) -> assertMethod(a, "void f() {}", "()V", "f"),
   592                     (a) -> assertMethod(a, "void f() {}", "()V", "f"),
   595                     (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
   593                     (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
   596                     (a) -> assertCommand(a, "/save " + startUpFile.toString(), null),
   594                     (a) -> assertCommand(a, "/save " + startUpFile.toString(), null),
   597                     (a) -> assertCommand(a, "/setstart " + startUpFile.toString(), null)
   595                     (a) -> assertCommand(a, "/set start " + startUpFile.toString(), null)
   598             );
   596             );
   599             Path unknown = compiler.getPath("UNKNOWN");
   597             Path unknown = compiler.getPath("UNKNOWN");
   600             test(
   598             test(
   601                     (a) -> assertCommand(a, "/setstart " + unknown.toString(),
   599                     (a) -> assertCommand(a, "/set start " + unknown.toString(),
   602                             "|  File '" + unknown + "' for /setstart is not found.\n")
   600                             "|  File '" + unknown + "' for /set start is not found.\n")
   603             );
   601             );
   604             test(false, new String[0],
   602             test(false, new String[0],
   605                     (a) -> {
   603                     (a) -> {
   606                         loadVariable(a, "int", "a");
   604                         loadVariable(a, "int", "a");
   607                         loadVariable(a, "double", "b", "10.0", "10.0");
   605                         loadVariable(a, "double", "b", "10.0", "10.0");
   617             removeStartup();
   615             removeStartup();
   618         }
   616         }
   619     }
   617     }
   620 
   618 
   621     private void removeStartup() {
   619     private void removeStartup() {
   622         Preferences preferences = Preferences.userRoot().node("tool/REPL");
   620         Preferences preferences = Preferences.userRoot().node("tool/JShell");
   623         if (preferences != null) {
   621         if (preferences != null) {
   624             preferences.remove("STARTUP");
   622             preferences.remove("STARTUP");
   625         }
   623         }
   626     }
   624     }
   627 
   625 
   634     public void testEmptyClassPath() {
   632     public void testEmptyClassPath() {
   635         test(after -> assertCommand(after, "/classpath", "|  /classpath requires a path argument\n"));
   633         test(after -> assertCommand(after, "/classpath", "|  /classpath requires a path argument\n"));
   636     }
   634     }
   637 
   635 
   638     public void testNoArgument() {
   636     public void testNoArgument() {
   639         String[] commands = {"/save", "/open", "/setstart"};
   637         String[] commands = {"/save", "/open", "/set start"};
   640         test(Stream.of(commands)
   638         test(Stream.of(commands)
   641                 .map(cmd -> {
   639                 .map(cmd -> {
   642                     String c = cmd;
   640                     String c = cmd;
   643                     final String finalC = c;
   641                     final String finalC = c;
   644                     return (ReplTest) after -> assertCommand(after, cmd,
   642                     return (ReplTest) after -> assertCommand(after, cmd,
   668 
   666 
   669     public void testRemoteExit() {
   667     public void testRemoteExit() {
   670         test(
   668         test(
   671                 a -> assertVariable(a, "int", "x"),
   669                 a -> assertVariable(a, "int", "x"),
   672                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
   670                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
   673                 a -> assertCommandCheckOutput(a, "System.exit(5);",  s ->
   671                 a -> assertCommandOutputContains(a, "System.exit(5);", "terminated"),
   674                         assertTrue(s.contains("terminated"), s)),
       
   675                 a -> assertCommandCheckOutput(a, "/vars", s ->
   672                 a -> assertCommandCheckOutput(a, "/vars", s ->
   676                         assertTrue(s.trim().isEmpty(), s)),
   673                         assertTrue(s.trim().isEmpty(), s)),
   677                 a -> assertMethod(a, "void f() { }", "()void", "f"),
   674                 a -> assertMethod(a, "void f() { }", "()void", "f"),
   678                 a -> assertCommandCheckOutput(a, "/methods", assertMethods())
   675                 a -> assertCommandCheckOutput(a, "/methods", assertMethods())
   679         );
   676         );
   697                         s -> checkLineToList(s, START_UP)),
   694                         s -> checkLineToList(s, START_UP)),
   698                 a -> assertCommandCheckOutput(a, "/list " + arg,
   695                 a -> assertCommandCheckOutput(a, "/list " + arg,
   699                         s -> assertEquals(s, "|  No definition or id named " + arg +
   696                         s -> assertEquals(s, "|  No definition or id named " + arg +
   700                                 " found.  There are no active definitions.\n")),
   697                                 " found.  There are no active definitions.\n")),
   701                 a -> assertVariable(a, "int", "aardvark"),
   698                 a -> assertVariable(a, "int", "aardvark"),
   702                 a -> assertCommandCheckOutput(a, "/list aardvark",
   699                 a -> assertCommandOutputContains(a, "/list aardvark", "aardvark"),
   703                         s -> assertTrue(s.contains("aardvark"))),
       
   704                 a -> assertCommandCheckOutput(a, "/list start",
   700                 a -> assertCommandCheckOutput(a, "/list start",
   705                         s -> checkLineToList(s, START_UP)),
   701                         s -> checkLineToList(s, START_UP)),
   706                 a -> assertCommandCheckOutput(a, "/list all",
   702                 a -> assertCommandCheckOutput(a, "/list all",
   707                         s -> checkLineToList(s, startVarList)),
   703                         s -> checkLineToList(s, startVarList)),
   708                 a -> assertCommandCheckOutput(a, "/list printf",
   704                 a -> assertCommandCheckOutput(a, "/list printf",
   712                                 " found.  Try /list without arguments.\n"))
   708                                 " found.  Try /list without arguments.\n"))
   713         );
   709         );
   714     }
   710     }
   715 
   711 
   716     public void testFeedbackNegative() {
   712     public void testFeedbackNegative() {
   717         test(a -> assertCommandCheckOutput(a, "/feedback aaaa",
   713         test(a -> assertCommandCheckOutput(a, "/set feedback aaaa",
   718                 assertStartsWith("|  Follow /feedback with of the following")));
   714                 assertStartsWith("|  Does not match any current feedback mode")));
   719     }
   715     }
   720 
   716 
   721     public void testFeedbackOff() {
   717     public void testFeedbackOff() {
   722         for (String off : new String[]{"o", "off"}) {
   718         for (String off : new String[]{"o", "off"}) {
   723             test(
   719             test(
   724                     a -> assertCommand(a, "/feedback " + off, ""),
   720                     a -> assertCommand(a, "/set feedback " + off, ""),
   725                     a -> assertCommand(a, "int a", ""),
   721                     a -> assertCommand(a, "int a", ""),
   726                     a -> assertCommand(a, "void f() {}", ""),
   722                     a -> assertCommand(a, "void f() {}", ""),
   727                     a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("|  Error:")),
   723                     a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("|  Error:")),
   728                     a -> assertCommandCheckOutput(a, "public void f() {}", assertStartsWith("|  Warning:"))
   724                     a -> assertCommandCheckOutput(a, "public void f() {}", assertStartsWith("|  Warning:"))
   729             );
       
   730         }
       
   731     }
       
   732 
       
   733     public void testFeedbackConcise() {
       
   734         Compiler compiler = new Compiler();
       
   735         Path testConciseFile = compiler.getPath("testConciseFeedback");
       
   736         String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"};
       
   737         compiler.writeToFile(testConciseFile, sources);
       
   738         for (String concise : new String[]{"c", "concise"}) {
       
   739             test(
       
   740                     a -> assertCommand(a, "/feedback " + concise, ""),
       
   741                     a -> assertCommand(a, sources[0], ""),
       
   742                     a -> assertCommand(a, sources[1], ""),
       
   743                     a -> assertCommand(a, sources[2], ""),
       
   744                     a -> assertCommand(a, sources[3], "|  a : 10\n"),
       
   745                     a -> assertCommand(a, "/o " + testConciseFile.toString(), "|  a : 10\n")
       
   746             );
   725             );
   747         }
   726         }
   748     }
   727     }
   749 
   728 
   750     public void testFeedbackNormal() {
   729     public void testFeedbackNormal() {
   757                 "|  Added method f()\n",
   736                 "|  Added method f()\n",
   758                 "|  Added class A\n",
   737                 "|  Added class A\n",
   759                 "|  Variable a has been assigned the value 10\n"
   738                 "|  Variable a has been assigned the value 10\n"
   760         };
   739         };
   761         compiler.writeToFile(testNormalFile, sources2);
   740         compiler.writeToFile(testNormalFile, sources2);
   762         for (String feedback : new String[]{"/f", "/feedback"}) {
   741         for (String feedback : new String[]{"/set f", "/set feedback"}) {
   763             for (String feedbackState : new String[]{"n", "normal", "v", "verbose"}) {
   742             for (String feedbackState : new String[]{"n", "normal", "o", "off"}) {
   764                 String f = null;
       
   765                 if (feedbackState.startsWith("n")) {
       
   766                     f = "normal";
       
   767                 } else if (feedbackState.startsWith("v")) {
       
   768                     f = "verbose";
       
   769                 }
       
   770                 final String finalF = f;
       
   771                 test(
   743                 test(
   772                         a -> assertCommand(a, feedback + " " + feedbackState, "|  Feedback mode: " + finalF +"\n"),
   744                         a -> assertCommand(a, feedback + " " + feedbackState, "|  Feedback mode: normal\n"),
   773                         a -> assertCommand(a, sources[0], output[0]),
   745                         a -> assertCommand(a, sources[0], output[0]),
   774                         a -> assertCommand(a, sources[1], output[1]),
   746                         a -> assertCommand(a, sources[1], output[1]),
   775                         a -> assertCommand(a, sources[2], output[2]),
   747                         a -> assertCommand(a, sources[2], output[2]),
   776                         a -> assertCommand(a, sources[3], output[3]),
   748                         a -> assertCommand(a, sources[3], output[3]),
   777                         a -> assertCommand(a, "/o " + testNormalFile.toString(),
   749                         a -> assertCommand(a, "/o " + testNormalFile.toString(), "")
   778                                 "|  Modified variable a of type int\n" +
       
   779                                 "|  Modified method f()\n" +
       
   780                                 "|    Update overwrote method f()\n" +
       
   781                                 "|  Modified class A\n" +
       
   782                                 "|    Update overwrote class A\n" +
       
   783                                 "|  Variable a has been assigned the value 10\n")
       
   784                 );
   750                 );
   785             }
   751             }
   786         }
       
   787     }
       
   788 
       
   789     public void testFeedbackDefault() {
       
   790         Compiler compiler = new Compiler();
       
   791         Path testDefaultFile = compiler.getPath("testDefaultFeedback");
       
   792         String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"};
       
   793         String[] output = new String[] {
       
   794                 "|  Added variable a of type int\n",
       
   795                 "|  Added method f()\n",
       
   796                 "|  Added class A\n",
       
   797                 "|  Variable a has been assigned the value 10\n"
       
   798         };
       
   799         compiler.writeToFile(testDefaultFile, sources);
       
   800         for (String defaultFeedback : new String[]{"", "d", "default"}) {
       
   801             test(
       
   802                     a -> assertCommand(a, "/feedback o", ""),
       
   803                     a -> assertCommand(a, "int x", ""),
       
   804                     a -> assertCommand(a, "/feedback " + defaultFeedback, "|  Feedback mode: default\n"),
       
   805                     a -> assertCommand(a, sources[0], output[0]),
       
   806                     a -> assertCommand(a, sources[1], output[1]),
       
   807                     a -> assertCommand(a, sources[2], output[2]),
       
   808                     a -> assertCommand(a, sources[3], output[3]),
       
   809                     a -> assertCommand(a, "/o " + testDefaultFile.toString(), "")
       
   810             );
       
   811         }
   752         }
   812     }
   753     }
   813 
   754 
   814     public void testDrop() {
   755     public void testDrop() {
   815         test(false, new String[]{"-nostartup"},
   756         test(false, new String[]{"-nostartup"},
   904         }
   845         }
   905     }
   846     }
   906 
   847 
   907     public void testCommandPrefix() {
   848     public void testCommandPrefix() {
   908         test(a -> assertCommandCheckOutput(a, "/s",
   849         test(a -> assertCommandCheckOutput(a, "/s",
   909                       assertStartsWith("|  Command: /s is ambiguous: /seteditor, /save, /setstart")),
   850                       assertStartsWith("|  Command: /s is ambiguous: /save, /set")),
   910              a -> assertCommand(a, "int var", "|  Added variable var of type int\n"),
   851              a -> assertCommand(a, "int var", "|  Added variable var of type int\n"),
   911              a -> assertCommandCheckOutput(a, "/va",
   852              a -> assertCommandCheckOutput(a, "/va",
   912                       assertStartsWith("|    int var = 0")),
   853                       assertStartsWith("|    int var = 0")),
   913              a -> assertCommandCheckOutput(a, "/save",
   854              a -> assertCommandCheckOutput(a, "/save",
   914                       assertStartsWith("|  The /save command requires a filename argument.")));
   855                       assertStartsWith("|  The /save command requires a filename argument.")));