ToolReloadTest.java revision 3968:fc0a9318d392
1/* 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. 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 * @key intermittent 27 * @bug 8081845 8147898 8143955 8165405 28 * @summary Tests for /reload in JShell tool 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.jdeps/com.sun.tools.javap 32 * jdk.jshell/jdk.internal.jshell.tool 33 * @library /tools/lib 34 * @build KullaTesting TestingInputStream toolbox.ToolBox Compiler 35 * @run testng ToolReloadTest 36 */ 37 38import java.nio.file.Path; 39import java.nio.file.Paths; 40import java.util.function.Function; 41 42import org.testng.annotations.Test; 43 44 45@Test 46public class ToolReloadTest extends ReplToolTesting { 47 48 public void testReloadSnippets() { 49 test( 50 (a) -> assertVariable(a, "int", "x", "5", "5"), 51 (a) -> assertMethod(a, "int m(int z) { return z * z; }", 52 "(int)int", "m"), 53 (a) -> evaluateExpression(a, "int", "m(x)", "25"), 54 (a) -> assertCommand(a, "/reload", 55 "| Restarting and restoring state.\n" + 56 "-: int x = 5;\n" + 57 "-: int m(int z) { return z * z; }\n" + 58 "-: m(x)\n"), 59 (a) -> evaluateExpression(a, "int", "m(x)", "25"), 60 (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), 61 (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()) 62 ); 63 } 64 65 public void testReloadClasspath() { 66 Function<String,String> prog = (s) -> String.format( 67 "package pkg; public class A { public String toString() { return \"%s\"; } }\n", s); 68 Compiler compiler = new Compiler(); 69 Path outDir = Paths.get("testClasspathDirectory"); 70 compiler.compile(outDir, prog.apply("A")); 71 Path classpath = compiler.getPath(outDir); 72 test( 73 (a) -> assertCommand(a, "/env --class-path " + classpath, 74 "| Setting new options and restoring state."), 75 (a) -> assertMethod(a, "String foo() { return (new pkg.A()).toString(); }", 76 "()String", "foo"), 77 (a) -> assertVariable(a, "String", "v", "foo()", "\"A\""), 78 (a) -> { 79 if (!a) compiler.compile(outDir, prog.apply("Aprime")); 80 assertCommand(a, "/reload", 81 "| Restarting and restoring state.\n" + 82 "-: String foo() { return (new pkg.A()).toString(); }\n" + 83 "-: String v = foo();\n"); 84 }, 85 (a) -> assertCommand(a, "v", "v ==> \"Aprime\""), 86 (a) -> evaluateExpression(a, "String", "foo()", "\"Aprime\""), 87 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "Aprime") 88 ); 89 } 90 91 public void testReloadDrop() { 92 test(false, new String[]{"--no-startup"}, 93 a -> assertVariable(a, "int", "a"), 94 a -> dropVariable(a, "/dr 1", "int a = 0", "| dropped variable a"), 95 a -> assertMethod(a, "int b() { return 0; }", "()int", "b"), 96 a -> dropMethod(a, "/drop b", "int b()", "| dropped method b()"), 97 a -> assertClass(a, "class A {}", "class", "A"), 98 a -> dropClass(a, "/dr A", "class A", "| dropped class A"), 99 a -> assertCommand(a, "/reload", 100 "| Restarting and restoring state.\n" + 101 "-: int a;\n" + 102 "-: /drop 1\n" + 103 "-: int b() { return 0; }\n" + 104 "-: /drop b\n" + 105 "-: class A {}\n" + 106 "-: /drop A\n"), 107 a -> assertCommandCheckOutput(a, "/vars", assertVariables()), 108 a -> assertCommandCheckOutput(a, "/methods", assertMethods()), 109 a -> assertCommandCheckOutput(a, "/types", assertClasses()), 110 a -> assertCommandCheckOutput(a, "/imports", assertImports()) 111 ); 112 } 113 114 public void testReloadQuiet() { 115 test(false, new String[]{"--no-startup"}, 116 a -> assertVariable(a, "int", "a"), 117 a -> dropVariable(a, "/dr 1", "int a = 0", "| dropped variable a"), 118 a -> assertMethod(a, "int b() { return 0; }", "()int", "b"), 119 a -> dropMethod(a, "/drop b", "int b()", "| dropped method b()"), 120 a -> assertClass(a, "class A {}", "class", "A"), 121 a -> dropClass(a, "/dr A", "class A", "| dropped class A"), 122 a -> assertCommand(a, "/reload -quiet", 123 "| Restarting and restoring state."), 124 a -> assertCommandCheckOutput(a, "/vars", assertVariables()), 125 a -> assertCommandCheckOutput(a, "/methods", assertMethods()), 126 a -> assertCommandCheckOutput(a, "/types", assertClasses()), 127 a -> assertCommandCheckOutput(a, "/imports", assertImports()) 128 ); 129 } 130 131 public void testReloadRepeat() { 132 test(false, new String[]{"--no-startup"}, 133 (a) -> assertVariable(a, "int", "c", "7", "7"), 134 (a) -> assertCommand(a, "++c", null), 135 (a) -> assertCommand(a, "/!", null), 136 (a) -> assertCommand(a, "/2", null), 137 (a) -> assertCommand(a, "/-1", null), 138 (a) -> assertCommand(a, "/reload", 139 "| Restarting and restoring state.\n" + 140 "-: int c = 7;\n" + 141 "-: ++c\n" + 142 "-: ++c\n" + 143 "-: ++c\n" + 144 "-: ++c\n" 145 ), 146 (a) -> assertCommand(a, "c", "c ==> 11"), 147 (a) -> assertCommand(a, "$4", "$4 ==> 10") 148 ); 149 } 150 151 public void testReloadIgnore() { 152 test(false, new String[]{"--no-startup"}, 153 (a) -> assertCommand(a, "(-)", null), 154 (a) -> assertCommand(a, "/list", null), 155 (a) -> assertCommand(a, "/history", null), 156 (a) -> assertCommand(a, "/help", null), 157 (a) -> assertCommand(a, "/vars", null), 158 (a) -> assertCommand(a, "/save abcd", null), 159 (a) -> assertCommand(a, "/reload", 160 "| Restarting and restoring state.") 161 ); 162 } 163 164 public void testReloadResetRestore() { 165 test( 166 (a) -> assertVariable(a, "int", "x", "5", "5"), 167 (a) -> assertMethod(a, "int m(int z) { return z * z; }", 168 "(int)int", "m"), 169 (a) -> evaluateExpression(a, "int", "m(x)", "25"), 170 (a) -> assertCommand(a, "/reset", "| Resetting state."), 171 (a) -> assertCommand(a, "/reload -restore", 172 "| Restarting and restoring from previous state.\n" + 173 "-: int x = 5;\n" + 174 "-: int m(int z) { return z * z; }\n" + 175 "-: m(x)\n"), 176 (a) -> evaluateExpression(a, "int", "m(x)", "25"), 177 (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), 178 (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()) 179 ); 180 } 181 182 public void testReloadCrashRestore() { 183 test( 184 (a) -> assertVariable(a, "int", "x", "5", "5"), 185 (a) -> assertMethod(a, "int m(int z) { return z * z; }", 186 "(int)int", "m"), 187 (a) -> evaluateExpression(a, "int", "m(x)", "25"), 188 (a) -> assertCommand(a, "System.exit(1);", 189 "| State engine terminated.\n" + 190 "| Restore definitions with: /reload -restore"), 191 (a) -> assertCommand(a, "/reload -restore", 192 "| Restarting and restoring from previous state.\n" + 193 "-: int x = 5;\n" + 194 "-: int m(int z) { return z * z; }\n" + 195 "-: m(x)\n"), 196 (a) -> evaluateExpression(a, "int", "m(x)", "25"), 197 (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()), 198 (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()) 199 ); 200 } 201 202 public void testReloadExitRestore() { 203 test(false, new String[]{"--no-startup"}, 204 (a) -> assertVariable(a, "int", "x", "5", "5"), 205 (a) -> assertMethod(a, "int m(int z) { return z * z; }", 206 "(int)int", "m"), 207 (a) -> evaluateExpression(a, "int", "m(x)", "25") 208 ); 209 test(false, new String[]{"--no-startup"}, 210 (a) -> assertCommand(a, "/reload -restore", 211 "| Restarting and restoring from previous state.\n" + 212 "-: int x = 5;\n" + 213 "-: int m(int z) { return z * z; }\n" + 214 "-: m(x)\n"), 215 (a) -> evaluateExpression(a, "int", "m(x)", "25") 216 ); 217 } 218} 219