Path: blob/master/test/langtools/jdk/jshell/ExceptionMessageTest.java
40931 views
/*1* Copyright (c) 2017, 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*/2223/*24* @test25* @bug 818510826* @summary Test exception().getMessage() in events returned by eval()27* @run testng ExceptionMessageTest28*/2930import java.util.HashMap;31import java.util.Map;32import java.util.List;3334import jdk.jshell.JShell;35import jdk.jshell.SnippetEvent;36import jdk.jshell.execution.DirectExecutionControl;37import jdk.jshell.execution.JdiExecutionControlProvider;38import jdk.jshell.execution.LocalExecutionControlProvider;39import jdk.jshell.spi.ExecutionControl;40import jdk.jshell.spi.ExecutionControlProvider;41import jdk.jshell.spi.ExecutionEnv;4243import org.testng.annotations.DataProvider;44import org.testng.annotations.Test;45import static org.testng.Assert.assertEquals;46import static org.testng.Assert.assertTrue;4748@Test49public class ExceptionMessageTest {5051public void testDefaultEC() {52doTestCases(new JdiExecutionControlProvider(), "default");53}5455public void testLocalEC() {56doTestCases(new LocalExecutionControlProvider(), "local");57}5859public void testDirectEC() {60doTestCases(new ExecutionControlProvider() {61public ExecutionControl generate(ExecutionEnv env, Map<String, String> param) throws Throwable {62return new DirectExecutionControl();63}6465public String name() {66return "direct";67}6869}, "direct");70}7172private JShell shell(ExecutionControlProvider ec) {73return JShell.builder().executionEngine(ec, new HashMap<>()).build();74}7576private void doTestCases(ExecutionControlProvider ec, String label) {77JShell jshell = shell(ec);78doTest(jshell, label, "throw new java.io.IOException();", null);79doTest(jshell, label, "throw new java.io.IOException((String)null);", null);80doTest(jshell, label, "throw new java.io.IOException(\"\");", "");81doTest(jshell, label, "throw new java.io.IOException(\"test\");", "test");82}8384private void doTest(JShell jshell, String label, String code, String expected) {85List<SnippetEvent> result = jshell.eval(code);86assertEquals(result.size(), 1, "Expected only one event");87SnippetEvent evt = result.get(0);88Exception exc = evt.exception();89String out = exc.getMessage();90assertEquals(out, expected, "Exception message not as expected: " +91label + " -- " + code);92}93}949596