001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import junit.framework.*;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class _Thread_UncaughtExceptionHandlerTestCase extends TestCase {
039:
040: private static class MyHandler implements
041: Thread.UncaughtExceptionHandler {
042:
043: public Thread thread;
044: public Throwable exception;
045:
046: public void uncaughtException(Thread t, Throwable e) {
047: thread = t;
048: exception = e;
049: }
050: }
051:
052: private static class MyGroup extends ThreadGroup {
053:
054: public Thread thread;
055: public Throwable exception;
056:
057: public MyGroup() {
058: super ("test");
059: }
060:
061: public void uncaughtException(Thread t, Throwable e) {
062: thread = t;
063: exception = e;
064: }
065: }
066:
067: public void testExecuteInstanceOfInstruction() throws Exception {
068: assertTrue(new MyHandler() instanceof Thread.UncaughtExceptionHandler);
069: assertTrue(new MyGroup() instanceof Thread.UncaughtExceptionHandler);
070: assertTrue(new ThreadGroup("test") instanceof Thread.UncaughtExceptionHandler);
071: assertFalse(new Object() instanceof Thread.UncaughtExceptionHandler);
072: }
073:
074: public void testExecuteCheckCastInstruction() throws Exception {
075: Object handler = new MyHandler();
076: ((Thread.UncaughtExceptionHandler) handler).uncaughtException(
077: null, null);
078: Object group = new MyGroup();
079: ((Thread.UncaughtExceptionHandler) group).uncaughtException(
080: null, null);
081: Object string = "test";
082: try {
083: ((Thread.UncaughtExceptionHandler) string)
084: .uncaughtException(null, null);
085: fail();
086: } catch (ClassCastException e) {
087: //ok
088: }
089: }
090:
091: public void testUncaughtException_Handler() throws Exception {
092: MyHandler handler = new MyHandler();
093: Thread thread = new Thread();
094: Exception exception = new Exception();
095: dispatchException(handler, thread, exception);
096: assertSame(thread, handler.thread);
097: assertSame(exception, handler.exception);
098: }
099:
100: public void testUncaughtException_Group() throws Exception {
101: MyGroup group = new MyGroup();
102: Thread thread = new Thread();
103: Exception exception = new Exception();
104: dispatchException(group, thread, exception);
105: assertSame(thread, group.thread);
106: assertSame(exception, group.exception);
107: }
108:
109: private void dispatchException(
110: Thread.UncaughtExceptionHandler handler, Thread thread,
111: Exception exception) {
112: handler.uncaughtException(thread, exception);
113: }
114:
115: }
|