Path: blob/master/test/langtools/jdk/jshell/DropTest.java
40931 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8081431 8080069 8167128 819962326* @summary Test of JShell#drop().27* @build KullaTesting TestingInputStream28* @run testng DropTest29*/3031import jdk.jshell.DeclarationSnippet;32import jdk.jshell.Snippet;33import jdk.jshell.MethodSnippet;34import jdk.jshell.VarSnippet;35import org.testng.annotations.Test;3637import static jdk.jshell.Snippet.Status.*;3839@Test40public class DropTest extends KullaTesting {4142public void testDrop() {43Snippet var = varKey(assertEval("int x;"));44Snippet method = methodKey(assertEval("int mu() { return x * 4; }"));45Snippet clazz = classKey(assertEval("class C { String v() { return \"#\" + mu(); } }"));46assertDrop(var,47ste(var, VALID, DROPPED, true, null),48ste(method, VALID, RECOVERABLE_DEFINED, false, var));49assertDrop(method,50ste(method, RECOVERABLE_DEFINED, DROPPED, true, null),51ste(clazz, VALID, RECOVERABLE_DEFINED, false, method));52VarSnippet cc = varKey(assertEval("C c;"));53assertEvalUnresolvedException("new C();", "C", 1, 0);54assertVariables();55assertMethods();56assertClasses();57assertActiveKeys();5859method = methodKey(assertEval("int mu() { return x * 4; }",60added(RECOVERABLE_DEFINED),61ste(clazz, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET)));62assertEval("int x = 10;", "10",63added(VALID),64ste(method, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET));65Snippet c0 = varKey(assertEval("C c0 = new C();"));66assertEval("c0.v();", "\"#40\"");67assertEval("C c = new C();",68ste(MAIN_SNIPPET, VALID, VALID, false, null),69ste(cc, VALID, OVERWRITTEN, false, MAIN_SNIPPET));70assertEval("c.v();", "\"#40\"");71assertEval("int mu() { return x * 3; }",72ste(MAIN_SNIPPET, VALID, VALID, false, null),73ste(method, VALID, OVERWRITTEN, false, MAIN_SNIPPET));74assertEval("c.v();", "\"#30\"");75assertEval("class C { String v() { return \"@\" + mu(); } }",76ste(MAIN_SNIPPET, VALID, VALID, false, null),77ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET));78assertEval("c0.v();", "\"@30\"");79assertDrop(c0,80ste(c0, VALID, DROPPED, true, null));81assertEval("c = new C();");82assertEval("c.v();", "\"@30\"");8384assertVariables();85assertMethods();86assertClasses();87assertActiveKeys();88}8990public void testDropImport() {91Snippet imp = importKey(assertEval("import java.util.*;"));92Snippet decl = varKey(93assertEval("List<Integer> list = Arrays.asList(1, 2, 3);", "[1, 2, 3]"));94assertEval("list;", "[1, 2, 3]");95assertDrop(imp,96DiagCheck.DIAG_OK,97DiagCheck.DIAG_ERROR,98ste(imp, VALID, DROPPED, true, null),99ste(decl, VALID, RECOVERABLE_NOT_DEFINED, true, imp));100assertDeclareFail("list;", "compiler.err.cant.resolve.location");101}102103public void testDropStatement() {104Snippet x = key(assertEval("if (true);"));105assertDrop(x, ste(x, VALID, DROPPED, true, null));106}107108public void testDropVarToMethod() {109Snippet x = varKey(assertEval("int x;"));110DeclarationSnippet method = methodKey(assertEval("double mu() { return x * 4; }"));111assertEval("x == 0;", "true");112assertEval("mu() == 0.0;", "true");113114assertDrop(x,115ste(x, VALID, DROPPED, true, null),116ste(method, VALID, RECOVERABLE_DEFINED, false, x));117assertUnresolvedDependencies1(method, RECOVERABLE_DEFINED, "variable x");118assertEvalUnresolvedException("mu();", "mu", 1, 0);119120assertVariables();121assertMethods();122assertActiveKeys();123}124125public void testDropMethodToMethod() {126Snippet a = methodKey(assertEval("double a() { return 2; }"));127DeclarationSnippet b = methodKey(assertEval("double b() { return a() * 10; }"));128assertEval("double c() { return b() * 3; }");129DeclarationSnippet d = methodKey(assertEval("double d() { return c() + 1000; }"));130assertEval("d();", "1060.0");131assertDrop(a,132ste(a, VALID, DROPPED, true, null),133ste(b, VALID, RECOVERABLE_DEFINED, false, a));134assertUnresolvedDependencies1(b, RECOVERABLE_DEFINED, "method a()");135assertUnresolvedDependencies(d, 0);136assertEvalUnresolvedException("d();", "b", 1, 0);137assertMethods();138assertActiveKeys();139}140141public void testDropClassToMethod() {142Snippet c = classKey(assertEval("class C { int f() { return 7; } }"));143DeclarationSnippet m = methodKey(assertEval("int m() { return new C().f(); }"));144assertDrop(c,145ste(c, VALID, DROPPED, true, null),146ste(m, VALID, RECOVERABLE_DEFINED, false, c));147assertUnresolvedDependencies1(m, RECOVERABLE_DEFINED, "class C");148assertEvalUnresolvedException("m();", "m", 1, 0);149assertActiveKeys();150}151152public void testDropVarToClass() {153Snippet x = varKey(assertEval("int x;"));154DeclarationSnippet a = classKey(assertEval("class A { double a = 4 * x; }"));155assertDrop(x,156DiagCheck.DIAG_OK,157DiagCheck.DIAG_ERROR,158ste(x, VALID, DROPPED, true, null),159ste(a, VALID, RECOVERABLE_DEFINED, false, x));160assertEval("A foo() { return null; }");161assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable x");162assertEvalUnresolvedException("new A();", "A", 1, 0);163assertVariables();164assertActiveKeys();165}166167public void testDropMethodToClass() {168Snippet x = methodKey(assertEval("int x() { return 0; }"));169DeclarationSnippet a = classKey(assertEval("class A { double a = 4 * x(); }"));170assertDrop(x,171DiagCheck.DIAG_OK,172DiagCheck.DIAG_ERROR,173ste(x, VALID, DROPPED, true, null),174ste(a, VALID, RECOVERABLE_DEFINED, false, x));175assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "method x()");176assertEvalUnresolvedException("new A();", "A", 1, 0);177assertMethods();178assertActiveKeys();179}180181public void testDropClassToClass() {182Snippet a = classKey(assertEval("class A {}"));183Snippet b = classKey(assertEval("class B extends A {}"));184Snippet c = classKey(assertEval("class C extends B {}"));185Snippet d = classKey(assertEval("class D extends C {}"));186assertDrop(a,187DiagCheck.DIAG_OK,188DiagCheck.DIAG_ERROR,189ste(a, VALID, DROPPED, true, null),190ste(b, VALID, RECOVERABLE_NOT_DEFINED, true, a),191ste(c, VALID, RECOVERABLE_NOT_DEFINED, true, b),192ste(d, VALID, RECOVERABLE_NOT_DEFINED, true, c));193assertUnresolvedDependencies1((DeclarationSnippet) b, RECOVERABLE_NOT_DEFINED, "class A");194assertDrop(c,195DiagCheck.DIAG_OK,196DiagCheck.DIAG_ERROR,197ste(c, RECOVERABLE_NOT_DEFINED, DROPPED, false, null));198assertEval("interface A {}", null, null,199DiagCheck.DIAG_OK,200DiagCheck.DIAG_ERROR,201added(VALID));202assertClasses();203assertActiveKeys();204}205206public void testDropNoUpdate() {207String as1 = "class A {}";208String as2 = "class A extends java.util.ArrayList<Boolean> {}";209Snippet a = classKey(assertEval(as1, added(VALID)));210Snippet b = classKey(assertEval("class B extends A {}", added(VALID)));211Snippet ax = classKey(assertEval(as2,212ste(MAIN_SNIPPET, VALID, VALID, true, null),213ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET),214ste(b, VALID, VALID, true, MAIN_SNIPPET)));215ax = classKey(assertEval(as1,216ste(MAIN_SNIPPET, VALID, VALID, true, null),217ste(ax, VALID, OVERWRITTEN, false, MAIN_SNIPPET),218ste(b, VALID, VALID, true, MAIN_SNIPPET)));219assertDrop(b,220ste(b, VALID, DROPPED, true, null));221ax = classKey(assertEval(as2,222ste(MAIN_SNIPPET, VALID, VALID, true, null),223ste(ax, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));224assertEval(as1,225ste(MAIN_SNIPPET, VALID, VALID, true, null),226ste(ax, VALID, OVERWRITTEN, false, MAIN_SNIPPET));227}228229// 8199623230public void testTwoForkedDrop() {231MethodSnippet p = methodKey(assertEval("void p() throws Exception { ((String) null).toString(); }"));232MethodSnippet n = methodKey(assertEval("void n() throws Exception { try { p(); } catch (Exception ex) { throw new RuntimeException(\"bar\", ex); }}"));233MethodSnippet m = methodKey(assertEval("void m() { try { n(); } catch (Exception ex) { throw new RuntimeException(\"foo\", ex); }}"));234MethodSnippet c = methodKey(assertEval("void c() throws Throwable { p(); }"));235assertDrop(p,236ste(p, VALID, DROPPED, true, null),237ste(n, VALID, RECOVERABLE_DEFINED, false, p),238ste(c, VALID, RECOVERABLE_DEFINED, false, p));239assertActiveKeys();240}241}242243244