View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.student.r2.common.datadictionary.util;
17  
18  import org.junit.Ignore;
19  import org.junit.Test;
20  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
21  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
22  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
23  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
24  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
25  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
26  import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
27  import org.springframework.context.ApplicationContext;
28  import org.springframework.context.support.ClassPathXmlApplicationContext;
29  
30  import java.util.Map;
31  
32  import static org.junit.Assert.*;
33  
34  public class TestBaseValidCharsDictionary {
35  
36      private String test(ValidCharactersConstraintProcessor vccp, ValidCharactersConstraint vcc, String value) {
37          DictionaryValidationResult dvr = new DictionaryValidationResult();
38          AttributeValueReader avr = new MockAttributeValueReader();
39          ProcessorResult result = vccp.process(dvr, value, vcc, avr);
40  //        System.out.println("Number of results=" + result.getConstraintValidationResults().size());
41          for (ConstraintValidationResult cvr : result.getConstraintValidationResults()) {
42  //            System.out.println("cvr.status =" + cvr.getStatus());
43              if (! cvr.getStatus().equals(ErrorLevel.OK)) {
44  //                System.out.println("cvr.errorKey =" + cvr.getErrorKey());
45                  return "error";
46              }
47          }
48          return null;
49      }
50  
51      @Test
52      @Ignore //KSENROLL-3725 // TODO: RICE-M9 UPGRADE
53      public void testValidCharsConstraints() {
54          System.out.println("testing base dictionary");
55          String contextFile = "ks-base-dictionary-validchars.xml";
56          ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:"
57                  + contextFile);
58          Map<String, ValidCharactersConstraint> vccs = (Map<String, ValidCharactersConstraint>) ac.getBeansOfType(
59                  ValidCharactersConstraint.class);
60          for (String id : vccs.keySet()) {
61              ValidCharactersConstraint vcc = vccs.get(id);
62              System.out.println("valid chars constraint: " + id + " "
63                      + vcc.getMessageKey() + " " + vcc.getValue());
64          }
65  
66          String id = null;
67          ValidCharactersConstraint vc = null;
68          ValidCharactersConstraintProcessor vccp = new ValidCharactersConstraintProcessor();
69  
70          id = "AlphanumericHyphenPeriod";
71          vc = vccs.get(id);
72          assertNotNull(vc);
73          assertNull(test(vccp, vc, "1"));
74          assertNull(test(vccp, vc, "A"));
75          assertNull(test(vccp, vc, "."));
76          assertNull(test(vccp, vc, "-"));
77          assertNotNull(test(vccp, vc, " "));
78          assertNotNull(test(vccp, vc, ","));
79          assertNotNull(test(vccp, vc, "\n"));
80          assertNotNull(test(vccp, vc, "\r"));
81          assertNotNull(test(vccp, vc, "\t"));
82  
83          id = "Digits";
84          vc = vccs.get(id);
85          assertNotNull(vc);
86          assertNull(test(vccp, vc, "1"));
87          assertNotNull(test(vccp, vc, "A"));
88          assertNotNull(test(vccp, vc, "."));
89          assertNotNull(test(vccp, vc, "-"));
90          assertNotNull(test(vccp, vc, " "));
91          assertNotNull(test(vccp, vc, ","));
92          assertNotNull(test(vccp, vc, "\n"));
93          assertNotNull(test(vccp, vc, "\r"));
94          assertNotNull(test(vccp, vc, "\t"));
95  
96          id = "Numeric";
97          vc = vccs.get(id);
98          assertNotNull(vc);
99          assertNull(test(vccp, vc, "0"));
100         assertNull(test(vccp, vc, "1"));
101         assertNull(test(vccp, vc, "20"));
102         assertNull(test(vccp, vc, "-20"));
103         assertNull(test(vccp, vc, "-12345620"));
104         assertNull(test(vccp, vc, "20.1"));
105         assertNull(test(vccp, vc, "20.0"));
106         assertNull(test(vccp, vc, "20.01"));
107         assertNull(test(vccp, vc, "20.00"));
108         assertNull(test(vccp, vc, "120.00"));
109         assertNull(test(vccp, vc, "1120.00"));
110         assertNull(test(vccp, vc, "11120.00"));
111         assertNull(test(vccp, vc, "111120.00"));
112         assertNull(test(vccp, vc, "1111120.00"));
113         assertNull(test(vccp, vc, "11111120.00"));
114         assertNotNull(test(vccp, vc, "020"));
115         assertNotNull(test(vccp, vc, "-020"));
116         assertNotNull(test(vccp, vc, "-020.1"));
117         assertNotNull(test(vccp, vc, "20.010"));
118         assertNotNull(test(vccp, vc, "20.011"));
119         assertNotNull(test(vccp, vc, "A"));
120         assertNotNull(test(vccp, vc, "."));
121         assertNotNull(test(vccp, vc, "-"));
122         assertNotNull(test(vccp, vc, " "));
123         assertNotNull(test(vccp, vc, ","));
124         assertNotNull(test(vccp, vc, "\n"));
125         assertNotNull(test(vccp, vc, "\r"));
126         assertNotNull(test(vccp, vc, "\t"));
127 
128         id = "Numeric.range";
129         vc = vccs.get(id);
130         assertNotNull(vc);
131         assertNull(test(vccp, vc, "0"));
132         assertNull(test(vccp, vc, "1"));
133         assertNull(test(vccp, vc, "20"));
134         assertNull(test(vccp, vc, "-20"));
135         assertNull(test(vccp, vc, "-12345620"));
136         assertNull(test(vccp, vc, "20.1"));
137         assertNull(test(vccp, vc, "20.0"));
138         assertNull(test(vccp, vc, "20.01"));
139         assertNull(test(vccp, vc, "20.00"));
140         assertNull(test(vccp, vc, "120.00"));
141         assertNull(test(vccp, vc, "1120.00"));
142         assertNull(test(vccp, vc, "11120.00"));
143         assertNull(test(vccp, vc, "111120.00"));
144         assertNull(test(vccp, vc, "1111120.00"));
145         assertNull(test(vccp, vc, "11111120.00"));
146         assertNull(test(vccp, vc, "1-2"));
147         assertNull(test(vccp, vc, "1.1-2.2"));
148         assertNull(test(vccp, vc, "1 - 2"));
149         assertNull(test(vccp, vc, "1--2"));
150         assertNull(test(vccp, vc, "1 - -2"));
151         assertNull(test(vccp, vc, "-1 - -2"));
152         assertNotNull(test(vccp, vc, "1 -2"));
153         assertNotNull(test(vccp, vc, "1 - 2 - 3"));
154         assertNotNull(test(vccp, vc, "1- 2"));
155         assertNotNull(test(vccp, vc, "020"));
156         assertNotNull(test(vccp, vc, "-020"));
157         assertNotNull(test(vccp, vc, "-020.1"));
158         assertNotNull(test(vccp, vc, "20.010"));
159         assertNotNull(test(vccp, vc, "20.011"));
160         assertNotNull(test(vccp, vc, "A"));
161         assertNotNull(test(vccp, vc, "."));
162         assertNotNull(test(vccp, vc, "-"));
163         assertNotNull(test(vccp, vc, " "));
164         assertNotNull(test(vccp, vc, ","));
165         assertNotNull(test(vccp, vc, "\n"));
166         assertNotNull(test(vccp, vc, "\r"));
167         assertNotNull(test(vccp, vc, "\t"));
168 
169         id = "Positive.numeric.range";
170         vc = vccs.get(id);
171         assertNotNull(vc);
172         assertNull(test(vccp, vc, "0"));
173         assertNull(test(vccp, vc, "1"));
174         assertNull(test(vccp, vc, "20"));
175         assertNotNull(test(vccp, vc, "-20"));
176         assertNotNull(test(vccp, vc, "-12345620"));
177         assertNull(test(vccp, vc, "20.1"));
178         assertNull(test(vccp, vc, "20.0"));
179         assertNull(test(vccp, vc, "20.01"));
180         assertNull(test(vccp, vc, "20.00"));
181         assertNull(test(vccp, vc, "120.00"));
182         assertNull(test(vccp, vc, "1120.00"));
183         assertNull(test(vccp, vc, "11120.00"));
184         assertNull(test(vccp, vc, "111120.00"));
185         assertNull(test(vccp, vc, "1111120.00"));
186         assertNull(test(vccp, vc, "11111120.00"));
187         assertNull(test(vccp, vc, "1-2"));
188         assertNull(test(vccp, vc, "1.1-2.2"));
189         assertNull(test(vccp, vc, "1 - 2"));
190         assertNotNull(test(vccp, vc, "1--2"));
191         assertNotNull(test(vccp, vc, "1 - -2"));
192         assertNotNull(test(vccp, vc, "-1 - -2"));
193         assertNotNull(test(vccp, vc, "1 -2"));
194         assertNotNull(test(vccp, vc, "1 - 2 - 3"));
195         assertNotNull(test(vccp, vc, "1- 2"));
196         assertNotNull(test(vccp, vc, "020"));
197         assertNotNull(test(vccp, vc, "-020"));
198         assertNotNull(test(vccp, vc, "-020.1"));
199         assertNotNull(test(vccp, vc, "20.010"));
200         assertNotNull(test(vccp, vc, "20.011"));
201         assertNotNull(test(vccp, vc, "A"));
202         assertNotNull(test(vccp, vc, "."));
203         assertNotNull(test(vccp, vc, "-"));
204         assertNotNull(test(vccp, vc, " "));
205         assertNotNull(test(vccp, vc, ","));
206         assertNotNull(test(vccp, vc, "\n"));
207         assertNotNull(test(vccp, vc, "\r"));
208         assertNotNull(test(vccp, vc, "\t"));
209 
210         id = "Gpa4.0";
211         vc = vccs.get(id);
212         assertNotNull(vc);
213         assertNull(test(vccp, vc, "0.0"));
214         assertNull(test(vccp, vc, "4.0"));
215         assertNull(test(vccp, vc, "1.0"));
216         // TODO: fix reg ex so it allows 4.0 but excludes 4.1, 4.2 etc...
217 //  assertNotNull (v.processValidCharConstraint ("test", vc, null, "4.1"));
218         assertNotNull(test(vccp, vc, "1"));
219         assertNull(test(vccp, vc, "1.0"));
220         assertNotNull(test(vccp, vc, "\n"));
221         assertNotNull(test(vccp, vc, "\r"));
222         assertNotNull(test(vccp, vc, "\t"));
223 
224         id = "Positive.integer";
225         vc = vccs.get(id);
226         assertNotNull(vc);
227         assertNull(test(vccp, vc, "1"));
228         assertNull(test(vccp, vc, "22"));
229         assertNull(test(vccp, vc, "33"));
230         assertNotNull(test(vccp, vc, "0"));
231         assertNotNull(test(vccp, vc, "1.0"));
232         assertNotNull(test(vccp, vc, "-1"));
233         assertNotNull(test(vccp, vc, "\n"));
234         assertNotNull(test(vccp, vc, "\r"));
235         assertNotNull(test(vccp, vc, "\t"));
236 
237         id = "RelationalOperator";
238         vc = vccs.get(id);
239         assertNotNull(vc);
240         assertNull(test(vccp, vc, "="));
241         assertNull(test(vccp, vc, ">"));
242         assertNull(test(vccp, vc, "<"));
243         assertNull(test(vccp, vc, ">="));
244         assertNull(test(vccp, vc, "<="));
245         assertNull(test(vccp, vc, "<>"));
246         assertNotNull(test(vccp, vc, "!="));
247         assertNotNull(test(vccp, vc, "=>"));
248         assertNotNull(test(vccp, vc, "=<"));
249         assertNotNull(test(vccp, vc, "0"));
250         assertNotNull(test(vccp, vc, "1.0"));
251         assertNotNull(test(vccp, vc, "-1"));
252         assertNotNull(test(vccp, vc, "\n"));
253         assertNotNull(test(vccp, vc, "\r"));
254         assertNotNull(test(vccp, vc, "\t"));
255 
256         id = "Alpha";
257         vc = vccs.get(id);
258         assertNotNull(vc);
259         assertNull(test(vccp, vc, "a"));
260         assertNull(test(vccp, vc, "A"));
261         assertNull(test(vccp, vc, "Z"));
262         assertNull(test(vccp, vc, "zzzzz"));
263         assertNull(test(vccp, vc,
264                 "abcdefghijklmnopqrstuvwxyz"));
265         assertNotNull(test(vccp, vc, "1"));
266         assertNotNull(test(vccp, vc, "0"));
267         assertNotNull(test(vccp, vc, "X\n"));
268         assertNotNull(test(vccp, vc, "X\r"));
269         assertNotNull(test(vccp, vc, "X\t"));
270 
271         id = "UpperCase";
272         vc = vccs.get(id);
273         assertNotNull(vc);
274         assertNull(test(vccp, vc, "AB"));
275         assertNull(test(vccp, vc, "A"));
276         assertNull(test(vccp, vc,
277                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
278         assertNull(test(vccp, vc, "-"));
279         assertNull(test(vccp, vc, "."));
280         assertNull(test(vccp, vc, "1"));
281         assertNull(test(vccp, vc, "0"));
282         assertNotNull(test(vccp, vc, "a"));
283         assertNotNull(test(vccp, vc, "X\n"));
284         assertNotNull(test(vccp, vc, "X\r"));
285         assertNotNull(test(vccp, vc, "X\t"));
286 
287         id = "UpperAlpha";
288         vc = vccs.get(id);
289         assertNotNull(vc);
290         assertNull(test(vccp, vc, "AB"));
291         assertNull(test(vccp, vc, "A"));
292         assertNull(test(vccp, vc, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
293         assertNotNull(test(vccp, vc, "a"));
294         assertNotNull(test(vccp, vc, "1"));
295         assertNotNull(test(vccp, vc, "0"));
296         assertNotNull(test(vccp, vc, "X\n"));
297         assertNotNull(test(vccp, vc, "X\r"));
298         assertNotNull(test(vccp, vc, "X\t"));
299 
300         id = "LineText";
301         vc = vccs.get(id);
302         assertNotNull(vc);
303         assertNull(test(vccp, vc, "a"));
304         assertNull(test(vccp, vc, "A"));
305         assertNull(test(vccp, vc, "Z"));
306         assertNull(test(vccp, vc, "Z"));
307         assertNull(test(vccp, vc, "!"));
308         assertNull(test(vccp, vc, "@"));
309         assertNull(test(vccp, vc, "#"));
310         assertNull(test(vccp, vc, "$"));
311         assertNull(test(vccp, vc, "%"));
312         //TODO: maybe allow these special characters
313         assertNotNull(test(vccp, vc, "^"));
314         assertNull(test(vccp, vc, "&"));
315         assertNotNull(test(vccp, vc, "*"));
316         assertNull(test(vccp, vc, "("));
317         assertNull(test(vccp, vc, ")"));
318         assertNull(test(vccp, vc, "_"));
319         assertNull(test(vccp, vc, "+"));
320         assertNull(test(vccp, vc, "-"));
321         assertNull(test(vccp, vc, "="));
322         assertNotNull(test(vccp, vc, "{"));
323         assertNotNull(test(vccp, vc, "}"));
324         assertNull(test(vccp, vc, "["));
325         assertNull(test(vccp, vc, "]"));
326         assertNotNull(test(vccp, vc, "|"));
327         assertNull(test(vccp, vc, "\\"));
328         assertNull(test(vccp, vc, ","));
329         assertNull(test(vccp, vc, "."));
330         assertNull(test(vccp, vc, "/"));
331         assertNull(test(vccp, vc, "<"));
332         assertNull(test(vccp, vc, ">"));
333         assertNull(test(vccp, vc, "?"));
334         assertNotNull(test(vccp, vc, "~"));
335         assertNotNull(test(vccp, vc, "`"));
336         assertNull(test(vccp, vc, "zzzzz"));
337         assertNull(test(vccp, vc,"abcdefghijklmnopqrstuvwxyz"));
338         assertNull(test(vccp, vc,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
339         assertNull(test(vccp, vc, "1"));
340         assertNull(test(vccp, vc, "0"));
341         assertNotNull(test(vccp, vc, "X\n"));
342         assertNotNull(test(vccp, vc, "X\r"));
343         assertNull(test(vccp, vc, "X\t"));
344 
345         id = "MultiLineText";
346         vc = vccs.get(id);
347         assertNotNull(vc);
348         assertNull(test(vccp, vc, "a"));
349         assertNull(test(vccp, vc, "A"));
350         assertNull(test(vccp, vc, "Z"));
351         assertNull(test(vccp, vc, "Z"));
352         assertNull(test(vccp, vc, "!"));
353         assertNull(test(vccp, vc, "@"));
354         assertNull(test(vccp, vc, "#"));
355         assertNull(test(vccp, vc, "$"));
356         assertNull(test(vccp, vc, "%"));
357         //TODO: maybe allow these special characters
358         assertNotNull(test(vccp, vc, "^"));
359         assertNull(test(vccp, vc, "&"));
360         assertNotNull(test(vccp, vc, "*"));
361         assertNull(test(vccp, vc, "("));
362         assertNull(test(vccp, vc, ")"));
363         assertNull(test(vccp, vc, "_"));
364         assertNull(test(vccp, vc, "+"));
365         assertNull(test(vccp, vc, "-"));
366         assertNull(test(vccp, vc, "="));
367         assertNotNull(test(vccp, vc, "{"));
368         assertNotNull(test(vccp, vc, "}"));
369         assertNull(test(vccp, vc, "["));
370         assertNull(test(vccp, vc, "]"));
371         assertNotNull(test(vccp, vc, "|"));
372         assertNull(test(vccp, vc, "\\"));
373         assertNull(test(vccp, vc, ","));
374         assertNull(test(vccp, vc, "."));
375         assertNull(test(vccp, vc, "/"));
376         assertNull(test(vccp, vc, "<"));
377         assertNull(test(vccp, vc, ">"));
378         assertNull(test(vccp, vc, "?"));
379         assertNotNull(test(vccp, vc, "~"));
380         assertNotNull(test(vccp, vc, "`"));
381 
382         assertNull(test(vccp, vc, "zzzzz"));
383         assertNull(test(vccp, vc,
384                 "abcdefghijklmnopqrstuvwxyz"));
385         assertNull(test(vccp, vc,
386                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
387         assertNull(test(vccp, vc, "1"));
388         assertNull(test(vccp, vc, "0"));
389         assertNull(test(vccp, vc, "X\n"));
390         assertNull(test(vccp, vc, "X\r"));
391         assertNull(test(vccp, vc, "X\t"));
392 
393         // If you get this error -- don't just change the number
394         // also add a unit test for new valid chars definition that you must have added into the base dictionary!
395         assertEquals(13, vccs.size());
396     }
397 }