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.TestCase;
034: import java.util.Map;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class _ThreadTestCase extends TestCase {
040:
041: private Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: defaultUncaughtExceptionHandler = Thread
046: .getDefaultUncaughtExceptionHandler();
047: }
048:
049: protected void tearDown() throws Exception {
050: Thread
051: .setDefaultUncaughtExceptionHandler(defaultUncaughtExceptionHandler);
052: super .tearDown();
053: }
054:
055: private static class MyHandler implements
056: Thread.UncaughtExceptionHandler {
057:
058: public Thread thread;
059: public Throwable exception;
060:
061: public void uncaughtException(Thread t, Throwable e) {
062: thread = t;
063: exception = e;
064: }
065: }
066:
067: private static class MyThread extends Thread {
068:
069: public boolean done;
070:
071: public void run() {
072: try {
073: throw new Exception();
074: } catch (Exception e) {
075: //ok
076: }
077: done = true;
078: }
079: }
080:
081: public void testGetStackTrace() throws Exception {
082: StackTraceElement[] stackTrace = Thread.currentThread()
083: .getStackTrace();
084: String className = _ThreadTestCase.class.getName();
085: StackTraceElement element = null;
086: for (int i = 1; i < stackTrace.length; i++) {
087: element = stackTrace[i];
088: if (className.equals(element.getClassName()))
089: break;
090: }
091: assertNotNull(element);
092: assertEquals("testGetStackTrace", element.getMethodName());
093: for (StackTraceElement stackTraceElement : stackTrace) {
094: assertNotNull(stackTraceElement);
095: }
096: }
097:
098: public void testGetId() throws Exception {
099: long currentId = Thread.currentThread().getId();
100: assertTrue(currentId > 0);
101: Thread thread = new Thread();
102: long newId = thread.getId();
103: assertTrue(newId > 0);
104: assertTrue(currentId != newId);
105: assertEquals(currentId, Thread.currentThread().getId());
106: assertEquals(newId, thread.getId());
107: }
108:
109: public void testGetId_Custom() throws Exception {
110: class MyThread extends Thread {
111: public int hashCode() {
112: return 0;
113: }
114:
115: public boolean equals(Object obj) {
116: return true;
117: }
118: }
119: Thread thread1 = new MyThread();
120: Thread thread2 = new MyThread();
121: assertEquals(thread1.getId(), thread1.getId());
122: assertEquals(thread2.getId(), thread2.getId());
123: assertTrue(thread1.getId() != Thread.currentThread().getId());
124: assertTrue(thread2.getId() != Thread.currentThread().getId());
125: assertTrue(thread1.getId() != thread2.getId());
126: }
127:
128: public void testSetUncaughtExceptionHandler() throws Exception {
129: final ThreadGroup group = Thread.currentThread()
130: .getThreadGroup();
131: final IllegalStateException exception = new IllegalStateException(
132: "test");
133: Runnable runnable = new Runnable() {
134: public void run() {
135: throw exception;
136: }
137: };
138: Thread[] threads = { new Thread() {
139: public void run() {
140: throw exception;
141: }
142: }, new Thread(runnable), new Thread(group, runnable),
143: new Thread("name") {
144: public void run() {
145: throw exception;
146: }
147: }, new Thread(group, "name") {
148: public void run() {
149: try {
150: throw new RuntimeException();
151: } catch (RuntimeException e) {
152: throw exception;
153: }
154: }
155: }, new Thread(runnable, "name"),
156: new Thread(group, runnable, "name"),
157: createThreadWithStackSize(group, runnable), };
158: for (Thread thread : threads) {
159: MyHandler handler = new MyHandler();
160: assertSame(thread.getThreadGroup(), thread
161: .getUncaughtExceptionHandler());
162: thread.setUncaughtExceptionHandler(handler);
163: assertSame(handler, thread.getUncaughtExceptionHandler());
164: thread.start();
165: thread.join();
166: assertSame(thread, handler.thread);
167: assertSame(exception, handler.exception);
168: }
169: }
170:
171: protected Thread createThreadWithStackSize(ThreadGroup group,
172: Runnable runnable) {
173: return new Thread(group, runnable, "name");
174: }
175:
176: public void testSetUncaughtExceptionHandler_NoException()
177: throws Exception {
178: MyThread myThread = new MyThread();
179: MyHandler myHandler = new MyHandler();
180: ((Thread) myThread).setUncaughtExceptionHandler(myHandler);
181: assertFalse(myThread.done);
182: myThread.start();
183: myThread.join();
184: assertTrue(myThread.done);
185: assertNull(myHandler.thread);
186: assertNull(myHandler.exception);
187: Thread thread = new Thread() {
188: public void run() {
189: }
190: };
191: thread.start();
192: thread.join();
193: }
194:
195: public void testSetDefaultUncaughtExceptionHandler()
196: throws Exception {
197: final ThreadGroup group = Thread.currentThread()
198: .getThreadGroup();
199: final IllegalStateException exception = new IllegalStateException(
200: "test");
201: Runnable runnable = new Runnable() {
202: public void run() {
203: throw exception;
204: }
205: };
206: Thread[] threads = { new Thread() {
207: public void run() {
208: throw exception;
209: }
210: }, new Thread(runnable), new Thread(group, runnable),
211: new Thread("name") {
212: public void run() {
213: throw exception;
214: }
215: }, new Thread(group, "name") {
216: public void run() {
217: throw exception;
218: }
219: }, new Thread(runnable, "name"),
220: new Thread(group, runnable, "name"),
221: createThreadWithStackSize(group, runnable), };
222: for (Thread thread : threads) {
223: MyHandler handler = new MyHandler();
224: Thread.setDefaultUncaughtExceptionHandler(handler);
225: assertSame(handler, Thread
226: .getDefaultUncaughtExceptionHandler());
227: try {
228: thread.run();
229: } catch (IllegalStateException e) {
230: assertSame(exception, e);
231: }
232: assertNull(handler.thread);
233: assertNull(handler.exception);
234: }
235: for (Thread thread : threads) {
236: MyHandler handler = new MyHandler();
237: Thread.setDefaultUncaughtExceptionHandler(handler);
238: assertSame(handler, Thread
239: .getDefaultUncaughtExceptionHandler());
240: thread.start();
241: thread.join();
242: assertSame(thread, handler.thread);
243: assertSame(exception, handler.exception);
244: }
245: }
246:
247: public void testSetDefaultUncaughtExceptionHandler_NoException()
248: throws Exception {
249: MyThread myThread = new MyThread();
250: MyHandler myHandler = new MyHandler();
251: Thread.setDefaultUncaughtExceptionHandler(myHandler);
252: assertFalse(myThread.done);
253: myThread.start();
254: myThread.join();
255: assertTrue(myThread.done);
256: assertNull(myHandler.thread);
257: assertNull(myHandler.exception);
258: }
259:
260: public void testGetState() throws Exception {
261: assertSame(Thread.State.RUNNABLE, Thread.currentThread()
262: .getState());
263: Thread thread = new Thread() {
264: public void run() {
265: try {
266: Thread.sleep(1000);
267: } catch (InterruptedException e) {
268: // ignore
269: }
270: }
271: };
272: assertSame(Thread.State.NEW, thread.getState());
273: thread.start();
274: assertSame(Thread.State.RUNNABLE, thread.getState());
275: thread.join();
276: assertSame(Thread.State.TERMINATED, thread.getState());
277: }
278:
279: public void testGetAllStackTraces() throws Exception {
280: Map<Thread, StackTraceElement[]> map = Thread
281: .getAllStackTraces();
282: assertTrue(map.size() > 1);
283: StackTraceElement[] elements = map.get(Thread.currentThread());
284: assertNotNull(elements);
285: assertTrue(elements.length > 0);
286: }
287:
288: }
|