diff --git a/src/java/org/apache/commons/lang/Validate.java b/src/java/org/apache/commons/lang/Validate.java index a565da3f4..bf6518048 100644 --- a/src/java/org/apache/commons/lang/Validate.java +++ b/src/java/org/apache/commons/lang/Validate.java @@ -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 @@ public static void notEmpty(Collection collection) { notEmpty(collection, "The validated collection is empty"); } + /** + *

Validate an argument, throwing IllegalArgumentException + * if the argument String is blank (null, empty or whitespace).

+ * + *
+     * Validate.notBlank(myString);
+     * 
+ * + *

The message in the exception is 'The validated string is blank'.

+ * + * @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"); + } + } + + /** + *

Validate an argument, throwing IllegalArgumentException + * if the argument String is blank (null, empty or whitespace).

+ * + *
+     * Validate.notBlank(myString, "The string must not be blank");
+     * 
+ * + * @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 //--------------------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/lang/ValidateTest.java b/src/test/org/apache/commons/lang/ValidateTest.java index 7213f54e2..8b8c3a5c0 100644 --- a/src/test/org/apache/commons/lang/ValidateTest.java +++ b/src/test/org/apache/commons/lang/ValidateTest.java @@ -284,6 +284,192 @@ public void testNotEmptyString2() { } } + //----------------------------------------------------------------------- + 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"};