commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1166685 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/Validate.java site/changes/changes.xml test/java/org/apache/commons/lang3/ValidateTest.java
Date Thu, 08 Sep 2011 13:41:06 GMT
Author: ggregory
Date: Thu Sep  8 13:41:05 2011
New Revision: 1166685

URL: http://svn.apache.org/viewvc?rev=1166685&view=rev
Log:
[LANG-751] Include the actual type in the Validate.isInstance and isAssignableFrom exception
messages.

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
    commons/proper/lang/trunk/src/site/changes/changes.xml
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java?rev=1166685&r1=1166684&r2=1166685&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java Thu Sep
 8 13:41:05 2011
@@ -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 class Validate {
     //---------------------------------------------------------------------------------
 
     /**
-     * <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 class Validate {
      */
     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 class Validate {
     //---------------------------------------------------------------------------------
 
     /**
-     * <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>
      *

Modified: commons/proper/lang/trunk/src/site/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/site/changes/changes.xml?rev=1166685&r1=1166684&r2=1166685&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/site/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/site/changes/changes.xml Thu Sep  8 13:41:05 2011
@@ -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>

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java?rev=1166685&r1=1166684&r2=1166685&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java Thu
Sep  8 13:41:05 2011
@@ -849,11 +849,14 @@ public class ValidateTest extends TestCa
     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 class ValidateTest extends TestCa
     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());
         }
     }
     



Mime
View raw message