[LANG-751] Include the actual type in the Validate.isInstance and isAssignableFrom exception messages.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1166685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2011-09-08 13:41:05 +00:00
parent 960b9a6cf7
commit 603d818735
3 changed files with 28 additions and 22 deletions

View File

@ -69,9 +69,8 @@ public class Validate {
private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE =
"The validated collection index is invalid: %d";
private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false";
private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE =
"The validated class can not be converted to the %s class";
private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "The validated object is not an instance of %s";
private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = "Cannot assign a %s to a %s";
private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "Expected type: %s, actual: %s";
/**
* Constructor. This class should not normally be instantiated.
@ -975,14 +974,13 @@ public static <T> void exclusiveBetween(T start, T end, Comparable<T> value, Str
//---------------------------------------------------------------------------------
/**
* <p>Validate that the argument is an instance of the specified class; otherwise
* throwing an exception. This method is useful when validating according to an arbitrary
* class</p>
* Validates that the argument is an instance of the specified class, if not throws an exception.
*
* <p>This method is useful when validating according to an arbitrary class</p>
*
* <pre>Validate.isInstanceOf(OkClass.class, object);</pre>
*
* <p>The message of the exception is &quot;The validated object is not an instance of&quot;
* followed by the name of the class</p>
* <p>The message of the exception is &quot;Expected type: {type}, actual: {obj_type}&quot;</p>
*
* @param type the class the object must be validated against, not null
* @param obj the object to check, null throws an exception
@ -993,7 +991,8 @@ public static <T> void exclusiveBetween(T start, T end, Comparable<T> value, Str
*/
public static void isInstanceOf(Class<?> type, Object obj) {
if (type.isInstance(obj) == false) {
throw new IllegalArgumentException(String.format(DEFAULT_IS_INSTANCE_OF_EX_MESSAGE, type.getName()));
throw new IllegalArgumentException(String.format(DEFAULT_IS_INSTANCE_OF_EX_MESSAGE, type.getName(),
obj == null ? "null" : obj.getClass().getName()));
}
}
@ -1024,32 +1023,32 @@ public static void isInstanceOf(Class<?> type, Object obj, String message, Objec
//---------------------------------------------------------------------------------
/**
* <p>Validate that the argument can be converted to the specified class; otherwise
* throwing an exception with the specified message. This method is useful when
* validating if there will be no casting errors.</p>
* Validates that the argument can be converted to the specified class, if not, throws an exception.
*
* <p>This method is useful when validating that there will be no casting errors.</p>
*
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
*
* <p>The message of the exception is &quot;The validated object can not be converted to the&quot;
* followed by the name of the class and &quot;class&quot;</p>
* <p>The message format of the exception is &quot;Cannot assign {type} to {superType}&quot;</p>
*
* @param superType the class the class must be validated against, not null
* @param type the class to check, not null
* @throws IllegalArgumentException if argument can not be converted to the specified class
* @throws IllegalArgumentException if type argument is not assignable to the specified superType
* @see #isAssignableFrom(Class, Class, String, Object...)
*
* @since 3.0
*/
public static void isAssignableFrom(Class<?> superType, Class<?> type) {
if (superType.isAssignableFrom(type) == false) {
throw new IllegalArgumentException(String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, superType.getName()));
throw new IllegalArgumentException(String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, type == null ? "null" : type.getName(),
superType.getName()));
}
}
/**
* <p>Validate that the argument can be converted to the specified class; otherwise
* throwing an exception. This method is useful when validating if there will be no
* casting errors.</p>
* Validates that the argument can be converted to the specified class, if not throws an exception.
*
* <p>This method is useful when validating if there will be no casting errors.</p>
*
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
*

View File

@ -21,7 +21,8 @@
</properties>
<body>
<release version="3.0.2" date="unreleased" description="September release">
<release version="3.0.2" date="unreleased" description="September release">
<action type="update" issue="LANG-751">Include the actual type in the Validate.isInstance and isAssignableFrom exception messages</action>
<action type="fix" issue="LANG-746">NumberUtils does not handle upper-case hex: 0X and -0X</action>
<action type="update" issue="LANG-736">CharUtils static final array CHAR_STRING is not needed to compute CHAR_STRING_ARRAY</action>
<action type="fix" issue="LANG-744">StringUtils throws java.security.AccessControlException on Google App Engine</action>

View File

@ -849,11 +849,14 @@ public void testExclusiveBetween_withMessage()
public void testIsInstanceOf() {
Validate.isInstanceOf(String.class, "hi");
Validate.isInstanceOf(Integer.class, 1);
}
public void testIsInstanceOfExceptionMessage() {
try {
Validate.isInstanceOf(List.class, "hi");
fail("Expecting IllegalArgumentException");
} catch(IllegalArgumentException e) {
assertEquals("The validated object is not an instance of java.util.List", e.getMessage());
assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage());
}
}
@ -871,11 +874,14 @@ public void testIsInstanceOf_withMessage() {
public void testIsAssignable() {
Validate.isAssignableFrom(CharSequence.class, String.class);
Validate.isAssignableFrom(AbstractList.class, ArrayList.class);
}
public void testIsAssignableExceptionMessage() {
try {
Validate.isAssignableFrom(List.class, String.class);
fail("Expecting IllegalArgumentException");
} catch(IllegalArgumentException e) {
assertEquals("The validated class can not be converted to the java.util.List class", e.getMessage());
assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage());
}
}