Author: haint
Date: 2011-10-18 04:33:42 -0400 (Tue, 18 Oct 2011)
New Revision: 7764
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java
Log:
GTNPORTAL-2186 New UsernameValidator, NaturalLanguageValidator and cleanup, improve, unit
test validatos
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/DateTimeValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -40,14 +40,13 @@
public class DateTimeValidator implements Validator
{
- static private final String SPLIT_REGEX = "/|\\s+|:";
-
public void validate(UIFormInput uiInput) throws Exception
{
if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
+ {
return;
+ }
String s = (String)uiInput.getValue();
- DateFormat stFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
UIFormDateTimeInput uiDateInput = (UIFormDateTimeInput)uiInput;
SimpleDateFormat sdf = new SimpleDateFormat(uiDateInput.getDatePattern_().trim());
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/EmailAddressValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -33,13 +33,21 @@
* Jun 7, 2006
*
* Validates whether an email is in the correct format
+ * Valid characters that can be used in a domain name are:
+ * a-z
+ * 0-9
+ * - (dash) or . (dot) but not as a starting or ending character
+ * . (dot) as a separator for the textual portions of a domain name
+ *
+ * Valid characters that can be used in a domain name are:
+ * a-z
+ * 0-9
+ * _ (underscore) or . (dot) but not as a starting or ending character
*/
@Serialized
public class EmailAddressValidator implements Validator
{
- static private final String EMAIL_REGEX =
"[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*(a)[_A-Za-z0-9-.]+";
-
public void validate(UIFormInput uiInput) throws Exception
{
// modified by Pham Dinh Tan
@@ -48,19 +56,118 @@
String label;
try
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ label = uiForm.getId() + ".label." + uiInput.getName();
}
catch (Exception e)
{
label = uiInput.getName();
}
+ Object[] args = {label};
+
if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
+ {
return;
+ }
+
String s = (String)uiInput.getValue();
- if (s.matches(EMAIL_REGEX))
- return;
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("EmailAddressValidator.msg.Invalid-input", args,
- ApplicationMessage.WARNING));
+ int atIndex = s.indexOf('@');
+ if (atIndex == -1)
+ {
+ throw new MessageException(new
ApplicationMessage("EmailAddressValidator.msg.Invalid-input", args,
+ ApplicationMessage.WARNING));
+ }
+
+ String localPart = s.substring(0, atIndex);
+ String domainName = s.substring(atIndex + 1);
+
+ if (!validateLocalPart(localPart.toCharArray()) ||
!validateDomainName(domainName.toCharArray()))
+ {
+ throw new MessageException(new
ApplicationMessage("EmailAddressValidator.msg.Invalid-input", args,
+ ApplicationMessage.WARNING));
+ }
}
+
+ private boolean validateLocalPart(char[] localPart)
+ {
+ if(!isAlphabet(localPart[0]) || !isAlphabetOrDigit(localPart[localPart.length
-1]))
+ {
+ return false;
+ }
+
+ for(int i = 1; i < localPart.length -1; i++)
+ {
+ char c = localPart[i];
+ char next = localPart[i+1];
+
+ if(isAlphabetOrDigit(c) || (isLocalPartSymbol(c) &&
isAlphabetOrDigit(next)))
+ {
+ continue;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean validateDomainName(char[] domainName)
+ {
+ if(!isAlphabet(domainName[0]) || !isAlphabetOrDigit(domainName[domainName.length
-1]))
+ {
+ return false;
+ }
+
+ //Check if there is no non-alphabet following the last dot
+ boolean foundValidLastDot = false;
+ for(int i = 1; i < domainName.length -1; i++)
+ {
+ char c = domainName[i];
+ char next = domainName[i+1];
+
+ if(c == '.')
+ {
+ foundValidLastDot = true;
+ }
+ else if(!isAlphabet(c))
+ {
+ foundValidLastDot = false;
+ }
+
+ if(isAlphabetOrDigit(c) || (isDomainNameSymbol(c) &&
isAlphabetOrDigit(next)))
+ {
+ continue;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ return foundValidLastDot;
+ }
+
+ private boolean isAlphabet(char c)
+ {
+ return c >= 'a' && c <= 'z';
+ }
+
+ private boolean isDigit(char c)
+ {
+ return c >= '0' && c <= '9';
+ }
+
+ private boolean isAlphabetOrDigit(char c)
+ {
+ return isAlphabet(c) || isDigit(c);
+ }
+
+ private boolean isLocalPartSymbol(char c)
+ {
+ return c == '_' || c == '.';
+ }
+
+ private boolean isDomainNameSymbol(char c)
+ {
+ return c == '-' || c == '.';
+ }
}
\ No newline at end of file
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/ExpressionValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -19,6 +19,8 @@
package org.exoplatform.webui.form.validator;
+import java.util.regex.Pattern;
+
import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
import org.exoplatform.webui.core.UIComponent;
@@ -37,7 +39,7 @@
@Serialized
public class ExpressionValidator implements Validator
{
- private String expression_;
+ private Pattern pattern;
private String key_;
@@ -46,15 +48,15 @@
{
}
- public ExpressionValidator(final String expression)
+ public ExpressionValidator(final String regex)
{
- expression_ = expression;
+ pattern = Pattern.compile(regex);
key_ = "ExpressionValidator.msg.value-invalid";
}
- public ExpressionValidator(final String exp, final String key)
+ public ExpressionValidator(final String regex, final String key)
{
- expression_ = exp;
+ pattern = Pattern.compile(regex);
key_ = key;
}
@@ -66,7 +68,7 @@
}
String value = ((String)uiInput.getValue()).trim();
- if (value.matches(expression_))
+ if (pattern.matcher(value).find())
{
return;
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/IdentifierValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -33,7 +33,8 @@
* minhdv81(a)yahoo.com
* Jun 7, 2006
*
- * Validates whether the value is composed of letters, numbers or '_'
+ * Validates whether the value is composed of letters, digit , '_' or
'-'h
+ * First character could not be digit or '-'
*/
public class IdentifierValidator implements Validator, Serializable
{
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NaturalLanguageValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -0,0 +1,68 @@
+/**
+ * Copyright (C) 2003-2011 eXo Platform SAS.
+ *
+ * This program 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 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.webui.form.validator;
+
+import org.exoplatform.commons.serialization.api.annotations.Serialized;
+import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.webui.core.UIComponent;
+import org.exoplatform.webui.exception.MessageException;
+import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.webui.form.UIFormInput;
+
+/**
+ * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
+ *
+ * @datOct 11, 2011
+ *
+ * Validates whether this value is composed of letters or spaces
+ */
+@Serialized
+public class NaturalLanguageValidator implements Validator
+{
+
+ public void validate(UIFormInput uiInput) throws Exception
+ {
+ UIComponent uiComponent = (UIComponent)uiInput;
+ UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
+ String label;
+ try
+ {
+ label = uiForm.getId() + ".label." + uiInput.getName();
+ }
+ catch (Exception e)
+ {
+ label = uiInput.getName();
+ }
+ Object[] args = {label};
+
+ if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
+ {
+ return;
+ }
+
+ String s = (String)uiInput.getValue();
+ for (int i = 0; i < s.length(); i++)
+ {
+ char c = s.charAt(i);
+ if (Character.isLetter(c) || Character.isSpaceChar(c))
+ {
+ continue;
+ }
+ throw new MessageException(new
ApplicationMessage("NaturalLanguageValidator.msg.Invalid-char", args));
+ }
+ }
+}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/NumberFormatValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -39,31 +39,41 @@
public void validate(UIFormInput uiInput) throws Exception
{
if (uiInput.getValue() == null || ((String)uiInput.getValue()).length() == 0)
+ {
return;
+ }
+
// modified by Pham Dinh Tan
UIComponent uiComponent = (UIComponent)uiInput;
UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
String label;
try
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ label = uiForm.getId() + ".label." + uiInput.getName();
}
catch (Exception e)
{
- label = uiInput.getName();
+ label = uiInput.getName().trim();
}
- label = label.trim();
+ Object[] args = {label};
+
String s = (String)uiInput.getValue();
- for (int i = 0; i < s.length(); i++)
+ if(s.charAt(0) == '0' && s.length() > 1)
{
- char c = s.charAt(i);
- if (Character.isDigit(c) || (s.charAt(0) == '-' && i == 0
&& s.length() > 1))
- {
- continue;
- }
- Object[] args = {label};
throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
}
+ else if(s.charAt(0) == '-' && s.length() > 1 &&
s.charAt(1) == '0')
+ {
+ throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
+ }
+ try
+ {
+ Integer.parseInt(s);
+ }
+ catch(NumberFormatException e)
+ {
+ throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
+ }
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/PositiveNumberFormatValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -40,37 +40,48 @@
public void validate(UIFormInput uiInput) throws Exception
{
if (uiInput.getValue() == null || ((String)uiInput.getValue()).length() == 0)
+ {
return;
+ }
// modified by Pham Dinh Tan
UIComponent uiComponent = (UIComponent)uiInput;
UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
String label;
try
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ label = uiForm.getId() + ".label." + uiInput.getName();
}
catch (Exception e)
{
label = uiInput.getName();
}
+ Object[] args = {label, uiInput.getBindingField()};
+
String s = (String)uiInput.getValue();
- boolean error = false;
- for (int i = 0; i < s.length(); i++)
+
+ if(s.charAt(0) == '0' && s.length() > 1)
{
- char c = s.charAt(i);
- if (Character.isDigit(c) || (s.charAt(0) == '-' && i == 0))
- {
- error = true;
- continue;
- }
- error = false;
- Object[] args = {label, uiInput.getBindingField()};
throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
}
- if (error == true && s.charAt(0) == '-')
+ else if(s.charAt(0) == '-' && s.length() > 1 &&
s.charAt(1) == '0')
{
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("PositiveNumberFormatValidator.msg.Invalid-number", args));
+ throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
}
+
+ int value;
+ try
+ {
+ value = Integer.parseInt(s);
+ }
+ catch(NumberFormatException e)
+ {
+ throw new MessageException(new
ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args));
+ }
+
+ if(value >= 0)
+ {
+ return;
+ }
+ throw new MessageException(new
ApplicationMessage("PositiveNumberFormatValidator.msg.Invalid-number", args));
}
}
\ No newline at end of file
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java 2011-10-18
06:43:08 UTC (rev 7763)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/URLValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -35,15 +35,12 @@
*/
public class URLValidator implements Validator, Serializable
{
-
+
static private final String IP_REGEX =
-
"(((25[0-5])|(2[0-4][0-9])|([01]?[0-9]?[0-9]))\\.){3}((25[0-5])|(2[0-4][0-9])|([01]?[0-9]?[0-9]))";
-
- // static public final String URL_REGEX =
- // "^(ht|f)tp(s?)://(\\w+:\\w+@)?(("+ IP_REGEX
+")|((www.)?(\\S+\\.){1,2}(\\w{2,5}))|([a-zA-Z][-a-zA-Z0-9]+))" +
- // "(:\\d{1,5})?($|((/[-+%.\\w\\d
]+)*/?((\\.\\w+)?($|\\?([-.:+\\w\\d%/]+=[-.:+\\w\\d%/]+)(&[-.:+\\w\\d%/]+=[-.:+\\w\\d%/]+)*))?))"
;
+
"(((((25[0-5])|(2[0-4][0-9])|([01]?[0-9]?[0-9]))\\.){3}((25[0-4])|(2[0-4][0-9])|((1?[1-9]?[1-9])|([1-9]0))))|(0\\.){3}0)";
+
static public final String URL_REGEX = "^((ht|f)tp(s?)://)" //protocol
- + "(\\w+:\\w+@)?" //username:password@
+ + "(\\w+(:\\w+)?@)?" //username:password@
+ "(" + IP_REGEX //ip
+
"|([0-9a-z_!~*'()-]+\\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.[a-z]{2,6}"
//domain like
www.exoplatform.org
+ "|([a-zA-Z][-a-zA-Z0-9]+))" // domain like localhost
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -0,0 +1,140 @@
+/**
+ * Copyright (C) 2003-2011 eXo Platform SAS.
+ *
+ * This program 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 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.webui.form.validator;
+
+import org.exoplatform.commons.serialization.api.annotations.Serialized;
+import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.webui.core.UIComponent;
+import org.exoplatform.webui.exception.MessageException;
+import org.exoplatform.webui.form.UIForm;
+import org.exoplatform.webui.form.UIFormInput;
+
+/**
+ * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
+ *
+ * @datSep 28, 2011
+ *
+ * Validate username whether the value is only alpha lower, digit, dot and underscore
with first, last character is alpha lower or digit
+ * and cannot contain consecutive underscore, dot or both.
+ */
+
+@Serialized
+public class UsernameValidator implements Validator
+{
+ private Integer min = 3;
+ private Integer max = 30;
+
+ public UsernameValidator(Integer min, Integer max)
+ {
+ this.min = min;
+ this.max = max;
+ }
+
+ @Override
+ public void validate(UIFormInput uiInput) throws Exception
+ {
+ if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
+ {
+ return;
+ }
+ UIComponent uiComponent = (UIComponent)uiInput;
+ UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
+ String label;
+ try
+ {
+ label = uiForm.getId() + ".label." + uiInput.getName();
+ }
+ catch (Exception e)
+ {
+ label = uiInput.getName();
+ }
+
+ char[] buff = ((String)uiInput.getValue()).toCharArray();
+ if(buff.length < min || buff.length > max)
+ {
+ Object[] args = {label, min.toString(), max.toString()};
+ throw new MessageException(new
ApplicationMessage("StringLengthValidator.msg.length-invalid", args,
+ ApplicationMessage.WARNING));
+ }
+
+ if(!isAlphabet(buff[0]))
+ {
+ Object[] args = {label};
+ throw new MessageException(new
ApplicationMessage("FirstCharacterNameValidator.msg", args,
+ ApplicationMessage.WARNING));
+ }
+
+ if(!isAlphabetOrDigit(buff[buff.length - 1]))
+ {
+ Object[] args = {label, buff[buff.length - 1]};
+ throw new MessageException(new
ApplicationMessage("LastCharacterUsernameValidator.msg", args,
+ ApplicationMessage.WARNING));
+ }
+
+ for(int i = 1; i < buff.length -1; i++)
+ {
+ char c = buff[i];
+
+ if (isAlphabetOrDigit(c))
+ {
+ continue;
+ }
+
+ if (isSymbol(c))
+ {
+ char next = buff[i + 1];
+ if (isSymbol(next))
+ {
+ Object[] args = {label, buff[i], buff[i + 1]};
+ throw new MessageException(new
ApplicationMessage("ConsecutiveSymbolValidator.msg", args,
+ ApplicationMessage.WARNING));
+ }
+ else if (!isAlphabetOrDigit(next))
+ {
+ Object[] args = {label};
+ throw new MessageException(new
ApplicationMessage("UsernameValidator.msg.Invalid-char", args,
ApplicationMessage.WARNING));
+ }
+ }
+ else
+ {
+ Object[] args = {label};
+ throw new MessageException(new
ApplicationMessage("UsernameValidator.msg.Invalid-char", args,
ApplicationMessage.WARNING));
+ }
+ }
+ }
+
+ private boolean isAlphabet(char c)
+ {
+ return c >= 'a' && c <= 'z';
+ }
+
+ private boolean isDigit(char c)
+ {
+ return c >= '0' && c <= '9';
+ }
+
+ private boolean isSymbol(char c)
+ {
+ return c == '_' || c == '.';
+ }
+
+ private boolean isAlphabetOrDigit(char c)
+ {
+ return isAlphabet(c) || isDigit(c);
+ }
+
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -0,0 +1,127 @@
+/**
+ * Copyright (C) 2003-2011 eXo Platform SAS.
+ *
+ * This program 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 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.webui.test.validator;
+
+import java.util.Locale;
+
+import org.exoplatform.portal.mop.user.UserPortal;
+import org.exoplatform.services.resources.Orientation;
+import org.exoplatform.web.application.URLBuilder;
+import org.exoplatform.web.url.PortalURL;
+import org.exoplatform.web.url.ResourceType;
+import org.exoplatform.web.url.URLFactory;
+import org.exoplatform.webui.application.WebuiRequestContext;
+import org.exoplatform.webui.core.UIComponent;
+
+/**
+ * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
+ *
+ * @datSep 28, 2011
+ */
+public class MockRequestContext extends WebuiRequestContext
+{
+ private Locale locale;
+
+ public MockRequestContext(Locale locale)
+ {
+ super(null);
+ this.locale = locale;
+ }
+
+ public Locale getLocale()
+ {
+ return locale;
+ }
+
+ @Override
+ public URLBuilder<UIComponent> getURLBuilder()
+ {
+ return null;
+ }
+
+ @Override
+ public String getRequestContextPath()
+ {
+ return null;
+ }
+
+ @Override
+ public String getPortalContextPath()
+ {
+ return null;
+ }
+
+ @Override
+ public <T> T getRequest() throws Exception
+ {
+ return null;
+ }
+
+ @Override
+ public <T> T getResponse() throws Exception
+ {
+ return null;
+ }
+
+ @Override
+ public void sendRedirect(String url) throws Exception
+ {
+
+ }
+
+ @Override
+ public URLFactory getURLFactory()
+ {
+ return null;
+ }
+
+ @Override
+ public <R, U extends PortalURL<R, U>> U newURL(ResourceType<R, U>
resourceType, URLFactory urlFactory)
+ {
+ return null;
+ }
+
+ @Override
+ public Orientation getOrientation()
+ {
+ return null;
+ }
+
+ @Override
+ public String getRequestParameter(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public String[] getRequestParameterValues(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public boolean useAjax()
+ {
+ return false;
+ }
+
+ @Override
+ public UserPortal getUserPortal()
+ {
+ return null;
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java 2011-10-18
08:33:42 UTC (rev 7764)
@@ -0,0 +1,262 @@
+/**
+ * Copyright (C) 2003-2011 eXo Platform SAS.
+ *
+ * This program 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 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.webui.test.validator;
+
+import java.util.List;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+import org.exoplatform.webui.application.WebuiRequestContext;
+import org.exoplatform.webui.core.UIComponent;
+import org.exoplatform.webui.exception.MessageException;
+import org.exoplatform.webui.form.UIFormDateTimeInput;
+import org.exoplatform.webui.form.UIFormInput;
+import org.exoplatform.webui.form.validator.DateTimeValidator;
+import org.exoplatform.webui.form.validator.EmailAddressValidator;
+import org.exoplatform.webui.form.validator.IdentifierValidator;
+import org.exoplatform.webui.form.validator.NameValidator;
+import org.exoplatform.webui.form.validator.NumberFormatValidator;
+import org.exoplatform.webui.form.validator.PositiveNumberFormatValidator;
+import org.exoplatform.webui.form.validator.ResourceValidator;
+import org.exoplatform.webui.form.validator.SpecialCharacterValidator;
+import org.exoplatform.webui.form.validator.URLValidator;
+import org.exoplatform.webui.form.validator.UsernameValidator;
+import org.exoplatform.webui.form.validator.Validator;
+
+/**
+ * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh
Hai</a>
+ *
+ * @datSep 26, 2011
+ */
+public class TestWebuiValidator extends TestCase
+{
+ public void testUrlValidator()
+ {
+ Validator validator = new URLValidator();
+ // Test ip address and invalidate subnet masks ip
+ assertTrue(expected(validator, "https://192.168.1.1"));
+ assertTrue(expected(validator, "ftp://255.255.255.1"));
+ assertTrue(expected(validator, "ftps://255.255.0.1"));
+ assertTrue(expected(validator, "ftp://0.0.0.0"));
+ assertTrue(expected(validator, "http://127.0.0.1"));
+ assertTrue(expected(validator, "https://192.168.4.90"));
+ assertTrue(expected(validator, "https://192.168.4.90:8080"));
+ assertTrue(expected(validator, "http://127.0.0.1:8080"));
+ assertFalse(expected(validator, "http://127.0.0.01"));
+ assertFalse(expected(validator, "ftp://255.255.255.255"));
+
+ // Test domain name and uri
+ assertTrue(expected(validator, "https://www.exoplatform.com"));
+ assertTrue(expected(validator, "ftps://root:gtn@exoplatform.com"));
+ assertTrue(expected(validator, "ftps://root@exoplatform.com"));
+ assertTrue(expected(validator, "https://www.dev.exoplatform.com"));
+ assertTrue(expected(validator, "https://www.dev.exoplatform.com:8888"));
+ assertFalse(expected(validator,
"https://www.dev.exoplatform.com:8888?arg=value"));
+ assertTrue(expected(validator,
"https://www.dev.exoplatform.com:8888/path?arg=value"));
+ assertTrue(expected(validator,
"https://www.dev.exoplatform.com:8888/path?arg=value#"));
+ }
+
+ public void testDateTimeValidator()
+ {
+ Validator validator = new DateTimeValidator();
+ WebuiRequestContext.setCurrentInstance(new MockRequestContext(new
Locale("fr")));
+ UIFormDateTimeInput uiInput = new UIFormDateTimeInput("currentDate",
"currentDate", null);
+ uiInput.setValue("28/09/2011 10:59:59");
+ assertTrue(expected(validator, uiInput));
+ uiInput.setValue("09/28/2011 10:59:59");
+ assertFalse(expected(validator, uiInput));
+
+ WebuiRequestContext.setCurrentInstance(new MockRequestContext(new
Locale("en")));
+ uiInput = new UIFormDateTimeInput("currentDate", "currentDate",
null);
+ uiInput.setValue("09/28/2011 10:59:59");
+ assertTrue(expected(validator, uiInput));
+ uiInput.setValue("09-28-2011 10:59:59");
+ assertFalse(expected(validator, uiInput));
+ uiInput.setValue("28/09/2011 10:59:59");
+ assertFalse(expected(validator, uiInput));
+ }
+
+ public void testUsernameValidator()
+ {
+ Validator validator = new UsernameValidator(3, 30);
+ assertTrue(expected(validator, "root.gtn"));
+ assertTrue(expected(validator, "root_gtn"));
+ assertTrue(expected(validator, "root_gtn.01"));
+ assertFalse(expected(validator, "root_gtn_"));
+ assertFalse(expected(validator, "_root_gtn"));
+ assertFalse(expected(validator, "root__gtn"));
+ assertFalse(expected(validator, "root._gtn"));
+ assertFalse(expected(validator, "root--gtn"));
+ assertFalse(expected(validator, "root*gtn"));
+ assertFalse(expected(validator, "Root"));
+ }
+
+ public void testEmailValidator()
+ {
+ Validator validator = new EmailAddressValidator();
+ assertTrue(expected(validator, "root.gtn(a)exoplatform.com"));
+ assertTrue(expected(validator,
"root.exo.gtn.portal(a)explatform.biz.edu.vn"));
+ assertTrue(expected(validator,
"root_exo_gtn_portal(a)explatform-edu.biz.vn"));
+ assertFalse(expected(validator,
"root_exo_gtn_portal(a)explatform-edu.biz-vn"));
+ assertFalse(expected(validator,
"root_exo_gtn_portal(a)explatform-edu.biz9vn"));
+ assertFalse(expected(validator,
"root_exo_gtn_portal(a)explatform--edu.biz.vn"));
+ assertFalse(expected(validator, "root_exo_gtn_portal(a)-explatform.biz"));
+ assertFalse(expected(validator,
"root_exo_gtn_portal(a)explatform_biz_edu.vn"));
+ assertFalse(expected(validator,
"root_exo_gtn_portal@explatform_biz_edu_vn"));
+ assertFalse(expected(validator, "root_gtn--(a)portal.org"));
+ assertFalse(expected(validator, "root__gtn(a)portal.org"));
+ assertFalse(expected(validator, "root_.gtn(a)portal.org"));
+ assertFalse(expected(validator, "--root.gtn(a)portal.org"));
+ assertFalse(expected(validator, "root.gtn@.portal.org"));
+ }
+
+ public void testNumberValidator()
+ {
+ Validator validator = new NumberFormatValidator();
+ assertTrue(expected(validator, "1001"));
+ assertTrue(expected(validator, "0"));
+ assertFalse(expected(validator, "01"));
+ assertFalse(expected(validator, "-01"));
+ assertFalse(expected(validator, "-0"));
+ assertFalse(expected(validator, "000"));
+ assertFalse(expected(validator, "-01"));
+ assertFalse(expected(validator, "1,5"));
+ assertFalse(expected(validator, "1.5"));
+ }
+
+ public void testPositiveNumberValidator()
+ {
+ Validator validator = new PositiveNumberFormatValidator();
+ assertTrue(expected(validator, "1"));
+ assertTrue(expected(validator, "0"));
+ assertFalse(expected(validator, "-1"));
+ assertFalse(expected(validator, "01"));
+ assertFalse(expected(validator, "-01"));
+ }
+
+ public void testSpecialCharacterValidator()
+ {
+ Validator validator= new SpecialCharacterValidator();
+ assertTrue(expected(validator,"aAzZ caffé"));
+ assertFalse(expected(validator,"aAzZ\tcaffé"));
+ assertFalse(expected(validator,"aAzZ\ncaffé"));
+ assertFalse(expected(validator,"aAzZ \rcaffé"));
+ assertFalse(expected(validator,"\tcaffé"));
+ assertFalse(expected(validator,"\ncaffé"));
+ assertFalse(expected(validator,"\rcaffé"));
+ assertTrue(expected(validator,"\n"));
+ assertTrue(expected(validator, "\t"));
+ assertTrue(expected(validator, "\n"));
+ }
+
+ public void testResourceValidator()
+ {
+ Validator validator = new ResourceValidator();
+ assertTrue(expected(validator, "caffé_-.--"));
+ assertFalse(expected(validator, "_caffé"));
+ assertFalse(expected(validator, "0caffé"));
+ }
+
+ public void testNameValidator()
+ {
+ Validator validator = new NameValidator();
+ assertTrue(expected(validator, "caffé_-.*"));
+ assertTrue(expected(validator, "*caffé"));
+ assertTrue(expected(validator, "0caffé"));
+ }
+
+ public void testIdentifierValidator()
+ {
+ Validator validator = new IdentifierValidator();
+ assertTrue(expected(validator, "caffé-_"));
+ assertTrue(expected(validator, "caffé01"));
+ assertFalse(expected(validator, "-caffé"));
+ assertFalse(expected(validator, "01caffé"));
+ }
+
+ public boolean expected(Validator validator, final String input)
+ {
+ UIFormInput uiInput = new MockUIFormImput()
+ {
+ public Object getValue() throws Exception
+ {
+ return input;
+ }
+ };
+ return expected(validator, uiInput);
+ }
+
+ public boolean expected(Validator validator, UIFormInput uiInput)
+ {
+ try
+ {
+ validator.validate(uiInput);
+ return true;
+ }
+ catch (MessageException e)
+ {
+ return false;
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static class MockUIFormImput extends UIComponent implements UIFormInput
+ {
+ public String getBindingField()
+ {
+ return null;
+ }
+
+ public String getLabel()
+ {
+ return null;
+ }
+
+ public UIFormInput addValidator(Class clazz, Object... params) throws Exception
+ {
+ return null;
+ }
+
+ public List getValidators()
+ {
+ return null;
+ }
+
+ public Object getValue() throws Exception
+ {
+ return null;
+ }
+
+ public UIFormInput setValue(Object value) throws Exception
+ {
+ return null;
+ }
+
+ public Class getTypeValue()
+ {
+ return null;
+ }
+
+ public void reset()
+ {
+ }
+ }
+}