test/langtools/jdk/jshell/ExecutionControlTestBase.java
author dtitov
Tue, 08 Oct 2019 11:18:00 -0700
changeset 58506 dfe3d11378cc
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199136: Dead code in src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java Reviewed-by: dholmes, dtitov Contributed-by: mandrikov@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     1
/*
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     4
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     8
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    13
 * accompanied this code).
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    14
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    18
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    21
 * questions.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    22
 */
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    23
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    24
import javax.tools.Diagnostic;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    25
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
    26
import org.testng.annotations.Test;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    27
import jdk.jshell.VarSnippet;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    28
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    29
import static jdk.jshell.Snippet.Status.VALID;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    30
import static jdk.jshell.Snippet.SubKind.*;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    31
38836
b09d1cfbf28c 8131029: JShell: recover from VMConnection launch failure
rfield
parents: 38535
diff changeset
    32
public class ExecutionControlTestBase extends KullaTesting {
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    33
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
    34
    @Test
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    35
    public void classesDeclaration() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    36
        assertEval("interface A { }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    37
        assertEval("class B implements A { }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    38
        assertEval("interface C extends A { }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    39
        assertEval("enum D implements C { }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    40
        assertEval("@interface E { }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    41
        assertClasses(
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    42
                clazz(KullaTesting.ClassType.INTERFACE, "A"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    43
                clazz(KullaTesting.ClassType.CLASS, "B"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    44
                clazz(KullaTesting.ClassType.INTERFACE, "C"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    45
                clazz(KullaTesting.ClassType.ENUM, "D"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    46
                clazz(KullaTesting.ClassType.ANNOTATION, "E"));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    47
        assertActiveKeys();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    48
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    49
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
    50
    @Test
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    51
    public void interfaceTest() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    52
        String interfaceSource
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    53
                = "interface A {\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    54
                + "   default int defaultMethod() { return 1; }\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    55
                + "   static int staticMethod() { return 2; }\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    56
                + "   int method();\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    57
                + "   class Inner1 {}\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    58
                + "   static class Inner2 {}\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    59
                + "}";
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    60
        assertEval(interfaceSource);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    61
        assertEval("A.staticMethod();", "2");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    62
        String classSource
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    63
                = "class B implements A {\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    64
                + "   public int method() { return 3; }\n"
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    65
                + "}";
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    66
        assertEval(classSource);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    67
        assertEval("B b = new B();");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    68
        assertEval("b.defaultMethod();", "1");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    69
        assertDeclareFail("B.staticMethod();",
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    70
                new ExpectedDiagnostic("compiler.err.cant.resolve.location.args", 0, 14, 1, -1, -1, Diagnostic.Kind.ERROR));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    71
        assertEval("b.method();", "3");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    72
        assertEval("new A.Inner1();");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    73
        assertEval("new A.Inner2();");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    74
        assertEval("new B.Inner1();");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    75
        assertEval("new B.Inner2();");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    76
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    77
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
    78
    @Test
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    79
    public void variables() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    80
        VarSnippet snx = varKey(assertEval("int x = 10;"));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    81
        VarSnippet sny = varKey(assertEval("String y = \"hi\";"));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    82
        VarSnippet snz = varKey(assertEval("long z;"));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    83
        assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z"));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    84
        assertVarValue(snx, "10");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    85
        assertVarValue(sny, "\"hi\"");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    86
        assertVarValue(snz, "0");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    87
        assertActiveKeys();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    88
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    89
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
    90
    @Test
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    91
    public void methodOverload() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    92
        assertEval("int m() { return 1; }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    93
        assertEval("int m(int x) { return 2; }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    94
        assertEval("int m(String s) { return 3; }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    95
        assertEval("int m(int x, int y) { return 4; }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    96
        assertEval("int m(int x, String z) { return 5; }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    97
        assertEval("int m(int x, String z, long g) { return 6; }");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    98
        assertMethods(
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    99
                method("()int", "m"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   100
                method("(int)int", "m"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   101
                method("(String)int", "m"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   102
                method("(int,int)int", "m"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   103
                method("(int,String)int", "m"),
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   104
                method("(int,String,long)int", "m")
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   105
        );
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   106
        assertEval("m();", "1");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   107
        assertEval("m(3);", "2");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   108
        assertEval("m(\"hi\");", "3");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   109
        assertEval("m(7, 8);", "4");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   110
        assertEval("m(7, \"eight\");", "5");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   111
        assertEval("m(7, \"eight\", 9L);", "6");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   112
        assertActiveKeys();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   113
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   114
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
   115
    @Test
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   116
    public void testExprSanity() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   117
        assertEval("int x = 3;", "3");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   118
        assertEval("int y = 4;", "4");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   119
        assertEval("x + y;", "7");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   120
        assertActiveKeys();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   121
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   122
43566
71aef8336eaa 8173577: JShell tests: Some testng tests check nothing
rfield
parents: 38836
diff changeset
   123
    @Test
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   124
    public void testImportOnDemand() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   125
        assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   126
        assertEval("List<Integer> list = new ArrayList<>();");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   127
        assertEval("list.add(45);");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   128
        assertEval("list.size();", "1");
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   129
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   130
}