From cf51f0c0aa84d48de5b4ac2ea1029d80efb552c7 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 9 May 2015 21:02:40 +0200 Subject: [PATCH] Assert pre-conditions in AopTestUtils Issue: SPR-13005 --- .../org/springframework/test/util/AopTestUtils.java | 13 +++++++------ .../test/util/AopTestUtilsTests.java | 10 ++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java index f49b30551c..4eda8a45ac 100644 --- a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java @@ -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 * as is. * - * @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 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 as is. * - * @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 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; } diff --git a/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java b/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java index 21070c5646..dac809f4ea 100644 --- a/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java +++ b/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java @@ -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);