Author: chris.laprun(a)jboss.com
Date: 2012-03-14 13:16:44 -0400 (Wed, 14 Mar 2012)
New Revision: 8600
Added:
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
Modified:
epp/portal/branches/EPP_5_2_Branch/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableValidator.java
Log:
bz 801207 GTNPORTAL-2378: Improved group membership regular expression, added test cases.
Modified:
epp/portal/branches/EPP_5_2_Branch/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableValidator.java
===================================================================
---
epp/portal/branches/EPP_5_2_Branch/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableValidator.java 2012-03-14
16:24:13 UTC (rev 8599)
+++
epp/portal/branches/EPP_5_2_Branch/webui/core/src/main/java/org/exoplatform/webui/form/validator/UserConfigurableValidator.java 2012-03-14
17:16:44 UTC (rev 8600)
@@ -67,7 +67,8 @@
public static final String USERNAME = "username";
public static final String GROUPMEMBERSHIP = "groupmembership";
public static final String DEFAULT_LOCALIZATION_KEY =
"ExpressionValidator.msg.value-invalid";
- public static final String GROUP_MEMBERSHIP_VALIDATION_REGEX =
"^\\p{L}[\\p{L}\\d._\\-\\s*,\\s*]+$";
+ /** Note that this regular expression should actually validate comma-separated
usernames. This is not the case as some constraints (consecutive symbols for examples) are
not taken into account. */
+ public static final String GROUP_MEMBERSHIP_VALIDATION_REGEX =
"^(\\p{Lower}[\\p{Lower}\\d\\._]+)(\\s*,\\s*(\\p{Lower}[\\p{Lower}\\d\\._]+))*$";
public static final String GROUP_MEMBERSHIP_LOCALIZATION_KEY =
"UIGroupMembershipForm.msg.Invalid-char";
private static Map<String, ValidatorConfiguration> configurations = new
HashMap<String, ValidatorConfiguration>(3);
Added:
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java
===================================================================
---
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java
(rev 0)
+++
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/MockRequestContext.java 2012-03-14
17:16:44 UTC (rev 8600)
@@ -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:
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
===================================================================
---
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java
(rev 0)
+++
epp/portal/branches/EPP_5_2_Branch/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java 2012-03-14
17:16:44 UTC (rev 8600)
@@ -0,0 +1,306 @@
+/**
+ * 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 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.NumberRangeValidator;
+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.UserConfigurableValidator;
+import org.exoplatform.webui.form.validator.UsernameValidator;
+import org.exoplatform.webui.form.validator.Validator;
+
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * @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);
+ validateUsernames(validator);
+
+ validator = new UserConfigurableValidator(UserConfigurableValidator.USERNAME);
+ validateUsernames(validator);
+ }
+
+ private void validateUsernames(Validator validator)
+ {
+ 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 validateUsernamesInGroupMembership()
+ {
+ final UserConfigurableValidator validator = new
UserConfigurableValidator(UserConfigurableValidator.GROUPMEMBERSHIP);
+ assertTrue(expected(validator, "root.gtn"));
+ assertTrue(expected(validator, "root_gtn"));
+ assertTrue(expected(validator, "root_gtn.01"));
+ assertTrue(expected(validator, "a1,foo,bar"));
+ assertTrue(expected(validator, "a1 ,\tfoo,\nbar \r"));
+ assertFalse(expected(validator, "a1 ,\tfoo,\nbar \ra"));
+ assertFalse(expected(validator, "a1 ,\tfoo,\nbar \r,a"));
+ assertFalse(expected(validator, "root_gtn_"));
+ assertFalse(expected(validator, "_root_gtn"));
+// assertFalse(expected(validator, "root__gtn")); // not taken into
account yet
+// assertFalse(expected(validator, "root._gtn")); // not taken into
account yet
+ assertFalse(expected(validator, "root--gtn"));
+ assertFalse(expected(validator, "root*gtn"));
+ assertFalse(expected(validator, "Root"));
+ assertFalse(expected(validator, "a"));
+ }
+
+ public void testEmailValidator()
+ {
+ Validator validator = new EmailAddressValidator();
+ assertFalse(expected(validator, "root"));
+ 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 testNumberRangeValidator()
+ {
+ Validator validator = new NumberRangeValidator(-5, 5);
+ assertTrue(expected(validator, "-5"));
+ assertTrue(expected(validator, "-1"));
+ assertTrue(expected(validator, "0"));
+ assertTrue(expected(validator, "1"));
+ assertTrue(expected(validator, "5"));
+
+ assertFalse(expected(validator, "-10"));
+ assertFalse(expected(validator, "-6"));
+ assertFalse(expected(validator, "6"));
+ assertFalse(expected(validator, "10"));
+ }
+
+ 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()
+ {
+ }
+ }
+}