PageRenderTime 70ms CodeModel.GetById 34ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

/webui/core/src/test/java/org/exoplatform/webui/test/validator/TestWebuiValidator.java

https://github.com/thanhvc/exogtn
Java | 262 lines | 214 code | 25 blank | 23 comment | 0 complexity | 66db0015226b86fca94e2faf91f3d9ec MD5 | raw file
  1/**
  2 * Copyright (C) 2003-2011 eXo Platform SAS.
  3 *
  4 * This program is free software; you can redistribute it and/or
  5 * modify it under the terms of the GNU Lesser General Public License
  6 * as published by the Free Software Foundation; either version 3
  7 * of the License, or (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, see<http://www.gnu.org/licenses/>.
 16 */
 17package org.exoplatform.webui.test.validator;
 18
 19import java.util.List;
 20import java.util.Locale;
 21
 22import junit.framework.TestCase;
 23
 24import org.exoplatform.webui.application.WebuiRequestContext;
 25import org.exoplatform.webui.core.UIComponent;
 26import org.exoplatform.webui.exception.MessageException;
 27import org.exoplatform.webui.form.UIFormDateTimeInput;
 28import org.exoplatform.webui.form.UIFormInput;
 29import org.exoplatform.webui.form.validator.DateTimeValidator;
 30import org.exoplatform.webui.form.validator.EmailAddressValidator;
 31import org.exoplatform.webui.form.validator.IdentifierValidator;
 32import org.exoplatform.webui.form.validator.NameValidator;
 33import org.exoplatform.webui.form.validator.NumberFormatValidator;
 34import org.exoplatform.webui.form.validator.PositiveNumberFormatValidator;
 35import org.exoplatform.webui.form.validator.ResourceValidator;
 36import org.exoplatform.webui.form.validator.SpecialCharacterValidator;
 37import org.exoplatform.webui.form.validator.URLValidator;
 38import org.exoplatform.webui.form.validator.UsernameValidator;
 39import org.exoplatform.webui.form.validator.Validator;
 40
 41/**
 42 * @author <a href="mailto:haint@exoplatform.com">Nguyen Thanh Hai</a>
 43 * 
 44 * @datSep 26, 2011
 45 */
 46public class TestWebuiValidator extends TestCase
 47{
 48   public void testUrlValidator()
 49   {
 50      Validator validator = new URLValidator();
 51      // Test ip address and invalidate subnet masks ip
 52      assertTrue(expected(validator, "https://192.168.1.1"));
 53      assertTrue(expected(validator, "ftp://255.255.255.1"));
 54      assertTrue(expected(validator, "ftps://255.255.0.1"));
 55      assertTrue(expected(validator, "ftp://0.0.0.0"));
 56      assertTrue(expected(validator, "http://127.0.0.1"));
 57      assertTrue(expected(validator, "https://192.168.4.90"));
 58      assertTrue(expected(validator, "https://192.168.4.90:8080"));
 59      assertTrue(expected(validator, "http://127.0.0.1:8080"));
 60      assertFalse(expected(validator, "http://127.0.0.01"));
 61      assertFalse(expected(validator, "ftp://255.255.255.255"));
 62
 63      // Test domain name and uri
 64      assertTrue(expected(validator, "https://www.exoplatform.com"));
 65      assertTrue(expected(validator, "ftps://root:gtn@exoplatform.com"));
 66      assertTrue(expected(validator, "ftps://root@exoplatform.com"));
 67      assertTrue(expected(validator, "https://www.dev.exoplatform.com"));
 68      assertTrue(expected(validator, "https://www.dev.exoplatform.com:8888"));
 69      assertFalse(expected(validator, "https://www.dev.exoplatform.com:8888?arg=value"));
 70      assertTrue(expected(validator, "https://www.dev.exoplatform.com:8888/path?arg=value"));
 71      assertTrue(expected(validator, "https://www.dev.exoplatform.com:8888/path?arg=value#"));
 72   }
 73
 74   public void testDateTimeValidator()
 75   {
 76      Validator validator = new DateTimeValidator();
 77      WebuiRequestContext.setCurrentInstance(new MockRequestContext(new Locale("fr")));
 78      UIFormDateTimeInput uiInput = new UIFormDateTimeInput("currentDate", "currentDate", null);
 79      uiInput.setValue("28/09/2011 10:59:59");
 80      assertTrue(expected(validator, uiInput));
 81      uiInput.setValue("09/28/2011 10:59:59");
 82      assertFalse(expected(validator, uiInput));
 83
 84      WebuiRequestContext.setCurrentInstance(new MockRequestContext(new Locale("en")));
 85      uiInput = new UIFormDateTimeInput("currentDate", "currentDate", null);
 86      uiInput.setValue("09/28/2011 10:59:59");
 87      assertTrue(expected(validator, uiInput));
 88      uiInput.setValue("09-28-2011 10:59:59");
 89      assertFalse(expected(validator, uiInput));
 90      uiInput.setValue("28/09/2011 10:59:59");
 91      assertFalse(expected(validator, uiInput));
 92   }
 93
 94   public void testUsernameValidator()
 95   {
 96      Validator validator = new UsernameValidator(3, 30);
 97      assertTrue(expected(validator, "root.gtn"));
 98      assertTrue(expected(validator, "root_gtn"));
 99      assertTrue(expected(validator, "root_gtn.01"));
100      assertFalse(expected(validator, "root_gtn_"));
101      assertFalse(expected(validator, "_root_gtn"));
102      assertFalse(expected(validator, "root__gtn"));
103      assertFalse(expected(validator, "root._gtn"));
104      assertFalse(expected(validator, "root--gtn"));
105      assertFalse(expected(validator, "root*gtn"));
106      assertFalse(expected(validator, "Root"));
107   }
108
109   public void testEmailValidator()
110   {
111      Validator validator = new EmailAddressValidator();
112      assertTrue(expected(validator, "root.gtn@exoplatform.com"));
113      assertTrue(expected(validator, "root.exo.gtn.portal@explatform.biz.edu.vn"));
114      assertTrue(expected(validator, "root_exo_gtn_portal@explatform-edu.biz.vn"));
115      assertFalse(expected(validator, "root_exo_gtn_portal@explatform-edu.biz-vn"));
116      assertFalse(expected(validator, "root_exo_gtn_portal@explatform-edu.biz9vn"));
117      assertFalse(expected(validator, "root_exo_gtn_portal@explatform--edu.biz.vn"));
118      assertFalse(expected(validator, "root_exo_gtn_portal@-explatform.biz"));
119      assertFalse(expected(validator, "root_exo_gtn_portal@explatform_biz_edu.vn"));
120      assertFalse(expected(validator, "root_exo_gtn_portal@explatform_biz_edu_vn"));
121      assertFalse(expected(validator, "root_gtn--@portal.org"));
122      assertFalse(expected(validator, "root__gtn@portal.org"));
123      assertFalse(expected(validator, "root_.gtn@portal.org"));
124      assertFalse(expected(validator, "--root.gtn@portal.org"));
125      assertFalse(expected(validator, "root.gtn@.portal.org"));
126   }
127
128   public void testNumberValidator()
129   {
130      Validator validator = new NumberFormatValidator();
131      assertTrue(expected(validator, "1001"));
132      assertTrue(expected(validator, "0"));
133      assertFalse(expected(validator, "01"));
134      assertFalse(expected(validator, "-01"));
135      assertFalse(expected(validator, "-0"));
136      assertFalse(expected(validator, "000"));
137      assertFalse(expected(validator, "-01"));
138      assertFalse(expected(validator, "1,5"));
139      assertFalse(expected(validator, "1.5"));
140   }
141
142   public void testPositiveNumberValidator()
143   {
144      Validator validator = new PositiveNumberFormatValidator();
145      assertTrue(expected(validator, "1"));
146      assertTrue(expected(validator, "0"));
147      assertFalse(expected(validator, "-1"));
148      assertFalse(expected(validator, "01"));
149      assertFalse(expected(validator, "-01"));
150   }
151   
152   public void testSpecialCharacterValidator()
153   {
154      Validator validator= new SpecialCharacterValidator();
155      assertTrue(expected(validator,"aAzZ  caffé"));
156      assertFalse(expected(validator,"aAzZ\tcaffé"));
157      assertFalse(expected(validator,"aAzZ\ncaffé"));
158      assertFalse(expected(validator,"aAzZ \rcaffé"));
159      assertFalse(expected(validator,"\tcaffé"));
160      assertFalse(expected(validator,"\ncaffé"));
161      assertFalse(expected(validator,"\rcaffé"));
162      assertTrue(expected(validator,"\n"));
163      assertTrue(expected(validator, "\t"));
164      assertTrue(expected(validator, "\n"));
165   }
166   
167   public void testResourceValidator()
168   {
169      Validator validator = new ResourceValidator();
170      assertTrue(expected(validator, "caffé_-.--"));
171      assertFalse(expected(validator, "_caffé"));
172      assertFalse(expected(validator, "0caffé"));
173   }
174   
175   public void testNameValidator()
176   {
177      Validator validator = new NameValidator();
178      assertTrue(expected(validator, "caffé_-.*"));
179      assertTrue(expected(validator, "*caffé"));
180      assertTrue(expected(validator, "0caffé"));
181   }
182   
183   public void testIdentifierValidator()
184   {
185      Validator validator = new IdentifierValidator();
186      assertTrue(expected(validator, "caffé-_"));
187      assertTrue(expected(validator, "caffé01"));
188      assertFalse(expected(validator, "-caffé"));
189      assertFalse(expected(validator, "01caffé"));
190   }
191
192   public boolean expected(Validator validator, final String input)
193   {
194      UIFormInput uiInput = new MockUIFormImput()
195      {
196         public Object getValue() throws Exception
197         {
198            return input;
199         }
200      };
201      return expected(validator, uiInput);
202   }
203
204   public boolean expected(Validator validator, UIFormInput uiInput)
205   {
206      try
207      {
208         validator.validate(uiInput);
209         return true;
210      }
211      catch (MessageException e)
212      {
213         return false;
214      }
215      catch (Exception e)
216      {
217         throw new RuntimeException(e);
218      }
219   }
220
221   private static class MockUIFormImput extends UIComponent implements UIFormInput
222   {
223      public String getBindingField()
224      {
225         return null;
226      }
227
228      public String getLabel()
229      {
230         return null;
231      }
232
233      public UIFormInput addValidator(Class clazz, Object... params) throws Exception
234      {
235         return null;
236      }
237
238      public List getValidators()
239      {
240         return null;
241      }
242
243      public Object getValue() throws Exception
244      {
245         return null;
246      }
247
248      public UIFormInput setValue(Object value) throws Exception
249      {
250         return null;
251      }
252
253      public Class getTypeValue()
254      {
255         return null;
256      }
257
258      public void reset()
259      {
260      }
261   }
262}