1 /*
2  * Copyright (c) 2016, 2018, 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  * @bug 8160128 8159935 8168615
27  * @summary Tests for Aux channel, custom remote agents, custom JDI implementations.
28  * @build KullaTesting ExecutionControlTestBase MyExecutionControl MyRemoteExecutionControl MyExecutionControlProvider
29  * @run testng UserJdiUserRemoteTest
30  * @key intermittent
31  */
32 import java.io.ByteArrayOutputStream;
33 import org.testng.annotations.Test;
34 import org.testng.annotations.BeforeMethod;
35 import jdk.jshell.Snippet;
36 import static jdk.jshell.Snippet.Status.OVERWRITTEN;
37 import static jdk.jshell.Snippet.Status.VALID;
38 import jdk.jshell.VarSnippet;
39 import jdk.jshell.spi.ExecutionControl;
40 import jdk.jshell.spi.ExecutionControl.ExecutionControlException;
41 import static org.testng.Assert.assertEquals;
42 
43 @Test
44 public class UserJdiUserRemoteTest extends ExecutionControlTestBase {
45 
46     ExecutionControl currentEC;
47     ByteArrayOutputStream auxStream;
48 
49     @BeforeMethod
50     @Override
setUp()51     public void setUp() {
52         auxStream = new ByteArrayOutputStream();
53         setUp(builder -> builder.executionEngine(new MyExecutionControlProvider(this), null));
54     }
55 
testVarValue()56     public void testVarValue() {
57         VarSnippet dv = varKey(assertEval("double aDouble = 1.5;"));
58         String vd = getState().varValue(dv);
59         assertEquals(vd, "1.5");
60         assertEquals(auxStream.toString(), "aDouble");
61     }
62 
testExtension()63     public void testExtension() throws ExecutionControlException {
64         assertEval("42;");
65         Object res = currentEC.extensionCommand("FROG", "test");
66         assertEquals(res, "ribbit");
67     }
68 
testRedefine()69     public void testRedefine() {
70         Snippet vx = varKey(assertEval("int x;"));
71         Snippet mu = methodKey(assertEval("int mu() { return x * 4; }"));
72         Snippet c = classKey(assertEval("class C { String v() { return \"#\" + mu(); } }"));
73         assertEval("C c0  = new C();");
74         assertEval("c0.v();", "\"#0\"");
75         assertEval("int x = 10;", "10",
76                 ste(MAIN_SNIPPET, VALID, VALID, false, null),
77                 ste(vx, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
78         assertEval("c0.v();", "\"#40\"");
79         assertEval("C c = new C();");
80         assertEval("c.v();", "\"#40\"");
81         assertEval("int mu() { return x * 3; }",
82                 ste(MAIN_SNIPPET, VALID, VALID, false, null),
83                 ste(mu, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
84         assertEval("c.v();", "\"#30\"");
85         assertEval("class C { String v() { return \"@\" + mu(); } }",
86                 ste(MAIN_SNIPPET, VALID, VALID, false, null),
87                 ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
88         assertEval("c0.v();", "\"@30\"");
89         assertEval("c = new C();");
90         assertEval("c.v();", "\"@30\"");
91         assertActiveKeys();
92     }
93 }
94