Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Thread/UncaughtExceptionsTest.java
41149 views
1
/*
2
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import jdk.test.lib.process.OutputAnalyzer;
25
import jdk.test.lib.process.ProcessTools;
26
import org.testng.annotations.DataProvider;
27
import org.testng.annotations.Test;
28
29
import static java.lang.System.err;
30
import static java.lang.System.out;
31
32
/*
33
* @test
34
* @bug 4833089 4992454
35
* @summary Check for proper handling of uncaught exceptions
36
* @author Martin Buchholz
37
* @library /test/lib
38
* @build jdk.test.lib.process.*
39
* @run testng UncaughtExceptionsTest
40
*/
41
public class UncaughtExceptionsTest {
42
43
@DataProvider
44
public Object[][] testCases() {
45
return new Object[][]{
46
new Object[] { "ThreadIsDeadAfterJoin",
47
0,
48
UncaughtExitSimulator.EXPECTED_RESULT,
49
"Exception in thread \"Thread-0\".*simulateUncaughtExitEvent"
50
},
51
new Object[] {
52
"MainThreadAbruptTermination",
53
1,
54
UncaughtExitSimulator.EXPECTED_RESULT,
55
"Exception in thread \"main\".*simulateUncaughtExitEvent"
56
},
57
new Object[] { "MainThreadNormalTermination", 0, UncaughtExitSimulator.EXPECTED_RESULT, ""},
58
new Object[] { "DefaultUncaughtExceptionHandlerOnMainThread", 1, UncaughtExitSimulator.EXPECTED_RESULT, "" },
59
new Object[] { "DefaultUncaughtExceptionHandlerOnMainThreadOverride", 1, UncaughtExitSimulator.EXPECTED_RESULT, "" },
60
new Object[] { "DefaultUncaughtExceptionHandlerOnNonMainThreadOverride", 0, UncaughtExitSimulator.EXPECTED_RESULT, "" },
61
new Object[] { "DefaultUncaughtExceptionHandlerOnNonMainThread", 0, UncaughtExitSimulator.EXPECTED_RESULT, "" },
62
new Object[] { "ThreadGroupUncaughtExceptionHandlerOnNonMainThread", 0, UncaughtExitSimulator.EXPECTED_RESULT, "" }
63
};
64
}
65
66
@Test(dataProvider = "testCases")
67
public void test(String className, int exitValue, String stdOutMatch, String stdErrMatch) throws Throwable {
68
ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(String.format("UncaughtExitSimulator$%s",className));
69
OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(processBuilder);
70
outputAnalyzer.shouldHaveExitValue(exitValue);
71
outputAnalyzer.stderrShouldMatch(stdErrMatch);
72
outputAnalyzer.stdoutShouldMatch(stdOutMatch);
73
}
74
75
}
76
77
78
class OK implements Thread.UncaughtExceptionHandler {
79
public void uncaughtException(Thread t, Throwable e) {
80
out.println(UncaughtExitSimulator.EXPECTED_RESULT);
81
}
82
}
83
84
class NeverInvoked implements Thread.UncaughtExceptionHandler {
85
public void uncaughtException(Thread t, Throwable e) {
86
err.println("Test failure: This handler should never be invoked!");
87
}
88
}
89
90
class UncaughtExitSimulator extends Thread implements Runnable {
91
92
final static String EXPECTED_RESULT = "OK";
93
94
public static void throwRuntimeException() { throw new RuntimeException("simulateUncaughtExitEvent"); }
95
96
public void run() { throwRuntimeException(); }
97
98
/**
99
* A thread is never alive after you've join()ed it.
100
*/
101
public static class ThreadIsDeadAfterJoin extends UncaughtExitSimulator {
102
public static void main(String[] args) throws Exception {
103
Thread t = new UncaughtExitSimulator();
104
t.start(); t.join();
105
if (! t.isAlive()) {
106
out.println(EXPECTED_RESULT);
107
}
108
}
109
}
110
111
/**
112
* Even the main thread is mortal - here it terminates "abruptly"
113
*/
114
public static class MainThreadAbruptTermination extends UncaughtExitSimulator {
115
public static void main(String[] args) {
116
final Thread mainThread = currentThread();
117
new Thread() { public void run() {
118
try { mainThread.join(); }
119
catch (InterruptedException e) {}
120
if (! mainThread.isAlive())
121
out.println(EXPECTED_RESULT);
122
}}.start();
123
throwRuntimeException();
124
}
125
}
126
127
/**
128
* Even the main thread is mortal - here it terminates normally.
129
*/
130
public static class MainThreadNormalTermination extends UncaughtExitSimulator {
131
public static void main(String[] args) {
132
final Thread mainThread = currentThread();
133
new Thread() {
134
public void run() {
135
try {
136
mainThread.join();
137
} catch (InterruptedException e) {
138
}
139
if (!mainThread.isAlive())
140
out.println(EXPECTED_RESULT);
141
}
142
}.start();
143
}
144
}
145
146
/**
147
* Check uncaught exception handler mechanism on the main thread.
148
*/
149
public static class DefaultUncaughtExceptionHandlerOnMainThread extends UncaughtExitSimulator {
150
public static void main(String[] args) {
151
currentThread().setUncaughtExceptionHandler(new OK());
152
setDefaultUncaughtExceptionHandler(new NeverInvoked());
153
throwRuntimeException();
154
}
155
}
156
157
/**
158
* Check that thread-level handler overrides global default handler.
159
*/
160
public static class DefaultUncaughtExceptionHandlerOnMainThreadOverride extends UncaughtExitSimulator {
161
public static void main(String[] args) {
162
setDefaultUncaughtExceptionHandler(new OK());
163
throwRuntimeException();
164
}
165
}
166
167
/**
168
* Check uncaught exception handler mechanism on non-main threads.
169
*/
170
public static class DefaultUncaughtExceptionHandlerOnNonMainThreadOverride extends UncaughtExitSimulator {
171
public static void main(String[] args) {
172
Thread t = new UncaughtExitSimulator();
173
t.setUncaughtExceptionHandler(new OK());
174
t.start();
175
}
176
}
177
178
/**
179
* Check uncaught exception handler mechanism on non-main threads.
180
*/
181
public static class DefaultUncaughtExceptionHandlerOnNonMainThread extends UncaughtExitSimulator {
182
public static void main(String[] args) {
183
setDefaultUncaughtExceptionHandler(new OK());
184
new UncaughtExitSimulator().start();
185
}
186
}
187
188
/**
189
* Test ThreadGroup based uncaught exception handler mechanism.
190
* Since the handler for the main thread group cannot be changed,
191
* there are no tests for the main thread here.
192
*/
193
public static class ThreadGroupUncaughtExceptionHandlerOnNonMainThread extends UncaughtExitSimulator {
194
public static void main(String[] args) {
195
setDefaultUncaughtExceptionHandler(new NeverInvoked());
196
new Thread(
197
new ThreadGroup(EXPECTED_RESULT) {
198
public void uncaughtException(Thread t, Throwable e) {
199
out.println(EXPECTED_RESULT);
200
}
201
},
202
new UncaughtExitSimulator()
203
).start();
204
}
205
}
206
}
207
208