langtools/test/jdk/jshell/ToolReloadTest.java
changeset 37389 9c137b83a8b8
parent 36990 ec0b843a7af5
child 38514 f7df9ab653b0
equal deleted inserted replaced
37011:c84d0cce090e 37389:9c137b83a8b8
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @key intermittent
    26  * @key intermittent
    27  * @bug 8081845 8147898
    27  * @bug 8081845 8147898 8143955
    28  * @summary Tests for /reload in JShell tool
    28  * @summary Tests for /reload in JShell tool
    29  * @modules jdk.compiler/com.sun.tools.javac.api
    29  * @modules jdk.compiler/com.sun.tools.javac.api
    30  *          jdk.compiler/com.sun.tools.javac.main
    30  *          jdk.compiler/com.sun.tools.javac.main
    31  *          jdk.jdeps/com.sun.tools.javap
    31  *          jdk.jdeps/com.sun.tools.javap
    32  *          jdk.jshell/jdk.internal.jshell.tool
    32  *          jdk.jshell/jdk.internal.jshell.tool
    69         Path outDir = Paths.get("testClasspathDirectory");
    69         Path outDir = Paths.get("testClasspathDirectory");
    70         compiler.compile(outDir, prog.apply("A"));
    70         compiler.compile(outDir, prog.apply("A"));
    71         Path classpath = compiler.getPath(outDir);
    71         Path classpath = compiler.getPath(outDir);
    72         test(
    72         test(
    73                 (a) -> assertCommand(a, "/classpath " + classpath,
    73                 (a) -> assertCommand(a, "/classpath " + classpath,
    74                         String.format("|  Path '%s' added to classpath\n", classpath)),
    74                         String.format("|  Path '%s' added to classpath", classpath)),
    75                 (a) -> assertMethod(a, "String foo() { return (new pkg.A()).toString(); }",
    75                 (a) -> assertMethod(a, "String foo() { return (new pkg.A()).toString(); }",
    76                         "()String", "foo"),
    76                         "()String", "foo"),
    77                 (a) -> assertVariable(a, "String", "v", "foo()", "\"A\""),
    77                 (a) -> assertVariable(a, "String", "v", "foo()", "\"A\""),
    78                 (a) -> {
    78                 (a) -> {
    79                        if (!a) compiler.compile(outDir, prog.apply("Aprime"));
    79                        if (!a) compiler.compile(outDir, prog.apply("Aprime"));
    81                         "|  Restarting and restoring state.\n" +
    81                         "|  Restarting and restoring state.\n" +
    82                         "-: /classpath " + classpath + "\n" +
    82                         "-: /classpath " + classpath + "\n" +
    83                         "-: String foo() { return (new pkg.A()).toString(); }\n" +
    83                         "-: String foo() { return (new pkg.A()).toString(); }\n" +
    84                         "-: String v = foo();\n");
    84                         "-: String v = foo();\n");
    85                        },
    85                        },
    86                 (a) -> assertCommand(a, "v", "|  Variable v of type String has value \"Aprime\"\n"),
    86                 (a) -> assertCommand(a, "v", "v ==> \"Aprime\""),
    87                 (a) -> evaluateExpression(a, "String", "foo()", "\"Aprime\""),
    87                 (a) -> evaluateExpression(a, "String", "foo()", "\"Aprime\""),
    88                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"Aprime\"")
    88                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "Aprime")
    89         );
    89         );
    90     }
    90     }
    91 
    91 
    92     public void testReloadDrop() {
    92     public void testReloadDrop() {
    93         test(false, new String[]{"-nostartup"},
    93         test(false, new String[]{"-nostartup"},
    94                 a -> assertVariable(a, "int", "a"),
    94                 a -> assertVariable(a, "int", "a"),
    95                 a -> dropVariable(a, "/dr 1", "int a = 0", "|  Dropped variable a\n"),
    95                 a -> dropVariable(a, "/dr 1", "int a = 0", "|  dropped variable a"),
    96                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
    96                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
    97                 a -> dropMethod(a, "/drop b", "b ()I", "|  Dropped method b()\n"),
    97                 a -> dropMethod(a, "/drop b", "b ()I", "|  dropped method b()"),
    98                 a -> assertClass(a, "class A {}", "class", "A"),
    98                 a -> assertClass(a, "class A {}", "class", "A"),
    99                 a -> dropClass(a, "/dr A", "class A", "|  Dropped class A\n"),
    99                 a -> dropClass(a, "/dr A", "class A", "|  dropped class A"),
   100                 a -> assertCommand(a, "/reload",
   100                 a -> assertCommand(a, "/reload",
   101                         "|  Restarting and restoring state.\n" +
   101                         "|  Restarting and restoring state.\n" +
   102                         "-: int a;\n" +
   102                         "-: int a;\n" +
   103                         "-: /drop 1\n" +
   103                         "-: /drop 1\n" +
   104                         "-: int b() { return 0; }\n" +
   104                         "-: int b() { return 0; }\n" +
   113     }
   113     }
   114 
   114 
   115     public void testReloadQuiet() {
   115     public void testReloadQuiet() {
   116         test(false, new String[]{"-nostartup"},
   116         test(false, new String[]{"-nostartup"},
   117                 a -> assertVariable(a, "int", "a"),
   117                 a -> assertVariable(a, "int", "a"),
   118                 a -> dropVariable(a, "/dr 1", "int a = 0", "|  Dropped variable a\n"),
   118                 a -> dropVariable(a, "/dr 1", "int a = 0", "|  dropped variable a"),
   119                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
   119                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
   120                 a -> dropMethod(a, "/drop b", "b ()I", "|  Dropped method b()\n"),
   120                 a -> dropMethod(a, "/drop b", "b ()I", "|  dropped method b()"),
   121                 a -> assertClass(a, "class A {}", "class", "A"),
   121                 a -> assertClass(a, "class A {}", "class", "A"),
   122                 a -> dropClass(a, "/dr A", "class A", "|  Dropped class A\n"),
   122                 a -> dropClass(a, "/dr A", "class A", "|  dropped class A"),
   123                 a -> assertCommand(a, "/reload quiet",
   123                 a -> assertCommand(a, "/reload quiet",
   124                         "|  Restarting and restoring state.\n"),
   124                         "|  Restarting and restoring state."),
   125                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
   125                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
   126                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
   126                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
   127                 a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
   127                 a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
   128                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
   128                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
   129         );
   129         );
   142                         "-: ++c\n" +
   142                         "-: ++c\n" +
   143                         "-: ++c\n" +
   143                         "-: ++c\n" +
   144                         "-: ++c\n" +
   144                         "-: ++c\n" +
   145                         "-: ++c\n"
   145                         "-: ++c\n"
   146                 ),
   146                 ),
   147                 (a) -> assertCommand(a, "c", "|  Variable c of type int has value 11\n"),
   147                 (a) -> assertCommand(a, "c", "c ==> 11"),
   148                 (a) -> assertCommand(a, "$4", "|  Variable $4 of type int has value 10\n")
   148                 (a) -> assertCommand(a, "$4", "$4 ==> 10")
   149         );
   149         );
   150     }
   150     }
   151 
   151 
   152     public void testReloadIgnore() {
   152     public void testReloadIgnore() {
   153         test(false, new String[]{"-nostartup"},
   153         test(false, new String[]{"-nostartup"},
   156                 (a) -> assertCommand(a, "/history", null),
   156                 (a) -> assertCommand(a, "/history", null),
   157                 (a) -> assertCommand(a, "/help", null),
   157                 (a) -> assertCommand(a, "/help", null),
   158                 (a) -> assertCommand(a, "/vars", null),
   158                 (a) -> assertCommand(a, "/vars", null),
   159                 (a) -> assertCommand(a, "/save abcd", null),
   159                 (a) -> assertCommand(a, "/save abcd", null),
   160                 (a) -> assertCommand(a, "/reload",
   160                 (a) -> assertCommand(a, "/reload",
   161                         "|  Restarting and restoring state.\n")
   161                         "|  Restarting and restoring state.")
   162         );
   162         );
   163     }
   163     }
   164 
   164 
   165     public void testReloadResetRestore() {
   165     public void testReloadResetRestore() {
   166         test(
   166         test(
   167                 (a) -> assertVariable(a, "int", "x", "5", "5"),
   167                 (a) -> assertVariable(a, "int", "x", "5", "5"),
   168                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
   168                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
   169                         "(int)int", "m"),
   169                         "(int)int", "m"),
   170                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
   170                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
   171                 (a) -> assertCommand(a, "/reset", "|  Resetting state.\n"),
   171                 (a) -> assertCommand(a, "/reset", "|  Resetting state."),
   172                 (a) -> assertCommand(a, "/reload restore",
   172                 (a) -> assertCommand(a, "/reload restore",
   173                         "|  Restarting and restoring from previous state.\n" +
   173                         "|  Restarting and restoring from previous state.\n" +
   174                         "-: int x = 5;\n" +
   174                         "-: int x = 5;\n" +
   175                         "-: int m(int z) { return z * z; }\n" +
   175                         "-: int m(int z) { return z * z; }\n" +
   176                         "-: m(x)\n"),
   176                         "-: m(x)\n"),
   186                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
   186                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
   187                         "(int)int", "m"),
   187                         "(int)int", "m"),
   188                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
   188                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
   189                 (a) -> assertCommand(a, "System.exit(1);",
   189                 (a) -> assertCommand(a, "System.exit(1);",
   190                         "|  State engine terminated.\n" +
   190                         "|  State engine terminated.\n" +
   191                         "|  Restore definitions with: /reload restore\n"),
   191                         "|  Restore definitions with: /reload restore"),
   192                 (a) -> assertCommand(a, "/reload restore",
   192                 (a) -> assertCommand(a, "/reload restore",
   193                         "|  Restarting and restoring from previous state.\n" +
   193                         "|  Restarting and restoring from previous state.\n" +
   194                         "-: int x = 5;\n" +
   194                         "-: int x = 5;\n" +
   195                         "-: int m(int z) { return z * z; }\n" +
   195                         "-: int m(int z) { return z * z; }\n" +
   196                         "-: m(x)\n"),
   196                         "-: m(x)\n"),