0N/A#!/bin/sh
0N/A
0N/A#
2362N/A# Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A#
0N/A# This code is free software; you can redistribute it and/or modify it
0N/A# under the terms of the GNU General Public License version 2 only, as
0N/A# published by the Free Software Foundation.
0N/A#
0N/A# This code is distributed in the hope that it will be useful, but WITHOUT
0N/A# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A# version 2 for more details (a copy is included in the LICENSE file that
0N/A# accompanied this code).
0N/A#
0N/A# You should have received a copy of the GNU General Public License version
0N/A# 2 along with this work; if not, write to the Free Software Foundation,
0N/A# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A#
2362N/A# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A# or visit www.oracle.com if you need additional information or have any
2362N/A# questions.
0N/A#
0N/A
0N/A#
0N/A# @test
0N/A# @bug 4833089 4992454
0N/A# @summary Check for proper handling of uncaught exceptions
0N/A# @author Martin Buchholz
0N/A#
0N/A# @run shell UncaughtExceptions.sh
0N/A
0N/A# To run this test manually, simply do ./UncaughtExceptions.sh
0N/A
0N/A java="${TESTJAVA+${TESTJAVA}/bin/}java"
0N/Ajavac="${TESTJAVA+${TESTJAVA}/bin/}javac"
0N/A
0N/Afailed=""
0N/AFail() { echo "FAIL: $1"; failed="${failed}."; }
0N/A
0N/ADie() { printf "%s\n" "$*"; exit 1; }
0N/A
0N/ASys() {
0N/A "$@"; rc="$?";
0N/A test "$rc" -eq 0 || Die "Command \"$*\" failed with exitValue $rc";
0N/A}
0N/A
0N/AHorizontalRule() {
0N/A echo "-----------------------------------------------------------------"
0N/A}
0N/A
0N/ABottom() {
0N/A test "$#" = 1 -a "$1" = "Line" || Die "Usage: Bottom Line"
0N/A
0N/A HorizontalRule
0N/A if test -n "$failed"; then
0N/A count=`printf "%s" "$failed" | wc -c | tr -d ' '`
0N/A echo "FAIL: $count tests failed"
0N/A exit 1
0N/A else
0N/A echo "PASS: all tests gave expected results"
0N/A exit 0
0N/A fi
0N/A}
0N/A
0N/ACleanup() { Sys rm -f Seppuku* OK.class; }
0N/A
0N/Aset -u
0N/A
0N/AcheckOutput() {
0N/A name="$1" expected="$2" got="$3"
0N/A printf "$name:\n"; cat "$got"
0N/A if test -z "$expected"; then
0N/A test "`cat $got`" != "" && \
0N/A Fail "Unexpected $name: `cat $got`"
0N/A else
0N/A grep "$expected" "$got" >/dev/null || \
0N/A Fail "Expected \"$expected\", got `cat $got`"
0N/A fi
0N/A}
0N/A
0N/ACheckCommandResults() {
0N/A expectedRC="$1" expectedOut="$2" expectedErr="$3"; shift 3
0N/A saveFailed="${failed}"
0N/A "$@" >TmpTest.Out 2>TmpTest.Err; rc="$?";
0N/A printf "==> %s (rc=%d)\n" "$*" "$rc"
0N/A checkOutput "stdout" "$expectedOut" "TmpTest.Out"
0N/A checkOutput "stderr" "$expectedErr" "TmpTest.Err"
0N/A test "${saveFailed}" = "${failed}" && \
0N/A echo "PASS: command completed as expected"
0N/A Sys rm -f TmpTest.Out TmpTest.Err
0N/A}
0N/A
0N/ARun() {
0N/A expectedRC="$1" expectedOut="$2" expectedErr="$3" mainBody="$4"
0N/A cat > Seppuku.java <<EOJAVA
0N/Aimport static java.lang.Thread.*;
0N/Aimport static java.lang.System.*;
0N/A
0N/Aclass OK implements UncaughtExceptionHandler {
0N/A public void uncaughtException(Thread t, Throwable e) {
0N/A out.println("OK");
0N/A }
0N/A}
0N/A
0N/Aclass NeverInvoked implements UncaughtExceptionHandler {
0N/A public void uncaughtException(Thread t, Throwable e) {
0N/A err.println("Test failure: This handler should never be invoked!");
0N/A }
0N/A}
0N/A
0N/Apublic class Seppuku extends Thread implements Runnable {
0N/A public static void seppuku() { throw new RuntimeException("Seppuku!"); }
0N/A
0N/A public void run() { seppuku(); }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A $mainBody
0N/A }
0N/A}
0N/AEOJAVA
0N/A
1952N/A Sys "$javac" "Seppuku.java"
0N/A CheckCommandResults "$expectedRC" "$expectedOut" "$expectedErr" \
0N/A "$java" "Seppuku"
0N/A Cleanup
0N/A}
0N/A
0N/A#----------------------------------------------------------------
0N/A# A thread is never alive after you've join()ed it.
0N/A#----------------------------------------------------------------
0N/ARun 0 "OK" "Exception in thread \"Thread-0\".*Seppuku" "
0N/A Thread t = new Seppuku();
0N/A t.start(); t.join();
0N/A if (! t.isAlive())
0N/A out.println(\"OK\");"
0N/A
0N/A#----------------------------------------------------------------
0N/A# Even the main thread is mortal - here it terminates "abruptly"
0N/A#----------------------------------------------------------------
0N/ARun 1 "OK" "Exception in thread \"main\".*Seppuku" "
0N/A final Thread mainThread = currentThread();
0N/A new Thread() { public void run() {
0N/A try { mainThread.join(); }
0N/A catch (InterruptedException e) {}
0N/A if (! mainThread.isAlive())
0N/A out.println(\"OK\");
0N/A }}.start();
0N/A seppuku();"
0N/A
0N/A#----------------------------------------------------------------
0N/A# Even the main thread is mortal - here it terminates normally.
0N/A#----------------------------------------------------------------
0N/ARun 0 "OK" "" "
0N/A final Thread mainThread = currentThread();
0N/A new Thread() { public void run() {
0N/A try { mainThread.join(); }
0N/A catch (InterruptedException e) {}
0N/A if (! mainThread.isAlive())
0N/A out.println(\"OK\");
0N/A }}.start();"
0N/A
0N/A#----------------------------------------------------------------
0N/A# Check uncaught exception handler mechanism on the main thread.
0N/A# Check that thread-level handler overrides global default handler.
0N/A#----------------------------------------------------------------
0N/ARun 1 "OK" "" "
0N/A currentThread().setUncaughtExceptionHandler(new OK());
0N/A setDefaultUncaughtExceptionHandler(new NeverInvoked());
0N/A seppuku();"
0N/A
0N/ARun 1 "OK" "" "
0N/A setDefaultUncaughtExceptionHandler(new OK());
0N/A seppuku();"
0N/A
0N/A#----------------------------------------------------------------
0N/A# Check uncaught exception handler mechanism on non-main threads.
0N/A#----------------------------------------------------------------
0N/ARun 0 "OK" "" "
0N/A Thread t = new Seppuku();
0N/A t.setUncaughtExceptionHandler(new OK());
0N/A t.start();"
0N/A
0N/ARun 0 "OK" "" "
0N/A setDefaultUncaughtExceptionHandler(new OK());
0N/A new Seppuku().start();"
0N/A
0N/A#----------------------------------------------------------------
0N/A# Test ThreadGroup based uncaught exception handler mechanism.
0N/A# Since the handler for the main thread group cannot be changed,
0N/A# there are no tests for the main thread here.
0N/A#----------------------------------------------------------------
0N/ARun 0 "OK" "" "
0N/A setDefaultUncaughtExceptionHandler(new NeverInvoked());
0N/A new Thread(
0N/A new ThreadGroup(\"OK\") {
0N/A public void uncaughtException(Thread t, Throwable e) {
0N/A out.println(\"OK\");}},
0N/A new Seppuku()
0N/A ).start();"
0N/A
0N/ACleanup
0N/A
0N/ABottom Line