Add Validate class for argument validation

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137179 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-12-13 17:21:56 +00:00
parent b91dfd1a2c
commit 2bc43c9abb
3 changed files with 845 additions and 1 deletions

View File

@ -0,0 +1,476 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* <p>Validate is a static utility class for validating arguments.
*
* <p>The class is based along the lines of JUnit. If an argument value is
* deemed invalid, an IllegalArgumentException is thrown. For example:
*
* <pre>
* Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
* Validate.notNull( surname, "The surname must not be null");
* </pre>
*
* @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a>
* @author Stephen Colebourne
* @version $Id: Validate.java,v 1.1 2002/12/13 17:21:56 scolebourne Exp $
*/
public class Validate {
/**
* Constructor. This class should not normally be instantiated.
*/
public Validate() {
}
// isTrue
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the test
* result is false.
*
* <p>This is used when validating according to an arbitrary boolean expression,
* such as validating a primitive number or using your own custom validation
* expression.
* <pre>
* Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
* </pre>
* <p>For performance reasons, the object is passed as a separate parameter and
* appended to the message string only in the case of an error.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression is false
* @param value the value to append to the message in case of error
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message, Object value) {
if (expression == false) {
throw new IllegalArgumentException(message + value);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the test
* result is false.
*
* <p>This is used when validating according to an arbitrary boolean expression,
* such as validating a primitive number or using your own custom validation
* expression.
* <pre>
* Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
* </pre>
* <p>For performance reasons, the object is passed as a separate parameter and
* appended to the message string only in the case of an error.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression is false
* @param value the value to append to the message in case of error
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message, long value) {
if (expression == false) {
throw new IllegalArgumentException(message + value);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the test
* result is false.
*
* <p>This is used when validating according to an arbitrary boolean expression,
* such as validating a primitive number or using your own custom validation
* expression.
* <pre>
* Validate.isTrue( d > 0.0, "The value must be greater than zero: ", d);
* </pre>
* <p>For performance reasons, the object is passed as a separate parameter and
* appended to the message string only in the case of an error.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression is false
* @param value the value to append to the message in case of error
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message, double value) {
if (expression == false) {
throw new IllegalArgumentException(message + value);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the test
* result is false.
*
* <p>This is used when validating according to an arbitrary boolean expression,
* such as validating a primitive number or using your own custom validation
* expression.
* <pre>
* Validate.isTrue( (i > 0), "The value must be greater than zero");
* Validate.isTrue( myObject.isOk(), "The object is not OK");
* </pre>
* <p>For performance reasons, the message string should not involve a string append,
* instead use the {@link #isTrue(boolean, String, Object)} method.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression is false
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message) {
if (expression == false) {
throw new IllegalArgumentException(message);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the test
* result is false.
*
* <p>This is used when validating according to an arbitrary boolean expression,
* such as validating a primitive number or using your own custom validation
* expression.
* <pre>
* Validate.isTrue( i > 0 );
* Validate.isTrue( myObject.isOk() );
* </pre>
* <p>The message in the exception is 'The validated expression is false'.
*
* @param expression a boolean expression
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression) {
if (expression == false) {
throw new IllegalArgumentException("The validated expression is false");
}
}
// notNull
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument is null.
* <pre>
* Validate.notNull(myObject, "The object must not be null");
* </pre>
*
* @param object the object to check is not null
* @param message the exception message you would like to see if the object is null
* @throws IllegalArgumentException if the object is null
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument is null.
* <pre>
* Validate.notNull(myObject);
* </pre>
* <p>The message in the exception is 'The validated object is null'.
*
* @param object the object to check is not null
* @throws IllegalArgumentException if the object is null
*/
public static void notNull(Object object) {
if (object == null) {
throw new IllegalArgumentException("The validated object is null");
}
}
// notEmpty array
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument array is empty (null or no elements).
* <pre>
* Validate.notEmpty(myArray, "The array must not be empty");
* </pre>
*
* @param array the array to check is not empty
* @param message the exception message you would like to see if the array is empty
* @throws IllegalArgumentException if the array is empty
*/
public static void notEmpty(Object[] array, String message) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument array is empty (null or no elements).
* <pre>
* Validate.notEmpty(myArray);
* </pre>
* <p>The message in the exception is 'The validated array is empty'.
*
* @param array the array to check is not empty
* @throws IllegalArgumentException if the array is empty
*/
public static void notEmpty(Object[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("The validated array is empty");
}
}
// notEmpty collection
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument Collection is empty (null or no elements).
* <pre>
* Validate.notEmpty(myCollection, "The collection must not be empty");
* </pre>
*
* @param collection the collection to check is not empty
* @param message the exception message you would like to see if the collection is empty
* @throws IllegalArgumentException if the collection is empty
*/
public static void notEmpty(Collection collection, String message) {
if (collection == null || collection.size() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument Collection is empty (null or no elements).
* <pre>
* Validate.notEmpty(myCollection);
* </pre>
* <p>The message in the exception is 'The validated collection is empty'.
*
* @param collection the collection to check is not empty
* @throws IllegalArgumentException if the collection is empty
*/
public static void notEmpty(Collection collection) {
if (collection == null || collection.size() == 0) {
throw new IllegalArgumentException("The validated collection is empty");
}
}
// notEmpty map
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument Map is empty (null or no elements).
* <pre>
* Validate.notEmpty(myMap, "The collection must not be empty");
* </pre>
*
* @param map the map to check is not empty
* @param message the exception message you would like to see if the map is empty
* @throws IllegalArgumentException if the map is empty
*/
public static void notEmpty(Map map, String message) {
if (map == null || map.size() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument Map is empty (null or no elements).
* <pre>
* Validate.notEmpty(myMap);
* </pre>
* <p>The message in the exception is 'The validated map is empty'.
*
* @param map the map to check is not empty
* @throws IllegalArgumentException if the map is empty
*/
public static void notEmpty(Map map) {
if (map == null || map.size() == 0) {
throw new IllegalArgumentException("The validated map is empty");
}
}
// notEmpty string
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument String is empty (null or zero length).
* <pre>
* Validate.notEmpty(myString, "The string must not be empty");
* </pre>
*
* @param string the string to check is not empty
* @param message the exception message you would like to see if the string is empty
* @throws IllegalArgumentException if the string is empty
*/
public static void notEmpty(String string, String message) {
if (string == null || string.length() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument String is empty (null or zero length).
* <pre>
* Validate.notEmpty(myString);
* </pre>
* <p>The message in the exception is 'The validated string is empty'.
*
* @param string the string to check is not empty
* @throws IllegalArgumentException if the string is empty
*/
public static void notEmpty(String string) {
if (string == null || string.length() == 0) {
throw new IllegalArgumentException("The validated string is empty");
}
}
// notNullElements array
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument array has null elements or is null.
* <pre>
* Validate.notEmpty(myArray, "The array must not contain null elements");
* </pre>
*
* @param array the array to check
* @param message the exception message if the array has null elements
* @throws IllegalArgumentException if the array has null elements or is null
*/
public static void noNullElements(Object[] array, String message) {
Validate.notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException(message);
}
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument array has null elements or is null.
* <pre>
* Validate.notEmpty(myArray);
* </pre>
* <p>The message in the exception is 'The validated array contains null element at index: '.
*
* @param array the array to check
* @throws IllegalArgumentException if the array has null elements or is null
*/
public static void noNullElements(Object[] array) {
Validate.notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException("The validated array contains null element at index: " + i);
}
}
}
// notNullElements collection
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument collection has null elements or is null.
* <pre>
* Validate.notEmpty(myCollection, "The collection must not contain null elements");
* </pre>
*
* @param collection the collection to check
* @param message the exception message if the array has null elements
* @throws IllegalArgumentException if the collection has null elements or is null
*/
public static void noNullElements(Collection collection, String message) {
Validate.notNull(collection);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
throw new IllegalArgumentException(message);
}
}
}
/**
* <p>Validate an argument, throwing IllegalArgumentException if the
* argument collection has null elements or is null.
* <pre>
* Validate.notEmpty(myCollection);
* </pre>
* <p>The message in the exception is 'The validated collection contains null element at index: '.
*
* @param collection the collection to check
* @throws IllegalArgumentException if the collection has null elements or is null
*/
public static void noNullElements(Collection collection) {
Validate.notNull(collection);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
}
}
}
}

View File

@ -62,7 +62,7 @@
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: LangTestSuite.java,v 1.5 2002/10/13 22:42:59 scolebourne Exp $
* @version $Id: LangTestSuite.java,v 1.6 2002/12/13 17:21:56 scolebourne Exp $
*/
public class LangTestSuite extends TestCase {
@ -98,6 +98,7 @@ public static Test suite() {
suite.addTest(StringUtilsSubstringTest.suite());
suite.addTest(StringUtilsEqualsIndexOfTest.suite());
suite.addTest(StringUtilsIsTest.suite());
suite.addTest(ValidateTest.suite());
return suite;
}
}

View File

@ -0,0 +1,367 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Unit tests {@link org.apache.commons.lang.Validate}.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ValidateTest.java,v 1.1 2002/12/13 17:21:56 scolebourne Exp $
*/
public class ValidateTest extends TestCase {
public ValidateTest(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite(ValidateTest.class);
suite.setName("Validate Tests");
return suite;
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
//-----------------------------------------------------------------------
public void testIsTrue1() {
Validate.isTrue(true);
try {
Validate.isTrue(false);
} catch (IllegalArgumentException ex) {
assertEquals("The validated expression is false", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testIsTrue2() {
Validate.isTrue(true, "MSG");
try {
Validate.isTrue(false, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testIsTrue3() {
Validate.isTrue(true, "MSG", new Integer(6));
try {
Validate.isTrue(false, "MSG", new Integer(6));
} catch (IllegalArgumentException ex) {
assertEquals("MSG6", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testIsTrue4() {
Validate.isTrue(true, "MSG", 7);
try {
Validate.isTrue(false, "MSG", 7);
} catch (IllegalArgumentException ex) {
assertEquals("MSG7", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testIsTrue5() {
Validate.isTrue(true, "MSG", 7.4d);
try {
Validate.isTrue(false, "MSG", 7.4d);
} catch (IllegalArgumentException ex) {
assertEquals("MSG7.4", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNotNull1() {
Validate.notNull(new Object());
try {
Validate.notNull(null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNotNull2() {
Validate.notNull(new Object(), "MSG");
try {
Validate.notNull(null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNotEmptyArray1() {
Validate.notEmpty(new Object[] {null});
try {
Validate.notEmpty((Object[]) null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated array is empty", ex.getMessage());
}
try {
Validate.notEmpty(new Object[0]);
} catch (IllegalArgumentException ex) {
assertEquals("The validated array is empty", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNotEmptyArray2() {
Validate.notEmpty(new Object[] {null}, "MSG");
try {
Validate.notEmpty((Object[]) null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
try {
Validate.notEmpty(new Object[0], "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNotEmptyCollection1() {
Collection coll = new ArrayList();
try {
Validate.notEmpty((Collection) null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated collection is empty", ex.getMessage());
}
try {
Validate.notEmpty(coll);
} catch (IllegalArgumentException ex) {
assertEquals("The validated collection is empty", ex.getMessage());
}
coll.add(new Integer(8));
Validate.notEmpty(coll);
}
//-----------------------------------------------------------------------
public void testNotEmptyCollection2() {
Collection coll = new ArrayList();
try {
Validate.notEmpty((Collection) null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
try {
Validate.notEmpty(coll, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
coll.add(new Integer(8));
Validate.notEmpty(coll, "MSG");
}
//-----------------------------------------------------------------------
public void testNotEmptyMap1() {
Map map = new HashMap();
try {
Validate.notEmpty((Map) null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated map is empty", ex.getMessage());
}
try {
Validate.notEmpty(map);
} catch (IllegalArgumentException ex) {
assertEquals("The validated map is empty", ex.getMessage());
}
map.put("ll", new Integer(8));
Validate.notEmpty(map);
}
//-----------------------------------------------------------------------
public void testNotEmptyMap2() {
Map map = new HashMap();
try {
Validate.notEmpty((Map) null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
try {
Validate.notEmpty(map, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
map.put("ll", new Integer(8));
Validate.notEmpty(map, "MSG");
}
//-----------------------------------------------------------------------
public void testNotEmptyString1() {
Validate.notEmpty("hjl");
try {
Validate.notEmpty((String) null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated string is empty", ex.getMessage());
}
try {
Validate.notEmpty("");
} catch (IllegalArgumentException ex) {
assertEquals("The validated string is empty", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNotEmptyString2() {
Validate.notEmpty(new Object[] {null}, "MSG");
try {
Validate.notEmpty((String) null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
try {
Validate.notEmpty("", "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNoNullElementsArray1() {
String[] array = new String[] {"a", "b"};
Validate.noNullElements(array);
try {
Validate.noNullElements((Object[]) null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage());
}
array[1] = null;
try {
Validate.notEmpty(array);
} catch (IllegalArgumentException ex) {
assertEquals("The validated array contains null element at index: 1", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNoNullElementsArray2() {
String[] array = new String[] {"a", "b"};
Validate.noNullElements(array, "MSG");
try {
Validate.noNullElements((Object[]) null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage());
}
array[1] = null;
try {
Validate.notEmpty(array, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNoNullElementsCollection1() {
List coll = new ArrayList();
coll.add("a");
coll.add("b");
Validate.noNullElements(coll);
try {
Validate.noNullElements((Collection) null);
} catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage());
}
coll.set(1, null);
try {
Validate.notEmpty(coll);
} catch (IllegalArgumentException ex) {
assertEquals("The validated collection contains null element at index: 1", ex.getMessage());
}
}
//-----------------------------------------------------------------------
public void testNoNullElementsCollection2() {
List coll = new ArrayList();
coll.add("a");
coll.add("b");
Validate.noNullElements(coll, "MSG");
try {
Validate.noNullElements((Collection) null, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("The validated object is null", ex.getMessage());
}
coll.set(1, null);
try {
Validate.notEmpty(coll, "MSG");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
}
//-----------------------------------------------------------------------
}