1 15 package org.apache.tapestry.valid; 16 17 import java.text.DateFormat ; 18 import java.util.Calendar ; 19 import java.util.Date ; 20 import java.util.GregorianCalendar ; 21 import java.util.Locale ; 22 23 import org.apache.tapestry.form.IFormComponent; 24 import org.apache.tapestry.valid.DateValidator; 25 import org.apache.tapestry.valid.ValidationConstraint; 26 import org.apache.tapestry.valid.ValidatorException; 27 28 34 35 public class TestDateValidator extends BaseValidatorTestCase 36 { 37 private Calendar calendar = new GregorianCalendar (); 38 39 private DateValidator v = new DateValidator(); 40 41 private Date buildDate(int month, int day, int year) 42 { 43 calendar.clear(); 44 45 calendar.set(Calendar.MONTH, month); 46 calendar.set(Calendar.DAY_OF_MONTH, day); 47 calendar.set(Calendar.YEAR, year); 48 49 return calendar.getTime(); 50 } 51 52 public void testToStringNull() 53 { 54 String out = v.toString(null, null); 55 56 assertNull(out); 57 } 58 59 public void testToStringValid() 60 { 61 String out = v.toString(null, buildDate(Calendar.DECEMBER, 8, 2001)); 62 63 assertEquals("Result.", "12/08/2001", out); 64 } 65 66 public void testToStringFormat() 67 { 68 if (IS_JDK13) 69 return; 70 71 DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); 72 73 v.setFormat(format); 74 75 String out = v.toString(null, buildDate(Calendar.DECEMBER, 8, 2001)); 76 77 assertEquals("Result.", "08.12.01", out); 78 } 79 80 public void testToObjectNull() throws ValidatorException 81 { 82 IFormComponent field = newField(); 83 84 replayControls(); 85 86 Object out = v.toObject(field, null); 87 88 assertNull(out); 89 90 verifyControls(); 91 } 92 93 public void testToObjectEmpty() throws ValidatorException 94 { 95 IFormComponent field = newField(); 96 97 replayControls(); 98 99 Object out = v.toObject(field, ""); 100 101 assertNull(out); 102 103 verifyControls(); 104 } 105 106 public void testToObjectInvalid() 107 { 108 IFormComponent field = newField("badDatesIndy"); 109 110 replayControls(); 111 112 try 113 { 114 v.toObject(field, "frankenhooker"); 115 116 unreachable(); 117 } 118 catch (ValidatorException ex) 119 { 120 assertEquals("Invalid date format for badDatesIndy. Format is MM/dd/yyyy.", ex 121 .getMessage()); 122 assertEquals(ValidationConstraint.DATE_FORMAT, ex.getConstraint()); 123 } 124 125 verifyControls(); 126 } 127 128 public void testOverrideInvalidDateFormatMessage() 129 { 130 131 IFormComponent field = newField("badDatesIndy"); 132 133 replayControls(); 134 135 v.setInvalidDateFormatMessage("Enter a valid date for {0}."); 136 137 try 138 { 139 v.toObject(field, "frankenhooker"); 140 141 unreachable(); 142 } 143 catch (ValidatorException ex) 144 { 145 assertEquals("Enter a valid date for badDatesIndy.", ex.getMessage()); 146 147 } 148 149 verifyControls(); 150 } 151 152 public void testToObjectFormat() throws ValidatorException 153 { 154 if (IS_JDK13) 155 return; 156 157 DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); 158 159 v.setFormat(format); 160 161 163 Object out = v.toObject(null, "08.12.01"); 164 165 assertEquals("Result.", buildDate(Calendar.DECEMBER, 8, 2001), out); 166 } 167 168 public void testToObjectMinimum() 169 { 170 IFormComponent field = newField("toObjectMinimum", Locale.ENGLISH); 171 172 replayControls(); 173 174 v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001)); 175 176 try 177 { 178 v.toObject(field, "12/8/2001"); 179 unreachable(); 180 } 181 catch (ValidatorException ex) 182 { 183 assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint()); 184 } 185 186 verifyControls(); 187 } 188 189 public void testOverrideDateTooEarlyMessage() 190 { 191 IFormComponent field = newField("inputDate", Locale.ENGLISH); 192 193 replayControls(); 194 195 v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001)); 196 v.setDateTooEarlyMessage("Provide a date for {0} after Dec 24 2001."); 197 198 try 199 { 200 v.toObject(field, "12/8/2001"); 201 unreachable(); 202 } 203 catch (ValidatorException ex) 204 { 205 assertEquals("Provide a date for inputDate after Dec 24 2001.", ex.getMessage()); 206 assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint()); 207 } 208 209 verifyControls(); 210 } 211 212 public void testToObjectMinimumNull() throws ValidatorException 213 { 214 v.setMinimum(buildDate(Calendar.DECEMBER, 24, 2001)); 215 216 Object out = v.toObject(null, null); 217 218 assertNull(out); 219 } 220 221 public void testToObjectMaximum() 222 { 223 IFormComponent field = newField("toObjectMaximum"); 224 225 replayControls(); 226 227 v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001)); 228 229 try 230 { 231 v.toObject(field, "12/8/2002"); 232 unreachable(); 233 } 234 catch (ValidatorException ex) 235 { 236 assertEquals("toObjectMaximum must be on or before 12/24/2001.", ex.getMessage()); 237 assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint()); 238 } 239 240 verifyControls(); 241 } 242 243 public void testOverrideDateTooLateMessage() 244 { 245 IFormComponent field = newField("toObjectMaximum"); 246 247 replayControls(); 248 249 v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001)); 250 v.setDateTooLateMessage("Try again with a date before Dec 24 2001 in {0}."); 251 252 try 253 { 254 v.toObject(field, "12/8/2002"); 255 unreachable(); 256 } 257 catch (ValidatorException ex) 258 { 259 assertEquals("Try again with a date before Dec 24 2001 in toObjectMaximum.", ex 260 .getMessage()); 261 } 262 263 verifyControls(); 264 } 265 266 public void testToObjectMaximumNull() throws ValidatorException 267 { 268 v.setMaximum(buildDate(Calendar.DECEMBER, 24, 2001)); 269 270 Object out = v.toObject(null, null); 271 272 assertNull(out); 273 } 274 } | Popular Tags |