[jboss-svn-commits] JBoss Portal SVN: r5204 - in trunk: common/src/main/org/jboss/portal/common/util portlet portlet/src/main/org/jboss/portal/portlet portlet/src/main/org/jboss/portal/test/portlet
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Sep 14 18:57:25 EDT 2006
Author: julien at jboss.com
Date: 2006-09-14 18:57:17 -0400 (Thu, 14 Sep 2006)
New Revision: 5204
Modified:
trunk/common/src/main/org/jboss/portal/common/util/ParameterMap.java
trunk/common/src/main/org/jboss/portal/common/util/TypedMap.java
trunk/portlet/build.xml
trunk/portlet/src/main/org/jboss/portal/portlet/Parameters.java
trunk/portlet/src/main/org/jboss/portal/test/portlet/ParametersTestCase.java
Log:
- simplify code in Parameters in order to reuse code offered by the ParameterMap parent class
- added test case for Parameters.append(Map other)
Modified: trunk/common/src/main/org/jboss/portal/common/util/ParameterMap.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/ParameterMap.java 2006-09-14 20:37:59 UTC (rev 5203)
+++ trunk/common/src/main/org/jboss/portal/common/util/ParameterMap.java 2006-09-14 22:57:17 UTC (rev 5204)
@@ -32,16 +32,24 @@
protected void assertKeyValidity(Object value)
{
- if (!(value instanceof String))
+ if (value == null)
{
+ throw new IllegalArgumentException("Key must not be null");
+ }
+ if (value instanceof String == false)
+ {
throw new IllegalArgumentException("Key should be a String");
}
}
protected Object getInternalValue(Object value)
{
- if (!(value instanceof String[]))
+ if (value == null)
{
+ throw new IllegalArgumentException("Value must not be null");
+ }
+ if (value instanceof String[] == false)
+ {
throw new IllegalArgumentException("Value should be a String[]");
}
String[] strings = (String[])value;
Modified: trunk/common/src/main/org/jboss/portal/common/util/TypedMap.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/TypedMap.java 2006-09-14 20:37:59 UTC (rev 5203)
+++ trunk/common/src/main/org/jboss/portal/common/util/TypedMap.java 2006-09-14 22:57:17 UTC (rev 5204)
@@ -114,6 +114,7 @@
public Object get(Object key)
{
+ assertKeyValidity(key);
Object value = getDelegate().get(key);
return getExternalValue(value);
}
Modified: trunk/portlet/build.xml
===================================================================
--- trunk/portlet/build.xml 2006-09-14 20:37:59 UTC (rev 5203)
+++ trunk/portlet/build.xml 2006-09-14 22:57:17 UTC (rev 5204)
@@ -463,6 +463,8 @@
<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.portletmode.PortletModeTestSuite"/>
<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.portletconfig.PortletConfigTestSuite"/>
<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.dispatcher.DispatcherTestSuite"/>
+ <!--Misc Tests-->
+ <test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.misc.log4j.Log4jTestSuite"/>
<!--Test Framework test-->
<!--<test todir="${test.reports}" name="org.jboss.portal.test.framework.basic.BasicTestCase"/>-->
</x-test>
@@ -566,7 +568,7 @@
<!--<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.dispatcher.DispatcherTestSuite"/>-->
<!--<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.portletrequests.PortletRequestTestSuite"/>-->
- <test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.misc.log4j.Log4jTestSuite"/>
+ <test todir="${test.reports}" name="org.jboss.portal.test.portlet.ParametersTestCase"/>
</x-test>
<x-classpath>
Modified: trunk/portlet/src/main/org/jboss/portal/portlet/Parameters.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/Parameters.java 2006-09-14 20:37:59 UTC (rev 5203)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/Parameters.java 2006-09-14 22:57:17 UTC (rev 5204)
@@ -62,6 +62,8 @@
{
throw new IllegalArgumentException();
}
+
+ //
this.map = new HashMap(parameters.map);
}
@@ -73,6 +75,8 @@
public Parameters(Map parameterMap)
{
this.map = new HashMap();
+
+ //
replace(parameterMap);
}
@@ -90,11 +94,7 @@
*/
public String getValue(String name) throws IllegalArgumentException
{
- if (name == null)
- {
- throw new IllegalArgumentException("name cannot be null");
- }
- String[] value = (String[])map.get(name);
+ String[] value = (String[])get(name);
return value == null ? null : value[0];
}
@@ -107,11 +107,7 @@
*/
public String[] getValues(String name) throws IllegalArgumentException
{
- if (name == null)
- {
- throw new IllegalArgumentException("name cannot be null");
- }
- return (String[])map.get(name);
+ return (String[])get(name);
}
/**
@@ -124,18 +120,7 @@
*/
public void setValue(String name, String value)
{
- if (name == null)
- {
- throw new IllegalArgumentException("name must not be null");
- }
- if (value == null)
- {
- throw new IllegalArgumentException("value must not be null");
- }
- else
- {
- map.put(name, new String[]{value});
- }
+ put(name, new String[]{value});
}
/**
@@ -147,56 +132,33 @@
*/
public void setValues(String name, String[] values)
{
- if (name == null)
- {
- throw new IllegalArgumentException("name must not be null");
- }
- getInternalValue(values);
- map.put(name, values);
+ put(name, values);
}
/**
- * Remove a parameter.
- *
- * @throws IllegalArgumentException if the name is null
+ * @param params the parameters to appends
+ * @throws IllegalArgumentException if the params argument is not valid
*/
- public void remove(String name)
+ public void append(Map params) throws IllegalArgumentException
{
- if (name == null)
- {
- throw new IllegalArgumentException("name must not be null");
- }
- if (map != null)
- {
- map.remove(name);
- }
+ checkValidity(params);
+
+ //
+ internalAppend(params);
}
/**
- * Append the parameters to this object. All the entries will be inserted
- * in the current map. If a collision occurs then the new values will be
- * appended to the existing ones.
+ * Replace all the exisint parameters with the new ones.
*
- * @param params
- * @throws IllegalArgumentException if the params is null
+ * @throws IllegalArgumentException if the map is not valid
*/
- public void append(Parameters params) throws IllegalArgumentException
+ public void replace(Map params)
{
- if (params == null)
- {
- throw new IllegalArgumentException("Parameters to merge must be null");
- }
- internalAppend(params.map);
- }
+ checkValidity(params);
- /**
- * @param params the parameters to appends
- * @throws IllegalArgumentException if the params argument is not valid
- */
- public void append(Map params) throws IllegalArgumentException
- {
- assertMap(params);
- internalAppend(params);
+ //
+ clear();
+ putAll(params);
}
/**
@@ -230,46 +192,6 @@
}
/**
- * Replace all the exisint parameters with the new ones.
- *
- * @throws IllegalArgumentException if the map is not valid
- */
- public void replace(Map params)
- {
- assertMap(params);
- internalReplace(params);
- }
-
- /**
- * Replace all the parameters with the new ones.
- *
- * @throws IllegalArgumentException if the parameters is not valid
- */
- public void replace(Parameters params)
- {
- if (params == null)
- {
- throw new IllegalArgumentException("parameters must not be null");
- }
- internalReplace(params.map);
- }
-
- /**
- * Replace actual implementation.
- */
- private void internalReplace(Map params)
- {
- map.clear();
- for (Iterator i = params.entrySet().iterator(); i.hasNext();)
- {
- Map.Entry entry = (Entry)i.next();
- String name = (String)entry.getKey();
- String values[] = (String[])entry.getValue();
- map.put(name, values.clone());
- }
- }
-
- /**
* Compare to parameters objects.
*/
public boolean equals(Object o)
@@ -337,7 +259,7 @@
return buffer.toString();
}
- protected void assertMap(Map that) throws IllegalArgumentException
+ protected void checkValidity(Map that) throws IllegalArgumentException
{
if (that == null)
{
Modified: trunk/portlet/src/main/org/jboss/portal/test/portlet/ParametersTestCase.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/test/portlet/ParametersTestCase.java 2006-09-14 20:37:59 UTC (rev 5203)
+++ trunk/portlet/src/main/org/jboss/portal/test/portlet/ParametersTestCase.java 2006-09-14 22:57:17 UTC (rev 5204)
@@ -26,6 +26,7 @@
import java.util.Map;
import org.jboss.portal.test.AbstractTestCase;
+import org.jboss.portal.test.framework.junit.ExtendedAssert;
import org.jboss.portal.portlet.Parameters;
/**
@@ -231,46 +232,19 @@
public void testReplaceWithInvalidMap()
{
- try
+ Map[] maps = buildInvalidMaps();
+ for (int i = 0; i < maps.length; i++)
{
- Map map = new HashMap();
- map.put("a", null);
- param.replace(map);
- fail("Expected IllegalArgumentException");
+ try
+ {
+ Map map = maps[i];
+ param.replace(map);
+ fail("Expected IllegalArgumentException with map=" + map);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
}
- catch (IllegalArgumentException e)
- {
- }
- try
- {
- Map map = new HashMap();
- map.put("a", new String[0]);
- param.replace(map);
- fail("Expected IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- }
- try
- {
- Map map = new HashMap();
- map.put("a", new String[] { null });
- param.replace(map);
- fail("Expected IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- }
- try
- {
- Map map = new HashMap();
- map.put("a", new Object());
- param.replace(map);
- fail("Expected IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- }
}
public void testReplace()
@@ -281,18 +255,65 @@
Map map = new HashMap();
map.put("a", new String[]{"_b"});
map.put("c", new String[]{"_d","_e"});
- map.put("_f", new String[]{"_g"});
+ map.put("h", new String[]{"_i"});
param.replace(map);
- assertTrue(Arrays.equals(param.getValues("a"), new String[] {"_b"}));
- assertTrue(Arrays.equals(param.getValues("c"), new String[] {"_d","_e"}));
- assertTrue(Arrays.equals(param.getValues("_f"), new String[] {"_g"}));
- assertEquals(null, map.get("g"));
+ assertEquals(3, param.size());
+ ExtendedAssert.assertEquals(param.getValues("a"), new String[] {"_b"});
+ ExtendedAssert.assertEquals(param.getValues("c"), new String[] {"_d","_e"});
+ ExtendedAssert.assertEquals(param.getValues("h"), new String[] {"_i"});
}
+ public void testAppendWithInvalidMap()
+ {
+ Map[] maps = buildInvalidMaps();
+ for (int i = 0; i < maps.length; i++)
+ {
+ try
+ {
+ Map map = maps[i];
+ param.append(map);
+ fail("Expected IllegalArgumentException with map=" + map);
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+ }
+
+ public void testAppend()
+ {
+ param.setValue("a", "b");
+ param.setValues("c", new String[]{"d","e"});
+ param.setValue("f", "g");
+ Map map = new HashMap();
+ map.put("a", new String[]{"_b"});
+ map.put("c", new String[]{"_d","_e"});
+ map.put("h", new String[]{"_i"});
+ param.append(map);
+ assertEquals(4, param.size());
+ ExtendedAssert.assertEquals(param.getValues("a"), new String[] {"b","_b"});
+ ExtendedAssert.assertEquals(param.getValues("c"), new String[] {"d","e","_d","_e"});
+ ExtendedAssert.assertEquals(param.getValues("f"), new String[] {"g"});
+ ExtendedAssert.assertEquals(param.getValues("h"), new String[] {"_i"});
+ }
+
public void testClear()
{
param.setValue("a", "b");
param.clear();
assertNull(param.getValue("a"));
}
+
+ public Map[] buildInvalidMaps()
+ {
+ Map map1 = new HashMap();
+ map1.put("a", null);
+ Map map2 = new HashMap();
+ map2.put("a", new String[0]);
+ Map map3 = new HashMap();
+ map3.put("a", new String[] { null });
+ Map map4 = new HashMap();
+ map4.put("a", new Object());
+ return new Map[]{map1,map2,map3,map4};
+ }
}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list