1 /*
2  * Copyright (c) 2017, 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 8185108
27  * @summary Test exception().getMessage() in events returned by eval()
28  * @run testng ExceptionMessageTest
29  */
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.List;
34 
35 import jdk.jshell.JShell;
36 import jdk.jshell.SnippetEvent;
37 import jdk.jshell.execution.DirectExecutionControl;
38 import jdk.jshell.execution.JdiExecutionControlProvider;
39 import jdk.jshell.execution.LocalExecutionControlProvider;
40 import jdk.jshell.spi.ExecutionControl;
41 import jdk.jshell.spi.ExecutionControlProvider;
42 import jdk.jshell.spi.ExecutionEnv;
43 
44 import org.testng.annotations.DataProvider;
45 import org.testng.annotations.Test;
46 import static org.testng.Assert.assertEquals;
47 import static org.testng.Assert.assertTrue;
48 
49 @Test
50 public class ExceptionMessageTest {
51 
testDefaultEC()52     public void testDefaultEC() {
53         doTestCases(new JdiExecutionControlProvider(), "default");
54     }
55 
testLocalEC()56     public void testLocalEC() {
57         doTestCases(new LocalExecutionControlProvider(), "local");
58     }
59 
testDirectEC()60     public void testDirectEC() {
61         doTestCases(new ExecutionControlProvider() {
62             public ExecutionControl generate(ExecutionEnv env, Map<String, String> param) throws Throwable {
63                 return new DirectExecutionControl();
64             }
65 
66             public String name() {
67                 return "direct";
68             }
69 
70         }, "direct");
71     }
72 
shell(ExecutionControlProvider ec)73     private JShell shell(ExecutionControlProvider ec) {
74         return JShell.builder().executionEngine(ec, new HashMap<>()).build();
75     }
76 
doTestCases(ExecutionControlProvider ec, String label)77     private void doTestCases(ExecutionControlProvider ec, String label) {
78         JShell jshell = shell(ec);
79         doTest(jshell, label, "throw new java.io.IOException();", null);
80         doTest(jshell, label, "throw new java.io.IOException((String)null);", null);
81         doTest(jshell, label, "throw new java.io.IOException(\"\");", "");
82         doTest(jshell, label, "throw new java.io.IOException(\"test\");", "test");
83     }
84 
doTest(JShell jshell, String label, String code, String expected)85     private void doTest(JShell jshell, String label, String code, String expected) {
86         List<SnippetEvent> result = jshell.eval(code);
87         assertEquals(result.size(), 1, "Expected only one event");
88         SnippetEvent evt = result.get(0);
89         Exception exc = evt.exception();
90         String out = exc.getMessage();
91         assertEquals(out, expected, "Exception message not as expected: " +
92                 label + " -- " + code);
93     }
94 }
95