1 /* 2 * Copyright (c) 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 import javax.tools.Diagnostic; 25 26 import org.testng.annotations.Test; 27 import jdk.jshell.VarSnippet; 28 import java.net.InetAddress; 29 30 import static jdk.jshell.Snippet.Status.VALID; 31 import static jdk.jshell.Snippet.SubKind.*; 32 33 public class ExecutionControlTestBase extends KullaTesting { 34 standardListenSpec()35 String standardListenSpec() { 36 String loopback = InetAddress.getLoopbackAddress().getHostAddress(); 37 return "jdi:hostname(" + loopback + ")"; 38 } 39 standardLaunchSpec()40 String standardLaunchSpec() { 41 return "jdi:launch(true)"; 42 } 43 standardJdiSpec()44 String standardJdiSpec() { 45 return "jdi"; 46 } 47 standardSpecs()48 String standardSpecs() { 49 return "5(" + standardListenSpec() + "), 6(" + standardLaunchSpec() + "), 7(" + standardJdiSpec() + ")"; 50 } 51 52 @Test classesDeclaration()53 public void classesDeclaration() { 54 assertEval("interface A { }"); 55 assertEval("class B implements A { }"); 56 assertEval("interface C extends A { }"); 57 assertEval("enum D implements C { }"); 58 assertEval("@interface E { }"); 59 assertClasses( 60 clazz(KullaTesting.ClassType.INTERFACE, "A"), 61 clazz(KullaTesting.ClassType.CLASS, "B"), 62 clazz(KullaTesting.ClassType.INTERFACE, "C"), 63 clazz(KullaTesting.ClassType.ENUM, "D"), 64 clazz(KullaTesting.ClassType.ANNOTATION, "E")); 65 assertActiveKeys(); 66 } 67 68 @Test interfaceTest()69 public void interfaceTest() { 70 String interfaceSource 71 = "interface A {\n" 72 + " default int defaultMethod() { return 1; }\n" 73 + " static int staticMethod() { return 2; }\n" 74 + " int method();\n" 75 + " class Inner1 {}\n" 76 + " static class Inner2 {}\n" 77 + "}"; 78 assertEval(interfaceSource); 79 assertEval("A.staticMethod();", "2"); 80 String classSource 81 = "class B implements A {\n" 82 + " public int method() { return 3; }\n" 83 + "}"; 84 assertEval(classSource); 85 assertEval("B b = new B();"); 86 assertEval("b.defaultMethod();", "1"); 87 assertDeclareFail("B.staticMethod();", 88 new ExpectedDiagnostic("compiler.err.cant.resolve.location.args", 0, 14, 1, -1, -1, Diagnostic.Kind.ERROR)); 89 assertEval("b.method();", "3"); 90 assertEval("new A.Inner1();"); 91 assertEval("new A.Inner2();"); 92 assertEval("new B.Inner1();"); 93 assertEval("new B.Inner2();"); 94 } 95 96 @Test variables()97 public void variables() { 98 VarSnippet snx = varKey(assertEval("int x = 10;")); 99 VarSnippet sny = varKey(assertEval("String y = \"hi\";")); 100 VarSnippet snz = varKey(assertEval("long z;")); 101 assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z")); 102 assertVarValue(snx, "10"); 103 assertVarValue(sny, "\"hi\""); 104 assertVarValue(snz, "0"); 105 assertActiveKeys(); 106 } 107 108 @Test methodOverload()109 public void methodOverload() { 110 assertEval("int m() { return 1; }"); 111 assertEval("int m(int x) { return 2; }"); 112 assertEval("int m(String s) { return 3; }"); 113 assertEval("int m(int x, int y) { return 4; }"); 114 assertEval("int m(int x, String z) { return 5; }"); 115 assertEval("int m(int x, String z, long g) { return 6; }"); 116 assertMethods( 117 method("()int", "m"), 118 method("(int)int", "m"), 119 method("(String)int", "m"), 120 method("(int,int)int", "m"), 121 method("(int,String)int", "m"), 122 method("(int,String,long)int", "m") 123 ); 124 assertEval("m();", "1"); 125 assertEval("m(3);", "2"); 126 assertEval("m(\"hi\");", "3"); 127 assertEval("m(7, 8);", "4"); 128 assertEval("m(7, \"eight\");", "5"); 129 assertEval("m(7, \"eight\", 9L);", "6"); 130 assertActiveKeys(); 131 } 132 133 @Test testExprSanity()134 public void testExprSanity() { 135 assertEval("int x = 3;", "3"); 136 assertEval("int y = 4;", "4"); 137 assertEval("x + y;", "7"); 138 assertActiveKeys(); 139 } 140 141 @Test testImportOnDemand()142 public void testImportOnDemand() { 143 assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID)); 144 assertEval("List<Integer> list = new ArrayList<>();"); 145 assertEval("list.add(45);"); 146 assertEval("list.size();", "1"); 147 } 148 } 149