author | coleenp |
Fri, 31 Aug 2018 07:03:46 -0400 | |
changeset 51608 | 625a5bdde0c5 |
parent 49515 | 083318155ad1 |
child 55142 | e2dbcc6ed36d |
child 58678 | 9cf78a70fa4f |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
* @summary Tests for exceptions |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
27 |
* @bug 8198801 |
33362 | 28 |
* @build KullaTesting TestingInputStream |
29 |
* @run testng ExceptionsTest |
|
30 |
*/ |
|
31 |
||
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
32 |
import java.io.IOException; |
33362 | 33 |
import java.io.PrintWriter; |
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 | 40 |
|
41 |
import org.testng.annotations.Test; |
|
42 |
||
43 |
import static org.testng.Assert.*; |
|
44 |
||
45 |
@Test |
|
46 |
public class ExceptionsTest extends KullaTesting { |
|
47 |
||
48 |
public void throwUncheckedException() { |
|
49 |
String message = "error_message"; |
|
50 |
SnippetEvent cr = assertEvalException("throw new RuntimeException(\"" + message + "\");"); |
|
51 |
assertExceptionMatch(cr, |
|
52 |
new ExceptionInfo(RuntimeException.class, message, |
|
53 |
newStackTraceElement("", "", cr.snippet(), 1))); |
|
54 |
} |
|
55 |
||
56 |
public void throwCheckedException() { |
|
57 |
String message = "error_message"; |
|
58 |
SnippetEvent cr = assertEvalException("throw new Exception(\"" + message + "\");"); |
|
59 |
assertExceptionMatch(cr, |
|
60 |
new ExceptionInfo(Exception.class, message, |
|
61 |
newStackTraceElement("", "", cr.snippet(), 1))); |
|
62 |
} |
|
63 |
||
64 |
public void throwFromStaticMethodOfClass() { |
|
65 |
String message = "error_message"; |
|
66 |
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }")); |
|
67 |
Snippet s2 = classKey(assertEval("class A { static void g() { f(); } }")); |
|
68 |
SnippetEvent cr3 = assertEvalException("A.g();"); |
|
69 |
assertExceptionMatch(cr3, |
|
70 |
new ExceptionInfo(RuntimeException.class, message, |
|
71 |
newStackTraceElement("", "f", s1, 1), |
|
72 |
newStackTraceElement("A", "g", s2, 1), |
|
73 |
newStackTraceElement("", "", cr3.snippet(), 1))); |
|
74 |
} |
|
75 |
||
76 |
public void throwFromStaticMethodOfInterface() { |
|
77 |
String message = "error_message"; |
|
78 |
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }")); |
|
79 |
Snippet s2 = classKey(assertEval("interface A { static void g() { f(); } }")); |
|
80 |
SnippetEvent cr3 = assertEvalException("A.g();"); |
|
81 |
assertExceptionMatch(cr3, |
|
82 |
new ExceptionInfo(RuntimeException.class, message, |
|
83 |
newStackTraceElement("", "f", s1, 1), |
|
84 |
newStackTraceElement("A", "g", s2, 1), |
|
85 |
newStackTraceElement("", "", cr3.snippet(), 1))); |
|
86 |
} |
|
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 | 136 |
public void throwFromConstructor() { |
137 |
String message = "error_message"; |
|
138 |
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }")); |
|
139 |
Snippet s2 = classKey(assertEval("class A { A() { f(); } }")); |
|
140 |
SnippetEvent cr3 = assertEvalException("new A();"); |
|
141 |
assertExceptionMatch(cr3, |
|
142 |
new ExceptionInfo(RuntimeException.class, message, |
|
143 |
newStackTraceElement("", "f", s1, 1), |
|
144 |
newStackTraceElement("A", "<init>", s2, 1), |
|
145 |
newStackTraceElement("", "", cr3.snippet(), 1))); |
|
146 |
} |
|
147 |
||
148 |
public void throwFromDefaultMethodOfInterface() { |
|
149 |
String message = "error_message"; |
|
150 |
Snippet s1 = methodKey(assertEval("void f() { throw new RuntimeException(\"" + message + "\"); }")); |
|
151 |
Snippet s2 = classKey(assertEval("interface A { default void g() { f(); } }")); |
|
152 |
SnippetEvent cr3 = assertEvalException("new A() { }.g();"); |
|
153 |
assertExceptionMatch(cr3, |
|
154 |
new ExceptionInfo(RuntimeException.class, message, |
|
155 |
newStackTraceElement("", "f", s1, 1), |
|
156 |
newStackTraceElement("A", "g", s2, 1), |
|
157 |
newStackTraceElement("", "", cr3.snippet(), 1))); |
|
158 |
} |
|
159 |
||
160 |
public void throwFromLambda() { |
|
161 |
String message = "lambda"; |
|
162 |
Snippet s1 = varKey(assertEval( |
|
163 |
"Runnable run = () -> {\n" + |
|
164 |
" throw new RuntimeException(\"" + message + "\");\n" + |
|
165 |
"};" |
|
166 |
)); |
|
167 |
SnippetEvent cr2 = assertEvalException("run.run();"); |
|
168 |
assertExceptionMatch(cr2, |
|
169 |
new ExceptionInfo(RuntimeException.class, message, |
|
170 |
newStackTraceElement("", "lambda$", s1, 2), |
|
171 |
newStackTraceElement("", "", cr2.snippet(), 1))); |
|
172 |
} |
|
173 |
||
174 |
public void throwFromAnonymousClass() { |
|
175 |
String message = "anonymous"; |
|
176 |
Snippet s1 = varKey(assertEval( |
|
177 |
"Runnable run = new Runnable() {\n" + |
|
178 |
" public void run() {\n"+ |
|
179 |
" throw new RuntimeException(\"" + message + "\");\n" + |
|
180 |
" }\n" + |
|
181 |
"};" |
|
182 |
)); |
|
183 |
SnippetEvent cr2 = assertEvalException("run.run();"); |
|
184 |
assertExceptionMatch(cr2, |
|
185 |
new ExceptionInfo(RuntimeException.class, message, |
|
186 |
newStackTraceElement("1", "run", s1, 3), |
|
187 |
newStackTraceElement("", "", cr2.snippet(), 1))); |
|
188 |
} |
|
189 |
||
190 |
public void throwFromLocalClass() { |
|
191 |
String message = "local"; |
|
192 |
Snippet s1 = methodKey(assertEval( |
|
193 |
"void f() {\n" + |
|
194 |
" class A {\n" + |
|
195 |
" void f() {\n"+ |
|
196 |
" throw new RuntimeException(\"" + message + "\");\n" + |
|
197 |
" }\n" + |
|
198 |
" }\n" + |
|
199 |
" new A().f();\n" + |
|
200 |
"}" |
|
201 |
)); |
|
202 |
SnippetEvent cr2 = assertEvalException("f();"); |
|
203 |
assertExceptionMatch(cr2, |
|
204 |
new ExceptionInfo(RuntimeException.class, message, |
|
205 |
newStackTraceElement("1A", "f", s1, 4), |
|
206 |
newStackTraceElement("", "f", s1, 7), |
|
207 |
newStackTraceElement("", "", cr2.snippet(), 1))); |
|
208 |
} |
|
209 |
||
210 |
@Test(enabled = false) // TODO 8129427 |
|
211 |
public void outOfMemory() { |
|
212 |
assertEval("import java.util.*;"); |
|
213 |
assertEval("List<byte[]> list = new ArrayList<>();"); |
|
214 |
assertExecuteException("while (true) { list.add(new byte[10000]); }", OutOfMemoryError.class); |
|
215 |
} |
|
216 |
||
217 |
public void stackOverflow() { |
|
218 |
assertEval("void f() { f(); }"); |
|
219 |
assertExecuteException("f();", StackOverflowError.class); |
|
220 |
} |
|
221 |
||
222 |
private StackTraceElement newStackTraceElement(String className, String methodName, Snippet key, int lineNumber) { |
|
223 |
return new StackTraceElement(className, methodName, "#" + key.id(), lineNumber); |
|
224 |
} |
|
225 |
||
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
226 |
private static class AnyExceptionInfo { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
227 |
|
33362 | 228 |
public final StackTraceElement[] stackTraceElements; |
229 |
||
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
230 |
public AnyExceptionInfo(StackTraceElement... stackTraceElements) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
231 |
this.stackTraceElements = stackTraceElements.length == 0 ? null : stackTraceElements; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
232 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
233 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
234 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
235 |
private static class UnresolvedExceptionInfo extends AnyExceptionInfo { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
236 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
237 |
public final Snippet sn; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
238 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
239 |
public UnresolvedExceptionInfo(Snippet sn, StackTraceElement... stackTraceElements) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
240 |
super(stackTraceElements); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
241 |
this.sn = sn; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
242 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
243 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
244 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
245 |
private static class ExceptionInfo extends AnyExceptionInfo { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
246 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
247 |
public final Class<? extends Throwable> exception; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
248 |
public final String message; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
249 |
public final AnyExceptionInfo cause; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
250 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
251 |
public ExceptionInfo(Class<? extends Throwable> exception, String message, |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
252 |
StackTraceElement... stackTraceElements) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
253 |
this(exception, message, null, stackTraceElements); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
254 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
255 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
256 |
public ExceptionInfo(Class<? extends Throwable> exception, String message, |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
257 |
AnyExceptionInfo cause, StackTraceElement... stackTraceElements) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
258 |
super(stackTraceElements); |
33362 | 259 |
this.exception = exception; |
260 |
this.message = message; |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
261 |
this.cause = cause; |
33362 | 262 |
} |
263 |
} |
|
264 |
||
265 |
private void assertExecuteException(String input, Class<? extends Throwable> exception) { |
|
266 |
assertExceptionMatch(assertEvalException(input), new ExceptionInfo(exception, null)); |
|
267 |
} |
|
268 |
||
269 |
private void assertExceptionMatch(SnippetEvent cr, ExceptionInfo exceptionInfo) { |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
270 |
assertExceptionMatch(cr.exception(), cr.snippet().source(), exceptionInfo); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
271 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
272 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
273 |
private void assertExceptionMatch(Throwable exception, String source, ExceptionInfo exceptionInfo) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
274 |
assertNotNull(exception, "Expected exception was not thrown: " + exceptionInfo.exception); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
275 |
if (exception instanceof EvalException) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
276 |
EvalException ex = (EvalException) exception; |
33362 | 277 |
String actualException = ex.getExceptionClassName(); |
278 |
String expectedException = exceptionInfo.exception.getCanonicalName(); |
|
279 |
assertEquals(actualException, expectedException, |
|
280 |
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
|
281 |
source, expectedException, actualException, getStackTrace(ex))); |
33362 | 282 |
if (exceptionInfo.message != null) { |
283 |
assertEquals(ex.getMessage(), exceptionInfo.message, |
|
284 |
String.format("Given \"%s\" expected message: %s, got: %s", |
|
285 |
source, exceptionInfo.message, ex.getMessage())); |
|
286 |
} |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
287 |
assertStackMatch(ex, source, exceptionInfo); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
288 |
if (exceptionInfo.cause != null) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
289 |
assertAnyExceptionMatch(exception.getCause(), exceptionInfo.cause); |
33362 | 290 |
} |
291 |
} else { |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
292 |
fail("Unexpected exception: " + exception + " or exceptionInfo: " + exceptionInfo); |
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 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
295 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
296 |
private void assertStackMatch(JShellException exception, String source, AnyExceptionInfo exceptionInfo) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
297 |
if (exceptionInfo.stackTraceElements != null) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
298 |
assertStackTrace(exception.getStackTrace(), exceptionInfo.stackTraceElements, |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
299 |
String.format("Given \"%s\"%nStack trace:%n%s%n", |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
300 |
source, getStackTrace(exception))); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
301 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
302 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
303 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
304 |
private void assertAnyExceptionMatch(Throwable exception, AnyExceptionInfo exceptionInfo) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
305 |
if (exceptionInfo instanceof ExceptionInfo) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
306 |
assertExceptionMatch(exception, "", (ExceptionInfo) exceptionInfo); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
307 |
} else { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
308 |
assertTrue(exceptionInfo instanceof UnresolvedExceptionInfo, "Bad exceptionInfo: " + exceptionInfo); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
309 |
assertTrue(exception instanceof UnresolvedReferenceException, |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
310 |
"Expected UnresolvedReferenceException: " + exception); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
311 |
UnresolvedExceptionInfo uei = (UnresolvedExceptionInfo) exceptionInfo; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
312 |
UnresolvedReferenceException ure = (UnresolvedReferenceException) exception; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
313 |
assertEquals(ure.getSnippet(), uei.sn); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
314 |
assertStackMatch(ure, "", exceptionInfo); |
33362 | 315 |
} |
316 |
} |
|
317 |
||
318 |
private void assertStackTrace(StackTraceElement[] actual, StackTraceElement[] expected, String message) { |
|
319 |
if (actual != expected) { |
|
320 |
if (actual == null || expected == null) { |
|
321 |
fail(message); |
|
322 |
} else { |
|
323 |
assertEquals(actual.length, expected.length, message + " : arrays do not have the same size"); |
|
324 |
for (int i = 0; i < actual.length; ++i) { |
|
325 |
StackTraceElement actualElement = actual[i]; |
|
326 |
StackTraceElement expectedElement = expected[i]; |
|
327 |
assertEquals(actualElement.getClassName(), expectedElement.getClassName(), message + " : class names"); |
|
328 |
String expectedMethodName = expectedElement.getMethodName(); |
|
329 |
if (expectedMethodName.startsWith("lambda$")) { |
|
330 |
assertTrue(actualElement.getMethodName().startsWith("lambda$"), message + " : method names"); |
|
331 |
} else { |
|
332 |
assertEquals(actualElement.getMethodName(), expectedElement.getMethodName(), message + " : method names"); |
|
333 |
} |
|
334 |
assertEquals(actualElement.getFileName(), expectedElement.getFileName(), message + " : file names"); |
|
335 |
assertEquals(actualElement.getLineNumber(), expectedElement.getLineNumber(), message + " : line numbers"); |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
||
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
47216
diff
changeset
|
341 |
private String getStackTrace(Throwable ex) { |
33362 | 342 |
StringWriter st = new StringWriter(); |
343 |
ex.printStackTrace(new PrintWriter(st)); |
|
344 |
return st.toString(); |
|
345 |
} |
|
346 |
} |