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.apache.commons.lang.StringUtils;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.CoreConstants;
22  import org.kuali.rice.core.api.config.property.Config;
23  import org.kuali.rice.core.api.config.property.ConfigContext;
24  import org.kuali.rice.core.api.config.property.ConfigurationService;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.core.api.resourceloader.ResourceLoader;
27  import org.kuali.rice.core.api.resourceloader.ServiceLocator;
28  import org.kuali.rice.core.api.util.RiceConstants;
29  import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader;
30  import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
31  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
32  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
33  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
34  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
35  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
36  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
37  import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
38  import org.kuali.rice.krad.service.KRADServiceLocator;
39  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
40  import org.kuali.rice.krad.service.KualiModuleService;
41  import org.kuali.rice.krad.service.impl.KualiModuleServiceImpl;
42  import org.springframework.beans.BeansException;
43  import org.springframework.context.ApplicationContext;
44  import org.springframework.context.ApplicationContextAware;
45  import org.springframework.context.support.ClassPathXmlApplicationContext;
46  
47  import javax.xml.namespace.QName;
48  import java.util.Map;
49  
50  import static org.junit.Assert.assertEquals;
51  import static org.junit.Assert.assertNotNull;
52  import static org.junit.Assert.assertNull;
53  
54  public class TestBaseValidCharsDictionary {
55  
56      @Before
57      public void setUp(){
58          Config config = new JAXBConfigImpl();
59         config.putProperty(CoreConstants.Config.APPLICATION_ID, "test");
60         ConfigContext.init(config);
61  
62         ResourceLoader resourceLoader =
63                 new BaseResourceLoader(
64                         new QName("test", RiceConstants.DEFAULT_ROOT_RESOURCE_LOADER_NAME), new SimpleSpringResourceLoader());
65  
66         try {
67             GlobalResourceLoader.stop();
68             GlobalResourceLoader.addResourceLoader(resourceLoader);
69             GlobalResourceLoader.start();
70         } catch (Exception e) {
71             throw new RuntimeException("Error initializing GRL", e);
72         }
73      }
74  
75      private String test(ValidCharactersConstraintProcessor vccp, ValidCharactersConstraint vcc, String value) {
76          DictionaryValidationResult dvr = new DictionaryValidationResult();
77          AttributeValueReader avr = new MockAttributeValueReader();
78          ProcessorResult result = vccp.process(dvr, value, vcc, avr);
79          for (ConstraintValidationResult cvr : result.getConstraintValidationResults()) {
80              if (! cvr.getStatus().equals(ErrorLevel.OK)) {
81                  return "error";
82              }
83          }
84          return null;
85      }
86  
87      @Test
88      public void testValidCharsConstraints() {
89  
90          System.out.println("testing base dictionary");
91          String contextFile = "ks-base-dictionary-validchars.xml";
92          ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:"
93                  + contextFile);
94          Map<String, ValidCharactersConstraint> vccs = (Map<String, ValidCharactersConstraint>) ac.getBeansOfType(
95                  ValidCharactersConstraint.class);
96          for (String id : vccs.keySet()) {
97              ValidCharactersConstraint vcc = vccs.get(id);
98              System.out.println("valid chars constraint: " + id + " "
99                      + vcc.getMessageKey() + " " + vcc.getValue());
100         }
101 
102         String id = null;
103         ValidCharactersConstraint vc = null;
104         ValidCharactersConstraintProcessor vccp = new ValidCharactersConstraintProcessor();
105 
106         id = "AlphanumericHyphenPeriod";
107         vc = vccs.get(id);
108         assertNotNull(vc);
109         assertNull(test(vccp, vc, "1"));
110         assertNull(test(vccp, vc, "A"));
111         assertNull(test(vccp, vc, "."));
112         assertNull(test(vccp, vc, "-"));
113         assertNotNull(test(vccp, vc, " "));
114         assertNotNull(test(vccp, vc, ","));
115         assertNotNull(test(vccp, vc, "\n"));
116         assertNotNull(test(vccp, vc, "\r"));
117         assertNotNull(test(vccp, vc, "\t"));
118 
119         id = "Digits";
120         vc = vccs.get(id);
121         assertNotNull(vc);
122         assertNull(test(vccp, vc, "1"));
123         assertNotNull(test(vccp, vc, "A"));
124         assertNotNull(test(vccp, vc, "."));
125         assertNotNull(test(vccp, vc, "-"));
126         assertNotNull(test(vccp, vc, " "));
127         assertNotNull(test(vccp, vc, ","));
128         assertNotNull(test(vccp, vc, "\n"));
129         assertNotNull(test(vccp, vc, "\r"));
130         assertNotNull(test(vccp, vc, "\t"));
131 
132         id = "Numeric";
133         vc = vccs.get(id);
134         assertNotNull(vc);
135         assertNull(test(vccp, vc, "0"));
136         assertNull(test(vccp, vc, "1"));
137         assertNull(test(vccp, vc, "20"));
138         assertNull(test(vccp, vc, "-20"));
139         assertNull(test(vccp, vc, "-12345620"));
140         assertNull(test(vccp, vc, "20.1"));
141         assertNull(test(vccp, vc, "20.0"));
142         assertNull(test(vccp, vc, "20.01"));
143         assertNull(test(vccp, vc, "20.00"));
144         assertNull(test(vccp, vc, "120.00"));
145         assertNull(test(vccp, vc, "1120.00"));
146         assertNull(test(vccp, vc, "11120.00"));
147         assertNull(test(vccp, vc, "111120.00"));
148         assertNull(test(vccp, vc, "1111120.00"));
149         assertNull(test(vccp, vc, "11111120.00"));
150         assertNotNull(test(vccp, vc, "020"));
151         assertNotNull(test(vccp, vc, "-020"));
152         assertNotNull(test(vccp, vc, "-020.1"));
153         assertNotNull(test(vccp, vc, "20.010"));
154         assertNotNull(test(vccp, vc, "20.011"));
155         assertNotNull(test(vccp, vc, "A"));
156         assertNotNull(test(vccp, vc, "."));
157         assertNotNull(test(vccp, vc, "-"));
158         assertNotNull(test(vccp, vc, " "));
159         assertNotNull(test(vccp, vc, ","));
160         assertNotNull(test(vccp, vc, "\n"));
161         assertNotNull(test(vccp, vc, "\r"));
162         assertNotNull(test(vccp, vc, "\t"));
163 
164         id = "Numeric.range";
165         vc = vccs.get(id);
166         assertNotNull(vc);
167         assertNull(test(vccp, vc, "0"));
168         assertNull(test(vccp, vc, "1"));
169         assertNull(test(vccp, vc, "20"));
170         assertNull(test(vccp, vc, "-20"));
171         assertNull(test(vccp, vc, "-12345620"));
172         assertNull(test(vccp, vc, "20.1"));
173         assertNull(test(vccp, vc, "20.0"));
174         assertNull(test(vccp, vc, "20.01"));
175         assertNull(test(vccp, vc, "20.00"));
176         assertNull(test(vccp, vc, "120.00"));
177         assertNull(test(vccp, vc, "1120.00"));
178         assertNull(test(vccp, vc, "11120.00"));
179         assertNull(test(vccp, vc, "111120.00"));
180         assertNull(test(vccp, vc, "1111120.00"));
181         assertNull(test(vccp, vc, "11111120.00"));
182         assertNull(test(vccp, vc, "1-2"));
183         assertNull(test(vccp, vc, "1.1-2.2"));
184         assertNull(test(vccp, vc, "1 - 2"));
185         assertNull(test(vccp, vc, "1--2"));
186         assertNull(test(vccp, vc, "1 - -2"));
187         assertNull(test(vccp, vc, "-1 - -2"));
188         assertNotNull(test(vccp, vc, "1 -2"));
189         assertNotNull(test(vccp, vc, "1 - 2 - 3"));
190         assertNotNull(test(vccp, vc, "1- 2"));
191         assertNotNull(test(vccp, vc, "020"));
192         assertNotNull(test(vccp, vc, "-020"));
193         assertNotNull(test(vccp, vc, "-020.1"));
194         assertNotNull(test(vccp, vc, "20.010"));
195         assertNotNull(test(vccp, vc, "20.011"));
196         assertNotNull(test(vccp, vc, "A"));
197         assertNotNull(test(vccp, vc, "."));
198         assertNotNull(test(vccp, vc, "-"));
199         assertNotNull(test(vccp, vc, " "));
200         assertNotNull(test(vccp, vc, ","));
201         assertNotNull(test(vccp, vc, "\n"));
202         assertNotNull(test(vccp, vc, "\r"));
203         assertNotNull(test(vccp, vc, "\t"));
204 
205         id = "Positive.numeric.range";
206         vc = vccs.get(id);
207         assertNotNull(vc);
208         assertNull(test(vccp, vc, "0"));
209         assertNull(test(vccp, vc, "1"));
210         assertNull(test(vccp, vc, "20"));
211         assertNotNull(test(vccp, vc, "-20"));
212         assertNotNull(test(vccp, vc, "-12345620"));
213         assertNull(test(vccp, vc, "20.1"));
214         assertNull(test(vccp, vc, "20.0"));
215         assertNull(test(vccp, vc, "20.01"));
216         assertNull(test(vccp, vc, "20.00"));
217         assertNull(test(vccp, vc, "120.00"));
218         assertNull(test(vccp, vc, "1120.00"));
219         assertNull(test(vccp, vc, "11120.00"));
220         assertNull(test(vccp, vc, "111120.00"));
221         assertNull(test(vccp, vc, "1111120.00"));
222         assertNull(test(vccp, vc, "11111120.00"));
223         assertNull(test(vccp, vc, "1-2"));
224         assertNull(test(vccp, vc, "1.1-2.2"));
225         assertNull(test(vccp, vc, "1 - 2"));
226         assertNotNull(test(vccp, vc, "1--2"));
227         assertNotNull(test(vccp, vc, "1 - -2"));
228         assertNotNull(test(vccp, vc, "-1 - -2"));
229         assertNotNull(test(vccp, vc, "1 -2"));
230         assertNotNull(test(vccp, vc, "1 - 2 - 3"));
231         assertNotNull(test(vccp, vc, "1- 2"));
232         assertNotNull(test(vccp, vc, "020"));
233         assertNotNull(test(vccp, vc, "-020"));
234         assertNotNull(test(vccp, vc, "-020.1"));
235         assertNotNull(test(vccp, vc, "20.010"));
236         assertNotNull(test(vccp, vc, "20.011"));
237         assertNotNull(test(vccp, vc, "A"));
238         assertNotNull(test(vccp, vc, "."));
239         assertNotNull(test(vccp, vc, "-"));
240         assertNotNull(test(vccp, vc, " "));
241         assertNotNull(test(vccp, vc, ","));
242         assertNotNull(test(vccp, vc, "\n"));
243         assertNotNull(test(vccp, vc, "\r"));
244         assertNotNull(test(vccp, vc, "\t"));
245 
246         id = "Gpa4.0";
247         vc = vccs.get(id);
248         assertNotNull(vc);
249         assertNull(test(vccp, vc, "0.0"));
250         assertNull(test(vccp, vc, "4.0"));
251         assertNull (test(vccp, vc, "4.1"));
252         assertNotNull(test(vccp, vc, "1"));
253         assertNull(test(vccp, vc, "1.0"));
254         assertNull(test(vccp, vc, "0.1"));
255         assertNotNull(test(vccp, vc, "\n"));
256         assertNotNull(test(vccp, vc, "\r"));
257         assertNotNull(test(vccp, vc, "\t"));
258 
259         id = "Positive.integer";
260         vc = vccs.get(id);
261         assertNotNull(vc);
262         assertNull(test(vccp, vc, "1"));
263         assertNull(test(vccp, vc, "22"));
264         assertNull(test(vccp, vc, "33"));
265         assertNotNull(test(vccp, vc, "0"));
266         assertNotNull(test(vccp, vc, "1.0"));
267         assertNotNull(test(vccp, vc, "-1"));
268         assertNotNull(test(vccp, vc, "\n"));
269         assertNotNull(test(vccp, vc, "\r"));
270         assertNotNull(test(vccp, vc, "\t"));
271 
272         id = "RelationalOperator";
273         vc = vccs.get(id);
274         assertNotNull(vc);
275         assertNull(test(vccp, vc, "="));
276         assertNull(test(vccp, vc, ">"));
277         assertNull(test(vccp, vc, "<"));
278         assertNull(test(vccp, vc, ">="));
279         assertNull(test(vccp, vc, "<="));
280         assertNull(test(vccp, vc, "<>"));
281         assertNotNull(test(vccp, vc, "!="));
282         assertNotNull(test(vccp, vc, "=>"));
283         assertNotNull(test(vccp, vc, "=<"));
284         assertNotNull(test(vccp, vc, "0"));
285         assertNotNull(test(vccp, vc, "1.0"));
286         assertNotNull(test(vccp, vc, "-1"));
287         assertNotNull(test(vccp, vc, "\n"));
288         assertNotNull(test(vccp, vc, "\r"));
289         assertNotNull(test(vccp, vc, "\t"));
290 
291         id = "Alpha";
292         vc = vccs.get(id);
293         assertNotNull(vc);
294         assertNull(test(vccp, vc, "a"));
295         assertNull(test(vccp, vc, "A"));
296         assertNull(test(vccp, vc, "Z"));
297         assertNull(test(vccp, vc, "zzzzz"));
298         assertNull(test(vccp, vc,
299                 "abcdefghijklmnopqrstuvwxyz"));
300         assertNotNull(test(vccp, vc, "1"));
301         assertNotNull(test(vccp, vc, "0"));
302         assertNotNull(test(vccp, vc, "X\n"));
303         assertNotNull(test(vccp, vc, "X\r"));
304         assertNotNull(test(vccp, vc, "X\t"));
305 
306         id = "UpperCase";
307         vc = vccs.get(id);
308         assertNotNull(vc);
309         assertNull(test(vccp, vc, "AB"));
310         assertNull(test(vccp, vc, "A"));
311         assertNull(test(vccp, vc,
312                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
313         assertNull(test(vccp, vc, "-"));
314         assertNull(test(vccp, vc, "."));
315         assertNull(test(vccp, vc, "1"));
316         assertNull(test(vccp, vc, "0"));
317         assertNotNull(test(vccp, vc, "a"));
318         assertNotNull(test(vccp, vc, "X\n"));
319         assertNotNull(test(vccp, vc, "X\r"));
320         assertNotNull(test(vccp, vc, "X\t"));
321 
322         id = "UpperAlpha";
323         vc = vccs.get(id);
324         assertNotNull(vc);
325         assertNull(test(vccp, vc, "AB"));
326         assertNull(test(vccp, vc, "A"));
327         assertNull(test(vccp, vc, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
328         assertNotNull(test(vccp, vc, "a"));
329         assertNotNull(test(vccp, vc, "1"));
330         assertNotNull(test(vccp, vc, "0"));
331         assertNotNull(test(vccp, vc, "X\n"));
332         assertNotNull(test(vccp, vc, "X\r"));
333         assertNotNull(test(vccp, vc, "X\t"));
334 
335         id = "LineText";
336         //AnyCharacter constraint allows only Visible characters (i.e. anything except spaces, control characters, etc.)
337         //http://www.regular-expressions.info/posixbrackets.html - [\x21-\x7E]
338         //http://www.asciitable.com/
339         vc = vccs.get(id);
340         assertNotNull(vc);
341         assertNull(test(vccp, vc, "a"));
342         assertNull(test(vccp, vc, "A"));
343         assertNull(test(vccp, vc, "Z"));
344         assertNull(test(vccp, vc, "Z"));
345         assertNull(test(vccp, vc, "!"));
346         assertNull(test(vccp, vc, "@"));
347         assertNull(test(vccp, vc, "#"));
348         assertNull(test(vccp, vc, "$"));
349         assertNull(test(vccp, vc, "%"));
350         assertNull(test(vccp, vc, "&"));
351         assertNull(test(vccp, vc, "*"));
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         assertNull(test(vccp, vc, "="));
358         assertNull(test(vccp, vc, "{"));
359         assertNull(test(vccp, vc, "}"));
360         assertNull(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         assertNull(test(vccp, vc, "<"));
368         assertNull(test(vccp, vc, ">"));
369         assertNull(test(vccp, vc, "?"));
370         assertNull(test(vccp, vc, "~"));
371         assertNull(test(vccp, vc, "`"));
372         assertNull(test(vccp, vc, "zzzzz"));
373         assertNull(test(vccp, vc,"abcdefghijklmnopqrstuvwxyz"));
374         assertNull(test(vccp, vc,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
375         assertNull(test(vccp, vc, "1"));
376         assertNull(test(vccp, vc, "0"));
377         assertNull(test(vccp, vc, "X\n"));
378         assertNull(test(vccp, vc, "X\r"));
379         assertNull(test(vccp, vc, "X\t"));
380 
381         id = "MultiLineText";
382         vc = vccs.get(id);
383         assertNotNull(vc);
384         assertNull(test(vccp, vc, "a"));
385         assertNull(test(vccp, vc, "A"));
386         assertNull(test(vccp, vc, "Z"));
387         assertNull(test(vccp, vc, "Z"));
388         assertNull(test(vccp, vc, "!"));
389         assertNull(test(vccp, vc, "@"));
390         assertNull(test(vccp, vc, "#"));
391         assertNull(test(vccp, vc, "$"));
392         assertNull(test(vccp, vc, "%"));
393         assertNull(test(vccp, vc, "&"));
394         assertNull(test(vccp, vc, "*"));
395         assertNull(test(vccp, vc, "("));
396         assertNull(test(vccp, vc, ")"));
397         assertNull(test(vccp, vc, "_"));
398         assertNull(test(vccp, vc, "+"));
399         assertNull(test(vccp, vc, "-"));
400         assertNull(test(vccp, vc, "="));
401         assertNull(test(vccp, vc, "{"));
402         assertNull(test(vccp, vc, "}"));
403         assertNull(test(vccp, vc, "["));
404         assertNull(test(vccp, vc, "]"));
405         assertNull(test(vccp, vc, "|"));
406         assertNull(test(vccp, vc, "\\"));
407         assertNull(test(vccp, vc, ","));
408         assertNull(test(vccp, vc, "."));
409         assertNull(test(vccp, vc, "/"));
410         assertNull(test(vccp, vc, "<"));
411         assertNull(test(vccp, vc, ">"));
412         assertNull(test(vccp, vc, "?"));
413         assertNull(test(vccp, vc, "~"));
414         assertNull(test(vccp, vc, "`"));
415         assertNull(test(vccp, vc, "zzzzz"));
416         assertNull(test(vccp, vc,"abcdefghijklmnopqrstuvwxyz"));
417         assertNull(test(vccp, vc,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
418         assertNull(test(vccp, vc, "1"));
419         assertNull(test(vccp, vc, "0"));
420         assertNull(test(vccp, vc, "X\n"));
421         assertNull(test(vccp, vc, "X\r"));
422         assertNull(test(vccp, vc, "X\t"));
423 
424         // If you get this error -- don't just change the number
425         // also add a unit test for new valid chars definition that you must have added into the base dictionary!
426         assertEquals(14, vccs.size());
427     }
428 }
429 
430 /**
431  * Copied from ks-core-test.. As this test class is in ks-enroll-api and it doesnt make sense to make ks-enroll-api depends on
432  * ks-core-test, just copied this class from there to make it simple. Also, in ks-enroll-api, this is the only class
433  * uses the resource loader.
434  */
435 class SimpleSpringResourceLoader implements ApplicationContextAware, ServiceLocator {
436 
437     private static ConfigurationService configurationService = new ConfigurationService() {
438         @Override public String getPropertyValueAsString(String key) { return "{0} message"; }
439         @Override public boolean getPropertyValueAsBoolean(String key) { return false; }
440         @Override public Map<String, String> getAllProperties() { return null; }
441     };
442 
443     private static KualiModuleService kualiModuleService = new KualiModuleServiceImpl();
444 
445     private ApplicationContext applicationContext;
446 
447     public Object getService(QName qname) {
448         if (qname == null || StringUtils.isEmpty(qname.toString())) {
449             throw new IllegalArgumentException("The service name must be non-null.");
450         }
451 
452         String localServiceName = qname.toString();
453 
454         if (KRADServiceLocator.KUALI_CONFIGURATION_SERVICE.equals(localServiceName)) {
455             return configurationService;
456         } else if (KRADServiceLocatorWeb.KUALI_MODULE_SERVICE.equals(localServiceName)) {
457             return kualiModuleService;
458         } else {
459             return applicationContext.getBean(localServiceName);
460         }
461     }
462 
463     @Override
464     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
465         this.applicationContext = applicationContext;
466     }
467 
468     @Override
469     public String getContents(String indent, boolean servicePerLine) {
470         return null;
471     }
472 
473     @Override
474     public void start() throws Exception {
475     }
476 
477     @Override
478     public void stop() throws Exception {
479     }
480 
481     @Override
482     public boolean isStarted() {
483         return false;
484     }
485 }
486