1   /*
2    *  XNap
3    *
4    *  A pure java file sharing client.
5    *
6    *  See AUTHORS for copyright information.
7    *
8    *  This program is free software; you can redistribute it and/or modify
9    *  it under the terms of the GNU General Public License as published by
10   *  the Free Software Foundation; either version 2 of the License, or
11   *  (at your option) any later version.
12   *
13   *  This program is distributed in the hope that it will be useful,
14   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   *  GNU General Public License for more details.
17   *
18   *  You should have received a copy of the GNU General Public License
19   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   *
22   */
23  package org.xnap.util.prefs;
24  
25  import junit.framework.TestCase;
26  
27  /***
28   * @author Steffen Pingel
29   */
30  public class StringValidatorTest extends TestCase {
31      
32      public StringValidatorTest(String name) 
33      {
34  		super(name);
35      }
36  
37      protected void setUp()
38      {
39      }
40  
41      protected void tearDown()
42      {
43      }
44  
45  	public void testEmptyConstructor()
46  	{
47  		// should validate anything
48  		StringValidator v = new StringValidator();
49  		v.validate("");
50  		v.validate(" ");
51  		v.validate("a");
52  	}
53  
54  	public void testStringConstructor()
55  	{
56  		StringValidator v = new StringValidator("a");
57  		v.validate("aaa");
58  		v.validate("a");
59  		v.validate("");
60  		invalidate(v, "aba");
61  		invalidate(v, "b");
62  	}
63  	
64  	public void testStringLengthConstructor()
65  	{
66  		StringValidator v = new StringValidator("a", 0);
67  		v.validate("aaa");
68  		v.validate("a");
69  		v.validate("");
70  
71  		v = new StringValidator("a", 1);
72  		v.validate("aaa");
73  		v.validate("aa");
74  		v.validate("a");
75  		invalidate(v, "");
76  	}
77  
78  	public void testNull()
79  	{
80  		StringValidator v = new StringValidator();
81  		invalidate(v, null);
82  	}
83  
84      public void testEmail() 
85      {
86  		StringValidator.EMAIL.validate("a");
87  		StringValidator.EMAIL.validate("a@a.com");
88  
89  		invalidate(StringValidator.EMAIL, null);
90  		invalidate(StringValidator.EMAIL, "");
91  		invalidate(StringValidator.EMAIL, " ");
92      }
93  
94      public void testRegularString() 
95      {
96  		StringValidator.REGULAR_STRING.validate("a");
97  
98  		invalidate(StringValidator.REGULAR_STRING, null);
99  		invalidate(StringValidator.REGULAR_STRING, "");
100 		invalidate(StringValidator.REGULAR_STRING, " ");
101 	}
102 
103     public void invalidate(StringValidator validator, String s) 
104     {
105 		try {
106 			validator.validate(s);
107 			assertTrue(false);
108 		}
109 		catch (IllegalArgumentException e) {
110 		}
111 	}
112 
113 }
114