View Javadoc

1   /*
2    * Copyright 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/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.rice.krad.datadictionary.validation.constraint;
17  
18  
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Properties;
23  
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
28  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
29  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
30  import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
31  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
32  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
33  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
34  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
35  
36  
37  /**
38   * Things this test should check:
39   * 
40   * 1. empty value check. (failure) {@link #testValueInvalidMonthEmpty()}
41   * 2. value with valid month. (success) {@link #testValueValidMonth()}
42   * 3. value with valid month. (success) {@link #testValueValidMonth1()}
43   * 4. value with valid month. (success) {@link #testValueValidMonth2()}
44   * 5. value with invalid month. (failure) {@link testValueInvalidMonth()}
45   * 6. value with invalid month. (failure) {@link testValueInvalidMonth1()}
46   * 7. value with invalid month. (failure) {@link testValueInvalidMonth2()}
47   * 8. value with invalid month. (failure) {@link testValueInvalidMonth3()}  
48   * 
49   * @author Kuali Rice Team (rice.collab@kuali.org) 
50   */
51  public class MonthPatternConstraintTest {
52  	
53  	private final String PATTERN_CONSTRAINT = "validationPatternRegex.month";
54  
55  	private AttributeDefinition monthDefinition;
56  	
57  	private DictionaryValidationResult dictionaryValidationResult;
58  	
59  	private ValidCharactersConstraintProcessor processor;
60  	
61  	private String validMonth;
62  	private String validMonth1;
63  	private String validMonth2;
64  	private String invalidMonthEmpty;
65  	private String invalidMonth;
66  	private String invalidMonth1;
67  	private String invalidMonth2;
68  	private String invalidMonth3;
69  		
70  	private ConfigurationBasedRegexPatternConstraint monthPatternConstraint;		
71  	
72  	@Before
73  	public void setUp() throws Exception {
74  		
75  		String regex = getProperty(PATTERN_CONSTRAINT);
76  		
77  		processor = new ValidCharactersConstraintProcessor();
78  		
79  		dictionaryValidationResult = new DictionaryValidationResult();
80  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
81  		
82  		validMonth = "1";
83  		validMonth1 = "05";
84  		validMonth2 = "12"; 					
85  		invalidMonthEmpty = "";
86  		invalidMonth = "00";
87  		invalidMonth1 = "0";
88  		invalidMonth2 = "13";
89  		invalidMonth3 = "JAN";
90  					
91  		monthPatternConstraint = new ConfigurationBasedRegexPatternConstraint();
92  		monthPatternConstraint.setValue(regex);
93  
94  		monthDefinition = new AttributeDefinition();
95  		monthDefinition.setName("month");
96  		monthDefinition.setValidCharactersConstraint(monthPatternConstraint);												
97  	}
98  	
99  	@Test
100 	public void testValueInvalidMonthEmpty() {
101 		ConstraintValidationResult result = process(invalidMonthEmpty, "month", monthPatternConstraint);
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 testValueValidMonth() {
110 		ConstraintValidationResult result = process(validMonth, "month", monthPatternConstraint);
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 testValueValidMonth1() {
119 		ConstraintValidationResult result = process(validMonth1, "Mmonth", monthPatternConstraint);
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 testValueValidMonth2() {
128 		ConstraintValidationResult result = process(validMonth2, "month", monthPatternConstraint);
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 testValueInvalidMonth() {
137 		ConstraintValidationResult result = process(invalidMonth, "month", monthPatternConstraint);
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 testValueInvalidMonth1() {
146 		ConstraintValidationResult result = process(invalidMonth1, "month", monthPatternConstraint);
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 testValueInvalidMonth2() {
155 		ConstraintValidationResult result = process(invalidMonth2, "month", monthPatternConstraint);
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 testValueInvalidMonth3() {
164 		ConstraintValidationResult result = process(invalidMonth3, "month", monthPatternConstraint);
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,  monthDefinition);		
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 }