Path: blob/master/test/langtools/jdk/jshell/ExecutionControlTestBase.java
40931 views
/*1* Copyright (c) 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*/2223import javax.tools.Diagnostic;2425import org.testng.annotations.Test;26import jdk.jshell.VarSnippet;27import java.net.InetAddress;2829import static jdk.jshell.Snippet.Status.VALID;30import static jdk.jshell.Snippet.SubKind.*;3132public class ExecutionControlTestBase extends KullaTesting {3334String standardListenSpec() {35String loopback = InetAddress.getLoopbackAddress().getHostAddress();36return "jdi:hostname(" + loopback + ")";37}3839String standardLaunchSpec() {40return "jdi:launch(true)";41}4243String standardJdiSpec() {44return "jdi";45}4647String standardSpecs() {48return "5(" + standardListenSpec() + "), 6(" + standardLaunchSpec() + "), 7(" + standardJdiSpec() + ")";49}5051@Test52public void classesDeclaration() {53assertEval("interface A { }");54assertEval("class B implements A { }");55assertEval("interface C extends A { }");56assertEval("enum D implements C { }");57assertEval("@interface E { }");58assertClasses(59clazz(KullaTesting.ClassType.INTERFACE, "A"),60clazz(KullaTesting.ClassType.CLASS, "B"),61clazz(KullaTesting.ClassType.INTERFACE, "C"),62clazz(KullaTesting.ClassType.ENUM, "D"),63clazz(KullaTesting.ClassType.ANNOTATION, "E"));64assertActiveKeys();65}6667@Test68public void interfaceTest() {69String interfaceSource70= "interface A {\n"71+ " default int defaultMethod() { return 1; }\n"72+ " static int staticMethod() { return 2; }\n"73+ " int method();\n"74+ " class Inner1 {}\n"75+ " static class Inner2 {}\n"76+ "}";77assertEval(interfaceSource);78assertEval("A.staticMethod();", "2");79String classSource80= "class B implements A {\n"81+ " public int method() { return 3; }\n"82+ "}";83assertEval(classSource);84assertEval("B b = new B();");85assertEval("b.defaultMethod();", "1");86assertDeclareFail("B.staticMethod();",87new ExpectedDiagnostic("compiler.err.cant.resolve.location.args", 0, 14, 1, -1, -1, Diagnostic.Kind.ERROR));88assertEval("b.method();", "3");89assertEval("new A.Inner1();");90assertEval("new A.Inner2();");91assertEval("new B.Inner1();");92assertEval("new B.Inner2();");93}9495@Test96public void variables() {97VarSnippet snx = varKey(assertEval("int x = 10;"));98VarSnippet sny = varKey(assertEval("String y = \"hi\";"));99VarSnippet snz = varKey(assertEval("long z;"));100assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z"));101assertVarValue(snx, "10");102assertVarValue(sny, "\"hi\"");103assertVarValue(snz, "0");104assertActiveKeys();105}106107@Test108public void methodOverload() {109assertEval("int m() { return 1; }");110assertEval("int m(int x) { return 2; }");111assertEval("int m(String s) { return 3; }");112assertEval("int m(int x, int y) { return 4; }");113assertEval("int m(int x, String z) { return 5; }");114assertEval("int m(int x, String z, long g) { return 6; }");115assertMethods(116method("()int", "m"),117method("(int)int", "m"),118method("(String)int", "m"),119method("(int,int)int", "m"),120method("(int,String)int", "m"),121method("(int,String,long)int", "m")122);123assertEval("m();", "1");124assertEval("m(3);", "2");125assertEval("m(\"hi\");", "3");126assertEval("m(7, 8);", "4");127assertEval("m(7, \"eight\");", "5");128assertEval("m(7, \"eight\", 9L);", "6");129assertActiveKeys();130}131132@Test133public void testExprSanity() {134assertEval("int x = 3;", "3");135assertEval("int y = 4;", "4");136assertEval("x + y;", "7");137assertActiveKeys();138}139140@Test141public void testImportOnDemand() {142assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));143assertEval("List<Integer> list = new ArrayList<>();");144assertEval("list.add(45);");145assertEval("list.size();", "1");146}147}148149150