Author: julien(a)jboss.com
Date: 2007-11-12 05:54:49 -0500 (Mon, 12 Nov 2007)
New Revision: 8875
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java
Log:
more test cases for tools and fix a bug (huge) in appendTo (my bad)
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2007-11-12
09:48:33 UTC (rev 8874)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2007-11-12
10:54:49 UTC (rev 8875)
@@ -227,7 +227,7 @@
return set;
}
- public static <E> Set<E> toSet(E[] objects)
+ public static <E> Set<E> toSet(E... objects)
{
if (objects == null)
{
@@ -313,7 +313,7 @@
return list;
}
- public static <E> List<E> toList(E[] objects)
+ public static <E> List<E> toList(E... objects)
{
if (objects == null)
{
@@ -382,7 +382,7 @@
* @return the iterator
* @throws IllegalArgumentException if the object array is null or the specified range
is not valid
*/
- public static <E> Iterator<E> iterator(final E[] objects) throws
IllegalArgumentException
+ public static <E> Iterator<E> iterator(final E... objects) throws
IllegalArgumentException
{
if (objects == null)
{
@@ -791,13 +791,20 @@
{
throw new IllegalArgumentException();
}
+
+ //
Class componentType = array.getClass().getComponentType();
- if (o != null && componentType.isAssignableFrom(o.getClass()))
+ if (o != null && !componentType.isAssignableFrom(o.getClass()))
{
- throw new ClassCastException("Object with class " +
o.getClass().getName() + " cannot be casted to class " +
componentType.getComponentType().getName());
+ throw new ClassCastException("Object with class " +
o.getClass().getName() + " cannot be casted to class " +
componentType.getName());
}
+
+ //
E[] copy = (E[])Array.newInstance(componentType, array.length + 1);
+ System.arraycopy(array, 0, copy, 0, array.length);
copy[array.length] = o;
+
+ //
return copy;
}
@@ -829,6 +836,17 @@
return replaceBoundedString(initial, prefix, suffix, replacement, true, false);
}
+ /**
+ * Todo : define what "bounded" means.
+ *
+ * @param initial
+ * @param prefix
+ * @param suffix
+ * @param replacement
+ * @param replaceIfBoundedStringEmpty
+ * @param keepBoundaries
+ * @return
+ */
public static String replaceBoundedString(String initial, String prefix, String
suffix, String replacement,
boolean replaceIfBoundedStringEmpty, boolean
keepBoundaries)
{
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java 2007-11-12
09:48:33 UTC (rev 8874)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java 2007-11-12
10:54:49 UTC (rev 8875)
@@ -24,6 +24,7 @@
package org.jboss.portal.test.common;
import junit.framework.TestCase;
+import org.jboss.portal.common.junit.ExtendedAssert;
import org.jboss.portal.common.util.Tools;
import java.util.ArrayList;
@@ -262,7 +263,7 @@
{
try
{
- Tools.iterator(null);
+ Tools.iterator((Object[])null);
fail();
}
catch (IllegalArgumentException ignore)
@@ -335,6 +336,37 @@
assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 2),
"a", "b");
}
+ public void testArrayAppendToThrowsIAE()
+ {
+ try
+ {
+ Tools.appendTo(null, "a");
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testEmptyEnumeration()
+ {
+ assertEnumeration(Tools.EMPTY_ENUMERATION);
+ }
+
+ public void testEmptyIterator()
+ {
+ assertIterator(Tools.EMPTY_ITERATOR);
+ }
+
+ public void testArrayAppendTo()
+ {
+ ExtendedAssert.assertEquals(new String[]{"a"}, Tools.appendTo(new
String[0], "a"));
+ ExtendedAssert.assertEquals(new String[]{null}, Tools.appendTo(new String[0],
null));
+ ExtendedAssert.assertEquals(new String[]{"a", "b"},
Tools.appendTo(new String[]{"a"}, "b"));
+ ExtendedAssert.assertEquals(new String[]{null, "b"}, Tools.appendTo(new
String[]{null}, "b"));
+ ExtendedAssert.assertEquals(new String[]{"a", null}, Tools.appendTo(new
String[]{"a"}, null));
+ }
+
private <E> void assertEnumeration(Enumeration<E> elements, E...
expectedElements)
{
assertNotNull(elements);