001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.datadictionary.validation.constraint;
017    
018    
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.util.Properties;
023    
024    import org.junit.Assert;
025    import org.junit.Before;
026    import org.junit.Test;
027    import org.kuali.rice.krad.datadictionary.AttributeDefinition;
028    import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
029    import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
030    import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
031    import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
032    import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
033    import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
034    import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
035    
036    
037    /**
038     * Things this test should check:
039     * 
040     * 1. empty value check. (failure) {@link #testValueInvalidJavaClassEmpty()}
041     * 2. value with valid java class. (success) {@link #testValueValidJavaClass()}
042     * 3. value with valid java class. (success) {@link #testValueValidJavaClass1()}
043     * 4. value with valid java class. (success) {@link #testValueValidJavaClass2()}
044     * 5. value with invalid java class. (failure) {@link #testValueInvalidJavaClass()}
045     * 6. value with invalid java class. (failure) {@link #testValueInvalidJavaClass1()}
046     * 7. value with invalid java class. (failure) {@link #testValueInvalidJavaClass2()}
047     * 8. value with invalid java class. (failure) {@link #testValueInvalidJavaClass3()}
048     * 
049     * @author Kuali Rice Team (rice.collab@kuali.org) 
050     */
051    public class JavaClassPatternConstraintTest {
052            
053            private final String PATTERN_CONSTRAINT = "validationPatternRegex.javaClass";
054    
055            private AttributeDefinition javaClassDefinition;
056            
057            private DictionaryValidationResult dictionaryValidationResult;
058            
059            private ValidCharactersConstraintProcessor processor;
060            
061            private String validJavaClass;
062            private String validJavaClass1;
063            private String validJavaClass2;
064            private String invalidJavaClassEmpty;
065            private String invalidJavaClass;
066            private String invalidJavaClass1;
067            private String invalidJavaClass2;
068            private String invalidJavaClass3;
069                    
070            private ConfigurationBasedRegexPatternConstraint javaClassPatternConstraint;            
071            
072            @Before
073            public void setUp() throws Exception {  
074                    
075                    String regex = getProperty(PATTERN_CONSTRAINT);
076                    
077                    processor = new ValidCharactersConstraintProcessor();
078                    
079                    dictionaryValidationResult = new DictionaryValidationResult();
080                    dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
081                    
082                    validJavaClass = Integer.class.getName();
083                    validJavaClass1 = "org.kuali.rice.krad.datadictionary.validation.constraint.JavaClassPattternConstraintTest";
084                    validJavaClass2 = "String";                                     
085                    invalidJavaClassEmpty = "";
086                    invalidJavaClass = "123.mypackage.com";
087                    invalidJavaClass1 = "mypackage.com.";
088                    invalidJavaClass2 = "123 mypackage";
089                    invalidJavaClass3 = "something.mypackage:com";
090                                            
091                    javaClassPatternConstraint = new ConfigurationBasedRegexPatternConstraint();
092                    javaClassPatternConstraint.setValue(regex);
093    
094                    javaClassDefinition = new AttributeDefinition();
095                    javaClassDefinition.setName("javaClass");
096                    javaClassDefinition.setValidCharactersConstraint(javaClassPatternConstraint);                                                                                           
097            }
098            
099            @Test
100            public void testValueInvalidJavaClassEmpty() {
101                    ConstraintValidationResult result = process(invalidJavaClassEmpty, "javaClass", javaClassPatternConstraint);
102                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
103                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
104                    Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
105                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
106            }
107            
108            @Test
109            public void testValueValidJavaClass() {
110                    ConstraintValidationResult result = process(validJavaClass, "javaClass", javaClassPatternConstraint);
111                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
112                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
113                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
114                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
115            }
116            
117            @Test
118            public void testValueValidJavaClass1() {
119                    ConstraintValidationResult result = process(validJavaClass1, "javaClass", javaClassPatternConstraint);
120                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
121                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
122                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
123                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
124            }
125            
126            @Test
127            public void testValueValidJavaClass2() {
128                    ConstraintValidationResult result = process(validJavaClass2, "javaClass", javaClassPatternConstraint);
129                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
130                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
131                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
132                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
133            }                               
134            
135            @Test
136            public void testValueInvalidJavaClass() {
137                    ConstraintValidationResult result = process(invalidJavaClass, "javaClass", javaClassPatternConstraint);
138                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
139                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
140                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
141                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
142            }
143            
144            @Test
145            public void testValueInvalidJavaClass1() {
146                    ConstraintValidationResult result = process(invalidJavaClass1, "javaClass", javaClassPatternConstraint);
147                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
148                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
149                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
150                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
151            }
152            
153            @Test
154            public void testValueInvalidJavaClass2() {
155                    ConstraintValidationResult result = process(invalidJavaClass2, "javaClass", javaClassPatternConstraint);
156                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
157                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
158                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
159                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
160            }
161            
162            @Test
163            public void testValueInvalidJavaClass3() {
164                    ConstraintValidationResult result = process(invalidJavaClass3, "javaClass", javaClassPatternConstraint);
165                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
166                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
167                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
168                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
169            }               
170            
171            private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
172                    AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName,  javaClassDefinition);            
173                    Object value = attributeValueReader.getValue();
174                    return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
175            }
176            
177            private String getProperty(String key) {
178                    String value = null;
179                    String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
180                    Properties properties = new Properties();
181                    try {                   
182                            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
183                            properties.load(in);
184                            value = properties.getProperty(key);                    
185                    } catch (IOException e) {               
186                    }
187                    return value;   
188            }
189    }