Author: chris.laprun(a)jboss.com
Date: 2009-01-14 09:14:31 -0500 (Wed, 14 Jan 2009)
New Revision: 12499
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/ParameterValidationTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java
Log:
- Added sanitize method in ParameterValidation and related test case.
- Added CSS distance validation pattern for use in sanitize.
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java 2009-01-14
14:13:21 UTC (rev 12498)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java 2009-01-14
14:14:31 UTC (rev 12499)
@@ -22,6 +22,8 @@
******************************************************************************/
package org.jboss.portal.common.util;
+import java.util.regex.Pattern;
+
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision: 5757 $
@@ -29,6 +31,8 @@
*/
public class ParameterValidation
{
+ public final static Pattern CSS_DISTANCE =
Pattern.compile("\\d+\\W*(em|ex|px|in|cm|mm|pt|pc|%)?");
+
public static void throwIllegalArgExceptionIfNullOrEmpty(String valueToCheck, String
valueName, String contextName)
{
if (isNullOrEmpty(valueToCheck))
@@ -81,4 +85,27 @@
throw new IllegalArgumentException("Must pass a non-empty " + name);
}
}
+
+ /**
+ * Checks if a given value matches the given format (as a regular expression). If yes,
returns it as is. Otherwise,
+ * returns the default value.
+ *
+ * @param value value to sanitize if needed, <code>null</code> is
considered as not matching
+ * @param regex format the value needs to conform to
+ * @param defaultValue default value to use if the specified value does not conform to
the specified format
+ * @return the specified value if it conforms to the expected format, the given
default value otherwise.
+ */
+ public static String sanitize(String value, Pattern regex, String defaultValue)
+ {
+ throwIllegalArgExceptionIfNull(regex, "expected value format");
+
+ if (value == null || !regex.matcher(value).matches())
+ {
+ return defaultValue;
+ }
+ else
+ {
+ return value;
+ }
+ }
}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/ParameterValidationTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/ParameterValidationTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/ParameterValidationTestCase.java 2009-01-14
14:14:31 UTC (rev 12499)
@@ -0,0 +1,83 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2009, 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.ParameterValidation;
+
+import java.util.regex.Pattern;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class ParameterValidationTestCase extends TestCase
+{
+ public void testNullPattern()
+ {
+ try
+ {
+ ParameterValidation.sanitize("foo", null, null);
+ fail("Should have thrown an IAE on null Pattern");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+ }
+
+ public void testNullValue()
+ {
+ String defaultValue = "default";
+ assertEquals(defaultValue, ParameterValidation.sanitize(null,
Pattern.compile(""), defaultValue));
+ }
+
+ public void testNullDefault()
+ {
+ assertNull(ParameterValidation.sanitize(null, Pattern.compile(""),
null));
+ }
+
+ public void testCSSDistance()
+ {
+ String defaultValue = "300px";
+ assertEquals(defaultValue,
+
ParameterValidation.sanitize("0%20;%20background-image:%20url(http:/...;,
+ ParameterValidation.CSS_DISTANCE, defaultValue));
+
+ assertEquals(defaultValue, ParameterValidation.sanitize(defaultValue,
ParameterValidation.CSS_DISTANCE, null));
+
+ String value = "0";
+ assertEquals(value, ParameterValidation.sanitize(value,
ParameterValidation.CSS_DISTANCE, null));
+
+ value = "10%";
+ assertEquals(value, ParameterValidation.sanitize(value,
ParameterValidation.CSS_DISTANCE, null));
+
+ value = "10 %";
+ assertEquals(value, ParameterValidation.sanitize(value,
ParameterValidation.CSS_DISTANCE, null));
+
+ value = "100 \n\tin";
+ assertEquals(value, ParameterValidation.sanitize(value,
ParameterValidation.CSS_DISTANCE, null));
+
+ assertEquals(defaultValue, ParameterValidation.sanitize("",
ParameterValidation.CSS_DISTANCE, defaultValue));
+ }
+}