1 7 8 package org.jdesktop.swing.data; 9 10 import java.awt.Color ; 11 12 import java.beans.PropertyChangeEvent ; 13 import java.beans.PropertyChangeListener ; 14 15 import java.text.DateFormat ; 16 import java.text.SimpleDateFormat ; 17 18 import java.util.ArrayList ; 19 import java.util.Locale ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 25 import org.jdesktop.swing.util.TestSupport; 26 27 32 public class MetaDataUnitTest extends TestCase { 33 34 public static void verifyMetaDataProperties(MetaData metaData, 35 String name, Class klass, 36 String label, 37 int minValueCount, int maxValueCount, 38 boolean readonly, 39 Converter converter, 40 Object decodeFormat, Object encodeFormat) { 41 42 assertEquals(klass, metaData.getElementClass()); 43 assertEquals(name, metaData.getName()); 44 assertEquals(label, metaData.getLabel()); 45 assertEquals(minValueCount, metaData.getMinValueCount()); 46 assertEquals(maxValueCount, metaData.getMaxValueCount()); 47 assertEquals(readonly, metaData.isReadOnly()); 48 assertEquals(converter, metaData.getConverter()); 49 assertEquals(decodeFormat, metaData.getDecodeFormat()); 50 assertEquals(encodeFormat, metaData.getEncodeFormat()); 51 } 52 53 public static void verifyStringMetaDataProperties(StringMetaData metaData, 54 int minLength, int maxLength, 55 boolean multiLine) { 56 57 assertEquals(minLength, metaData.getMinLength()); 58 assertEquals(maxLength, metaData.getMaxLength()); 59 assertEquals(multiLine, metaData.isMultiLine()); 60 } 61 62 public static void verifyNumberMetaDataProperties(NumberMetaData metaData, 63 Number theMinimum, Number theMaximum, 64 boolean theCurrency) { 65 Number minimum = metaData.getMinimum(); 66 Number maximum = metaData.getMaximum(); 67 boolean currency = metaData.isCurrency(); 68 69 assertEquals(theMinimum, minimum); 70 assertEquals(theMaximum, maximum); 71 assertEquals(theCurrency, currency); 72 } 73 74 public static void verifyEnumeratedMetaDataProperties(EnumeratedMetaData metaData, 75 Object [] theEnumeration) { 76 Object enumeration[] = metaData.getEnumeration(); 77 assertEquals(theEnumeration.length, enumeration.length); 78 79 for(int i = 0; i < enumeration.length; i++) { 80 assertEquals(theEnumeration[i], enumeration[i]); 81 } 82 83 } 84 85 private static void verifyNumberValidation(NumberMetaData metaData, 86 Number min, Number mid, Number max, 87 Number tooSmall, Number tooBig) { 88 89 Validator validators[] = metaData.getValidators(); 90 String error[] = new String [1]; 91 for(int i = 0; i < validators.length; i++) { 92 boolean valid = validators[i].validate(min, null, error); 94 assertTrue(valid); 95 valid = validators[i].validate(mid, null, error); 96 assertTrue(valid); 97 valid = validators[i].validate(max, null, error); 98 assertTrue(valid); 99 100 valid = validators[i].validate(tooSmall, null, error); 102 assertFalse(valid); 103 valid = validators[i].validate(tooBig, null, error); 104 assertFalse(valid); 105 } 106 } 107 108 public void testMetaDataConstructors() { 109 110 MetaData metaData = new MetaData(); 111 verifyMetaDataProperties(metaData, "value", String .class, 112 "value", 0, 1, false, 113 Converters.get(String .class), null, null); 114 115 metaData = new MetaData("firstname"); 116 verifyMetaDataProperties(metaData, "firstname", String .class, 117 "firstname", 0, 1, false, 118 Converters.get(String .class), null, null); 119 120 metaData = new MetaData("zipcode", Integer .class); 121 verifyMetaDataProperties(metaData, "zipcode", Integer .class, 122 "zipcode", 0, 1, false, 123 Converters.get(Integer .class), null, null); 124 metaData.addPropertyChangeListener(new PropertyChangeListener () { 125 public void propertyChange(PropertyChangeEvent e) { 126 127 } 128 }); 129 130 131 metaData = new MetaData("phonenumber", String .class, "Phone Number"); 132 verifyMetaDataProperties(metaData, "phonenumber", String .class, "Phone Number", 133 0, 1, false, 134 Converters.get(String .class), null, null); 135 136 Validator validators[] = metaData.getValidators(); 137 assertEquals(0, validators.length); 138 139 } 140 141 public void testMetaDataProperties() { 142 MetaData metaData = new MetaData("zipcode"); 143 metaData.setElementClass(Integer .class); 144 metaData.setName("zip-code"); 145 metaData.setLabel("Zipcode"); 146 metaData.setMinValueCount(1); 147 metaData.setMaxValueCount(2); 148 metaData.setReadOnly(true); 149 Converter dummy = new DummyConverter(); 150 metaData.setConverter(dummy); 151 DateFormat format = new SimpleDateFormat (); 152 metaData.setDecodeFormat(format); 153 metaData.setEncodeFormat(format); 154 verifyMetaDataProperties(metaData, "zip-code", Integer .class, "Zipcode", 155 1, 2, true, 156 dummy, format, format); 157 158 metaData.setRequired(false); 159 assertEquals(0, metaData.getMinValueCount()); 160 161 metaData.setRequired(true); 162 assertEquals(1, metaData.getMinValueCount()); 163 164 } 165 166 public void testMetaDataValidator() { 167 MetaData metaData = new MetaData("lastname"); 168 169 Validator dummyV = new DummyValidator(); 170 metaData.addValidator(dummyV); 171 Validator validators[] = metaData.getValidators(); 172 assertEquals(validators[0], dummyV); 173 174 metaData.removeValidator(dummyV); 175 validators = metaData.getValidators(); 176 assertEquals(0, validators.length); 177 178 } 179 180 public void testStringMetaDataConstructors() { 181 StringMetaData metaData = new StringMetaData(); 182 verifyMetaDataProperties(metaData, "stringvalue", String .class, 183 "stringvalue", 0, 1, false, 184 Converters.get(String .class), null, null); 185 verifyStringMetaDataProperties(metaData, 0, Integer.MAX_VALUE, false); 186 187 metaData = new StringMetaData("lastname"); 188 verifyMetaDataProperties(metaData, "lastname", String .class, 189 "lastname", 0, 1, false, 190 Converters.get(String .class), null, null); 191 verifyStringMetaDataProperties(metaData, 0, Integer.MAX_VALUE, false); 192 193 metaData = new StringMetaData("firstname", "First Name"); 194 verifyMetaDataProperties(metaData, "firstname", String .class, 195 "First Name", 0, 1, false, 196 Converters.get(String .class), null, null); 197 verifyStringMetaDataProperties(metaData, 0, Integer.MAX_VALUE, false); 198 199 } 200 201 public void testStringMetaDataProperties() { 202 StringMetaData metaData = new StringMetaData(); 203 metaData.setName("comments"); 204 metaData.setDisplayWidth(18); 205 metaData.setMinLength(1); 206 metaData.setMaxLength(100); 207 metaData.setMultiLine(true); 208 verifyStringMetaDataProperties(metaData, 1, 100, true); 209 assertEquals(18, metaData.getDisplayWidth()); 210 } 211 212 public void testNumberMetaDataConstructors() { 213 NumberMetaData metaData = new NumberMetaData(); 214 verifyMetaDataProperties(metaData, "numbervalue", Integer .class, 215 "numbervalue", 0, 1, false, 216 Converters.get(Integer .class), null, null); 217 218 metaData = new NumberMetaData("age"); 219 verifyMetaDataProperties(metaData, "age", Integer .class, 220 "age", 0, 1, false, 221 Converters.get(Integer .class), null, null); 222 verifyNumberMetaDataProperties(metaData, null, null, false); 223 224 metaData = new NumberMetaData("interestrate", Float .class); 225 verifyMetaDataProperties(metaData, "interestrate", Float .class, 226 "interestrate", 0, 1, false, 227 Converters.get(Float .class), null, null); 228 verifyNumberMetaDataProperties(metaData, null, null, false); 229 230 metaData = new NumberMetaData("partnumber", Long .class, "Part Number"); 231 verifyMetaDataProperties(metaData, "partnumber", Long .class, "Part Number", 232 0, 1, false, 233 Converters.get(Long .class), null, null); 234 235 Validator validators[] = metaData.getValidators(); 236 assertEquals(0, validators.length); 237 238 } 239 240 public void testNumberMetaDataProperties() { 241 NumberMetaData metaData = new NumberMetaData("price"); 242 metaData.setElementClass(Float .class); 243 metaData.setMinimum(new Float (0.99)); 244 metaData.setMaximum(new Float (99.99)); 245 metaData.setCurrency(true); 246 verifyNumberMetaDataProperties(metaData, new Float (0.99), new Float (99.99), 247 true); 248 249 Validator validators[] = metaData.getValidators(); 250 assertEquals(1, validators.length); 251 } 252 253 public void testNumberMetaDataValidation() { 254 NumberMetaData metaData = new NumberMetaData("quantity"); 255 metaData.setMinimum(new Integer (0)); 256 metaData.setMaximum(new Integer (10)); 257 verifyNumberValidation(metaData, new Integer (0), new Integer (3), 258 new Integer (10), new Integer (-1), 259 new Integer (20)); 260 261 metaData = new NumberMetaData("age", Short .class); 262 metaData.setMinimum(new Short ((short)18)); 263 metaData.setMaximum(new Short ((short)120)); 264 verifyNumberValidation(metaData, new Short ((short)18), new Short ((short)39), 265 new Short ((short)120), new Short ((short)5), 266 new Short ((short)220)); 267 268 metaData = new NumberMetaData("count", Long .class); 269 metaData.setMinimum(new Long (100)); 270 metaData.setMaximum(new Long (1000000)); 271 verifyNumberValidation(metaData, new Long (100), new Long (111111), 272 new Long (1000000), new Long (10), 273 new Long (1000001)); 274 275 metaData = new NumberMetaData("price", Float .class); 276 metaData.setMinimum(new Float (1.00)); 277 metaData.setMaximum(new Float (100.00)); 278 metaData.setCurrency(true); 279 verifyNumberValidation(metaData, new Float (1.00), new Float (33.3), 280 new Float (100.00), new Float (-1.1), new Float (101.1)); 281 282 metaData = new NumberMetaData("molecules", Double .class); 283 metaData.setMinimum(new Double (0.0)); 284 metaData.setMaximum(new Double (839329392339.234)); 285 verifyNumberValidation(metaData, new Double (0.0), new Double (3333333.3), 286 new Double (839329392339.234), new Double (-323.34), 287 new Double (99393999939393.32)); 288 } 289 290 public void testEnumeratedMetaDataConstructors() { 291 EnumeratedMetaData metaData = new EnumeratedMetaData(); 292 verifyMetaDataProperties(metaData, "enumvalue", String .class, 293 "enumvalue", 0, 1, false, 294 Converters.get(String .class), null, null); 295 296 metaData = new EnumeratedMetaData("months"); 297 verifyMetaDataProperties(metaData, "months", String .class, 298 "months",0, 1, false, 299 Converters.get(String .class), null, null); 300 verifyEnumeratedMetaDataProperties(metaData, new Object [0]); 301 302 metaData = new EnumeratedMetaData("background", Color .class); 303 verifyMetaDataProperties(metaData, "background", Color .class, 304 "background", 0, 1, false, 305 null, null, null); 306 verifyEnumeratedMetaDataProperties(metaData, new Object [0]); 307 308 metaData = new EnumeratedMetaData("month", String .class, "Month"); 309 verifyMetaDataProperties(metaData, "month", String .class, "Month", 310 0, 1, false, 311 Converters.get(String .class), null, null); 312 verifyEnumeratedMetaDataProperties(metaData, new Object [0]); 313 314 Validator validators[] = metaData.getValidators(); 315 assertEquals(0, validators.length); 316 317 } 318 319 public void testEnumeratedMetaDataProperties() { 320 String days[] = {"sunday", "monday", "tuesday", "wednesday", 321 "thursday", "friday", "saturday"}; 322 EnumeratedMetaData metaData = new EnumeratedMetaData("daysofweek"); 323 metaData.setEnumeration(days); 324 verifyEnumeratedMetaDataProperties(metaData, days); 325 326 ArrayList list = new ArrayList (); 327 for(int i=1; i < 6; i++) { 328 list.add(new String (""+i)); 329 } 330 String listcopy[] = (String [])list.toArray(new String [1]); 331 metaData = new EnumeratedMetaData("priority"); 332 metaData.setEnumeration(list); 333 verifyEnumeratedMetaDataProperties(metaData, listcopy); 334 } 335 336 public void testMetaDataCustomProperties() { 337 MetaData metaData = new MetaData("favoredcustomer"); 338 339 String keys[] = metaData.getCustomPropertyKeys(); 340 assertEquals(0, keys.length); 341 342 metaData.setCustomProperty("hidden", Boolean.TRUE); 344 assertEquals(Boolean.TRUE, metaData.getCustomProperty("hidden")); 345 assertEquals(1, metaData.getCustomPropertyKeys().length); 346 347 assertNull(metaData.getCustomProperty("missing")); 348 assertEquals(Boolean.TRUE, metaData.getCustomProperty("missing", Boolean.TRUE)); 349 350 TestSupport.PropertyChangeTracker tracker = 352 new TestSupport.PropertyChangeTracker(); 353 metaData.addPropertyChangeListener(tracker); 354 355 metaData.setCustomProperty("hidden", Boolean.FALSE); 356 assertTrue(tracker.gotPropertyChangeEvent("hidden")); 357 assertEquals(Boolean.FALSE, tracker.getNewPropertyValue("hidden")); 358 359 metaData.removeCustomProperty("hidden"); 361 assertNull(metaData.getCustomProperty("hidden")); 362 } 363 364 public void testMetaDataPropertyChangeEvents() { 365 MetaData metaData = new MetaData("favcolor"); 366 367 TestSupport.PropertyChangeTracker tracker = 368 new TestSupport.PropertyChangeTracker(); 369 metaData.addPropertyChangeListener(tracker); 370 371 metaData.setElementClass(Color .class); 372 assertTrue(tracker.gotPropertyChangeEvent("elementClass")); 373 assertEquals(Color .class, tracker.getNewPropertyValue("elementClass")); 374 375 metaData.setName("favoriteColor"); 376 assertTrue(tracker.gotPropertyChangeEvent("name")); 377 assertEquals("favoriteColor", tracker.getNewPropertyValue("name")); 378 379 metaData.setLabel("Favorite Color"); 380 assertTrue(tracker.gotPropertyChangeEvent("label")); 381 assertEquals("Favorite Color", tracker.getNewPropertyValue("label")); 382 383 metaData.setDisplayWidth(16); 384 assertTrue(tracker.gotPropertyChangeEvent("displayWidth")); 385 assertEquals(16, ((Integer )tracker.getNewPropertyValue("displayWidth")).intValue()); 386 387 metaData.setMinValueCount(1); 388 assertTrue(tracker.gotPropertyChangeEvent("minValueCount")); 389 assertEquals(new Integer (1), tracker.getNewPropertyValue("minValueCount")); 390 391 metaData.setMaxValueCount(5); 392 assertTrue(tracker.gotPropertyChangeEvent("maxValueCount")); 393 assertEquals(new Integer (5), tracker.getNewPropertyValue("maxValueCount")); 394 395 metaData.setReadOnly(true); 396 assertTrue(tracker.gotPropertyChangeEvent("readOnly")); 397 assertEquals(Boolean.TRUE, tracker.getNewPropertyValue("readOnly")); 398 399 Converter dummy = new DummyConverter(); 400 metaData.setConverter(dummy); 401 assertTrue(tracker.gotPropertyChangeEvent("converter")); 402 assertEquals(dummy, tracker.getNewPropertyValue("converter")); 403 404 DateFormat format = new SimpleDateFormat (); 405 metaData.setDecodeFormat(format); 406 assertTrue(tracker.gotPropertyChangeEvent("decodeFormat")); 407 assertEquals(format, tracker.getNewPropertyValue("decodeFormat")); 408 409 metaData.setEncodeFormat(format); 410 assertTrue(tracker.gotPropertyChangeEvent("encodeFormat")); 411 assertEquals(format, tracker.getNewPropertyValue("encodeFormat")); 412 413 metaData.setCustomProperty("silly", "hahaha"); 414 assertTrue(tracker.gotPropertyChangeEvent("silly")); 415 assertNull(tracker.getOldPropertyValue("silly")); 416 assertEquals("hahaha", tracker.getNewPropertyValue("silly")); 417 tracker.clear(); 418 metaData.setCustomProperty("silly", "heeheehee"); 419 assertTrue(tracker.gotPropertyChangeEvent("silly")); 420 assertEquals("hahaha", tracker.getOldPropertyValue("silly")); 421 assertEquals("heeheehee", tracker.getNewPropertyValue("silly")); 422 tracker.clear(); 423 metaData.removeCustomProperty("silly"); 424 assertTrue(tracker.gotPropertyChangeEvent("silly")); 425 assertNull(tracker.getNewPropertyValue("silly")); 426 assertEquals("heeheehee", tracker.getOldPropertyValue("silly")); 427 428 } 429 430 public class DummyConverter implements Converter { 431 public Object decode(String value, Object format) 432 throws ConversionException { 433 return null; 434 } 435 public String encode(Object value, Object format) 436 throws ConversionException { 437 return null; 438 } 439 } 440 441 public class DummyValidator implements Validator { 442 public boolean validate(Object value, Locale locale, String [] error) { 443 return false; 444 } 445 } 446 447 public static void main(String args[]) { 448 MetaDataUnitTest test = new MetaDataUnitTest(); 449 try { 450 test.setUp(); 451 } catch (Exception e) { 452 453 } 454 test.testEnumeratedMetaDataConstructors(); 455 test.testEnumeratedMetaDataProperties(); 456 test.testMetaDataConstructors(); 457 test.testMetaDataProperties(); 458 test.testMetaDataValidator(); 459 test.testNumberMetaDataConstructors(); 460 test.testNumberMetaDataProperties(); 461 test.testNumberMetaDataValidation(); 462 test.testStringMetaDataConstructors(); 463 test.testStringMetaDataProperties(); 464 test.testMetaDataCustomProperties(); 465 test.testMetaDataPropertyChangeEvents(); 466 467 } 468 } 469 | Popular Tags |