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.impl;
032:
033: import java.io.IOException;
034: import java.lang.reflect.*;
035: import junit.framework.TestCase;
036:
037: /**
038: * @author Taras Puchko
039: */
040: public class RuntimeToolsTestCase extends TestCase {
041:
042: public void testInvokeMethod() throws Exception {
043: StringBuffer buffer = new StringBuffer("abc");
044: Class[] parameterTypes = new Class[] { char[].class, int.class,
045: int.class };
046: Object[] args = new Object[] { "12345".toCharArray(), 1, 3 };
047: Object result = RuntimeTools.invokeMethod(buffer, "append",
048: parameterTypes, args);
049: assertSame(buffer, result);
050: assertEquals("abc234", buffer.toString());
051: }
052:
053: public void testInvokeMethod_NoSuchMethodException()
054: throws Exception {
055: StringBuffer buffer = new StringBuffer("abc");
056: Class[] parameterTypes = new Class[] { char[].class, int.class,
057: Integer.class };
058: Object[] args = new Object[] { "12345".toCharArray(), 1, 3 };
059: try {
060: RuntimeTools.invokeMethod(buffer, "append", parameterTypes,
061: args);
062: fail();
063: } catch (NoSuchMethodException e) {
064: //ok
065: }
066: }
067:
068: public void testInvokeMethod_InvocationTargetException()
069: throws Throwable {
070: StringBuffer buffer = new StringBuffer("abc");
071: Class[] parameterTypes = new Class[] { char[].class, int.class,
072: int.class };
073: Object[] args = new Object[] { "12345".toCharArray(), -1, 3 };
074: try {
075: RuntimeTools.invokeMethod(buffer, "append", parameterTypes,
076: args);
077: fail();
078: } catch (InvocationTargetException e) {
079: try {
080: throw e.getTargetException();
081: } catch (IndexOutOfBoundsException ex) {
082: //ok
083: }
084: }
085: }
086:
087: public void testUnwrap_RuntimeException() throws Exception {
088: IllegalStateException exception = new IllegalStateException();
089: try {
090: RuntimeTools
091: .unwrap(new InvocationTargetException(exception));
092: fail();
093: } catch (IllegalStateException e) {
094: assertSame(exception, e);
095: }
096: }
097:
098: public void testUnwrap_Error() throws Exception {
099: InternalError error = new InternalError();
100: try {
101: RuntimeTools.unwrap(new InvocationTargetException(error));
102: fail();
103: } catch (InternalError e) {
104: assertSame(error, e);
105: }
106: }
107:
108: public void testUnwrap_Exception() throws Exception {
109: IOException error = new IOException();
110: UndeclaredThrowableException exception = RuntimeTools
111: .unwrap(new InvocationTargetException(error));
112: assertSame(error, exception.getCause());
113: }
114:
115: public void testUnwrap_Throwable() throws Exception {
116: Throwable throwable = new Throwable();
117: UndeclaredThrowableException exception = RuntimeTools
118: .unwrap(new InvocationTargetException(throwable));
119: assertSame(throwable, exception.getCause());
120: }
121:
122: }
|