From 7bbb4ec7aff9ca171f2d34f747d7e0d96a6ce1b0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 7 Feb 2013 13:35:31 -0800 Subject: [PATCH] Fix Assert.instanceOf exception message Update the exception message used when Assert.instanceOf fails such that it expects the provided message to end with '.'. This reverts commit 5874383ef081bb52a872dd49d63e5b542fbf20ca which caused the implementation to be at odds with the JavaDoc and the previous release. The updated code also has the benefit of protecting against a null message. Issue: SPR-10269 --- .../java/org/springframework/util/Assert.java | 7 +++--- .../org/springframework/util/AssertTests.java | 23 ++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/Assert.java b/spring-core/src/main/java/org/springframework/util/Assert.java index 05c1c4babe..193d461a05 100644 --- a/spring-core/src/main/java/org/springframework/util/Assert.java +++ b/spring-core/src/main/java/org/springframework/util/Assert.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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 @@ -334,8 +334,9 @@ public abstract class Assert { public static void isInstanceOf(Class type, Object obj, String message) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { - throw new IllegalArgumentException(message + - ". Object of class [" + (obj != null ? obj.getClass().getName() : "null") + + throw new IllegalArgumentException( + (StringUtils.hasLength(message) ? message + " " : "") + + "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type); } } diff --git a/spring-core/src/test/java/org/springframework/util/AssertTests.java b/spring-core/src/test/java/org/springframework/util/AssertTests.java index 744b492f61..0dfe86f1dc 100644 --- a/spring-core/src/test/java/org/springframework/util/AssertTests.java +++ b/spring-core/src/test/java/org/springframework/util/AssertTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -24,7 +24,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Unit tests for the {@link Assert} class. @@ -36,6 +38,9 @@ import org.junit.Test; */ public class AssertTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test(expected = IllegalArgumentException.class) public void instanceOf() { final Set set = new HashSet(); @@ -43,6 +48,22 @@ public class AssertTests { Assert.isInstanceOf(HashMap.class, set); } + @Test + public void instanceOfNoMessage() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Object of class [java.lang.Object] must be an instance " + + "of interface java.util.Set"); + Assert.isInstanceOf(Set.class, new Object(), null); + } + + @Test + public void instanceOfMessage() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Custom message. Object of class [java.lang.Object] must " + + "be an instance of interface java.util.Set"); + Assert.isInstanceOf(Set.class, new Object(), "Custom message."); + } + @Test public void isNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() { Assert.isNull(null, "Bla");