View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary.validation.charlevel;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kns.datadictionary.validation.charlevel.AlphaNumericValidationPattern;
20  import org.kuali.rice.krad.datadictionary.validation.ValidationTestUtils;
21  import org.kuali.rice.test.BaseRiceTestCase;
22  
23  /**
24   * AlphaNumericValidationPatternTest tests {@link AlphaNumericValidationPattern}
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  public class AlphaNumericValidationPatternTest extends BaseRiceTestCase {
29      private AlphaNumericValidationPattern pattern;
30  
31      @Override
32      public void setUp() throws Exception {
33          super.setUp();
34          pattern = new AlphaNumericValidationPattern();
35      }
36  
37      /**
38       * tests that only numbers and letters of the alphabet are allowed
39       */
40      @Test public final void testMatch_allowDefault() {
41          boolean[] expected = { true, // ""
42                  false, // "!!!"
43                  false, // "[a-9]"
44                  false, // "^A-Z"
45                  true, // "abc"
46                  false, // "a bc"
47                  false, // "a_bc"
48                  true, // "123"
49                  false, // "12 3"
50                  false, // "12_3"
51                  true, // "a1b2c3"
52                  false, // "a1b2_c3"
53                  false, // "a 1b2c3"
54                  false, // "a 1b2_c3"
55                  false, //"foo.bar"
56                  false, //"foo.bar_baz"
57                  false, //".bar_foo baz"
58          };
59  
60          ValidationTestUtils.assertPatternMatches(pattern, expected);
61      }
62  
63      /**
64       * tests that underscores, numbers and letters of the alphabet are allowed
65       */
66      @Test public final void testMatch_allowUnderscore() {
67          boolean[] expected = { true, // ""
68                  false, // "!!!"
69                  false, // "[a-9]"
70                  false, // "^A-Z"
71                  true, // "abc"
72                  false, // "a bc"
73                  true, // "a_bc"
74                  true, // "123"
75                  false, // "12 3"
76                  true, // "12_3"
77                  true, // "a1b2c3"
78                  true, // "a1b2_c3"
79                  false, // "a 1b2c3"
80                  false, // "a 1b2_c3"
81                  false, //"foo.bar"
82                  false, //"foo.bar_baz"
83                  false, //".bar_foo baz"
84          };
85  
86          pattern.setAllowUnderscore(true);
87          ValidationTestUtils.assertPatternMatches(pattern, expected);
88      }
89  
90      /**
91       * tests that white space, numbers and letters of the alphabet are allowed
92       */
93      @Test public final void testMatch_allowWhitespace() {
94          boolean[] expected = { true, // ""
95                  false, // "!!!"
96                  false, // "[a-9]"
97                  false, // "^A-Z"
98                  true, // "abc"
99                  true, // "a bc"
100                 false, // "a_bc"
101                 true, // "123"
102                 true, // "12 3"
103                 false, // "12_3"
104                 true, // "a1b2c3"
105                 false, // "a1b2_c3"
106                 true, // "a 1b2c3"
107                 false, // "a 1b2_c3"
108                 false, //"foo.bar"
109                 false, //"foo.bar_baz"
110                 false, //".bar_foo baz"
111         };
112 
113         pattern.setAllowWhitespace(true);
114         ValidationTestUtils.assertPatternMatches(pattern, expected);
115     }
116 
117     /**
118      * tests that white space, underscores, numbers and letters of the alphabet are allowed
119      */
120     @Test public final void testMatch_allowUnderScoreAndWhiteSpace() {
121         boolean[] expected = { true, // ""
122                 false, // "!!!"
123                 false, // "[a-9]"
124                 false, // "^A-Z"
125                 true, // "abc"
126                 true, // "a bc"
127                 true, // "a_bc"
128                 true, // "123"
129                 true, // "12 3"
130                 true, // "12_3"
131                 true, // "a1b2c3"
132                 true, // "a1b2_c3"
133                 true, // "a 1b2c3"
134                 true, // "a 1b2_c3"
135                 false, //"foo.bar"
136                 false, //"foo.bar_baz"
137                 false, //".bar_foo baz"
138         };
139 
140         pattern.setAllowUnderscore(true);
141         pattern.setAllowWhitespace(true);
142 
143         ValidationTestUtils.assertPatternMatches(pattern, expected);
144     }
145 
146     /**
147      * tests that periods, numbers and letters of the alphabet are allowed
148      */
149     @Test public final void testMatch_allowPeriod() {
150         boolean[] expected = { true, // ""
151                 false, // "!!!"
152                 false, // "[a-9]"
153                 false, // "^A-Z"
154                 true, // "abc"
155                 false, // "a bc"
156                 false, // "a_bc"
157                 true, // "123"
158                 false, // "12 3"
159                 false, // "12_3"
160                 true, // "a1b2c3"
161                 false, // "a1b2_c3"
162                 false, // "a 1b2c3"
163                 false, // "a 1b2_c3"
164                 true, //"foo.bar"
165                 false, //"foo.bar_baz"
166                 false, //".bar_foo baz"
167         };
168 
169         pattern.setAllowPeriod(true);
170 
171         ValidationTestUtils.assertPatternMatches(pattern, expected);
172     }
173 
174     /**
175      * tests that periods, underscores, numbers and letters of the alphabet are allowed
176      */
177     @Test public final void testMatch_allowPeriodAndUnderscore() {
178         boolean[] expected = { true, // ""
179                 false, // "!!!"
180                 false, // "[a-9]"
181                 false, // "^A-Z"
182                 true, // "abc"
183                 false, // "a bc"
184                 true, // "a_bc"
185                 true, // "123"
186                 false, // "12 3"
187                 true, // "12_3"
188                 true, // "a1b2c3"
189                 true, // "a1b2_c3"
190                 false, // "a 1b2c3"
191                 false, // "a 1b2_c3"
192                 true, //"foo.bar"
193                 true, //"foo.bar_baz"
194                 false, //".bar_foo baz"
195         };
196 
197         pattern.setAllowPeriod(true);
198         pattern.setAllowUnderscore(true);
199 
200         ValidationTestUtils.assertPatternMatches(pattern, expected);
201     }
202 
203     /**
204      * tests that periods, underscores, white space, numbers and letters of the alphabet are allowed
205      */
206     @Test public final void testMatch_allowPeriodAndUnderscoreAndSpace() {
207         boolean[] expected = { true, // ""
208                 false, // "!!!"
209                 false, // "[a-9]"
210                 false, // "^A-Z"
211                 true, // "abc"
212                 true, // "a bc"
213                 true, // "a_bc"
214                 true, // "123"
215                 true, // "12 3"
216                 true, // "12_3"
217                 true, // "a1b2c3"
218                 true, // "a1b2_c3"
219                 true, // "a 1b2c3"
220                 true, // "a 1b2_c3"
221                 true, //"foo.bar"
222                 true, //"foo.bar_baz"
223                 true, //".bar_foo baz"
224         };
225 
226         pattern.setAllowPeriod(true);
227         pattern.setAllowUnderscore(true);
228         pattern.setAllowWhitespace(true);
229 
230         ValidationTestUtils.assertPatternMatches(pattern, expected);
231     }
232 }