Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Thread/UncaughtExceptions.sh
38812 views
#!/bin/sh12#3# Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.5#6# This code is free software; you can redistribute it and/or modify it7# under the terms of the GNU General Public License version 2 only, as8# published by the Free Software Foundation.9#10# This code is distributed in the hope that it will be useful, but WITHOUT11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13# version 2 for more details (a copy is included in the LICENSE file that14# accompanied this code).15#16# You should have received a copy of the GNU General Public License version17# 2 along with this work; if not, write to the Free Software Foundation,18# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19#20# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21# or visit www.oracle.com if you need additional information or have any22# questions.23#2425#26# @test27# @bug 4833089 499245428# @summary Check for proper handling of uncaught exceptions29# @author Martin Buchholz30#31# @run shell UncaughtExceptions.sh3233# To run this test manually, simply do ./UncaughtExceptions.sh3435java="${TESTJAVA+${TESTJAVA}/bin/}java"36javac="${COMPILEJAVA+${COMPILEJAVA}/bin/}javac"3738failed=""39Fail() { echo "FAIL: $1"; failed="${failed}."; }4041Die() { printf "%s\n" "$*"; exit 1; }4243Sys() {44"$@"; rc="$?";45test "$rc" -eq 0 || Die "Command \"$*\" failed with exitValue $rc";46}4748HorizontalRule() {49echo "-----------------------------------------------------------------"50}5152Bottom() {53test "$#" = 1 -a "$1" = "Line" || Die "Usage: Bottom Line"5455HorizontalRule56if test -n "$failed"; then57count=`printf "%s" "$failed" | wc -c | tr -d ' '`58echo "FAIL: $count tests failed"59exit 160else61echo "PASS: all tests gave expected results"62exit 063fi64}6566Cleanup() { Sys rm -f Seppuku* OK.class; }6768set -u6970checkOutput() {71name="$1" expected="$2" got="$3"72printf "$name:\n"; cat "$got"73if test -z "$expected"; then74test "`cat $got`" != "" && \75Fail "Unexpected $name: `cat $got`"76else77grep "$expected" "$got" >/dev/null || \78Fail "Expected \"$expected\", got `cat $got`"79fi80}8182CheckCommandResults() {83expectedRC="$1" expectedOut="$2" expectedErr="$3"; shift 384saveFailed="${failed}"85"$@" >TmpTest.Out 2>TmpTest.Err; rc="$?";86printf "==> %s (rc=%d)\n" "$*" "$rc"87checkOutput "stdout" "$expectedOut" "TmpTest.Out"88checkOutput "stderr" "$expectedErr" "TmpTest.Err"89test "${saveFailed}" = "${failed}" && \90echo "PASS: command completed as expected"91Sys rm -f TmpTest.Out TmpTest.Err92}9394Run() {95expectedRC="$1" expectedOut="$2" expectedErr="$3" mainBody="$4"96cat > Seppuku.java <<EOJAVA97import static java.lang.Thread.*;98import static java.lang.System.*;99100class OK implements UncaughtExceptionHandler {101public void uncaughtException(Thread t, Throwable e) {102out.println("OK");103}104}105106class NeverInvoked implements UncaughtExceptionHandler {107public void uncaughtException(Thread t, Throwable e) {108err.println("Test failure: This handler should never be invoked!");109}110}111112public class Seppuku extends Thread implements Runnable {113public static void seppuku() { throw new RuntimeException("Seppuku!"); }114115public void run() { seppuku(); }116117public static void main(String[] args) throws Exception {118$mainBody119}120}121EOJAVA122123Sys "$javac" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} "Seppuku.java"124CheckCommandResults "$expectedRC" "$expectedOut" "$expectedErr" \125"$java" "Seppuku"126Cleanup127}128129#----------------------------------------------------------------130# A thread is never alive after you've join()ed it.131#----------------------------------------------------------------132Run 0 "OK" "Exception in thread \"Thread-0\".*Seppuku" "133Thread t = new Seppuku();134t.start(); t.join();135if (! t.isAlive())136out.println(\"OK\");"137138#----------------------------------------------------------------139# Even the main thread is mortal - here it terminates "abruptly"140#----------------------------------------------------------------141Run 1 "OK" "Exception in thread \"main\".*Seppuku" "142final Thread mainThread = currentThread();143new Thread() { public void run() {144try { mainThread.join(); }145catch (InterruptedException e) {}146if (! mainThread.isAlive())147out.println(\"OK\");148}}.start();149seppuku();"150151#----------------------------------------------------------------152# Even the main thread is mortal - here it terminates normally.153#----------------------------------------------------------------154Run 0 "OK" "" "155final Thread mainThread = currentThread();156new Thread() { public void run() {157try { mainThread.join(); }158catch (InterruptedException e) {}159if (! mainThread.isAlive())160out.println(\"OK\");161}}.start();"162163#----------------------------------------------------------------164# Check uncaught exception handler mechanism on the main thread.165# Check that thread-level handler overrides global default handler.166#----------------------------------------------------------------167Run 1 "OK" "" "168currentThread().setUncaughtExceptionHandler(new OK());169setDefaultUncaughtExceptionHandler(new NeverInvoked());170seppuku();"171172Run 1 "OK" "" "173setDefaultUncaughtExceptionHandler(new OK());174seppuku();"175176#----------------------------------------------------------------177# Check uncaught exception handler mechanism on non-main threads.178#----------------------------------------------------------------179Run 0 "OK" "" "180Thread t = new Seppuku();181t.setUncaughtExceptionHandler(new OK());182t.start();"183184Run 0 "OK" "" "185setDefaultUncaughtExceptionHandler(new OK());186new Seppuku().start();"187188#----------------------------------------------------------------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#----------------------------------------------------------------193Run 0 "OK" "" "194setDefaultUncaughtExceptionHandler(new NeverInvoked());195new Thread(196new ThreadGroup(\"OK\") {197public void uncaughtException(Thread t, Throwable e) {198out.println(\"OK\");}},199new Seppuku()200).start();"201202Cleanup203204Bottom Line205206207