View Javadoc

1   /**
2    * Copyright 2005-2012 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.constraint;
17  
18  
19  
20  import java.util.ArrayList;
21  import java.util.List;
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.core.api.CoreConstants;
28  import org.kuali.rice.core.api.config.property.Config;
29  import org.kuali.rice.core.api.config.property.ConfigContext;
30  import org.kuali.rice.core.framework.config.property.SimpleConfig;
31  import org.kuali.rice.core.framework.config.property.SimpleConfig;
32  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
33  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
34  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
35  import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
36  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
37  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
38  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
39  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
40  
41  
42  /**
43   * Things this test should check:
44   * 
45   * 1. empty value check. (failure) {@link #testValueInvalidDateEmpty()}
46   * 2. value with valid date. (success) {@link #testValueValidDate()}
47   * 3. value with valid date. (success) {@link #testValueValidDate1()}
48   * 4. value with invalid date. (failure) {@link testValueInvalidDate()}
49   * 5. value with invalid date. (failure) {@link testValueInvalidDate1()}
50   * 6. value with invalid date. (failure) {@link testValueInvalidDate2()}
51   * 7. value with invalid date. (failure) {@link testValueInvalidDate3()}  
52   * 
53   * @author Kuali Rice Team (rice.collab@kuali.org) 
54   */
55  public class DatePatternConstraintTest {
56  	
57  	private AttributeDefinition dateDefinition;
58  	
59  	private DictionaryValidationResult dictionaryValidationResult;
60  	
61  	private ValidCharactersConstraintProcessor processor;
62  	
63  	private String validDate;
64  	private String validDate1;
65  	private String invalidDateEmpty;
66  	private String invalidDate;
67  	private String invalidDate1;
68  	private String invalidDate2;
69  	private String invalidDate3;
70  		
71  	private DatePatternConstraint datePatternConstraint;		
72  	
73  	@Before
74  	public void setUp() throws Exception {
75  		
76  		String formats = "MM/dd/yy;MM/dd/yyyy;MM-dd-yy;MM-dd-yyyy"; 
77  		
78  		Properties props = new Properties();
79  		props.put(CoreConstants.STRING_TO_DATE_FORMATS, formats);
80  		Config config = new SimpleConfig(props);
81  		
82  		ConfigContext.init(config);
83  		ConfigContext.getCurrentContextConfig().putProperty(CoreConstants.STRING_TO_DATE_FORMATS, formats);
84  		
85  		processor = new ValidCharactersConstraintProcessor();
86  				
87  		dictionaryValidationResult = new DictionaryValidationResult();
88  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
89  		
90  		validDate = "07-28-2011";
91  		validDate1 = "12-31-83";				
92  		invalidDateEmpty = "";
93  		invalidDate = "31-12-2010";
94  		invalidDate1 = "31-12-2010 12:30:45.456";
95  		invalidDate2 = "2011-07-28 IST";
96  		invalidDate3 = "12/31/2011";					        
97  					
98  		List<String> allowedFormats = new ArrayList<String>();
99  		allowedFormats.add("MM-dd-yy");
100 		allowedFormats.add("MM-dd-yyyy");
101 		
102 		datePatternConstraint = new DatePatternConstraint();
103 		datePatternConstraint.setAllowedFormats(allowedFormats);
104 
105 		dateDefinition = new AttributeDefinition();
106 		dateDefinition.setName("date");
107 		dateDefinition.setValidCharactersConstraint(datePatternConstraint);												
108 	}
109 	
110 	@Test
111 	public void testValueInvalidDateEmpty() {
112 		ConstraintValidationResult result = process(invalidDateEmpty, "date", datePatternConstraint);
113 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
114 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
115 		Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
116 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
117 	}
118 	
119 	@Test
120 	public void testValueValidDate() {
121 		ConstraintValidationResult result = process(validDate, "date", datePatternConstraint);
122 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
123 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
124 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
125 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
126 	}
127 	
128 	@Test
129 	public void testValueValidDate1() {
130 		ConstraintValidationResult result = process(validDate1, "Mdate", datePatternConstraint);
131 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
132 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
133 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
134 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
135 	}				
136 	
137 	@Test
138 	public void testValueInvalidDate() {
139 		ConstraintValidationResult result = process(invalidDate, "date", datePatternConstraint);
140 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
141 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
142 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
143 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
144 	}
145 	
146 	@Test
147 	public void testValueInvalidDate1() {
148 		ConstraintValidationResult result = process(invalidDate1, "date", datePatternConstraint);
149 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
150 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
151 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
152 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
153 	}
154 	
155 	@Test
156 	public void testValueInvalidDate2() {
157 		ConstraintValidationResult result = process(invalidDate2, "date", datePatternConstraint);
158 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
159 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
160 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
161 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
162 	}
163 	
164 	@Test
165 	public void testValueInvalidDate3() {
166 		ConstraintValidationResult result = process(invalidDate3, "date", datePatternConstraint);
167 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
168 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
169 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
170 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
171 	}		
172 	
173 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
174 		AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName,  dateDefinition);		
175 		Object value = attributeValueReader.getValue();
176 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
177 	}
178 }