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());
+ }
+}