KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > MetaDataUnitTest


1 /*
2  * $Id: MetaDataUnitTest.java,v 1.5 2004/09/10 21:54:48 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.awt.Color JavaDoc;
11
12 import java.beans.PropertyChangeEvent JavaDoc;
13 import java.beans.PropertyChangeListener JavaDoc;
14
15 import java.text.DateFormat JavaDoc;
16 import java.text.SimpleDateFormat JavaDoc;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Locale JavaDoc;
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 /**
28  * JUnit test class for meta data
29  *
30  * @author Amy Fowler
31  */

32 public class MetaDataUnitTest extends TestCase {
33
34     public static void verifyMetaDataProperties(MetaData metaData,
35                                           String JavaDoc name, Class JavaDoc klass,
36                                           String JavaDoc label,
37                                           int minValueCount, int maxValueCount,
38                                           boolean readonly,
39                                           Converter converter,
40                                           Object JavaDoc decodeFormat, Object JavaDoc 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 JavaDoc theMinimum, Number JavaDoc theMaximum,
64                                                 boolean theCurrency) {
65         Number JavaDoc minimum = metaData.getMinimum();
66         Number JavaDoc 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 JavaDoc[] theEnumeration) {
76         Object JavaDoc 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 JavaDoc min, Number JavaDoc mid, Number JavaDoc max,
87                                         Number JavaDoc tooSmall, Number JavaDoc tooBig) {
88
89          Validator validators[] = metaData.getValidators();
90          String JavaDoc error[] = new String JavaDoc[1];
91          for(int i = 0; i < validators.length; i++) {
92              // these should pass
93
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              // these should fail
101
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 JavaDoc.class,
112                                  "value", 0, 1, false,
113                                  Converters.get(String JavaDoc.class), null, null);
114
115         metaData = new MetaData("firstname");
116         verifyMetaDataProperties(metaData, "firstname", String JavaDoc.class,
117                                  "firstname", 0, 1, false,
118                                  Converters.get(String JavaDoc.class), null, null);
119
120         metaData = new MetaData("zipcode", Integer JavaDoc.class);
121         verifyMetaDataProperties(metaData, "zipcode", Integer JavaDoc.class,
122                                  "zipcode", 0, 1, false,
123                                  Converters.get(Integer JavaDoc.class), null, null);
124         metaData.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
125             public void propertyChange(PropertyChangeEvent JavaDoc e) {
126
127             }
128         });
129
130
131         metaData = new MetaData("phonenumber", String JavaDoc.class, "Phone Number");
132         verifyMetaDataProperties(metaData, "phonenumber", String JavaDoc.class, "Phone Number",
133                                  0, 1, false,
134                                  Converters.get(String JavaDoc.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 JavaDoc.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 JavaDoc format = new SimpleDateFormat JavaDoc();
152         metaData.setDecodeFormat(format);
153         metaData.setEncodeFormat(format);
154         verifyMetaDataProperties(metaData, "zip-code", Integer JavaDoc.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 JavaDoc.class,
183                                  "stringvalue", 0, 1, false,
184                                  Converters.get(String JavaDoc.class), null, null);
185         verifyStringMetaDataProperties(metaData, 0, Integer.MAX_VALUE, false);
186
187         metaData = new StringMetaData("lastname");
188         verifyMetaDataProperties(metaData, "lastname", String JavaDoc.class,
189                                  "lastname", 0, 1, false,
190                                  Converters.get(String JavaDoc.class), null, null);
191         verifyStringMetaDataProperties(metaData, 0, Integer.MAX_VALUE, false);
192
193         metaData = new StringMetaData("firstname", "First Name");
194         verifyMetaDataProperties(metaData, "firstname", String JavaDoc.class,
195                                  "First Name", 0, 1, false,
196                                  Converters.get(String JavaDoc.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 JavaDoc.class,
215                                  "numbervalue", 0, 1, false,
216                                  Converters.get(Integer JavaDoc.class), null, null);
217
218         metaData = new NumberMetaData("age");
219         verifyMetaDataProperties(metaData, "age", Integer JavaDoc.class,
220                                  "age", 0, 1, false,
221                                  Converters.get(Integer JavaDoc.class), null, null);
222         verifyNumberMetaDataProperties(metaData, null, null, false);
223
224         metaData = new NumberMetaData("interestrate", Float JavaDoc.class);
225         verifyMetaDataProperties(metaData, "interestrate", Float JavaDoc.class,
226                                  "interestrate", 0, 1, false,
227                                  Converters.get(Float JavaDoc.class), null, null);
228         verifyNumberMetaDataProperties(metaData, null, null, false);
229
230         metaData = new NumberMetaData("partnumber", Long JavaDoc.class, "Part Number");
231         verifyMetaDataProperties(metaData, "partnumber", Long JavaDoc.class, "Part Number",
232                                  0, 1, false,
233                                  Converters.get(Long JavaDoc.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 JavaDoc.class);
243         metaData.setMinimum(new Float JavaDoc(0.99));
244         metaData.setMaximum(new Float JavaDoc(99.99));
245         metaData.setCurrency(true);
246         verifyNumberMetaDataProperties(metaData, new Float JavaDoc(0.99), new Float JavaDoc(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 JavaDoc(0));
256         metaData.setMaximum(new Integer JavaDoc(10));
257         verifyNumberValidation(metaData, new Integer JavaDoc(0), new Integer JavaDoc(3),
258                                new Integer JavaDoc(10), new Integer JavaDoc(-1),
259                                new Integer JavaDoc(20));
260
261         metaData = new NumberMetaData("age", Short JavaDoc.class);
262         metaData.setMinimum(new Short JavaDoc((short)18));
263         metaData.setMaximum(new Short JavaDoc((short)120));
264         verifyNumberValidation(metaData, new Short JavaDoc((short)18), new Short JavaDoc((short)39),
265                                new Short JavaDoc((short)120), new Short JavaDoc((short)5),
266                                new Short JavaDoc((short)220));
267
268         metaData = new NumberMetaData("count", Long JavaDoc.class);
269         metaData.setMinimum(new Long JavaDoc(100));
270         metaData.setMaximum(new Long JavaDoc(1000000));
271         verifyNumberValidation(metaData, new Long JavaDoc(100), new Long JavaDoc(111111),
272                                new Long JavaDoc(1000000), new Long JavaDoc(10),
273                                new Long JavaDoc(1000001));
274
275         metaData = new NumberMetaData("price", Float JavaDoc.class);
276         metaData.setMinimum(new Float JavaDoc(1.00));
277         metaData.setMaximum(new Float JavaDoc(100.00));
278         metaData.setCurrency(true);
279         verifyNumberValidation(metaData, new Float JavaDoc(1.00), new Float JavaDoc(33.3),
280                                new Float JavaDoc(100.00), new Float JavaDoc(-1.1), new Float JavaDoc(101.1));
281
282         metaData = new NumberMetaData("molecules", Double JavaDoc.class);
283         metaData.setMinimum(new Double JavaDoc(0.0));
284         metaData.setMaximum(new Double JavaDoc(839329392339.234));
285         verifyNumberValidation(metaData, new Double JavaDoc(0.0), new Double JavaDoc(3333333.3),
286                                new Double JavaDoc(839329392339.234), new Double JavaDoc(-323.34),
287                                new Double JavaDoc(99393999939393.32));
288     }
289
290     public void testEnumeratedMetaDataConstructors() {
291         EnumeratedMetaData metaData = new EnumeratedMetaData();
292         verifyMetaDataProperties(metaData, "enumvalue", String JavaDoc.class,
293                                          "enumvalue", 0, 1, false,
294                                          Converters.get(String JavaDoc.class), null, null);
295
296         metaData = new EnumeratedMetaData("months");
297         verifyMetaDataProperties(metaData, "months", String JavaDoc.class,
298                                  "months",0, 1, false,
299                                  Converters.get(String JavaDoc.class), null, null);
300         verifyEnumeratedMetaDataProperties(metaData, new Object JavaDoc[0]);
301
302         metaData = new EnumeratedMetaData("background", Color JavaDoc.class);
303         verifyMetaDataProperties(metaData, "background", Color JavaDoc.class,
304                                  "background", 0, 1, false,
305                                  null, null, null);
306         verifyEnumeratedMetaDataProperties(metaData, new Object JavaDoc[0]);
307
308         metaData = new EnumeratedMetaData("month", String JavaDoc.class, "Month");
309         verifyMetaDataProperties(metaData, "month", String JavaDoc.class, "Month",
310                                  0, 1, false,
311                                  Converters.get(String JavaDoc.class), null, null);
312         verifyEnumeratedMetaDataProperties(metaData, new Object JavaDoc[0]);
313
314         Validator validators[] = metaData.getValidators();
315         assertEquals(0, validators.length);
316
317     }
318
319     public void testEnumeratedMetaDataProperties() {
320         String JavaDoc 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 JavaDoc list = new ArrayList JavaDoc();
327         for(int i=1; i < 6; i++) {
328             list.add(new String JavaDoc(""+i));
329         }
330         String JavaDoc listcopy[] = (String JavaDoc[])list.toArray(new String JavaDoc[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 JavaDoc keys[] = metaData.getCustomPropertyKeys();
340         assertEquals(0, keys.length);
341
342         // test adding
343
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         // test property change events on custom properties
351
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         // test removal
360
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 JavaDoc.class);
372         assertTrue(tracker.gotPropertyChangeEvent("elementClass"));
373         assertEquals(Color JavaDoc.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 JavaDoc)tracker.getNewPropertyValue("displayWidth")).intValue());
386
387         metaData.setMinValueCount(1);
388         assertTrue(tracker.gotPropertyChangeEvent("minValueCount"));
389         assertEquals(new Integer JavaDoc(1), tracker.getNewPropertyValue("minValueCount"));
390
391         metaData.setMaxValueCount(5);
392         assertTrue(tracker.gotPropertyChangeEvent("maxValueCount"));
393         assertEquals(new Integer JavaDoc(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 JavaDoc format = new SimpleDateFormat JavaDoc();
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 JavaDoc decode(String JavaDoc value, Object JavaDoc format)
432             throws ConversionException {
433             return null;
434         }
435         public String JavaDoc encode(Object JavaDoc value, Object JavaDoc format)
436             throws ConversionException {
437             return null;
438         }
439     }
440
441     public class DummyValidator implements Validator {
442         public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
443             return false;
444         }
445     }
446
447     public static void main(String JavaDoc args[]) {
448         MetaDataUnitTest test = new MetaDataUnitTest();
449         try {
450             test.setUp();
451         } catch (Exception JavaDoc 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