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.runtime13.v14.java.lang;
032:
033: import java.awt.print.PrinterIOException;
034: import java.io.*;
035: import java.lang.reflect.*;
036: import java.rmi.RemoteException;
037: import java.rmi.activation.ActivationException;
038: import java.rmi.server.ServerCloneException;
039: import java.security.PrivilegedActionException;
040: import java.util.*;
041: import javax.naming.NamingException;
042: import junit.framework.TestCase;
043:
044: /**
045: * @author Taras Puchko
046: */
047: public class _ThrowableTestCase extends TestCase {
048:
049: public void testCreateInstanceBuilder_OneArg() throws Exception {
050: IOException exception = new IOException("123");
051: Throwable throwable = new Throwable(exception);
052: assertEquals(exception.toString(), throwable.getMessage());
053: assertEquals("123", throwable.getCause().getMessage());
054: }
055:
056: public void testCreateInstanceBuilder_NullArg() throws Exception {
057: IOException exception = null;
058: Throwable throwable = new Throwable(exception);
059: assertNull(throwable.getMessage());
060: assertNull(throwable.getCause());
061: }
062:
063: public void testCreateInstanceBuilder_TwoArgs() throws Exception {
064: Throwable throwable = new Throwable("abc", new IOException(
065: "123"));
066: assertEquals("abc", throwable.getMessage());
067: assertEquals("123", throwable.getCause().getMessage());
068: }
069:
070: public void testGetCause() throws Exception {
071: IOException exception = new IOException("123");
072: assertSame(exception, new Throwable(exception).getCause());
073: NamingException namingException = new NamingException();
074: namingException.initCause(exception);
075: assertSame(exception, ((Throwable) namingException).getCause());
076: for (Throwable throwable : getSpecialThrowables(exception)) {
077: assertSame(exception, throwable.getCause());
078: }
079: }
080:
081: public void testInitCause() throws Exception {
082: Throwable throwable = new Throwable("abc");
083: assertNull(throwable.getCause());
084: try {
085: throwable.initCause(throwable);
086: fail();
087: } catch (IllegalArgumentException e) {
088: //ok
089: }
090: IOException exception = new IOException("123");
091: throwable.initCause(exception);
092: assertSame(exception, throwable.getCause());
093: try {
094: throwable.initCause(new Exception("def"));
095: fail();
096: } catch (IllegalStateException e) {
097: //ok
098: }
099: }
100:
101: public void testInitCause_Special() throws Exception {
102: IOException exception = new IOException("123");
103: for (Throwable specialThrowable : getSpecialThrowables(exception)) {
104: try {
105: specialThrowable.initCause(new Exception("def"));
106: fail();
107: } catch (IllegalStateException e) {
108: //ok
109: }
110: }
111: Throwable namingException = new NamingException();
112: namingException.initCause(exception);
113: assertSame(exception, namingException.getCause());
114: try {
115: namingException.initCause(new Exception("def"));
116: fail();
117: } catch (IllegalStateException e) {
118: //ok
119: }
120: }
121:
122: public void testGetStackTrace() throws Exception {
123: StackTraceElement[] elements = new Throwable().getStackTrace();
124: assertTrue(elements.length > 1);
125: StackTraceElement element = elements[0];
126: if (element.getClassName().equals(Throwable.class.getName())) {
127: element = elements[1];
128: }
129: assertEquals(getClass().getName(), element.getClassName());
130: assertEquals(getClass().getSimpleName() + ".java", element
131: .getFileName());
132: assertTrue(element.getLineNumber() > 0);
133: assertEquals("testGetStackTrace", element.getMethodName());
134: }
135:
136: private static List<Throwable> getSpecialThrowables(
137: IOException exception) {
138: return Arrays.asList(new ActivationException("", exception),
139: new ClassNotFoundException("", exception),
140: new ExceptionInInitializerError(exception),
141: new InvocationTargetException(exception),
142: new PrinterIOException(exception),
143: new PrivilegedActionException(exception),
144: new RemoteException("", exception),
145: new ServerCloneException("", exception),
146: new UndeclaredThrowableException(exception),
147: new WriteAbortedException("", exception));
148: }
149:
150: }
|