JBoss Portal SVN: r8876 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common/util and 1 other directory.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-12 07:05:59 -0500 (Mon, 12 Nov 2007)
New Revision: 8876
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java
Log:
test case for Version class
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java 2007-11-12 10:54:49 UTC (rev 8875)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Version.java 2007-11-12 12:05:59 UTC (rev 8876)
@@ -32,12 +32,25 @@
public class Version
{
+ /** . */
private final String codeName;
+
+ /** . */
private final String toString;
+
+ /** . */
private final String name;
+
+ /** . */
private final int major;
+
+ /** . */
private final int minor;
+
+ /** . */
private final int patch;
+
+ /** . */
private final Qualifier qualifier;
public Version(String name, int major, int minor, int patch, Qualifier qualifier, String codeName)
@@ -50,18 +63,10 @@
{
throw new IllegalArgumentException("Major cannot be negative");
}
- if (major > 9)
- {
- throw new IllegalArgumentException("Major cannot be greater than 9");
- }
if (minor < 0)
{
throw new IllegalArgumentException("Minor cannot be negative");
}
- if (minor > 9)
- {
- throw new IllegalArgumentException("Minor cannot be greater than 9");
- }
if (patch < 0)
{
throw new IllegalArgumentException("Patch cannot be negative");
@@ -80,7 +85,7 @@
this.patch = patch;
this.qualifier = qualifier;
this.codeName = codeName;
- this.toString = buildToString();
+ this.toString = Format.JBOSS_PRODUCT_CONVENTION.toString(this);
}
public String getCodeName()
@@ -123,33 +128,36 @@
return toString;
}
- private String buildToString()
+ public String toString(Format format) throws IllegalArgumentException
{
- StringBuffer buffer = new StringBuffer(getName());
- buffer.append(" ")
- .append(getMajor()).append('.')
- .append(getMinor()).append('.')
- .append(getPatch()).append('-')
- .append(getQualifier());
- return buffer.toString();
+ if (format == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ return format.toString(this);
}
/** Type safe enum for intermediate major. */
public static class Qualifier
{
- public static class Prefix
+ public enum Prefix
{
- public static final Prefix SNAPSHOT = new Prefix("SNAPSHOT", false, "Snapshot");
- public static final Prefix ALPHA = new Prefix("ALPHA", true, "Alpha");
- public static final Prefix BETA = new Prefix("BETA", true, "Beta");
- public static final Prefix CR = new Prefix("CR", true, "Candidate for release");
- public static final Prefix GA = new Prefix("GA", false, "General Availability");
- public static final Prefix SP = new Prefix("SP", true, "Service pack");
+ SNAPSHOT("SNAPSHOT", false, "Snapshot"),
+ ALPHA("ALPHA", true, "Alpha"),
+ BETA("BETA", true, "Beta"),
+ CR("CR", true, "Candidate for release"),
+ GA("GA", false, "General Availability"),
+ SP("SP", true, "Service pack");
+ /** . */
private final String name;
+
+ /** . */
private final String description;
+
+ /** . */
private final boolean suffixable;
private Prefix(String name, boolean suffixable, String description)
@@ -180,16 +188,16 @@
}
}
- public static class Suffix
+ public enum Suffix
{
- public static final Suffix EMPTY = new Suffix("");
- public static final Suffix SUFFIX_1 = new Suffix("1");
- public static final Suffix SUFFIX_2 = new Suffix("2");
- public static final Suffix SUFFIX_3 = new Suffix("3");
- public static final Suffix SUFFIX_4 = new Suffix("4");
- public static final Suffix SUFFIX_5 = new Suffix("5");
- public static final Suffix SUFFIX_6 = new Suffix("6");
+ EMPTY(""),
+ SUFFIX_1("1"),
+ SUFFIX_2("2"),
+ SUFFIX_3("3"),
+ SUFFIX_4("4"),
+ SUFFIX_5("5"),
+ SUFFIX_6("6");
/** . */
private final String value;
@@ -227,7 +235,7 @@
}
if (suffix == null)
{
- suffix = new Suffix("");
+ suffix = Suffix.EMPTY;
}
if (prefix.isSuffixable() == false && suffix.value.length() > 0)
{
@@ -253,4 +261,27 @@
return toString;
}
}
+
+ public interface Format
+ {
+
+ /**
+ * Implement formatting as defined <a href="http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossProductVersioning">here</a>
+ */
+ Format JBOSS_PRODUCT_CONVENTION = new Format()
+ {
+ public String toString(Version version)
+ {
+ StringBuffer buffer = new StringBuffer(version.getName());
+ buffer.append(" ")
+ .append(version.getMajor()).append('.')
+ .append(version.getMinor()).append('.')
+ .append(version.getPatch()).append('-')
+ .append(version.getQualifier());
+ return buffer.toString();
+ }
+ };
+
+ String toString(Version version);
+ }
}
Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java (rev 0)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/VersionTestCase.java 2007-11-12 12:05:59 UTC (rev 8876)
@@ -0,0 +1,150 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.Version;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class VersionTestCase extends TestCase
+{
+
+ /** . */
+ private final Version.Qualifier GAQualifer = new Version.Qualifier(Version.Qualifier.Prefix.GA);
+
+ public void testIllegalNameThrowsIAE()
+ {
+ try
+ {
+ new Version(null, 0, 0, 0, GAQualifer, "code");
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIllegalMajorThrowsIAE()
+ {
+ try
+ {
+ new Version("name", -1, 0, 0, GAQualifer, "code");
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIllegalMinorThrowsIAE()
+ {
+ try
+ {
+ new Version("name", 0, -1, 0, GAQualifer, "code");
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIllegalQualifierThrowsIAE()
+ {
+ try
+ {
+ new Version("name", 0, 0, 0, null, "code");
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+
+ //
+ try
+ {
+ new Version.Qualifier(null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+
+ //
+ try
+ {
+ new Version.Qualifier(null, Version.Qualifier.Suffix.EMPTY);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+
+ //
+ try
+ {
+ new Version.Qualifier(Version.Qualifier.Prefix.GA, Version.Qualifier.Suffix.SUFFIX_1);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIllegalCodeNameThrowsIAE()
+ {
+ try
+ {
+ new Version("name", 0, 0, 0, GAQualifer, null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testState()
+ {
+ Version version = new Version("foo", 1, 2, 3, new Version.Qualifier(Version.Qualifier.Prefix.CR, Version.Qualifier.Suffix.SUFFIX_3), "bar");
+ assertEquals("foo", version.getName());
+ assertEquals(1, version.getMajor());
+ assertEquals(2, version.getMinor());
+ assertEquals(3, version.getPatch());
+ assertEquals(Version.Qualifier.Prefix.CR, version.getQualifier().getPrefix());
+ assertEquals(Version.Qualifier.Suffix.SUFFIX_3, version.getQualifier().getSuffix());
+ assertEquals("bar", version.getCodeName());
+ }
+
+ public void testQualifier()
+ {
+
+ }
+
+ public void testFormat()
+ {
+ Version version = new Version("foo", 1, 2, 3, new Version.Qualifier(Version.Qualifier.Prefix.CR, Version.Qualifier.Suffix.SUFFIX_3), "bar");
+ assertEquals("foo 1.2.3-CR3", version.toString());
+ }
+}
17 years
JBoss Portal SVN: r8875 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common and 1 other directory.
by portal-commits@lists.jboss.org
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);
17 years
JBoss Portal SVN: r8874 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common and 1 other directory.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-12 04:48:33 -0500 (Mon, 12 Nov 2007)
New Revision: 8874
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:
generified Tools collection related utility, fixed a few bugs and added test cases
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 08:42:53 UTC (rev 8873)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2007-11-12 09:48:33 UTC (rev 8874)
@@ -138,26 +138,34 @@
}
};
- public static Enumeration toEnumeration(final Iterator iterator)
+ public static <E> Enumeration<E> toEnumeration(final Iterator<E> iterator)
{
- return new Enumeration()
+ if (iterator == null)
{
+ throw new IllegalArgumentException();
+ }
+ return new Enumeration<E>()
+ {
public boolean hasMoreElements()
{
return iterator.hasNext();
}
- public Object nextElement()
+ public E nextElement()
{
return iterator.next();
}
};
}
- public static Enumeration toEnumeration(final Object[] objects)
+ public static <E> Enumeration<E> toEnumeration(final E[] objects)
{
- return new Enumeration()
+ if (objects == null)
{
+ throw new IllegalArgumentException();
+ }
+ return new Enumeration<E>()
+ {
int index = 0;
public boolean hasMoreElements()
@@ -165,7 +173,7 @@
return index < objects.length;
}
- public Object nextElement()
+ public E nextElement()
{
if (index < objects.length)
{
@@ -179,18 +187,18 @@
};
}
- public static Enumeration toEnumeration(final Object o)
+ public static <E> Enumeration<E> toEnumeration(final E o)
{
- return new Enumeration()
+ return new Enumeration<E>()
{
- boolean hasMore = false;
+ boolean hasMore = true;
public boolean hasMoreElements()
{
return hasMore;
}
- public Object nextElement()
+ public E nextElement()
{
if (hasMore)
{
@@ -205,9 +213,13 @@
};
}
- public static Set toSet(Enumeration e)
+ public static <E> Set<E> toSet(Enumeration<E> e)
{
- HashSet set = new HashSet();
+ if (e == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ HashSet<E> set = new HashSet<E>();
while (e.hasMoreElements())
{
set.add(e.nextElement());
@@ -215,59 +227,56 @@
return set;
}
- public static Object[] toArray(Iterator i)
+ public static <E> Set<E> toSet(E[] objects)
{
- return toList(i).toArray();
- }
-
- public static List toList(Enumeration e)
- {
- List list = new ArrayList();
- while (e.hasMoreElements())
+ if (objects == null)
{
- list.add(e.nextElement());
+ throw new IllegalArgumentException();
}
- return list;
- }
-
- public static Set toSet(Object[] objects)
- {
- HashSet set = new HashSet();
- for (int i = 0; i < objects.length; i++)
+ HashSet<E> set = new HashSet<E>();
+ for (E object : objects)
{
- set.add(objects[i]);
+ set.add(object);
}
return set;
}
/**
* Transforms an iterator into an unordered Set
- *
+ *
* @param iterator The iterator to transform
* @return A HashSet
*/
- public static Set toSet(Iterator iterator)
+ public static <E> Set<E> toSet(Iterator<E> iterator)
{
+ if (iterator == null)
+ {
+ throw new IllegalArgumentException();
+ }
return toSet(iterator, false);
}
/**
* Transforms an iterator into a Set
- *
+ *
* @param iterator The iterator to transform
- * @param ordered true if the set must respect the ordering of the iterator
+ * @param preserveOrder true if the set must respect the ordering of the iterator
* @return a LinkedHashSet if ordered is true, a HashSet otherwise
*/
- public static Set toSet(Iterator iterator, boolean ordered)
+ public static <E> Set<E> toSet(Iterator<E> iterator, boolean preserveOrder)
{
- Set set = null;
- if (ordered)
+ if (iterator == null)
{
- set = new LinkedHashSet();
+ throw new IllegalArgumentException();
}
+ Set<E> set;
+ if (preserveOrder)
+ {
+ set = new LinkedHashSet<E>();
+ }
else
{
- set = new HashSet();
+ set = new HashSet<E>();
}
while (iterator.hasNext())
{
@@ -276,20 +285,27 @@
return set;
}
- public static List toList(Object[] objects)
+ public static <E> List<E> toList(Enumeration<E> e)
{
- List list = new ArrayList(objects.length);
- for (int i = 0; i < objects.length; i++)
+ if (e == null)
{
- Object object = objects[i];
- list.add(object);
+ throw new IllegalArgumentException();
}
+ List<E> list = new ArrayList<E>();
+ while (e.hasMoreElements())
+ {
+ list.add(e.nextElement());
+ }
return list;
}
- public static List toList(Iterator iterator)
+ public static <E> List<E> toList(Iterator<E> iterator)
{
- List list = new ArrayList();
+ if (iterator == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ List<E> list = new ArrayList<E>();
while (iterator.hasNext())
{
list.add(iterator.next());
@@ -297,15 +313,42 @@
return list;
}
+ public static <E> List<E> toList(E[] objects)
+ {
+ if (objects == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ List<E> list = new ArrayList<E>(objects.length);
+ for (E object : objects)
+ {
+ list.add(object);
+ }
+ return list;
+ }
+
/**
+ * Consider remove this method as it cannot be generified.
+ *
+ * @param i
+ * @return
+ */
+ @Deprecated
+ public static Object[] toArray(Iterator i)
+ {
+ // This method cannot be generified.
+ return toList(i).toArray();
+ }
+
+ /**
* Returns a singleton iterator.
*
* @param o the singleton object
* @return the iterator
*/
- public static Iterator iterator(final Object o)
+ public static <E> Iterator<E> iterator(final E o)
{
- return new Iterator()
+ return new Iterator<E>()
{
/** The status of the iterator. */
boolean done = false;
@@ -315,7 +358,7 @@
return !done;
}
- public Object next()
+ public E next()
{
if (done)
{
@@ -339,7 +382,7 @@
* @return the iterator
* @throws IllegalArgumentException if the object array is null or the specified range is not valid
*/
- public static Iterator iterator(final Object[] objects) throws IllegalArgumentException
+ public static <E> Iterator<E> iterator(final E[] objects) throws IllegalArgumentException
{
if (objects == null)
{
@@ -349,15 +392,17 @@
}
/**
- * Returns an iterator over the array elements within the specified range.
+ * Returns an iterator over the array elements within the specified range. The range is considered as valid
+ * if the from argument is greater or equals than zero, the to argument is lesser or equals than array size
+ * and the from argument is lesser or equals to the to argument.
*
* @param objects the array containing the objects to iterate on
* @param from the inclusive start index
* @param to the exclusive stop index
* @return the iterator
- * @throws IllegalArgumentException if the object array is null or the specified range is not valid
+ * @throws IllegalArgumentException if the object array is null or the specified range is not valid or if the range is not valid
*/
- public static Iterator iterator(final Object[] objects, final int from, final int to) throws IllegalArgumentException
+ public static <E> Iterator<E> iterator(final E[] objects, final int from, final int to) throws IllegalArgumentException
{
if (objects == null)
{
@@ -367,7 +412,7 @@
{
throw new IllegalArgumentException("Invalid range [" + from + "," + to + "] for array of length " + objects.length);
}
- return new Iterator()
+ return new Iterator<E>()
{
/** . */
int index = from;
@@ -377,9 +422,9 @@
return index < to;
}
- public Object next()
+ public E next()
{
- if (index >= objects.length)
+ if (index >= to)
{
throw new NoSuchElementException("Index is greater than the array length");
}
@@ -597,13 +642,7 @@
public static String getShortNameOf(Class clazz)
{
- String name = clazz.getName();
- int index = name.lastIndexOf('.');
- if (index != -1)
- {
- name = name.substring(index + 1);
- }
- return name;
+ return clazz.getSimpleName();
}
public static String getPackageOf(Class clazz)
@@ -746,7 +785,7 @@
* @throws IllegalArgumentException if the array is null
* @throws ClassCastException if the appended object class prevents it from being added to the array
*/
- public static Object[] appendTo(Object[] array, Object o) throws IllegalArgumentException, ClassCastException
+ public static <E> E[] appendTo(E[] array, E o) throws IllegalArgumentException, ClassCastException
{
if (array == null)
{
@@ -757,7 +796,7 @@
{
throw new ClassCastException("Object with class " + o.getClass().getName() + " cannot be casted to class " + componentType.getComponentType().getName());
}
- Object[] copy = (Object[])Array.newInstance(componentType, array.length + 1);
+ E[] copy = (E[])Array.newInstance(componentType, array.length + 1);
copy[array.length] = o;
return copy;
}
@@ -838,6 +877,12 @@
/**
* Determines if value is contained in array.
*
+ * todo: correct this method contract in order to make it more reusable, it looks like for now like a method
+ * which has a contract convenient only for some kind of callers.
+ *
+ * 1/ null value should be accepted (or the method should be called isContainedInButNotForNullValue ?)
+ * 2/ null array should not be accepted (or the method should be called isContainedInExceptIfThePassedArrayIsNull ?)
+ *
* @param value
* @param array
* @return
@@ -850,11 +895,12 @@
return false;
}
+ //
if (array != null)
{
- for (int i = 0; i < array.length; i++)
+ for (Object anArray : array)
{
- if (value.equals(array[i]))
+ if (value.equals(anArray))
{
return true;
}
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 08:42:53 UTC (rev 8873)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java 2007-11-12 09:48:33 UTC (rev 8874)
@@ -26,6 +26,15 @@
import junit.framework.TestCase;
import org.jboss.portal.common.util.Tools;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.Vector;
+
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
* @version $Revision: 6384 $
@@ -44,4 +53,337 @@
assertFalse(Tools.isContainedIn(null, null));
assertFalse(Tools.isContainedIn("bat", array));
}
+
+ public void testIteratorToEnumerationThrowsIAE()
+ {
+ try
+ {
+ Tools.toEnumeration((Iterator)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIteratorToEnumeration1()
+ {
+ assertEnumeration(Tools.toEnumeration(new ArrayList<String>().iterator()));
+ }
+
+ public void testIteratorToEnumeration2()
+ {
+ List<String> tmp = new ArrayList<String>();
+ tmp.add("a");
+ assertEnumeration(Tools.toEnumeration(tmp.iterator()), "a");
+ }
+
+ public void testIteratorToEnumeration3()
+ {
+ List<String> tmp = new ArrayList<String>();
+ tmp.add("a");
+ tmp.add("b");
+ assertEnumeration(Tools.toEnumeration(tmp.iterator()), "a", "b");
+ }
+
+ public void testArrayToEnumerationThrowsIAE()
+ {
+ try
+ {
+ Tools.toEnumeration((Object[])null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayToEnumeration1()
+ {
+ assertEnumeration(Tools.toEnumeration(new String[]{}));
+ }
+
+ public void testArrayToEnumeration2()
+ {
+ assertEnumeration(Tools.toEnumeration(new String[]{"a"}), "a");
+ }
+
+ public void testArrayToEnumeration3()
+ {
+ assertEnumeration(Tools.toEnumeration(new String[]{"a","b"}), "a", "b");
+ }
+
+ public void testElementToEnumeration()
+ {
+ assertEnumeration(Tools.toEnumeration("a"), "a");
+ }
+
+ public void testEnumerationToSetThrowsIAE()
+ {
+ try
+ {
+ Tools.toSet((Enumeration)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testEnumerationToSet()
+ {
+ Vector<String> v = new Vector<String>();
+ v.add("a");
+ v.add("b");
+ v.add("a");
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, Tools.toSet(v.elements()));
+ }
+
+ public void testArrayToSetThrowsIAE()
+ {
+ try
+ {
+ Tools.toSet((Object[])null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayToSet()
+ {
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, Tools.toSet(new String[]{"a","b","a"}));
+ }
+
+ public void testIteratorToSetThrowsIAE()
+ {
+ try
+ {
+ Tools.toSet((Iterator)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIteratorToSet()
+ {
+ List<String> list = new ArrayList<String>();
+ list.add("a");
+ list.add("b");
+ list.add("a");
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, Tools.toSet(list.iterator()));
+ }
+
+ public void testEnumerationToListThrowsIAE()
+ {
+ try
+ {
+ Tools.toList((Enumeration)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testEnumerationToList()
+ {
+ Vector<String> v = new Vector<String>();
+ v.add("a");
+ v.add("b");
+ v.add("a");
+ List<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, Tools.toList(v.elements()));
+ }
+
+ public void testArrayToListThrowsIAE()
+ {
+ try
+ {
+ Tools.toList((Object[])null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayToList()
+ {
+ List<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, Tools.toList(new String[]{"a","b","a"}));
+ }
+
+ public void testIteratorToListThrowsIAE()
+ {
+ try
+ {
+ Tools.toList((Iterator)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIteratorToList()
+ {
+ List<String> list = new ArrayList<String>();
+ list.add("a");
+ list.add("b");
+ list.add("a");
+ List<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, Tools.toList(list.iterator()));
+ }
+
+
+ public void testArrayIteratorThrowsIAE()
+ {
+ try
+ {
+ Tools.iterator(null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayIterator1()
+ {
+ assertIterator(Tools.iterator(new String[]{}));
+ }
+
+ public void testArrayIterator2()
+ {
+ assertIterator(Tools.iterator(new String[]{"a"}), "a");
+ }
+
+ public void testArrayIterator3()
+ {
+ assertIterator(Tools.iterator(new String[]{"a","b"}), "a", "b");
+ }
+
+ public void testElementIterator()
+ {
+ assertIterator(Tools.iterator("a"), "a");
+ }
+
+ public void testArrayRangeIteratorThrowsIAE()
+ {
+ try
+ {
+ Tools.iterator(null, 0, 0);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayRangeIterator1()
+ {
+ assertIteratorMethodThrowIAE(new String[]{}, -1, 0);
+ assertIteratorMethodThrowIAE(new String[]{}, 0, 1);
+ assertIteratorMethodThrowIAE(new String[]{}, 1, 0);
+ assertIterator(Tools.iterator(new String[]{}, 0, 0));
+ }
+
+ public void testArrayRangeIterator2()
+ {
+ assertIteratorMethodThrowIAE(new String[]{"a"}, -1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a"}, 1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a"}, 1, 2);
+ assertIterator(Tools.iterator(new String[]{"a"}, 0, 0));
+ assertIterator(Tools.iterator(new String[]{"a"}, 1, 1));
+ assertIterator(Tools.iterator(new String[]{"a"}, 0, 1), "a");
+ }
+
+ public void testArrayRangeIterator3()
+ {
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, -1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 2, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 2, 1);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 2, 3);
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 0));
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 1, 1));
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 2, 2));
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 1), "a");
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 1, 2), "b");
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 2), "a", "b");
+ }
+
+ private <E> void assertEnumeration(Enumeration<E> elements, E... expectedElements)
+ {
+ assertNotNull(elements);
+ for (E expectedElement : expectedElements)
+ {
+ assertTrue(elements.hasMoreElements());
+ E element = elements.nextElement();
+ assertEquals(expectedElement, element);
+ }
+ assertFalse(elements.hasMoreElements());
+ try
+ {
+ elements.nextElement();
+ fail();
+ }
+ catch (NoSuchElementException expected)
+ {
+ }
+ }
+
+ private void assertIteratorMethodThrowIAE(Object[] array, int from, int to)
+ {
+ try
+ {
+ Tools.iterator(array, from, to);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ private <E> void assertIterator(Iterator<E> elements, E... expectedElements)
+ {
+ assertNotNull(elements);
+ for (E expectedElement : expectedElements)
+ {
+ assertTrue(elements.hasNext());
+ E element = elements.next();
+ assertEquals(expectedElement, element);
+ }
+ assertFalse(elements.hasNext());
+ try
+ {
+ elements.next();
+ fail();
+ }
+ catch (NoSuchElementException expected)
+ {
+ }
+ }
}
17 years
JBoss Portal SVN: r8873 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common/util and 1 other directory.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-12 03:42:53 -0500 (Mon, 12 Nov 2007)
New Revision: 8873
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionBuilderTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MapBuilderTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java
Log:
added test cases for CollectionBuilder and MapBuilder
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java 2007-11-11 23:21:24 UTC (rev 8872)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java 2007-11-12 08:42:53 UTC (rev 8873)
@@ -33,44 +33,65 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7234 $
*/
-public class CollectionBuilder<V>
+public class CollectionBuilder<C extends Collection<V>, V>
{
/** . */
- private Collection<V> collection;
+ private C collection;
- private CollectionBuilder(Collection<V> collection)
+ private CollectionBuilder(C collection)
{
this.collection = collection;
}
- public static <V> CollectionBuilder<V> create(Collection<V> collection)
+ public static <C extends Collection<V>, V> CollectionBuilder<C, V> create(C collection)
{
- return new CollectionBuilder<V>(collection);
+ return new CollectionBuilder<C, V>(collection);
}
- public static <V> CollectionBuilder<V> arrayList()
+ public static <V> CollectionBuilder<ArrayList<V>, V> arrayList()
{
- return new CollectionBuilder<V>(new ArrayList<V>());
+ return new CollectionBuilder<ArrayList<V>, V>(new ArrayList<V>());
}
- public static <V> CollectionBuilder<V> linkedList()
+ public static <V> CollectionBuilder<ArrayList<V>, V> arrayList(V v)
{
- return new CollectionBuilder<V>(new LinkedList<V>());
+ CollectionBuilder<ArrayList<V>, V> builder = new CollectionBuilder<ArrayList<V>, V>(new ArrayList<V>());
+ builder.add(v);
+ return builder;
}
- public static <V> CollectionBuilder<V> hashSet()
+ public static <V> CollectionBuilder<LinkedList<V>, V> linkedList()
{
- return new CollectionBuilder<V>(new HashSet<V>());
+ return new CollectionBuilder<LinkedList<V>, V>(new LinkedList<V>());
}
+ public static <V> CollectionBuilder<LinkedList<V>, V> linkedList(V v)
+ {
+ CollectionBuilder<LinkedList<V>, V> builder = new CollectionBuilder<LinkedList<V>, V>(new LinkedList<V>());
+ builder.add(v);
+ return builder;
+ }
+
+ public static <V> CollectionBuilder<HashSet<V>, V> hashSet()
+ {
+ return new CollectionBuilder<HashSet<V>, V>(new HashSet<V>());
+ }
+
+ public static <V> CollectionBuilder<HashSet<V>, V> hashSet(V v)
+ {
+ CollectionBuilder<HashSet<V>, V> builder = new CollectionBuilder<HashSet<V>, V>(new HashSet<V>());
+ builder.add(v);
+ return builder;
+ }
+
/**
* Add the object to the collection.
*
* @param o the object to add
* @return the builder
*/
- public CollectionBuilder<V> add(V o)
+ public CollectionBuilder<C, V> add(V o)
{
collection.add(o);
return this;
@@ -82,13 +103,13 @@
* @param all the objects to add
* @return the builder
*/
- public CollectionBuilder<V> addAll(Collection<V> all)
+ public CollectionBuilder<C, V> addAll(Collection<V> all)
{
collection.addAll(all);
return this;
}
- public Collection<V> get()
+ public C get()
{
return collection;
}
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java 2007-11-11 23:21:24 UTC (rev 8872)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java 2007-11-12 08:42:53 UTC (rev 8873)
@@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.TreeMap;
/**
* An helper to build map in a simple manner.
@@ -32,13 +33,13 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7228 $
*/
-public class MapBuilder<K, V>
+public class MapBuilder<M extends Map<K, V>, K, V>
{
/** . */
- private final Map<K, V> map;
+ private final M map;
- private MapBuilder(Map<K, V> map)
+ private MapBuilder(M map)
{
if (map == null)
{
@@ -52,9 +53,9 @@
*
* @return a new instance
*/
- public static <K, V> MapBuilder<K, V> hashMap()
+ public static <K, V> MapBuilder<HashMap<K, V>, K, V> hashMap()
{
- return new MapBuilder<K, V>(new HashMap<K, V>());
+ return new MapBuilder<HashMap<K, V>, K, V>(new HashMap<K, V>());
}
/**
@@ -62,24 +63,70 @@
*
* @return a new instance
*/
- public static <K, V> MapBuilder<K, V> linkedHashMap()
+ public static <K, V> MapBuilder<HashMap<K, V>, K, V> hashMap(K k, V v)
{
- return new MapBuilder<K, V>(new LinkedHashMap<K, V>());
+ MapBuilder<HashMap<K, V>, K, V> builder = new MapBuilder<HashMap<K, V>, K, V>(new HashMap<K, V>());
+ builder.put(k, v);
+ return builder;
}
- public static <K, V> MapBuilder<K, V> create(Map<K, V> map)
+ /**
+ * Creates a new instance.
+ *
+ * @return a new instance
+ */
+ public static <K, V> MapBuilder<TreeMap<K, V>, K, V> treeMap()
{
- return new MapBuilder<K, V>(map);
+ return new MapBuilder<TreeMap<K, V>, K, V>(new TreeMap<K, V>());
}
/**
+ * Creates a new instance.
+ *
+ * @return a new instance
+ */
+ public static <K, V> MapBuilder<TreeMap<K, V>, K, V> treeMap(K k, V v)
+ {
+ MapBuilder<TreeMap<K, V>, K, V> builder = new MapBuilder<TreeMap<K, V>, K, V>(new TreeMap<K, V>());
+ builder.put(k, v);
+ return builder;
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @return a new instance
+ */
+ public static <K, V> MapBuilder<LinkedHashMap<K, V>, K, V> linkedHashMap()
+ {
+ return new MapBuilder<LinkedHashMap<K, V>, K, V>(new LinkedHashMap<K, V>());
+ }
+
+ /**
+ * Creates a new instance.
+ *
+ * @return a new instance
+ */
+ public static <K, V> MapBuilder<LinkedHashMap<K, V>, K, V> linkedHashMap(K k, V v)
+ {
+ MapBuilder<LinkedHashMap<K, V>, K, V> builder = new MapBuilder<LinkedHashMap<K, V>, K, V>(new LinkedHashMap<K, V>());
+ builder.put(k, v);
+ return builder;
+ }
+
+ public static <M extends Map<K, V>, K, V> MapBuilder<M, K, V> create(M m)
+ {
+ return new MapBuilder<M, K, V>(m);
+ }
+
+ /**
* Add the object to the collection.
*
* @param key the key
* @param value the value
* @return the builder
*/
- public MapBuilder<K, V> put(K key, V value)
+ public MapBuilder<M, K, V> put(K key, V value)
{
map.put(key, value);
return this;
@@ -91,13 +138,13 @@
* @param all the entries to add
* @return the builder
*/
- public MapBuilder<K, V> putAll(Map<K, V> all)
+ public MapBuilder<M, K, V> putAll(M all)
{
map.putAll(all);
return this;
}
- public Map<K, V> get()
+ public M get()
{
return map;
}
Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionBuilderTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionBuilderTestCase.java (rev 0)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionBuilderTestCase.java 2007-11-12 08:42:53 UTC (rev 8873)
@@ -0,0 +1,158 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.CollectionBuilder;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CollectionBuilderTestCase extends TestCase
+{
+
+ public void testHashSet1()
+ {
+ HashSet expected = new HashSet();
+ assertEquals(expected, CollectionBuilder.hashSet().get());
+ }
+
+ public void testHashSet2()
+ {
+ HashSet<String> expected = new HashSet<String>();
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.hashSet("a").get());
+ }
+
+ public void testHashSet3()
+ {
+ HashSet<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, CollectionBuilder.hashSet("a").add("b").get());
+ }
+
+ public void testHashSet4()
+ {
+ HashSet<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, CollectionBuilder.hashSet("a").add("b").add("a").get());
+ }
+
+ public void testArrayList1()
+ {
+ ArrayList expected = new ArrayList();
+ assertEquals(expected, CollectionBuilder.arrayList().get());
+ }
+
+ public void testArrayList2()
+ {
+ ArrayList<String> expected = new ArrayList<String>();
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.arrayList("a").get());
+ }
+
+ public void testArrayList3()
+ {
+ ArrayList<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, CollectionBuilder.arrayList("a").add("b").get());
+ }
+
+ public void testArrayList4()
+ {
+ ArrayList<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.arrayList("a").add("b").add("a").get());
+ }
+
+ public void testLinkedList1()
+ {
+ LinkedList expected = new LinkedList();
+ assertEquals(expected, CollectionBuilder.linkedList().get());
+ }
+
+ public void testLinkedList2()
+ {
+ LinkedList<String> expected = new LinkedList<String>();
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.linkedList("a").get());
+ }
+
+ public void testLinkedList3()
+ {
+ LinkedList<String> expected = new LinkedList<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, CollectionBuilder.linkedList("a").add("b").get());
+ }
+
+ public void testLinkedList4()
+ {
+ LinkedList<String> expected = new LinkedList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.linkedList("a").add("b").add("a").get());
+ }
+
+ public void testSet1()
+ {
+ Set<String> expected = new HashSet<String>();
+ assertEquals(expected, CollectionBuilder.create(new HashSet<String>()).get());
+ }
+
+ public void testSet2()
+ {
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.create(new HashSet<String>()).add("a").get());
+ }
+
+ public void testSet3()
+ {
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, CollectionBuilder.create(new HashSet<String>()).add("a").add("b").get());
+ }
+
+ public void testSet4()
+ {
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, CollectionBuilder.create(new HashSet<String>()).add("a").add("b").add("a").get());
+ }
+}
Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MapBuilderTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MapBuilderTestCase.java (rev 0)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MapBuilderTestCase.java 2007-11-12 08:42:53 UTC (rev 8873)
@@ -0,0 +1,151 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.MapBuilder;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class MapBuilderTestCase extends TestCase
+{
+
+ public void testHashMap1()
+ {
+ assertEquals(new HashMap(), MapBuilder.hashMap().get());
+ }
+
+ public void testHashMap2()
+ {
+ HashMap<String, String> expected = new HashMap<String, String>();
+ expected.put("a", "b");
+ assertEquals(expected, MapBuilder.hashMap("a", "b").get());
+ }
+
+ public void testHashMap3()
+ {
+ HashMap<String, String> expected = new HashMap<String, String>();
+ expected.put("a", "b");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.hashMap("a", "b").put("c", "d").get());
+ }
+
+ public void testHashMap4()
+ {
+ HashMap<String, String> expected = new HashMap<String, String>();
+ expected.put("a", "d");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.hashMap("a", "b").put("c", "d").put("a", "d").get());
+ }
+
+ public void testLinkedHashMap1()
+ {
+ assertEquals(new LinkedHashMap(), MapBuilder.linkedHashMap().get());
+ }
+
+ public void testLinkedHashMap2()
+ {
+ LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>();
+ expected.put("a", "b");
+ assertEquals(expected, MapBuilder.linkedHashMap("a", "b").get());
+ }
+
+ public void testLinkedHashMap3()
+ {
+ LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>();
+ expected.put("a", "b");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.linkedHashMap("a", "b").put("c", "d").get());
+ }
+
+ public void testLinkedHashMap4()
+ {
+ LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>();
+ expected.put("a", "d");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.linkedHashMap("a", "b").put("c", "d").put("a", "d").get());
+ }
+
+ public void testTreeMap1()
+ {
+ assertEquals(new TreeMap(), MapBuilder.treeMap().get());
+ }
+
+ public void testTreeMap2()
+ {
+ TreeMap<String, String> expected = new TreeMap<String, String>();
+ expected.put("a", "b");
+ assertEquals(expected, MapBuilder.treeMap("a", "b").get());
+ }
+
+ public void testTreeMap3()
+ {
+ TreeMap<String, String> expected = new TreeMap<String, String>();
+ expected.put("a", "b");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.treeMap("a", "b").put("c", "d").get());
+ }
+
+ public void testTreeMap4()
+ {
+ TreeMap<String, String> expected = new TreeMap<String, String>();
+ expected.put("a", "d");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.treeMap("a", "b").put("c", "d").put("a", "d").get());
+ }
+
+ public void testMap1()
+ {
+ assertEquals(new HashMap(), MapBuilder.create(new HashMap<String, String>()).get());
+ }
+
+ public void testMap2()
+ {
+ Map<String, String> expected = new HashMap<String, String>();
+ expected.put("a", "b");
+ assertEquals(expected, MapBuilder.create(new HashMap<String, String>()).put("a", "b").get());
+ }
+
+ public void testMap3()
+ {
+ Map<String, String> expected = new HashMap<String, String>();
+ expected.put("a", "b");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.create(new HashMap<String, String>()).put("a", "b").put("c", "d").get());
+ }
+
+ public void testMap4()
+ {
+ Map<String, String> expected = new HashMap<String, String>();
+ expected.put("a", "d");
+ expected.put("c", "d");
+ assertEquals(expected, MapBuilder.create(new HashMap<String, String>()).put("a", "b").put("c", "d").put("a", "d").get());
+ }
+}
17 years
JBoss Portal SVN: r8872 - branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/test/core/model/instance.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-11 18:21:24 -0500 (Sun, 11 Nov 2007)
New Revision: 8872
Modified:
branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/test/core/model/instance/InstanceContainerTestCase.java
Log:
udpate 2.6 to CollectionBuilder changes
Modified: branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/test/core/model/instance/InstanceContainerTestCase.java
===================================================================
--- branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/test/core/model/instance/InstanceContainerTestCase.java 2007-11-11 23:17:53 UTC (rev 8871)
+++ branches/JBoss_Portal_Branch_2_6/core/src/main/org/jboss/portal/test/core/model/instance/InstanceContainerTestCase.java 2007-11-11 23:21:24 UTC (rev 8872)
@@ -337,7 +337,7 @@
//
TransactionAssert.beginTransaction();
instance = instanceContainer.getDefinition("MyInstance");
- PropertyMap props = instance.getProperties(new CollectionBuilder().add("abc").add("ghi").toHashSet());
+ PropertyMap props = instance.getProperties((Set)CollectionBuilder.hashSet().add("abc").add("ghi").get());
PropertyMap expectedProps = new SimplePropertyMap();
expectedProps.setProperty("abc", new StringValue("_def"));
expectedProps.setProperty("ghi", new StringValue("_jkl"));
@@ -870,10 +870,10 @@
}
/** . */
- private final Set securityBindings1 = Collections.unmodifiableSet(CollectionBuilder.singleton(new RoleSecurityBinding("a,b", "admin")).add(new RoleSecurityBinding("a", "user")).toHashSet());
+ private final Set securityBindings1 = Collections.unmodifiableSet((Set)CollectionBuilder.hashSet().add(new RoleSecurityBinding("a,b", "admin")).add(new RoleSecurityBinding("a", "user")).get());
/** . */
- private final Set securityBindings2 = Collections.unmodifiableSet(CollectionBuilder.singleton(new RoleSecurityBinding("a,b", "user")).toHashSet());
+ private final Set securityBindings2 = Collections.unmodifiableSet((Set)CollectionBuilder.hashSet().add(new RoleSecurityBinding("a,b", "user")).get());
public void testSecurityConfiguration() throws Exception
{
17 years
JBoss Portal SVN: r8871 - in branches/JBoss_Portal_Branch_2_6/wsrp/src: resources/portal-wsrp-war/WEB-INF and 1 other directory.
by portal-commits@lists.jboss.org
Author: chris.laprun(a)jboss.com
Date: 2007-11-11 18:17:53 -0500 (Sun, 11 Nov 2007)
New Revision: 8871
Removed:
branches/JBoss_Portal_Branch_2_6/wsrp/src/main/org/jboss/portal/wsrp/servlet/WSDLPortFixFilter.java
Modified:
branches/JBoss_Portal_Branch_2_6/wsrp/src/resources/portal-wsrp-war/WEB-INF/web.xml
Log:
JBPORTAL-1723: Removed WSDLPortFixFilter as it's not needed anymore with AS 4.2+ which will be required for 2.6.3+
Deleted: branches/JBoss_Portal_Branch_2_6/wsrp/src/main/org/jboss/portal/wsrp/servlet/WSDLPortFixFilter.java
===================================================================
--- branches/JBoss_Portal_Branch_2_6/wsrp/src/main/org/jboss/portal/wsrp/servlet/WSDLPortFixFilter.java 2007-11-11 22:56:28 UTC (rev 8870)
+++ branches/JBoss_Portal_Branch_2_6/wsrp/src/main/org/jboss/portal/wsrp/servlet/WSDLPortFixFilter.java 2007-11-11 23:17:53 UTC (rev 8871)
@@ -1,325 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2007, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
- ******************************************************************************/
-
-package org.jboss.portal.wsrp.servlet;
-
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpServletResponseWrapper;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * A Filter that fixes the ports returned by JBWS. This is a hack and this class will be removed when JBWS 1.2.1 is
- * available. This includes code from URLTools that has been duplicated and simplified here for either inclusion.
- * Post-processing based on a <a href="http://www.ftponline.com/javapro/2002_02/magazine/features/kjones/default...">Java
- * Pro article</a>.
- *
- * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
- * @version $Revision$
- * @since 2.4.2
- */
-public class WSDLPortFixFilter implements Filter
-{
- private static final ThreadLocal local = new ThreadLocal();
-
- public void init(FilterConfig filterConfig) throws ServletException
- {
- }
-
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
- {
- doFilter((HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse, filterChain);
- }
-
- public void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException
- {
- // only process if we request the WSDL
- if ("wsdl".equalsIgnoreCase(req.getQueryString()))
- {
- // wrap the response to capture the data and be able to post-process it later
- WSDLProcessorResponseWrapper wrappedResponse = new WSDLProcessorResponseWrapper(resp);
-
- // continue processing
- chain.doFilter(req, wrappedResponse);
-
- // get the data
- String wsdl = wrappedResponse.getData();
-
- // if we don't have any data, just finish
- if (wsdl == null || wsdl.length() == 0)
- {
- return;
- }
-
- // create the processed wsdl if required and cache the value
- CachedDataHolder holder = (CachedDataHolder)local.get();
- if (holder == null)
- {
- holder = new CachedDataHolder();
- holder.processedWSDL = replaceURLsBy(wsdl, new PortReplacementGenerator(req.getServerPort()));
- StringWriter cachedWriter = new StringWriter(holder.processedWSDL.length());
- cachedWriter.write(holder.processedWSDL);
- holder.cachedWriter = cachedWriter;
- local.set(holder);
- }
-
- resp.setContentType("text/xml");
-
- resp.setContentLength(holder.cachedWriter.toString().length());
-
- resp.getWriter().print(holder.cachedWriter.toString());
- }
- else
- {
- chain.doFilter(req, resp);
- }
- }
-
- public void destroy()
- {
- }
-
- /** ServletOutputStream backed by a ByteArrayOutputStream to capture data (and more importantly access it later) */
- private static class ByteArrayServletStream extends ServletOutputStream
- {
- ByteArrayOutputStream baos;
-
- ByteArrayServletStream(ByteArrayOutputStream baos)
- {
- this.baos = baos;
- }
-
- public void write(int param) throws java.io.IOException
- {
- baos.write(param);
- }
- }
-
- /** Hybrid PrintWriter / ServletOutputStream to capture the data during the filtering process */
- private static class ByteArrayPrintWriter
- {
- private ByteArrayOutputStream baos = new ByteArrayOutputStream();
- private PrintWriter pw = new PrintWriter(baos);
- private ServletOutputStream sos = new ByteArrayServletStream(baos);
-
- public PrintWriter getWriter()
- {
- return pw;
- }
-
- public ServletOutputStream getStream()
- {
- return sos;
- }
-
- byte[] toByteArray()
- {
- return baos.toByteArray();
- }
-
- public String toString()
- {
- return baos.toString();
- }
- }
-
- /** Response wrapper using the hybrid PrintWriter / ServletOutputStream */
- public class WSDLProcessorResponseWrapper extends HttpServletResponseWrapper
- {
- public WSDLProcessorResponseWrapper(HttpServletResponse response)
- {
- super(response);
- }
-
- ByteArrayPrintWriter bapw = new ByteArrayPrintWriter();
-
- public PrintWriter getWriter()
- {
- return bapw.getWriter();
- }
-
- public ServletOutputStream getOutputStream() throws IOException
- {
- return bapw.getStream();
- }
-
- public String getData()
- {
- return bapw.toString();
- }
- }
-
- private class CachedDataHolder
- {
- private String processedWSDL;
- private StringWriter cachedWriter;
- }
-
- // === Code duplicated from URLTools for easier inclusion === //
-
- private static final Pattern LINK = Pattern.compile("(?:location)\\s*=\\s*('|\")\\s*([^'\"]*)\\s*('|\")",
- Pattern.CASE_INSENSITIVE);
-
- public static final String HTTP_PREFIX = "http://";
- public static final String HTTPS_PREFIX = "https://";
-
- public static boolean isNetworkURL(String url)
- {
- if (url == null || url.length() == 0)
- {
- return false;
- }
-
- return url.startsWith(HTTP_PREFIX) || url.startsWith(HTTPS_PREFIX);
- }
-
- public static URLMatch[] extractURLsFrom(String markup)
- {
- int length;
- if (markup != null && (length = markup.length()) != 0)
- {
- Matcher matcher = LINK.matcher(markup);
- int currentIndex = 0;
- List links = new ArrayList();
- while (matcher.find(currentIndex) && currentIndex < length)
- {
- links.add(new URLMatch(matcher.start(2), matcher.end(2), matcher.group(2)));
- currentIndex = matcher.end();
- }
-
- return (URLMatch[])links.toArray(new URLMatch[0]);
- }
- throw new IllegalArgumentException("Cannot extract URLs from a null or empty markup string!");
- }
-
- public static String replaceURLsBy(String markup, URLReplacementGenerator generator)
- {
-
- URLMatch[] urls = extractURLsFrom(markup);
- if (urls.length > 0)
- {
- StringBuffer newMarkup = new StringBuffer(markup.length());
- int currentIndex = 0;
- for (int i = 0; i < urls.length; i++)
- {
- URLMatch url = urls[i];
- newMarkup.append(markup.substring(currentIndex, url.getStart())).append(generator.getReplacementFor(i, url));
- currentIndex = url.getEnd();
- }
- newMarkup.append(markup.substring(currentIndex));
- markup = newMarkup.toString();
- }
- return markup;
- }
-
- public static class URLMatch
- {
- private int start;
- private int end;
- private String urlAsString;
-
- private URLMatch(int start, int end, String urlAsString)
- {
- this.start = start;
- this.end = end;
- this.urlAsString = urlAsString;
- }
-
- public int getStart()
- {
- return start;
- }
-
- public int getEnd()
- {
- return end;
- }
-
- public String getURLAsString()
- {
- return urlAsString;
- }
- }
-
- public abstract static class URLReplacementGenerator
- {
- public abstract String getReplacementFor(int currentIndex, URLMatch currentMatch);
- }
-
- public static class PortReplacementGenerator extends URLReplacementGenerator
- {
- private int replacementPort;
-
- public PortReplacementGenerator(int replacementPort)
- {
- this.replacementPort = replacementPort;
- }
-
- public String getReplacementFor(int currentIndex, URLMatch currentMatch)
- {
- return replaceServerPortInURL(currentMatch.getURLAsString(), replacementPort);
- }
- }
-
- public static String replaceServerPortInURL(String url, int newPort)
- {
- if (!isNetworkURL(url))
- {
- return url;
- }
-
- StringBuffer buf = new StringBuffer(url);
- int afterProtocol = url.indexOf("://") + 3;
- int beforePort = url.indexOf(':', afterProtocol);
- int afterPort;
-
- if (beforePort != -1)
- {
- afterPort = url.indexOf('/', beforePort);
- buf.delete(beforePort + 1, afterPort);
- buf.insert(beforePort + 1, newPort);
- }
- else
- {
- // port number was not present
- afterPort = url.indexOf('/', afterProtocol);
- buf.insert(afterPort, ":" + newPort);
- }
-
- return buf.toString();
- }
-}
Modified: branches/JBoss_Portal_Branch_2_6/wsrp/src/resources/portal-wsrp-war/WEB-INF/web.xml
===================================================================
--- branches/JBoss_Portal_Branch_2_6/wsrp/src/resources/portal-wsrp-war/WEB-INF/web.xml 2007-11-11 22:56:28 UTC (rev 8870)
+++ branches/JBoss_Portal_Branch_2_6/wsrp/src/resources/portal-wsrp-war/WEB-INF/web.xml 2007-11-11 23:17:53 UTC (rev 8871)
@@ -47,16 +47,6 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
- <!-- Fixes port numbers that are not properly propagated by JBoss WS -->
- <filter>
- <filter-name>WSDLPortFixFilter</filter-name>
- <filter-class>org.jboss.portal.wsrp.servlet.WSDLPortFixFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>WSDLPortFixFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
<!-- WSRP Endpoints -->
<servlet>
<servlet-name>ServiceDescriptionService</servlet-name>
17 years
JBoss Portal SVN: r8870 - modules/common/trunk/common/src/test/java/org/jboss/portal/test/common.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-11 17:56:28 -0500 (Sun, 11 Nov 2007)
New Revision: 8870
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java
Log:
fix compile issue
Modified: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java 2007-11-11 22:31:47 UTC (rev 8869)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java 2007-11-11 22:56:28 UTC (rev 8870)
@@ -369,12 +369,12 @@
}
//
- boolean changed = left.keySet().retainAll(new CollectionBuilder().add("abc").add("def").add("ghi").toHashSet());
+ boolean changed = left.keySet().retainAll(CollectionBuilder.hashSet().add("abc").add("def").add("ghi").get());
assertFalse(changed);
assertEquals(right, left.delegate);
//
- changed = left.keySet().retainAll(new CollectionBuilder().add("def").toHashSet());
+ changed = left.keySet().retainAll(CollectionBuilder.hashSet().add("def").get());
assertTrue(changed);
right.remove("abc");
right.remove("ghi");
17 years
JBoss Portal SVN: r8869 - modules/common/trunk/common/src/main/java/org/jboss/portal/common/util.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-11 17:31:47 -0500 (Sun, 11 Nov 2007)
New Revision: 8869
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java
Log:
refactored and generifed CollectionBuilder and MapBuilder
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java 2007-11-11 22:20:21 UTC (rev 8868)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java 2007-11-11 22:31:47 UTC (rev 8869)
@@ -33,37 +33,44 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7234 $
*/
-public class CollectionBuilder
+public class CollectionBuilder<V>
{
/** . */
- private ArrayList collection;
+ private Collection<V> collection;
- public CollectionBuilder()
+ private CollectionBuilder(Collection<V> collection)
{
- this.collection = new ArrayList();
+ this.collection = collection;
}
- /**
- * Factory method to create a collection builder using a singleton.
- *
- * @param o the object to add
- * @return the builder created
- */
- public static CollectionBuilder singleton(Object o)
+ public static <V> CollectionBuilder<V> create(Collection<V> collection)
{
- CollectionBuilder builder = new CollectionBuilder();
- builder.collection.add(o);
- return builder;
+ return new CollectionBuilder<V>(collection);
}
+ public static <V> CollectionBuilder<V> arrayList()
+ {
+ return new CollectionBuilder<V>(new ArrayList<V>());
+ }
+
+ public static <V> CollectionBuilder<V> linkedList()
+ {
+ return new CollectionBuilder<V>(new LinkedList<V>());
+ }
+
+ public static <V> CollectionBuilder<V> hashSet()
+ {
+ return new CollectionBuilder<V>(new HashSet<V>());
+ }
+
/**
* Add the object to the collection.
*
* @param o the object to add
* @return the builder
*/
- public CollectionBuilder add(Object o)
+ public CollectionBuilder<V> add(V o)
{
collection.add(o);
return this;
@@ -75,39 +82,14 @@
* @param all the objects to add
* @return the builder
*/
- public CollectionBuilder addAll(Collection all)
+ public CollectionBuilder<V> addAll(Collection<V> all)
{
collection.addAll(all);
return this;
}
- /**
- * Return a set build from the collection.
- *
- * @return a set
- */
- public HashSet toHashSet()
+ public Collection<V> get()
{
- return new HashSet(collection);
+ return collection;
}
-
- /**
- * Return a list build from the collection.
- *
- * @return a list
- */
- public ArrayList toArrayList()
- {
- return new ArrayList(collection);
- }
-
- /**
- * Return a list build from the collection.
- *
- * @return a list
- */
- public LinkedList toLinkedList()
- {
- return new LinkedList(collection);
- }
}
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java 2007-11-11 22:20:21 UTC (rev 8868)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java 2007-11-11 22:31:47 UTC (rev 8869)
@@ -27,18 +27,18 @@
import java.util.Map;
/**
- * An helper to build collection of object in a simple manner.
+ * An helper to build map in a simple manner.
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7228 $
*/
-public class MapBuilder
+public class MapBuilder<K, V>
{
/** . */
- private final Map map;
+ private final Map<K, V> map;
- private MapBuilder(Map map)
+ private MapBuilder(Map<K, V> map)
{
if (map == null)
{
@@ -52,9 +52,9 @@
*
* @return a new instance
*/
- public static MapBuilder hashMap()
+ public static <K, V> MapBuilder<K, V> hashMap()
{
- return new MapBuilder(new HashMap());
+ return new MapBuilder<K, V>(new HashMap<K, V>());
}
/**
@@ -62,14 +62,14 @@
*
* @return a new instance
*/
- public static MapBuilder linkedHashMap()
+ public static <K, V> MapBuilder<K, V> linkedHashMap()
{
- return new MapBuilder(new LinkedHashMap());
+ return new MapBuilder<K, V>(new LinkedHashMap<K, V>());
}
- public static MapBuilder create(Map map)
+ public static <K, V> MapBuilder<K, V> create(Map<K, V> map)
{
- return new MapBuilder(map);
+ return new MapBuilder<K, V>(map);
}
/**
@@ -79,7 +79,7 @@
* @param value the value
* @return the builder
*/
- public MapBuilder put(Object key, Object value)
+ public MapBuilder<K, V> put(K key, V value)
{
map.put(key, value);
return this;
@@ -91,13 +91,13 @@
* @param all the entries to add
* @return the builder
*/
- public MapBuilder putAll(Map all)
+ public MapBuilder<K, V> putAll(Map<K, V> all)
{
map.putAll(all);
return this;
}
- public Map get()
+ public Map<K, V> get()
{
return map;
}
17 years
JBoss Portal SVN: r8868 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common and 1 other directories.
by portal-commits@lists.jboss.org
Author: julien(a)jboss.com
Date: 2007-11-11 17:20:21 -0500 (Sun, 11 Nov 2007)
New Revision: 8868
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java
Log:
generified CollectionMap + added test cases
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java 2007-11-11 21:19:01 UTC (rev 8867)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java 2007-11-11 22:20:21 UTC (rev 8868)
@@ -36,32 +36,40 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public abstract class CollectionMap implements Serializable
+public abstract class CollectionMap<K, V> implements Serializable
{
/** The underlying map. */
- protected Map map;
+ private final Map<K, Collection<V>> map;
public CollectionMap()
{
- init(null);
+ map = init(null);
}
- public CollectionMap(SetMap other) throws IllegalArgumentException
+ public CollectionMap(SetMap<K, V> other) throws IllegalArgumentException
{
if (other == null)
{
throw new IllegalArgumentException("Cannot copy null argument");
}
- init(other);
+ map = init(other);
}
/**
* Add an object in the set keyed under the specified key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public void put(Object key, Object o)
+ public final void put(K key, V o) throws NullPointerException
{
- Collection collection = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> collection = map.get(key);
if (collection == null)
{
collection = newCollection();
@@ -73,28 +81,48 @@
/**
* Return the set of keys.
*/
- public Set keySet()
+ public final Set<K> keySet()
{
return map.keySet();
}
/**
* Remove the entire set of objects specified by the key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public void remove(Object key)
+ public final void remove(K key) throws NullPointerException
{
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
map.remove(key);
}
/**
* Remove an object in the set keyed under the specified key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public void remove(Object key, Object o)
+ public final void remove(K key, Object o) throws NullPointerException
{
- Collection collection = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> collection = map.get(key);
+
+ //
if (collection != null)
{
remove(collection, o);
+
+ //
if (collection.isEmpty())
{
map.remove(key);
@@ -104,10 +132,20 @@
/**
* Return true if the specified set contains the object o.
+ *
+ * @throws NullPointerException if the key is null
*/
- public boolean contains(Object key, Object o)
+ public final boolean contains(K key, Object o) throws NullPointerException
{
- Collection collection = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> collection = map.get(key);
+
+ //
if (collection == null)
{
return false;
@@ -119,25 +157,44 @@
}
/**
+ * Return the collection specified by the key.
+ */
+ public Collection<V> get(K key)
+ {
+ return map.get(key);
+ }
+
+ /**
* Return an iterator over the values in the set specified by the key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public Iterator iterator(final Object key)
+ public final Iterator<V> iterator(final K key)
{
- Collection set = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> set = map.get(key);
+
+ //
if (set == null)
{
- return Collections.EMPTY_SET.iterator();
+ Set<V> tmp = Collections.emptySet();
+ return tmp.iterator();
}
else
{
- final Iterator iterator = set.iterator();
- return new Iterator()
+ final Iterator<V> iterator = set.iterator();
+ return new Iterator<V>()
{
public boolean hasNext()
{
return iterator.hasNext();
}
- public Object next()
+ public V next()
{
return iterator.next();
}
@@ -153,29 +210,38 @@
}
}
- private void init(CollectionMap other)
+ private Map<K, Collection<V>> init(CollectionMap<K, V> other)
{
- //
- this.map = new HashMap();
+ Map<K, Collection<V>> map = new HashMap<K, Collection<V>>();
//
if (other != null)
{
- for (Iterator i = other.map.entrySet().iterator(); i.hasNext();)
+ for (Map.Entry<K, Collection<V>> entry : other.map.entrySet())
{
- Map.Entry entry = (Map.Entry)i.next();
- Object key = entry.getKey();
- Collection value = (Collection)entry.getValue();
+ K key = entry.getKey();
+ Collection<V> value = entry.getValue();
map.put(key, newCollection(value));
}
}
+
+ //
+ return map;
}
- protected abstract void add(Collection c, Object o);
+ protected abstract void add(Collection<V> c, V o);
- protected abstract void remove(Collection c, Object o);
+ /**
+ * Removes an object from the collection. The type of the object to remove is intentionnally
+ * <code>Object</code> and not the parameterized type <code><V></code> because the Collection<V>
+ * interface is designed that way.
+ *
+ * @param c the collection to remove from
+ * @param o the object to remove
+ */
+ protected abstract void remove(Collection<V> c, Object o);
- protected abstract Collection newCollection();
+ protected abstract Collection<V> newCollection();
- protected abstract Collection newCollection(Collection other);
+ protected abstract Collection<V> newCollection(Collection<V> other);
}
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java 2007-11-11 21:19:01 UTC (rev 8867)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java 2007-11-11 22:20:21 UTC (rev 8868)
@@ -27,27 +27,28 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Set;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public class ListMap extends CollectionMap
+public class ListMap<K, V> extends CollectionMap<K, V>
{
/** An optional comparator. */
- protected Comparator comparator;
+ protected Comparator<V> comparator;
public ListMap()
{
}
- public ListMap(SetMap other) throws IllegalArgumentException
+ public ListMap(SetMap<K, V> other) throws IllegalArgumentException
{
super(other);
}
- public ListMap(SetMap other, Comparator comparator) throws IllegalArgumentException
+ public ListMap(SetMap<K, V> other, Comparator<V> comparator) throws IllegalArgumentException
{
super(other);
@@ -61,7 +62,7 @@
this.comparator = comparator;
}
- public ListMap(Comparator comparator)
+ public ListMap(Comparator<V> comparator)
{
super();
@@ -75,35 +76,43 @@
this.comparator = comparator;
}
- protected void add(Collection c, Object o)
+ /**
+ * Return the list specified by the key.
+ */
+ public List<V> get(K key)
{
+ return (List<V>)super.get(key);
+ }
+
+ protected void add(Collection<V> c, V o)
+ {
c.add(o);
//
if (comparator != null)
{
- Collections.sort((List)c, comparator);
+ Collections.sort((List<V>)c, comparator);
}
}
- protected void remove(Collection c, Object o)
+ protected void remove(Collection<V> c, Object o)
{
c.remove(o);
//
if (comparator != null)
{
- Collections.sort((List)c, comparator);
+ Collections.sort((List<V>)c, comparator);
}
}
- protected Collection newCollection()
+ protected Collection<V> newCollection()
{
- return new ArrayList();
+ return new ArrayList<V>();
}
- protected Collection newCollection(Collection other)
+ protected Collection<V> newCollection(Collection<V> other)
{
- return new ArrayList(other);
+ return new ArrayList<V>(other);
}
}
Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java 2007-11-11 21:19:01 UTC (rev 8867)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java 2007-11-11 22:20:21 UTC (rev 8868)
@@ -28,6 +28,7 @@
import java.util.TreeSet;
import java.util.SortedSet;
import java.util.Collection;
+import java.util.Map;
/**
* A map of set. This object does not handle synchronization and use HashMap and HashSet
@@ -36,25 +37,25 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7322 $
*/
-public class SetMap extends CollectionMap
+public class SetMap<K, V> extends CollectionMap<K, V>
{
/** Version. */
static final long serialVersionUID = -7239767000556095977L;
/** An optional comparator. */
- protected Comparator comparator;
+ protected Comparator<V> comparator;
public SetMap()
{
}
- public SetMap(SetMap other) throws IllegalArgumentException
+ public SetMap(SetMap<K, V> other) throws IllegalArgumentException
{
super(other);
}
- public SetMap(SetMap other, Comparator comparator) throws IllegalArgumentException
+ public SetMap(SetMap<K, V> other, Comparator<V> comparator) throws IllegalArgumentException
{
super(other);
@@ -68,7 +69,7 @@
this.comparator = comparator;
}
- public SetMap(Comparator comparator)
+ public SetMap(Comparator<V> comparator)
{
super();
@@ -85,42 +86,42 @@
/**
* Return the set specified by the key.
*/
- public Set get(Object key)
+ public Set<V> get(K key)
{
- return (Set)map.get(key);
+ return (Set<V>)super.get(key);
}
- protected void add(Collection c, Object o)
+ protected void add(Collection<V> c, V o)
{
c.add(o);
}
- protected void remove(Collection c, Object o)
+ protected void remove(Collection<V> c, Object o)
{
c.remove(o);
}
- protected Collection newCollection()
+ protected Collection<V> newCollection()
{
if (comparator == null)
{
- return new HashSet();
+ return new HashSet<V>();
}
else
{
- return new TreeSet(comparator);
+ return new TreeSet<V>(comparator);
}
}
- protected Collection newCollection(Collection other)
+ protected Collection<V> newCollection(Collection<V> other)
{
if (comparator == null)
{
- return new HashSet(other);
+ return new HashSet<V>(other);
}
else
{
- SortedSet set = new TreeSet(comparator);
+ SortedSet<V> set = new TreeSet<V>(comparator);
set.addAll(other);
return set;
}
Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java (rev 0)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java 2007-11-11 22:20:21 UTC (rev 8868)
@@ -0,0 +1,220 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.CollectionMap;
+import org.jboss.portal.common.util.ListMap;
+import org.jboss.portal.common.util.SetMap;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CollectionMapTestCase extends TestCase
+{
+
+ private Key k1 = new Key();
+ private Value v1 = new Value();
+ private Value v2 = new Value();
+ private ValueExt ve1 = new ValueExt();
+ private ValueExt ve2 = new ValueExt();
+
+ public void testNormal()
+ {
+ testNormal(new SetMap<Key, Value>());
+ testNormal(new ListMap<Key, Value>());
+ }
+
+ public void testRemoveAbsent()
+ {
+ testRemoveAbsent(new SetMap<Key, Value>());
+ testRemoveAbsent(new ListMap<Key, Value>());
+ }
+
+ public void testRemoveNull()
+ {
+ testRemoveNull(new SetMap<Key, Value>());
+ testRemoveNull(new ListMap<Key, Value>());
+ }
+
+ public void testWithNullValue()
+ {
+ testWithNullValue(new SetMap<Key, Value>());
+ testWithNullValue(new ListMap<Key, Value>());
+ }
+
+ public void testClassCastException()
+ {
+ testClassCastException(new SetMap<Key, Value>());
+ testClassCastException(new ListMap<Key, Value>());
+ }
+
+ public void testThrowNPE()
+ {
+ testThrowNPE(new SetMap<Key, Value>());
+ testThrowNPE(new ListMap<Key, Value>());
+ }
+
+ private void testNormal(CollectionMap<Key, Value> map)
+ {
+ assertFalse(map.contains(k1, v1));
+ assertFalse(map.contains(k1, ve1));
+ map.put(k1, v1);
+ assertTrue(map.contains(k1, v1));
+ assertFalse(map.contains(k1, ve1));
+ map.put(k1, ve1);
+ assertTrue(map.contains(k1, v1));
+ assertTrue(map.contains(k1, ve1));
+ map.remove(k1, v1);
+ assertFalse(map.contains(k1, v1));
+ assertTrue(map.contains(k1, ve1));
+ map.remove(k1, ve1);
+ assertFalse(map.contains(k1, v1));
+ assertFalse(map.contains(k1, ve1));
+ }
+
+ private void testRemoveAbsent(CollectionMap<Key, Value> map)
+ {
+ map.put(k1, v1);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ map.remove(k1, v2);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ }
+
+ private void testRemoveNull(CollectionMap<Key, Value> map)
+ {
+ map.put(k1, v1);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ map.remove(k1, null);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ }
+
+ private void testWithNullValue(CollectionMap<Key, Value> map)
+ {
+ assertFalse(map.contains(k1, null));
+ map.put(k1, null);
+ assertTrue(map.contains(k1, null));
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(null));
+ map.remove(k1, null);
+ assertFalse(map.contains(k1, null));
+ assertEquals(null, map.get(k1));
+ }
+
+ private void testClassCastException(CollectionMap<Key, Value> map)
+ {
+ CollectionMap sm2 = map;
+ sm2.put(k1, new Object());
+ Iterator<Value> i = map.iterator(k1);
+ List<Value> lst = get(i);
+ try
+ {
+ Value v = lst.get(0);
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ private void testThrowNPE(CollectionMap<Key, Value> map)
+ {
+ try
+ {
+ map.put(null, v1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.remove(null);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.remove(null, v1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.contains(null, v1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.iterator(null);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ }
+
+ private <V> List<V> get(Iterator<V> i)
+ {
+ List<V> list = new ArrayList<V>();
+ while (i.hasNext())
+ {
+ V v = i.next();
+ list.add(v);
+ }
+ return list;
+ }
+
+ private static final class Key
+ {
+ }
+
+ private static class Value
+ {
+ }
+
+ private static class ValueExt extends Value
+ {
+ }
+}
17 years
JBoss Portal SVN: r8867 - in modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2: configuration and 1 other directory.
by portal-commits@lists.jboss.org
Author: bdaw
Date: 2007-11-11 16:19:01 -0500 (Sun, 11 Nov 2007)
New Revision: 8867
Modified:
modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/TestSuiteExecutor.java
modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/configuration/Testsuite.java
Log:
maven plugin jpda support
Modified: modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/TestSuiteExecutor.java
===================================================================
--- modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/TestSuiteExecutor.java 2007-11-11 20:40:50 UTC (rev 8866)
+++ modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/TestSuiteExecutor.java 2007-11-11 21:19:01 UTC (rev 8867)
@@ -188,6 +188,8 @@
javaBuilder.setHtmlReportDir(reports.getHtml());
}
+ javaBuilder.setJpda(testsuite.isJpda());
+
for (CasesDescription include : testsuite.getIncludes())
{
//Check attributes consistency
Modified: modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/configuration/Testsuite.java
===================================================================
--- modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/configuration/Testsuite.java 2007-11-11 20:40:50 UTC (rev 8866)
+++ modules/test/trunk/tooling/maven2/src/main/java/org/jboss/unit/tooling/maven2/configuration/Testsuite.java 2007-11-11 21:19:01 UTC (rev 8867)
@@ -56,7 +56,12 @@
*/
private Parameter[] parameters = new Parameter[0];
+ /**
+ * @parameter
+ */
+ boolean jpda = false;
+
public String getConfig()
{
@@ -87,4 +92,9 @@
{
return parameters;
}
+
+ public boolean isJpda()
+ {
+ return jpda;
+ }
}
17 years