Assert pre-conditions in AopTestUtils

Issue: SPR-13005
This commit is contained in:
Sam Brannen 2015-05-09 21:02:40 +02:00
parent efe3a35da8
commit cf51f0c0aa
2 changed files with 17 additions and 6 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.test.util;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.util.Assert;
/**
* {@code AopTestUtils} is a collection of AOP-related utility methods for
@ -41,7 +42,8 @@ public class AopTestUtils {
* be returned; otherwise, the {@code candidate} will be returned
* <em>as is</em>.
*
* @param candidate the instance to check (potentially a Spring AOP proxy)
* @param candidate the instance to check (potentially a Spring AOP proxy);
* never {@code null}
* @return the target object or the {@code candidate}; never {@code null}
* @throws IllegalStateException if an error occurs while unwrapping a proxy
* @see Advised#getTargetSource()
@ -49,6 +51,7 @@ public class AopTestUtils {
*/
@SuppressWarnings("unchecked")
public static <T> T getTargetObject(Object candidate) {
Assert.notNull(candidate, "candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && (candidate instanceof Advised)) {
return (T) ((Advised) candidate).getTargetSource().getTarget();
@ -57,8 +60,6 @@ public class AopTestUtils {
catch (Exception e) {
throw new IllegalStateException("Failed to unwrap proxied object.", e);
}
// else
return (T) candidate;
}
@ -71,7 +72,8 @@ public class AopTestUtils {
* nested proxies will be returned; otherwise, the {@code candidate}
* will be returned <em>as is</em>.
*
* @param candidate the instance to check (potentially a Spring AOP proxy)
* @param candidate the instance to check (potentially a Spring AOP proxy);
* never {@code null}
* @return the ultimate target object or the {@code candidate}; never
* {@code null}
* @throws IllegalStateException if an error occurs while unwrapping a proxy
@ -80,6 +82,7 @@ public class AopTestUtils {
*/
@SuppressWarnings("unchecked")
public static <T> T getUltimateTargetObject(Object candidate) {
Assert.notNull(candidate, "candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && (candidate instanceof Advised)) {
return (T) getUltimateTargetObject(((Advised) candidate).getTargetSource().getTarget());
@ -88,8 +91,6 @@ public class AopTestUtils {
catch (Exception e) {
throw new IllegalStateException("Failed to unwrap proxied object.", e);
}
// else
return (T) candidate;
}

View File

@ -36,6 +36,11 @@ public class AopTestUtilsTests {
private final FooImpl foo = new FooImpl();
@Test(expected = IllegalArgumentException.class)
public void getTargetObjectForNull() {
getTargetObject(null);
}
@Test
public void getTargetObjectForNonProxiedObject() {
Foo target = getTargetObject(foo);
@ -66,6 +71,11 @@ public class AopTestUtilsTests {
assertNotSame(foo, target);
}
@Test(expected = IllegalArgumentException.class)
public void getUltimateTargetObjectForNull() {
getUltimateTargetObject(null);
}
@Test
public void getUltimateTargetObjectForNonProxiedObject() {
Foo target = getUltimateTargetObject(foo);