langtools/test/jdk/jshell/ToolReloadTest.java
changeset 34999 aacf94dab449
child 35359 f04501964016
equal deleted inserted replaced
34998:416dfba03d33 34999:aacf94dab449
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     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
       
    26  * @bug 8081845
       
    27  * @summary Tests for /reload in JShell tool
       
    28  * @library /tools/lib
       
    29  * @build KullaTesting TestingInputStream ToolBox Compiler
       
    30  * @run testng ToolReloadTest
       
    31  */
       
    32 
       
    33 import java.nio.file.Path;
       
    34 import java.nio.file.Paths;
       
    35 import java.util.function.Function;
       
    36 
       
    37 import org.testng.annotations.Test;
       
    38 
       
    39 
       
    40 @Test
       
    41 public class ToolReloadTest extends ReplToolTesting {
       
    42 
       
    43     public void testReloadSnippets() {
       
    44         test(
       
    45                 (a) -> assertVariable(a, "int", "x", "5", "5"),
       
    46                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
       
    47                         "(int)int", "m"),
       
    48                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
       
    49                 (a) -> assertCommand(a, "/reload",
       
    50                         "|  Restarting and restoring state.\n" +
       
    51                         "-: int x = 5;\n" +
       
    52                         "-: int m(int z) { return z * z; }\n" +
       
    53                         "-: m(x)\n"),
       
    54                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
       
    55                 (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
       
    56                 (a) -> assertCommandCheckOutput(a, "/methods", assertMethods())
       
    57         );
       
    58     }
       
    59 
       
    60     public void testReloadClasspath() {
       
    61         Function<String,String> prog = (s) -> String.format(
       
    62                 "package pkg; public class A { public String toString() { return \"%s\"; } }\n", s);
       
    63         Compiler compiler = new Compiler();
       
    64         Path outDir = Paths.get("testClasspathDirectory");
       
    65         compiler.compile(outDir, prog.apply("A"));
       
    66         Path classpath = compiler.getPath(outDir);
       
    67         test(
       
    68                 (a) -> assertCommand(a, "/classpath " + classpath,
       
    69                         String.format("|  Path %s added to classpath\n", classpath)),
       
    70                 (a) -> assertMethod(a, "String foo() { return (new pkg.A()).toString(); }",
       
    71                         "()String", "foo"),
       
    72                 (a) -> assertVariable(a, "String", "v", "foo()", "\"A\""),
       
    73                 (a) -> {
       
    74                        if (!a) compiler.compile(outDir, prog.apply("Aprime"));
       
    75                        assertCommand(a, "/reload",
       
    76                         "|  Restarting and restoring state.\n" +
       
    77                         "-: /classpath " + classpath + "\n" +
       
    78                         "-: String foo() { return (new pkg.A()).toString(); }\n" +
       
    79                         "-: String v = foo();\n");
       
    80                        },
       
    81                 (a) -> assertCommand(a, "v", "|  Variable v of type String has value \"Aprime\"\n"),
       
    82                 (a) -> evaluateExpression(a, "String", "foo()", "\"Aprime\""),
       
    83                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "\"Aprime\"")
       
    84         );
       
    85     }
       
    86 
       
    87     public void testReloadDrop() {
       
    88         test(false, new String[]{"-nostartup"},
       
    89                 a -> assertVariable(a, "int", "a"),
       
    90                 a -> dropVariable(a, "/dr 1", "int a = 0"),
       
    91                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
       
    92                 a -> dropMethod(a, "/drop b", "b ()I"),
       
    93                 a -> assertClass(a, "class A {}", "class", "A"),
       
    94                 a -> dropClass(a, "/dr A", "class A"),
       
    95                 a -> assertCommand(a, "/reload",
       
    96                         "|  Restarting and restoring state.\n" +
       
    97                         "-: int a;\n" +
       
    98                         "-: /drop 1\n" +
       
    99                         "-: int b() { return 0; }\n" +
       
   100                         "-: /drop b\n" +
       
   101                         "-: class A {}\n" +
       
   102                         "-: /drop A\n"),
       
   103                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
       
   104                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
       
   105                 a -> assertCommandCheckOutput(a, "/classes", assertClasses()),
       
   106                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
       
   107         );
       
   108     }
       
   109 
       
   110     public void testReloadRepeat() {
       
   111         test(false, new String[]{"-nostartup"},
       
   112                 (a) -> assertVariable(a, "int", "c", "7", "7"),
       
   113                 (a) -> assertCommand(a, "++c", null),
       
   114                 (a) -> assertCommand(a, "/!", null),
       
   115                 (a) -> assertCommand(a, "/2", null),
       
   116                 (a) -> assertCommand(a, "/-1", null),
       
   117                 (a) -> assertCommand(a, "/reload",
       
   118                         "|  Restarting and restoring state.\n" +
       
   119                         "-: int c = 7;\n" +
       
   120                         "-: ++c\n" +
       
   121                         "-: ++c\n" +
       
   122                         "-: ++c\n" +
       
   123                         "-: ++c\n"
       
   124                 ),
       
   125                 (a) -> assertCommand(a, "c", "|  Variable c of type int has value 11\n"),
       
   126                 (a) -> assertCommand(a, "$4", "|  Variable $4 of type int has value 10\n")
       
   127         );
       
   128     }
       
   129 
       
   130     public void testReloadIgnore() {
       
   131         test(false, new String[]{"-nostartup"},
       
   132                 (a) -> assertCommand(a, "(-)", null),
       
   133                 (a) -> assertCommand(a, "/list", null),
       
   134                 (a) -> assertCommand(a, "/history", null),
       
   135                 (a) -> assertCommand(a, "/help", null),
       
   136                 (a) -> assertCommand(a, "/vars", null),
       
   137                 (a) -> assertCommand(a, "/save abcd", null),
       
   138                 (a) -> assertCommand(a, "/reload",
       
   139                         "|  Restarting and restoring state.\n")
       
   140         );
       
   141     }
       
   142 
       
   143     public void testReloadResetRestore() {
       
   144         test(
       
   145                 (a) -> assertVariable(a, "int", "x", "5", "5"),
       
   146                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
       
   147                         "(int)int", "m"),
       
   148                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
       
   149                 (a) -> assertCommand(a, "/reset", "|  Resetting state.\n"),
       
   150                 (a) -> assertCommand(a, "/reload restore",
       
   151                         "|  Restarting and restoring from previous state.\n" +
       
   152                         "-: int x = 5;\n" +
       
   153                         "-: int m(int z) { return z * z; }\n" +
       
   154                         "-: m(x)\n"),
       
   155                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
       
   156                 (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
       
   157                 (a) -> assertCommandCheckOutput(a, "/methods", assertMethods())
       
   158         );
       
   159     }
       
   160 
       
   161     public void testReloadCrashRestore() {
       
   162         test(
       
   163                 (a) -> assertVariable(a, "int", "x", "5", "5"),
       
   164                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
       
   165                         "(int)int", "m"),
       
   166                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
       
   167                 (a) -> assertCommand(a, "System.exit(1);",
       
   168                         "|  State engine terminated.\n" +
       
   169                         "|  Restore definitions with: /reload restore\n"),
       
   170                 (a) -> assertCommand(a, "/reload restore",
       
   171                         "|  Restarting and restoring from previous state.\n" +
       
   172                         "-: int x = 5;\n" +
       
   173                         "-: int m(int z) { return z * z; }\n" +
       
   174                         "-: m(x)\n"),
       
   175                 (a) -> evaluateExpression(a, "int", "m(x)", "25"),
       
   176                 (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
       
   177                 (a) -> assertCommandCheckOutput(a, "/methods", assertMethods())
       
   178         );
       
   179     }
       
   180 
       
   181     public void testReloadExitRestore() {
       
   182         test(false, new String[]{"-nostartup"},
       
   183                 (a) -> assertVariable(a, "int", "x", "5", "5"),
       
   184                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
       
   185                         "(int)int", "m"),
       
   186                 (a) -> evaluateExpression(a, "int", "m(x)", "25")
       
   187         );
       
   188         test(false, new String[]{"-nostartup"},
       
   189                 (a) -> assertCommand(a, "/reload restore",
       
   190                         "|  Restarting and restoring from previous state.\n" +
       
   191                         "-: int x = 5;\n" +
       
   192                         "-: int m(int z) { return z * z; }\n" +
       
   193                         "-: m(x)\n"),
       
   194                 (a) -> evaluateExpression(a, "int", "m(x)", "25")
       
   195         );
       
   196     }
       
   197 }