You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/02/14 01:48:20 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ValidateTest.java
scolebourne 2004/02/13 16:48:20
Modified: lang/src/java/org/apache/commons/lang Validate.java
lang/src/test/org/apache/commons/lang ValidateTest.java
Log:
Add validate methods for all elements same type
bug 25683, from Norm Deane
Revision Changes Path
1.9 +51 -1 jakarta-commons/lang/src/java/org/apache/commons/lang/Validate.java
Index: Validate.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/Validate.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Validate.java 11 Feb 2004 23:33:23 -0000 1.8
+++ Validate.java 14 Feb 2004 00:48:19 -0000 1.9
@@ -71,6 +71,7 @@
* @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a>
* @author Stephen Colebourne
* @author Gary Gregory
+ * @author Norm Deane
* @since 2.0
* @version $Id$
*/
@@ -527,6 +528,55 @@
throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
}
}
+ }
+
+ /**
+ * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+ * if the argument collection is <code>null</code> or has elements that
+ * are not of type <code>clazz</code>.</p>
+ *
+ * <pre>
+ * Validate.allElementsOfClass(collection, String.class, "Collection has invalid elements");
+ * </pre>
+ *
+ * @param collection the collection to check
+ * @param clazz the <code>Class</code> which the collection's elements are expected to be
+ * @param message the exception message if the <code>Collection</code> has elements not of type <code>clazz</code>
+ * @since 2.1
+ */
+ public static void allElementsOfClass(Collection collection, Class clazz, String message) {
+ Validate.notNull(collection);
+ for (Iterator it = collection.iterator(); it.hasNext(); ) {
+ if ((it.next().getClass().equals(clazz)) == false) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+ }
+
+ /**
+ * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
+ * if the argument collection is <code>null</code> or has elements that are not of
+ * type <code>clazz</code>.</p>
+ *
+ * <pre>
+ * Validate.allElementsOfClass(collection, String.class);
+ * </pre>
+ *
+ * <p>The message in the exception is 'The validated collection contains an element not of type clazz at index: '.</p>
+ *
+ * @param collection the collection to check
+ * @param clazz the <code>Class</code> which the collection's elements are expected to be
+ * @since 2.1
+ */
+ public static void allElementsOfClass(Collection collection, Class clazz) {
+ Validate.notNull(collection);
+ int i = 0;
+ for (Iterator it = collection.iterator(); it.hasNext(); i++) {
+ if ((it.next().getClass().equals(clazz)) == false) {
+ throw new IllegalArgumentException("The validated collection contains an element not of type "
+ + (clazz == null ? "null" : clazz.getName()) + " at index: " + i);
+ }
+ }
}
}
1.4 +21 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/ValidateTest.java
Index: ValidateTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ValidateTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ValidateTest.java 19 Aug 2003 02:32:16 -0000 1.3
+++ ValidateTest.java 14 Feb 2004 00:48:20 -0000 1.4
@@ -67,6 +67,7 @@
* Unit tests {@link org.apache.commons.lang.util.Validate}.
*
* @author Stephen Colebourne
+ * @author Norm Deane
* @version $Id$
*/
public class ValidateTest extends TestCase {
@@ -395,4 +396,23 @@
}
//-----------------------------------------------------------------------
+ public void testAllElementsOfClass() {
+ List coll = new ArrayList();
+ coll.add("a");
+ coll.add("b");
+ Validate.allElementsOfClass(coll, String.class, "MSG");
+ try {
+ Validate.allElementsOfClass(coll, Integer.class, "MSG");
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ assertEquals("MSG", ex.getMessage());
+ }
+ coll.set(1, Boolean.FALSE);
+ try {
+ Validate.allElementsOfClass(coll, String.class);
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ assertEquals("The validated collection contains an element not of type java.lang.String at index: 1", ex.getMessage());
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org