View Javadoc

1   /**
2    * Copyright 2005-2011 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  public class AlphaNumericValidationPatternTest extends BaseRiceTestCase {
24      private AlphaNumericValidationPattern pattern;
25  
26      @Override
27      public void setUp() throws Exception {
28          super.setUp();
29          pattern = new AlphaNumericValidationPattern();
30      }
31  
32  
33      @Test public final void testMatch_allowDefault() {
34          boolean[] expected = { true, // ""
35                  false, // "!!!"
36                  false, // "[a-9]"
37                  false, // "^A-Z"
38                  true, // "abc"
39                  false, // "a bc"
40                  false, // "a_bc"
41                  true, // "123"
42                  false, // "12 3"
43                  false, // "12_3"
44                  true, // "a1b2c3"
45                  false, // "a1b2_c3"
46                  false, // "a 1b2c3"
47                  false, // "a 1b2_c3"
48                  false, //"foo.bar"
49                  false, //"foo.bar_baz"
50                  false, //".bar_foo baz"
51          };
52  
53          ValidationTestUtils.assertPatternMatches(pattern, expected);
54      }
55  
56      @Test public final void testMatch_allowUnderscore() {
57          boolean[] expected = { true, // ""
58                  false, // "!!!"
59                  false, // "[a-9]"
60                  false, // "^A-Z"
61                  true, // "abc"
62                  false, // "a bc"
63                  true, // "a_bc"
64                  true, // "123"
65                  false, // "12 3"
66                  true, // "12_3"
67                  true, // "a1b2c3"
68                  true, // "a1b2_c3"
69                  false, // "a 1b2c3"
70                  false, // "a 1b2_c3"
71                  false, //"foo.bar"
72                  false, //"foo.bar_baz"
73                  false, //".bar_foo baz"
74          };
75  
76          pattern.setAllowUnderscore(true);
77          ValidationTestUtils.assertPatternMatches(pattern, expected);
78      }
79  
80      @Test public final void testMatch_allowWhitespace() {
81          boolean[] expected = { true, // ""
82                  false, // "!!!"
83                  false, // "[a-9]"
84                  false, // "^A-Z"
85                  true, // "abc"
86                  true, // "a bc"
87                  false, // "a_bc"
88                  true, // "123"
89                  true, // "12 3"
90                  false, // "12_3"
91                  true, // "a1b2c3"
92                  false, // "a1b2_c3"
93                  true, // "a 1b2c3"
94                  false, // "a 1b2_c3"
95                  false, //"foo.bar"
96                  false, //"foo.bar_baz"
97                  false, //".bar_foo baz"
98          };
99  
100         pattern.setAllowWhitespace(true);
101         ValidationTestUtils.assertPatternMatches(pattern, expected);
102     }
103 
104     @Test public final void testMatch_allowUnderScoreAndWhiteSpace() {
105         boolean[] expected = { true, // ""
106                 false, // "!!!"
107                 false, // "[a-9]"
108                 false, // "^A-Z"
109                 true, // "abc"
110                 true, // "a bc"
111                 true, // "a_bc"
112                 true, // "123"
113                 true, // "12 3"
114                 true, // "12_3"
115                 true, // "a1b2c3"
116                 true, // "a1b2_c3"
117                 true, // "a 1b2c3"
118                 true, // "a 1b2_c3"
119                 false, //"foo.bar"
120                 false, //"foo.bar_baz"
121                 false, //".bar_foo baz"
122         };
123 
124         pattern.setAllowUnderscore(true);
125         pattern.setAllowWhitespace(true);
126 
127         ValidationTestUtils.assertPatternMatches(pattern, expected);
128     }
129     
130     @Test public final void testMatch_allowPeriod() {
131         boolean[] expected = { true, // ""
132                 false, // "!!!"
133                 false, // "[a-9]"
134                 false, // "^A-Z"
135                 true, // "abc"
136                 false, // "a bc"
137                 false, // "a_bc"
138                 true, // "123"
139                 false, // "12 3"
140                 false, // "12_3"
141                 true, // "a1b2c3"
142                 false, // "a1b2_c3"
143                 false, // "a 1b2c3"
144                 false, // "a 1b2_c3"
145                 true, //"foo.bar"
146                 false, //"foo.bar_baz"
147                 false, //".bar_foo baz"
148         };
149 
150         pattern.setAllowPeriod(true);
151 
152         ValidationTestUtils.assertPatternMatches(pattern, expected);
153     }
154     
155     @Test public final void testMatch_allowPeriodAndUnderscore() {
156         boolean[] expected = { true, // ""
157                 false, // "!!!"
158                 false, // "[a-9]"
159                 false, // "^A-Z"
160                 true, // "abc"
161                 false, // "a bc"
162                 true, // "a_bc"
163                 true, // "123"
164                 false, // "12 3"
165                 true, // "12_3"
166                 true, // "a1b2c3"
167                 true, // "a1b2_c3"
168                 false, // "a 1b2c3"
169                 false, // "a 1b2_c3"
170                 true, //"foo.bar"
171                 true, //"foo.bar_baz"
172                 false, //".bar_foo baz"
173         };
174 
175         pattern.setAllowPeriod(true);
176         pattern.setAllowUnderscore(true);
177 
178         ValidationTestUtils.assertPatternMatches(pattern, expected);
179     }
180     
181     @Test public final void testMatch_allowPeriodAndUnderscoreAndSpace() {
182         boolean[] expected = { true, // ""
183                 false, // "!!!"
184                 false, // "[a-9]"
185                 false, // "^A-Z"
186                 true, // "abc"
187                 true, // "a bc"
188                 true, // "a_bc"
189                 true, // "123"
190                 true, // "12 3"
191                 true, // "12_3"
192                 true, // "a1b2c3"
193                 true, // "a1b2_c3"
194                 true, // "a 1b2c3"
195                 true, // "a 1b2_c3"
196                 true, //"foo.bar"
197                 true, //"foo.bar_baz"
198                 true, //".bar_foo baz"
199         };
200 
201         pattern.setAllowPeriod(true);
202         pattern.setAllowUnderscore(true);
203         pattern.setAllowWhitespace(true);
204 
205         ValidationTestUtils.assertPatternMatches(pattern, expected);
206     }
207 }