KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ConvertersUnitTest.java,v 1.1 2004/08/05 01:29:18 davidson1 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.Point JavaDoc;
11
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
14
15 import java.text.SimpleDateFormat JavaDoc;
16
17 import java.util.Calendar JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 /**
27  * JUnit test class for data coverters.
28  *
29  * @author Mark Davidson
30  * @author Amy Fowler
31  */

32 public class ConvertersUnitTest extends TestCase {
33
34     /**
35      * Simple test to ensure that the correct type converter has been
36      * returned per type.
37      */

38     public void testgetConverter() {
39         // Add supported type/converter mappings here for testing.
40
Class JavaDoc[] data = {
41         String JavaDoc.class, Date JavaDoc.class, Long JavaDoc.class, Short JavaDoc.class,
42             Float JavaDoc.class, Integer JavaDoc.class, Boolean JavaDoc.class, Float JavaDoc.class,
43             Link.class
44         };
45
46         for (int i = 0; i < data.length; i++) {
47             assertNotNull(Converters.get(data[i]));
48         }
49
50     // These type converters are not supported
51
assertNull(Converters.get(Byte JavaDoc.class));
52     assertNull(Converters.get(Character JavaDoc.class));
53     }
54
55     /**
56      * Test to ensure that all supported types are returned.
57      */

58     public void testGetTypes() {
59     Class JavaDoc[] types = Converters.getTypes();
60     assertNotNull(types);
61     assertTrue(types.length != 0);
62
63     for (int i = 0; i < types.length; i++) {
64             assertNotNull(Converters.get(types[i]));
65     }
66     }
67
68     // Type converter tests here.
69

70     public void testStringConverter() {
71         testConverter("Hello");
72         testConverter("");
73
74         testConverterFailure(new Integer JavaDoc(10), String JavaDoc.class);
75         testConverterFailure((Object JavaDoc)null, String JavaDoc.class);
76     }
77
78     public void testIntegerConverter() {
79         testConverter(new Integer JavaDoc(47));
80         testConverter(new Integer JavaDoc( -10));
81         testConverter(new Integer JavaDoc(0));
82         testConverter(new Integer JavaDoc(256), new Integer JavaDoc(16)/*radix*/);
83
84         testConverter("1000", Integer JavaDoc.class, new Integer JavaDoc(1000));
85         testConverter("-22", Integer JavaDoc.class, new Integer JavaDoc(-22));
86         testConverter("0", Integer JavaDoc.class, new Integer JavaDoc(0));
87         testConverter("ffffff", Integer JavaDoc.class, new Integer JavaDoc(16777215),
88                       new Integer JavaDoc(16)/*radix*/);
89
90         testConverterFailure(new StringBuffer JavaDoc(), Integer JavaDoc.class);
91         testConverterFailure("hello there", Integer JavaDoc.class);
92         testConverterFailure((Object JavaDoc)null, Integer JavaDoc.class);
93         testConverterFailure("", Integer JavaDoc.class);
94     }
95
96     public void testBooleanConverter() {
97         testConverter(Boolean.TRUE);
98         testConverter(Boolean.FALSE);
99
100         testConverter("true", Boolean JavaDoc.class, Boolean.TRUE);
101         testConverter("TRUE", Boolean JavaDoc.class, Boolean.TRUE);
102         testConverter("True", Boolean JavaDoc.class, Boolean.TRUE);
103         testConverter("false", Boolean JavaDoc.class, Boolean.FALSE);
104         testConverter("FALSE", Boolean JavaDoc.class, Boolean.FALSE);
105         testConverter("False", Boolean JavaDoc.class, Boolean.FALSE);
106
107         testConverterFailure(new Integer JavaDoc(10), Boolean JavaDoc.class);
108         testConverterFailure((Object JavaDoc)null, Boolean JavaDoc.class);
109
110     }
111
112     public void testShortConverter() {
113         testConverter(new Short JavaDoc((short)55));
114         testConverter(new Short JavaDoc((short)-1));
115         testConverter(new Short JavaDoc((short)0));
116
117         testConverter("256", Short JavaDoc.class, new Short JavaDoc((short)256));
118         testConverter("0", Short JavaDoc.class, new Short JavaDoc((short)0));
119         testConverter("-10", Short JavaDoc.class, new Short JavaDoc((short)-10));
120
121         testConverterFailure(new Integer JavaDoc(99), Short JavaDoc.class);
122         testConverterFailure("hi", Short JavaDoc.class);
123         testConverterFailure((Object JavaDoc)null, Short JavaDoc.class);
124         testConverterFailure("", Short JavaDoc.class);
125     }
126
127     public void testLongConverter() {
128         testConverter(new Long JavaDoc(Long.MAX_VALUE));
129         testConverter(new Long JavaDoc(Long.MIN_VALUE));
130         testConverter(new Long JavaDoc(0));
131         testConverter(new Long JavaDoc(8939), new Integer JavaDoc(16)/*radix*/);
132
133         testConverter("285678883", Long JavaDoc.class, new Long JavaDoc(285678883));
134         testConverter("0", Long JavaDoc.class, new Long JavaDoc(0));
135         testConverter("-138950399", Long JavaDoc.class, new Long JavaDoc(-138950399));
136         testConverter("ABCDEF", Long JavaDoc.class, new Long JavaDoc(11259375),
137                       new Integer JavaDoc(16)/*radix*/);
138
139         testConverterFailure(new Integer JavaDoc(33), Long JavaDoc.class);
140         testConverterFailure("hi", Long JavaDoc.class);
141         testConverterFailure((Object JavaDoc)null, Long JavaDoc.class);
142         testConverterFailure("", Long JavaDoc.class);
143     }
144
145     public void testFloatConverter() {
146         testConverter(new Float JavaDoc(48.2));
147         testConverter(new Float JavaDoc( -0000.0001));
148         testConverter(new Float JavaDoc(1.00002f));
149         testConverter(new Float JavaDoc(1.00002d));
150
151         testConverter("33.2", Float JavaDoc.class, new Float JavaDoc(33.2));
152         testConverter("-999.99", Float JavaDoc.class, new Float JavaDoc(-999.99));
153         testConverter("1.1001f", Float JavaDoc.class, new Float JavaDoc(1.1001f));
154         testConverter("-1.0E-4", Float JavaDoc.class, new Float JavaDoc(-1.0E-4));
155
156         testConverterFailure(new Integer JavaDoc(33), Float JavaDoc.class);
157         testConverterFailure("hi", Float JavaDoc.class);
158         testConverterFailure( (Object JavaDoc)null, Float JavaDoc.class);
159         testConverterFailure("", Float JavaDoc.class);
160     }
161
162     public void testDoubleConverter() {
163         testConverter(new Double JavaDoc(Double.NaN));
164         testConverter(new Double JavaDoc(Double.NEGATIVE_INFINITY));
165         testConverter(new Double JavaDoc(Double.POSITIVE_INFINITY));
166         testConverter(new Double JavaDoc(Double.MAX_VALUE));
167
168         testConverterFailure(new Integer JavaDoc(33), Double JavaDoc.class);
169         testConverterFailure("hi", Double JavaDoc.class);
170         testConverterFailure( (Object JavaDoc)null, Double JavaDoc.class);
171         testConverterFailure("", Double JavaDoc.class);
172     }
173
174     public void testDateConverter() {
175         Calendar JavaDoc calendar = Calendar.getInstance();
176         calendar.clear();
177         calendar.set(1965, 8, 30, 12, 1, 1);
178
179         testConverter(calendar.getTime());
180         testConverter(new Date JavaDoc(0), new SimpleDateFormat JavaDoc("MMM dd yyyy HH:mm"));
181
182         testConverter("30 Sep 65 12:01:01", Date JavaDoc.class,
183                       calendar.getTime(),
184                       new SimpleDateFormat JavaDoc("dd MMM yy HH:mm:ss"));
185
186         testConverterFailure(new Integer JavaDoc(38), Date JavaDoc.class);
187         testConverterFailure("my birthday", Date JavaDoc.class);
188         testConverterFailure("September 30, 1965", Date JavaDoc.class, new SimpleDateFormat JavaDoc("mmm dd yy"));
189
190     }
191
192     public void testLinkConverter() {
193         try {
194             testConverter(new Link("foo", "_blank",
195                                              new URL JavaDoc("http://foo.sun.com")));
196             testConverter(new Link("foo", "_blank",
197                                              new URL JavaDoc("http://foo.sun.com")));
198             testConverter(new Link("bar", "new_frame",
199                                              new URL JavaDoc("http://bar.sun.com")));
200             testConverter(new Link("bar", "new_frame",
201                                              new URL JavaDoc("http://bar.sun.com")));
202         }
203         catch (MalformedURLException JavaDoc ex) {
204             throw new RuntimeException JavaDoc("Error constructing URL: ", ex);
205         }
206
207         testConverter(new Link("bar", "new_frame",
208                                "http://bugz?cat=@{1}&subcat=@{2}",
209                                new String JavaDoc[] {"java", "classes_beans"}));
210         testConverter(new Link("bar", null /* no target */,
211                                "http://bugz?cat=@{1}&subcat=@{2}",
212                                new String JavaDoc[] {"java", "classes_beans"}));
213         testConverter(new Link(null, null, "http://bugz?cat=@{1}&subcat=@{2}",
214                                new String JavaDoc[] {"java", "classes_beans"}));
215         testConverter(new Link(null, null, "http://bugz?cat=@{1}&subcat=@{2}",
216                                new String JavaDoc[] {"java", "classes_beans"}));
217
218         // Test converting from raw strings to objects and back
219
Converter converter = Converters.get(Link.class);
220
221         try {
222             String JavaDoc linkString =
223                 "<a HREF=\"http://java.sun.com\" target=\"frame1\">java.sun.com</a>";
224             Link link = (Link) converter.decode(linkString, null);
225             assertEquals(link.getURL().toExternalForm(), "http://java.sun.com");
226             assertEquals(link.getText(), "java.sun.com");
227             assertEquals(link.getTarget(), "frame1");
228
229             linkString = "<a HREF=\"http://java.sun.com\">java.sun.com</a>";
230             link = (Link) converter.decode(linkString, null);
231             assertEquals(link.getURL().toExternalForm(), "http://java.sun.com");
232             assertEquals(link.getText(), "java.sun.com");
233
234             linkString = "<a HREF=\"http://java.sun.com\"></a>";
235             link = (Link) converter.decode(linkString, null);
236             assertEquals(link.getURL().toExternalForm(), "http://java.sun.com");
237             assertEquals(link.getText(), "");
238
239         } catch (ConversionException e) {
240             throw new RuntimeException JavaDoc("Error converting string to link", e);
241         }
242
243         testConverterFailure(new Integer JavaDoc(1), Link.class);
244         testConverterFailure((Object JavaDoc)null, Link.class);
245         testConverterFailure("http:\\java.sun.com", Link.class);
246     }
247
248     public void testPutConverter() {
249
250         Converter newPointConverter = new Converter() {
251             public Object JavaDoc decode(String JavaDoc value, Object JavaDoc format)
252                 throws ConversionException {
253                 return null;
254             }
255             public String JavaDoc encode(Object JavaDoc value, Object JavaDoc format)
256                 throws ConversionException {
257                 return null;
258             }
259         };
260
261         Converter pointConverter = Converters.get(Point JavaDoc.class);
262         assertNull(pointConverter);
263
264         Converters.put(Point JavaDoc.class, newPointConverter);
265         pointConverter = Converters.get(Point JavaDoc.class);
266         assertEquals(pointConverter, newPointConverter);
267
268     }
269
270     public void testConverter(Object JavaDoc objectValue) {
271         testConverter(objectValue, null);
272     }
273
274     public void testConverter(Object JavaDoc objectValue, Object JavaDoc format) {
275         testConverter(objectValue, objectValue.getClass(), format);
276     }
277
278     public void testConverter(Object JavaDoc objectValue, Class JavaDoc klass, Object JavaDoc format) {
279         Converter converter = Converters.get(klass);
280         String JavaDoc outString = null;
281         try {
282             outString = converter.encode(objectValue, format);
283             //System.out.println("testConverter "+klass.getName()+ " string="+outString);
284
Object JavaDoc newObject = converter.decode(outString, format);
285             assertEquals(objectValue, newObject);
286         }
287         catch (ConversionException ex) {
288             throw new RuntimeException JavaDoc("Error converting to Object: " +
289                                        outString, ex);
290         }
291     }
292
293     public void testConverter(String JavaDoc stringValue, Class JavaDoc klass, Object JavaDoc expected) {
294         testConverter(stringValue, klass, expected, null);
295     }
296
297     public void testConverter(String JavaDoc stringValue, Class JavaDoc klass, Object JavaDoc expected, Object JavaDoc format) {
298         Converter converter = Converters.get(klass);
299         Object JavaDoc newValue = null;
300         try {
301             newValue = converter.decode(stringValue, format);
302             assertEquals(expected, newValue);
303         }
304         catch (ConversionException ex) {
305             throw new RuntimeException JavaDoc("Error converting to " + klass.getName() +
306                                        " Object: " + stringValue, ex);
307         }
308     }
309
310     public void testConverterFailure(Object JavaDoc objectValue, Class JavaDoc klass) {
311         testConverterFailure(objectValue, klass, null);
312     }
313
314     public void testConverterFailure(Object JavaDoc objectValue, Class JavaDoc klass, Object JavaDoc format) {
315         Converter converter = Converters.get(klass);
316         try {
317             String JavaDoc outString = converter.encode(objectValue, format);
318             fail("converter for " + klass.getName() + " should not convert "+
319                  (objectValue != null? objectValue.getClass().getName() : "null") +
320                  " instance");
321         } catch (ConversionException e) {
322             // success!
323
}
324     }
325
326     public void testConverterFailure(String JavaDoc stringValue, Class JavaDoc klass) {
327         testConverterFailure(stringValue, klass, null);
328     }
329
330     public void testConverterFailure(String JavaDoc stringValue, Class JavaDoc klass, Object JavaDoc format) {
331         Converter converter = Converters.get(klass);
332         try {
333             Object JavaDoc value = converter.decode(stringValue, format);
334             fail("converter for " + klass.getName() + " should not convert string \""+
335                  stringValue + "\"");
336         } catch (ConversionException e) {
337             // success!
338
}
339     }
340
341 }
342
Popular Tags