commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r828310 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/Validate.java test/org/apache/commons/lang/ValidateTest.java
Date Thu, 22 Oct 2009 05:33:22 GMT
Author: bayard
Date: Thu Oct 22 05:33:21 2009
New Revision: 828310

URL: http://svn.apache.org/viewvc?rev=828310&view=rev
Log:
Applying Tomasz Nurkiewicz's patch from LANG-533 adding notBlank methods to Validate

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java?rev=828310&r1=828309&r2=828310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java Thu Oct 22 05:33:21
2009
@@ -39,8 +39,7 @@
  * @version $Id$
  */
 public class Validate {
-    // Validate has no dependencies on other classes in Commons Lang at present
-    
+
     /**
      * Constructor. This class should not normally be instantiated.
      */
@@ -297,6 +296,45 @@
         notEmpty(collection, "The validated collection is empty");
     }
 
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument String is blank (<code>null</code>, empty or whitespace).</p>
+     *
+     * <pre>
+     * Validate.notBlank(myString);
+     * </pre>
+     *
+     * <p>The message in the exception is 'The validated string is blank'.</p>
+     *
+     * @param string  the string to check is not blank
+     * @throws IllegalArgumentException if the string is blank
+     * @see StringUtils#isBlank(CharSequence)
+     */
+    public static void notBlank(String string) {
+        if(StringUtils.isBlank(string)) {
+            throw new IllegalArgumentException("The validated string is blank");
+        }
+    }
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+     * if the argument String is blank (<code>null</code>, empty or whitespace).</p>
+     *
+     * <pre>
+     * Validate.notBlank(myString, "The string must not be blank");
+     * </pre>
+     *
+     * @param string  the string to check is not blank
+     * @param message  the exception message you would like to see if the string is blank
+     * @throws IllegalArgumentException if the string is blank
+     * @see StringUtils#isBlank(CharSequence)
+     */
+    public static void notBlank(String string, String message) {
+        if(StringUtils.isBlank(string)) {
+            throw new IllegalArgumentException(message);
+        }
+    }
+
     // notEmpty map
     //---------------------------------------------------------------------------------
 

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java?rev=828310&r1=828309&r2=828310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java Thu Oct 22
05:33:21 2009
@@ -285,6 +285,192 @@
     }
 
     //-----------------------------------------------------------------------
+    public void testNotBlankNullStringShouldThrow() {
+        //given
+        String string = null;
+
+        try {
+            //when
+            Validate.notBlank(string);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("The validated string is blank", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgNullStringShouldThrow() {
+        //given
+        String string = null;
+
+        try {
+            //when
+            Validate.notBlank(string, "Message");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("Message", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankEmptyStringShouldThrow() {
+        //given
+        String string = "";
+
+        try {
+            //when
+            Validate.notBlank(string);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("The validated string is blank", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankBlankStringWithWhitespacesShouldThrow() {
+        //given
+        String string = "   ";
+
+        try {
+            //when
+            Validate.notBlank(string);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("The validated string is blank", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankBlankStringWithNewlinesShouldThrow() {
+        //given
+        String string = " \n \t \r \n ";
+
+        try {
+            //when
+            Validate.notBlank(string);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("The validated string is blank", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgBlankStringShouldThrow() {
+        //given
+        String string = " \n \t \r \n ";
+
+        try {
+            //when
+            Validate.notBlank(string, "Message");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("Message", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgBlankStringWithWhitespacesShouldThrow() {
+        //given
+        String string = "   ";
+
+        try {
+            //when
+            Validate.notBlank(string, "Message");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("Message", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgEmptyStringShouldThrow() {
+        //given
+        String string = "";
+
+        try {
+            //when
+            Validate.notBlank(string, "Message");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            //then
+            assertEquals("Message", e.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankNotBlankStringShouldNotThrow() {
+        //given
+        String string = "abc";
+
+        //when
+        Validate.notBlank(string);
+
+        //then should not throw
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankNotBlankStringWithWhitespacesShouldNotThrow() {
+        //given
+        String string = "  abc   ";
+
+        //when
+        Validate.notBlank(string);
+
+        //then should not throw
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankNotBlankStringWithNewlinesShouldNotThrow() {
+        //given
+        String string = " \n \t abc \r \n ";
+
+        //when
+        Validate.notBlank(string);
+
+        //then should not throw
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgNotBlankStringShouldNotThrow() {
+        //given
+        String string = "abc";
+
+        //when
+        Validate.notBlank(string, "Message");
+
+        //then should not throw
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgNotBlankStringWithWhitespacesShouldNotThrow() {
+        //given
+        String string = "  abc   ";
+
+        //when
+        Validate.notBlank(string, "Message");
+
+        //then should not throw
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNotBlankMsgNotBlankStringWithNewlinesShouldNotThrow() {
+        //given
+        String string = " \n \t abc \r \n ";
+
+        //when
+        Validate.notBlank(string, "Message");
+
+        //then should not throw
+    }
+
+    //-----------------------------------------------------------------------
     public void testNoNullElementsArray1() {
         String[] array = new String[] {"a", "b"};
         Validate.noNullElements(array);



Mime
View raw message