Path: blob/master/test/jdk/java/lang/Thread/UncaughtExceptionsTest.java
41149 views
/*1* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import jdk.test.lib.process.OutputAnalyzer;24import jdk.test.lib.process.ProcessTools;25import org.testng.annotations.DataProvider;26import org.testng.annotations.Test;2728import static java.lang.System.err;29import static java.lang.System.out;3031/*32* @test33* @bug 4833089 499245434* @summary Check for proper handling of uncaught exceptions35* @author Martin Buchholz36* @library /test/lib37* @build jdk.test.lib.process.*38* @run testng UncaughtExceptionsTest39*/40public class UncaughtExceptionsTest {4142@DataProvider43public Object[][] testCases() {44return new Object[][]{45new Object[] { "ThreadIsDeadAfterJoin",460,47UncaughtExitSimulator.EXPECTED_RESULT,48"Exception in thread \"Thread-0\".*simulateUncaughtExitEvent"49},50new Object[] {51"MainThreadAbruptTermination",521,53UncaughtExitSimulator.EXPECTED_RESULT,54"Exception in thread \"main\".*simulateUncaughtExitEvent"55},56new Object[] { "MainThreadNormalTermination", 0, UncaughtExitSimulator.EXPECTED_RESULT, ""},57new Object[] { "DefaultUncaughtExceptionHandlerOnMainThread", 1, UncaughtExitSimulator.EXPECTED_RESULT, "" },58new Object[] { "DefaultUncaughtExceptionHandlerOnMainThreadOverride", 1, UncaughtExitSimulator.EXPECTED_RESULT, "" },59new Object[] { "DefaultUncaughtExceptionHandlerOnNonMainThreadOverride", 0, UncaughtExitSimulator.EXPECTED_RESULT, "" },60new Object[] { "DefaultUncaughtExceptionHandlerOnNonMainThread", 0, UncaughtExitSimulator.EXPECTED_RESULT, "" },61new Object[] { "ThreadGroupUncaughtExceptionHandlerOnNonMainThread", 0, UncaughtExitSimulator.EXPECTED_RESULT, "" }62};63}6465@Test(dataProvider = "testCases")66public void test(String className, int exitValue, String stdOutMatch, String stdErrMatch) throws Throwable {67ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(String.format("UncaughtExitSimulator$%s",className));68OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(processBuilder);69outputAnalyzer.shouldHaveExitValue(exitValue);70outputAnalyzer.stderrShouldMatch(stdErrMatch);71outputAnalyzer.stdoutShouldMatch(stdOutMatch);72}7374}757677class OK implements Thread.UncaughtExceptionHandler {78public void uncaughtException(Thread t, Throwable e) {79out.println(UncaughtExitSimulator.EXPECTED_RESULT);80}81}8283class NeverInvoked implements Thread.UncaughtExceptionHandler {84public void uncaughtException(Thread t, Throwable e) {85err.println("Test failure: This handler should never be invoked!");86}87}8889class UncaughtExitSimulator extends Thread implements Runnable {9091final static String EXPECTED_RESULT = "OK";9293public static void throwRuntimeException() { throw new RuntimeException("simulateUncaughtExitEvent"); }9495public void run() { throwRuntimeException(); }9697/**98* A thread is never alive after you've join()ed it.99*/100public static class ThreadIsDeadAfterJoin extends UncaughtExitSimulator {101public static void main(String[] args) throws Exception {102Thread t = new UncaughtExitSimulator();103t.start(); t.join();104if (! t.isAlive()) {105out.println(EXPECTED_RESULT);106}107}108}109110/**111* Even the main thread is mortal - here it terminates "abruptly"112*/113public static class MainThreadAbruptTermination extends UncaughtExitSimulator {114public static void main(String[] args) {115final Thread mainThread = currentThread();116new Thread() { public void run() {117try { mainThread.join(); }118catch (InterruptedException e) {}119if (! mainThread.isAlive())120out.println(EXPECTED_RESULT);121}}.start();122throwRuntimeException();123}124}125126/**127* Even the main thread is mortal - here it terminates normally.128*/129public static class MainThreadNormalTermination extends UncaughtExitSimulator {130public static void main(String[] args) {131final Thread mainThread = currentThread();132new Thread() {133public void run() {134try {135mainThread.join();136} catch (InterruptedException e) {137}138if (!mainThread.isAlive())139out.println(EXPECTED_RESULT);140}141}.start();142}143}144145/**146* Check uncaught exception handler mechanism on the main thread.147*/148public static class DefaultUncaughtExceptionHandlerOnMainThread extends UncaughtExitSimulator {149public static void main(String[] args) {150currentThread().setUncaughtExceptionHandler(new OK());151setDefaultUncaughtExceptionHandler(new NeverInvoked());152throwRuntimeException();153}154}155156/**157* Check that thread-level handler overrides global default handler.158*/159public static class DefaultUncaughtExceptionHandlerOnMainThreadOverride extends UncaughtExitSimulator {160public static void main(String[] args) {161setDefaultUncaughtExceptionHandler(new OK());162throwRuntimeException();163}164}165166/**167* Check uncaught exception handler mechanism on non-main threads.168*/169public static class DefaultUncaughtExceptionHandlerOnNonMainThreadOverride extends UncaughtExitSimulator {170public static void main(String[] args) {171Thread t = new UncaughtExitSimulator();172t.setUncaughtExceptionHandler(new OK());173t.start();174}175}176177/**178* Check uncaught exception handler mechanism on non-main threads.179*/180public static class DefaultUncaughtExceptionHandlerOnNonMainThread extends UncaughtExitSimulator {181public static void main(String[] args) {182setDefaultUncaughtExceptionHandler(new OK());183new UncaughtExitSimulator().start();184}185}186187/**188* Test ThreadGroup based uncaught exception handler mechanism.189* Since the handler for the main thread group cannot be changed,190* there are no tests for the main thread here.191*/192public static class ThreadGroupUncaughtExceptionHandlerOnNonMainThread extends UncaughtExitSimulator {193public static void main(String[] args) {194setDefaultUncaughtExceptionHandler(new NeverInvoked());195new Thread(196new ThreadGroup(EXPECTED_RESULT) {197public void uncaughtException(Thread t, Throwable e) {198out.println(EXPECTED_RESULT);199}200},201new UncaughtExitSimulator()202).start();203}204}205}206207208