test/langtools/jdk/jshell/ExceptionsTest.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55397 5eeee2cc94f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
 * @test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
 * @summary Tests for exceptions
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
    27
 * @bug 8198801 8212167
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
 * @build KullaTesting TestingInputStream
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
 * @run testng ExceptionsTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    32
import java.io.IOException;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.io.PrintWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import java.io.StringWriter;
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    35
import jdk.jshell.EvalException;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    36
import jdk.jshell.JShellException;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    37
import jdk.jshell.Snippet;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    38
import jdk.jshell.SnippetEvent;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    39
import jdk.jshell.UnresolvedReferenceException;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import org.testng.annotations.Test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import static org.testng.Assert.*;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
@Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
public class ExceptionsTest extends KullaTesting {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
    public void throwUncheckedException() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
        String message = "error_message";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
        SnippetEvent cr = assertEvalException("throw new RuntimeException(\"" + message + "\");");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
        assertExceptionMatch(cr,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
                        newStackTraceElement("", "", cr.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
    public void throwCheckedException() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
        String message = "error_message";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
        SnippetEvent cr = assertEvalException("throw new Exception(\"" + message + "\");");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
        assertExceptionMatch(cr,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
                new ExceptionInfo(Exception.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
                        newStackTraceElement("", "", cr.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
    public void throwFromStaticMethodOfClass() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
        String message = "error_message";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
        Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
        Snippet s2 = classKey(assertEval("class A { static void g() { f(); } }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
        SnippetEvent cr3 = assertEvalException("A.g();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
        assertExceptionMatch(cr3,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
                        newStackTraceElement("", "f", s1, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
                        newStackTraceElement("A", "g", s2, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
                        newStackTraceElement("", "", cr3.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
    public void throwFromStaticMethodOfInterface() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
        String message = "error_message";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
        Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
        Snippet s2 = classKey(assertEval("interface A { static void g() { f(); } }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
        SnippetEvent cr3 = assertEvalException("A.g();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
        assertExceptionMatch(cr3,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
                        newStackTraceElement("", "f", s1, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
                        newStackTraceElement("A", "g", s2, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
                        newStackTraceElement("", "", cr3.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    88
    public void throwChained() {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    89
        String message1 = "error_message1";
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    90
        String message2 = "error_message2";
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    91
        Snippet s1 = methodKey(assertEval("void p() throws Exception { ((String) null).toString(); }"));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    92
        Snippet s2 = methodKey(assertEval("void n() throws Exception { try { p(); } catch (Exception ex) { throw new java.io.IOException(\"" + message2 + "\", ex); }}"));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    93
        Snippet s3 = methodKey(assertEval("void m() {\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    94
                + "try { n(); }\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    95
                + "catch (Exception ex) {\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    96
                + "    throw new RuntimeException(\"" + message1 + "\", ex);\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    97
                + "}}"));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    98
        SnippetEvent cr4 = assertEvalException("m();");
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
    99
        assertExceptionMatch(cr4,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   100
                new ExceptionInfo(RuntimeException.class, message1,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   101
                        new ExceptionInfo(IOException.class, message2,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   102
                                new ExceptionInfo(NullPointerException.class, null,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   103
                                        newStackTraceElement("", "p", s1, 1),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   104
                                        newStackTraceElement("", "n", s2, 1),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   105
                                        newStackTraceElement("", "m", s3, 2),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   106
                                        newStackTraceElement("", "", cr4.snippet(), 1)),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   107
                                newStackTraceElement("", "n", s2, 1),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   108
                                newStackTraceElement("", "m", s3, 2),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   109
                                newStackTraceElement("", "", cr4.snippet(), 1)),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   110
                        newStackTraceElement("", "m", s3, 4),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   111
                        newStackTraceElement("", "", cr4.snippet(), 1)));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   112
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   113
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   114
    public void throwChainedUnresolved() {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   115
        String message1 = "error_message1";
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   116
        String message2 = "error_message2";
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   117
        Snippet s1 = methodKey(assertEval("void p() throws Exception { ((String) null).toString(); }"));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   118
        Snippet s2 = methodKey(assertEval("void n() throws Exception { try { p(); } catch (Exception ex) { throw new java.io.IOException(\"" + message2 + "\", ex); }}"));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   119
        Snippet s3 = methodKey(assertEval("void m() {\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   120
                + "try { n(); }\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   121
                + "catch (Exception ex) {\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   122
                + "    throw new RuntimeException(\"" + message1 + "\", ex);\n"
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   123
                + "}}"));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   124
        getState().drop(s1);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   125
        SnippetEvent cr4 = assertEvalException("m();");
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   126
        assertExceptionMatch(cr4,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   127
                new ExceptionInfo(RuntimeException.class, message1,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   128
                        new UnresolvedExceptionInfo(s2,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   129
                                newStackTraceElement("", "n", s2, 1),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   130
                                newStackTraceElement("", "m", s3, 2),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   131
                                newStackTraceElement("", "", cr4.snippet(), 1)),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   132
                        newStackTraceElement("", "m", s3, 4),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   133
                        newStackTraceElement("", "", cr4.snippet(), 1)));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   134
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   135
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
    public void throwFromConstructor() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
        String message = "error_message";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
        Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
        Snippet s2 = classKey(assertEval("class A { A() { f(); } }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
        SnippetEvent cr3 = assertEvalException("new A();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
        assertExceptionMatch(cr3,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
                        newStackTraceElement("", "f", s1, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
                        newStackTraceElement("A", "<init>", s2, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
                        newStackTraceElement("", "", cr3.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
    public void throwFromDefaultMethodOfInterface() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
        String message = "error_message";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
        Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
        Snippet s2 = classKey(assertEval("interface A { default void g() { f(); } }"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
        SnippetEvent cr3 = assertEvalException("new A() { }.g();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
        assertExceptionMatch(cr3,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
                        newStackTraceElement("", "f", s1, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
                        newStackTraceElement("A", "g", s2, 1),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
                        newStackTraceElement("", "", cr3.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
    public void throwFromLambda() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
        String message = "lambda";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
        Snippet s1 = varKey(assertEval(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
                "Runnable run = () -> {\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
                "   throw new RuntimeException(\"" + message + "\");\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
                "};"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
        ));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
        SnippetEvent cr2 = assertEvalException("run.run();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
        assertExceptionMatch(cr2,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
                        newStackTraceElement("", "lambda$", s1, 2),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
                        newStackTraceElement("", "", cr2.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
    public void throwFromAnonymousClass() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
        String message = "anonymous";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        Snippet s1 = varKey(assertEval(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
                "Runnable run = new Runnable() {\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
                "   public void run() {\n"+
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
                "       throw new RuntimeException(\"" + message + "\");\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
                "   }\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
                "};"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
        ));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        SnippetEvent cr2 = assertEvalException("run.run();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        assertExceptionMatch(cr2,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
                        newStackTraceElement("1", "run", s1, 3),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
                        newStackTraceElement("", "", cr2.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
    public void throwFromLocalClass() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
        String message = "local";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
        Snippet s1 = methodKey(assertEval(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
                "void f() {\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
                "   class A {\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
                "       void f() {\n"+
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
                "           throw new RuntimeException(\"" + message + "\");\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
                "       }\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
                "   }\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
                "   new A().f();\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
                "}"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
        ));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
        SnippetEvent cr2 = assertEvalException("f();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
        assertExceptionMatch(cr2,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
                new ExceptionInfo(RuntimeException.class, message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
                        newStackTraceElement("1A", "f", s1, 4),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
                        newStackTraceElement("", "f", s1, 7),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
                        newStackTraceElement("", "", cr2.snippet(), 1)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   210
    // test 8212167
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   211
    public void throwLineFormat1() {
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   212
        SnippetEvent se = assertEvalException(
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   213
                "if (true) { \n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   214
                        "   int x = 10; \n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   215
                        "   int y = 10 / 0;}"
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   216
        );
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   217
        assertExceptionMatch(se,
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   218
                new ExceptionInfo(ArithmeticException.class, "/ by zero",
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   219
                        newStackTraceElement("", "", se.snippet(), 3)));
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   220
    }
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   221
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   222
    public void throwLineFormat3() {
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   223
        Snippet sp = methodKey(assertEval(
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   224
                "int p() \n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   225
                        "  { return 4/0; }"));
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   226
        Snippet sm = methodKey(assertEval(
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   227
                "int m(int x)\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   228
                        "       \n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   229
                        "       {\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   230
                        "          return p() + x; \n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   231
                        "       }"));
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   232
        Snippet sn = methodKey(assertEval(
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   233
                "int n(int x) {\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   234
                        "         try {\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   235
                        "           return m(x);\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   236
                        "         }\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   237
                        "         catch (Throwable ex) {\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   238
                        "           throw new IllegalArgumentException( \"GOT:\", ex);\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   239
                        "         }\n" +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   240
                        "       }"));
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   241
        SnippetEvent se = assertEvalException("n(33);");
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   242
        assertExceptionMatch(se,
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   243
                new ExceptionInfo(IllegalArgumentException.class, null,
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   244
                        new ExceptionInfo(ArithmeticException.class, "/ by zero",
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   245
                                newStackTraceElement("", "p", sp, 2),
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   246
                                newStackTraceElement("", "m", sm, 4),
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   247
                                newStackTraceElement("", "n", sn, 3),
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   248
                                newStackTraceElement("", "", se.snippet(), 1)),
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   249
                        newStackTraceElement("", "n", sn, 6),
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   250
                        newStackTraceElement("", "", se.snippet(), 1)));
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   251
    }
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   252
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
    @Test(enabled = false) // TODO 8129427
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
    public void outOfMemory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
        assertEval("import java.util.*;");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
        assertEval("List<byte[]> list = new ArrayList<>();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
        assertExecuteException("while (true) { list.add(new byte[10000]); }", OutOfMemoryError.class);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
    public void stackOverflow() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
        assertEval("void f() { f(); }");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
        assertExecuteException("f();", StackOverflowError.class);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
    private StackTraceElement newStackTraceElement(String className, String methodName, Snippet key, int lineNumber) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
        return new StackTraceElement(className, methodName, "#" + key.id(), lineNumber);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   269
    private static class AnyExceptionInfo {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   270
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
        public final StackTraceElement[] stackTraceElements;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   273
        public AnyExceptionInfo(StackTraceElement... stackTraceElements) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   274
            this.stackTraceElements = stackTraceElements.length == 0 ? null : stackTraceElements;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   275
        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   276
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   277
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   278
    private static class UnresolvedExceptionInfo extends AnyExceptionInfo {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   279
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   280
        public final Snippet sn;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   281
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   282
        public UnresolvedExceptionInfo(Snippet sn, StackTraceElement... stackTraceElements) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   283
            super(stackTraceElements);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   284
            this.sn = sn;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   285
        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   286
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   287
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   288
    private static class ExceptionInfo extends AnyExceptionInfo {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   289
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   290
        public final Class<? extends Throwable> exception;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   291
        public final String message;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   292
        public final AnyExceptionInfo cause;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   293
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   294
        public ExceptionInfo(Class<? extends Throwable> exception, String message,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   295
                StackTraceElement... stackTraceElements) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   296
            this(exception, message, null, stackTraceElements);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   297
        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   298
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   299
        public ExceptionInfo(Class<? extends Throwable> exception, String message,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   300
                AnyExceptionInfo cause, StackTraceElement... stackTraceElements) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   301
            super(stackTraceElements);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
            this.exception = exception;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
            this.message = message;
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   304
            this.cause = cause;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
    private void assertExecuteException(String input, Class<? extends Throwable> exception) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
        assertExceptionMatch(assertEvalException(input), new ExceptionInfo(exception, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
    private void assertExceptionMatch(SnippetEvent cr, ExceptionInfo exceptionInfo) {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   313
        assertExceptionMatch(cr.exception(), cr.snippet().source(), exceptionInfo);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   314
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   315
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   316
    private void assertExceptionMatch(Throwable exception, String source, ExceptionInfo exceptionInfo) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   317
        assertNotNull(exception, "Expected exception was not thrown: " + exceptionInfo.exception);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   318
        if (exception instanceof EvalException) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   319
            EvalException ex = (EvalException) exception;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   320
            String actualException = ex.getExceptionClassName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   321
            String expectedException = exceptionInfo.exception.getCanonicalName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   322
            assertEquals(actualException, expectedException,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   323
                    String.format("Given \"%s\" expected exception: %s, got: %s%nStack trace:%n%s",
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   324
                            source, expectedException, actualException, getStackTrace(ex)));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   325
            if (exceptionInfo.message != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   326
                assertEquals(ex.getMessage(), exceptionInfo.message,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   327
                        String.format("Given \"%s\" expected message: %s, got: %s",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   328
                                source, exceptionInfo.message, ex.getMessage()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   329
            }
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   330
            assertStackMatch(ex, source, exceptionInfo);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   331
            if (exceptionInfo.cause != null) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   332
                assertAnyExceptionMatch(exception.getCause(), exceptionInfo.cause);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   333
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   334
        } else {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   335
            fail("Unexpected exception: " + exception + " or exceptionInfo: " + exceptionInfo);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   336
        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   337
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   338
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   339
    private void assertStackMatch(JShellException exception, String source, AnyExceptionInfo exceptionInfo) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   340
        if (exceptionInfo.stackTraceElements != null) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   341
            assertStackTrace(exception.getStackTrace(), exceptionInfo.stackTraceElements,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   342
                    String.format("Given \"%s\"%nStack trace:%n%s%n",
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   343
                            source, getStackTrace(exception)));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   344
        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   345
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   346
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   347
    private void assertAnyExceptionMatch(Throwable exception, AnyExceptionInfo exceptionInfo) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   348
        if (exceptionInfo instanceof ExceptionInfo) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   349
            assertExceptionMatch(exception, "", (ExceptionInfo) exceptionInfo);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   350
        } else {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   351
            assertTrue(exceptionInfo instanceof UnresolvedExceptionInfo, "Bad exceptionInfo: " + exceptionInfo);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   352
            assertTrue(exception instanceof UnresolvedReferenceException,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   353
                    "Expected UnresolvedReferenceException: " + exception);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   354
            UnresolvedExceptionInfo uei = (UnresolvedExceptionInfo) exceptionInfo;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   355
            UnresolvedReferenceException ure = (UnresolvedReferenceException) exception;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   356
            assertEquals(ure.getSnippet(), uei.sn);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   357
            assertStackMatch(ure, "", exceptionInfo);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   358
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   361
    private void assertStackTrace(StackTraceElement[] actual, StackTraceElement[] expected, String message) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
        if (actual != expected) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   363
            if (actual == null || expected == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   364
                fail(message);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   365
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   366
                assertEquals(actual.length, expected.length, message + " : arrays do not have the same size");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   367
                for (int i = 0; i < actual.length; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   368
                    StackTraceElement actualElement = actual[i];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
                    StackTraceElement expectedElement = expected[i];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   370
                    assertEquals(actualElement.getClassName(), expectedElement.getClassName(), message + " : class names");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   371
                    String expectedMethodName = expectedElement.getMethodName();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   372
                    if (expectedMethodName.startsWith("lambda$")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   373
                        assertTrue(actualElement.getMethodName().startsWith("lambda$"), message + " : method names");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
                    } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   375
                        assertEquals(actualElement.getMethodName(), expectedElement.getMethodName(), message + " : method names");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   376
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
                    assertEquals(actualElement.getFileName(), expectedElement.getFileName(), message + " : file names");
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 49515
diff changeset
   378
                    assertEquals(actualElement.getLineNumber(), expectedElement.getLineNumber(), message + " : line numbers"
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   379
                        + " -- actual: " + actualElement.getLineNumber() + ", expected: " + expectedElement.getLineNumber() +
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55142
diff changeset
   380
                            " -- in: " + actualElement.getClassName());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   381
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   382
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   383
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   385
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 47216
diff changeset
   386
    private String getStackTrace(Throwable ex) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
        StringWriter st = new StringWriter();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
        ex.printStackTrace(new PrintWriter(st));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
        return st.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   391
}