diff --git a/spring-binding/src/main/java/org/springframework/binding/method/MethodInvocationException.java b/spring-binding/src/main/java/org/springframework/binding/method/MethodInvocationException.java index 6e9f5ac6..88ac9bc5 100644 --- a/spring-binding/src/main/java/org/springframework/binding/method/MethodInvocationException.java +++ b/spring-binding/src/main/java/org/springframework/binding/method/MethodInvocationException.java @@ -69,11 +69,10 @@ public class MethodInvocationException extends NestedRuntimeException { * @return the target throwable */ public Throwable getTargetException() { - if (getCause() instanceof InvocationTargetException) { - return ((InvocationTargetException)getCause()).getTargetException(); - } - else { - return getCause(); + Throwable targetException = getCause(); + while (targetException instanceof InvocationTargetException) { + targetException = ((InvocationTargetException)targetException).getTargetException(); } + return targetException; } } \ No newline at end of file diff --git a/spring-binding/src/test/java/org/springframework/binding/method/MethodInvocationExceptionTests.java b/spring-binding/src/test/java/org/springframework/binding/method/MethodInvocationExceptionTests.java new file mode 100644 index 00000000..87d9c7ef --- /dev/null +++ b/spring-binding/src/test/java/org/springframework/binding/method/MethodInvocationExceptionTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2004-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.binding.method; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import junit.framework.TestCase; + +/** + * Test case for {@link MethodInvocationException}. + * + * @author Erwin Vervaet + */ +public class MethodInvocationExceptionTests extends TestCase { + + public void testGetTargetException() { + // runtime exception + IllegalArgumentException iae = new IllegalArgumentException("test"); + MethodInvocationException ex = testException(iae); + assertSame(iae, ex.getTargetException()); + + // exception + IOException ioe = new IOException("test"); + ex = testException(ioe); + assertSame(ioe, ex.getTargetException()); + + // nested + InvocationTargetException ite = new InvocationTargetException(ioe); + ex = testException(ite); + assertSame(ioe, ex.getTargetException()); + + // deep nesting + ite = new InvocationTargetException(new InvocationTargetException(ioe)); + ex = testException(ite); + assertSame(ioe, ex.getTargetException()); + } + + // internal helpers + + private MethodInvocationException testException(Throwable cause) { + return new MethodInvocationException(new MethodSignature("test"), null, cause); + } +}