Applying Tomasz Nurkiewicz's patch from LANG-533 adding notBlank methods to Validate

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@828310 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-10-22 05:33:21 +00:00
parent 96acca756c
commit 7d3fbbfd43
2 changed files with 226 additions and 2 deletions

View File

@ -39,8 +39,7 @@ import java.util.Map;
* @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 class Validate {
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
//---------------------------------------------------------------------------------

View File

@ -284,6 +284,192 @@ public class ValidateTest extends TestCase {
}
}
//-----------------------------------------------------------------------
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"};