1 7 8 package org.jdesktop.swing.data; 9 10 import java.awt.Point ; 11 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 15 import java.text.SimpleDateFormat ; 16 17 import java.util.Calendar ; 18 import java.util.Date ; 19 import java.util.Map ; 20 import java.util.HashMap ; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 32 public class ConvertersUnitTest extends TestCase { 33 34 38 public void testgetConverter() { 39 Class [] data = { 41 String .class, Date .class, Long .class, Short .class, 42 Float .class, Integer .class, Boolean .class, Float .class, 43 Link.class 44 }; 45 46 for (int i = 0; i < data.length; i++) { 47 assertNotNull(Converters.get(data[i])); 48 } 49 50 assertNull(Converters.get(Byte .class)); 52 assertNull(Converters.get(Character .class)); 53 } 54 55 58 public void testGetTypes() { 59 Class [] 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 70 public void testStringConverter() { 71 testConverter("Hello"); 72 testConverter(""); 73 74 testConverterFailure(new Integer (10), String .class); 75 testConverterFailure((Object )null, String .class); 76 } 77 78 public void testIntegerConverter() { 79 testConverter(new Integer (47)); 80 testConverter(new Integer ( -10)); 81 testConverter(new Integer (0)); 82 testConverter(new Integer (256), new Integer (16)); 83 84 testConverter("1000", Integer .class, new Integer (1000)); 85 testConverter("-22", Integer .class, new Integer (-22)); 86 testConverter("0", Integer .class, new Integer (0)); 87 testConverter("ffffff", Integer .class, new Integer (16777215), 88 new Integer (16)); 89 90 testConverterFailure(new StringBuffer (), Integer .class); 91 testConverterFailure("hello there", Integer .class); 92 testConverterFailure((Object )null, Integer .class); 93 testConverterFailure("", Integer .class); 94 } 95 96 public void testBooleanConverter() { 97 testConverter(Boolean.TRUE); 98 testConverter(Boolean.FALSE); 99 100 testConverter("true", Boolean .class, Boolean.TRUE); 101 testConverter("TRUE", Boolean .class, Boolean.TRUE); 102 testConverter("True", Boolean .class, Boolean.TRUE); 103 testConverter("false", Boolean .class, Boolean.FALSE); 104 testConverter("FALSE", Boolean .class, Boolean.FALSE); 105 testConverter("False", Boolean .class, Boolean.FALSE); 106 107 testConverterFailure(new Integer (10), Boolean .class); 108 testConverterFailure((Object )null, Boolean .class); 109 110 } 111 112 public void testShortConverter() { 113 testConverter(new Short ((short)55)); 114 testConverter(new Short ((short)-1)); 115 testConverter(new Short ((short)0)); 116 117 testConverter("256", Short .class, new Short ((short)256)); 118 testConverter("0", Short .class, new Short ((short)0)); 119 testConverter("-10", Short .class, new Short ((short)-10)); 120 121 testConverterFailure(new Integer (99), Short .class); 122 testConverterFailure("hi", Short .class); 123 testConverterFailure((Object )null, Short .class); 124 testConverterFailure("", Short .class); 125 } 126 127 public void testLongConverter() { 128 testConverter(new Long (Long.MAX_VALUE)); 129 testConverter(new Long (Long.MIN_VALUE)); 130 testConverter(new Long (0)); 131 testConverter(new Long (8939), new Integer (16)); 132 133 testConverter("285678883", Long .class, new Long (285678883)); 134 testConverter("0", Long .class, new Long (0)); 135 testConverter("-138950399", Long .class, new Long (-138950399)); 136 testConverter("ABCDEF", Long .class, new Long (11259375), 137 new Integer (16)); 138 139 testConverterFailure(new Integer (33), Long .class); 140 testConverterFailure("hi", Long .class); 141 testConverterFailure((Object )null, Long .class); 142 testConverterFailure("", Long .class); 143 } 144 145 public void testFloatConverter() { 146 testConverter(new Float (48.2)); 147 testConverter(new Float ( -0000.0001)); 148 testConverter(new Float (1.00002f)); 149 testConverter(new Float (1.00002d)); 150 151 testConverter("33.2", Float .class, new Float (33.2)); 152 testConverter("-999.99", Float .class, new Float (-999.99)); 153 testConverter("1.1001f", Float .class, new Float (1.1001f)); 154 testConverter("-1.0E-4", Float .class, new Float (-1.0E-4)); 155 156 testConverterFailure(new Integer (33), Float .class); 157 testConverterFailure("hi", Float .class); 158 testConverterFailure( (Object )null, Float .class); 159 testConverterFailure("", Float .class); 160 } 161 162 public void testDoubleConverter() { 163 testConverter(new Double (Double.NaN)); 164 testConverter(new Double (Double.NEGATIVE_INFINITY)); 165 testConverter(new Double (Double.POSITIVE_INFINITY)); 166 testConverter(new Double (Double.MAX_VALUE)); 167 168 testConverterFailure(new Integer (33), Double .class); 169 testConverterFailure("hi", Double .class); 170 testConverterFailure( (Object )null, Double .class); 171 testConverterFailure("", Double .class); 172 } 173 174 public void testDateConverter() { 175 Calendar calendar = Calendar.getInstance(); 176 calendar.clear(); 177 calendar.set(1965, 8, 30, 12, 1, 1); 178 179 testConverter(calendar.getTime()); 180 testConverter(new Date (0), new SimpleDateFormat ("MMM dd yyyy HH:mm")); 181 182 testConverter("30 Sep 65 12:01:01", Date .class, 183 calendar.getTime(), 184 new SimpleDateFormat ("dd MMM yy HH:mm:ss")); 185 186 testConverterFailure(new Integer (38), Date .class); 187 testConverterFailure("my birthday", Date .class); 188 testConverterFailure("September 30, 1965", Date .class, new SimpleDateFormat ("mmm dd yy")); 189 190 } 191 192 public void testLinkConverter() { 193 try { 194 testConverter(new Link("foo", "_blank", 195 new URL ("http://foo.sun.com"))); 196 testConverter(new Link("foo", "_blank", 197 new URL ("http://foo.sun.com"))); 198 testConverter(new Link("bar", "new_frame", 199 new URL ("http://bar.sun.com"))); 200 testConverter(new Link("bar", "new_frame", 201 new URL ("http://bar.sun.com"))); 202 } 203 catch (MalformedURLException ex) { 204 throw new RuntimeException ("Error constructing URL: ", ex); 205 } 206 207 testConverter(new Link("bar", "new_frame", 208 "http://bugz?cat=@{1}&subcat=@{2}", 209 new String [] {"java", "classes_beans"})); 210 testConverter(new Link("bar", null , 211 "http://bugz?cat=@{1}&subcat=@{2}", 212 new String [] {"java", "classes_beans"})); 213 testConverter(new Link(null, null, "http://bugz?cat=@{1}&subcat=@{2}", 214 new String [] {"java", "classes_beans"})); 215 testConverter(new Link(null, null, "http://bugz?cat=@{1}&subcat=@{2}", 216 new String [] {"java", "classes_beans"})); 217 218 Converter converter = Converters.get(Link.class); 220 221 try { 222 String 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 ("Error converting string to link", e); 241 } 242 243 testConverterFailure(new Integer (1), Link.class); 244 testConverterFailure((Object )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 decode(String value, Object format) 252 throws ConversionException { 253 return null; 254 } 255 public String encode(Object value, Object format) 256 throws ConversionException { 257 return null; 258 } 259 }; 260 261 Converter pointConverter = Converters.get(Point .class); 262 assertNull(pointConverter); 263 264 Converters.put(Point .class, newPointConverter); 265 pointConverter = Converters.get(Point .class); 266 assertEquals(pointConverter, newPointConverter); 267 268 } 269 270 public void testConverter(Object objectValue) { 271 testConverter(objectValue, null); 272 } 273 274 public void testConverter(Object objectValue, Object format) { 275 testConverter(objectValue, objectValue.getClass(), format); 276 } 277 278 public void testConverter(Object objectValue, Class klass, Object format) { 279 Converter converter = Converters.get(klass); 280 String outString = null; 281 try { 282 outString = converter.encode(objectValue, format); 283 Object newObject = converter.decode(outString, format); 285 assertEquals(objectValue, newObject); 286 } 287 catch (ConversionException ex) { 288 throw new RuntimeException ("Error converting to Object: " + 289 outString, ex); 290 } 291 } 292 293 public void testConverter(String stringValue, Class klass, Object expected) { 294 testConverter(stringValue, klass, expected, null); 295 } 296 297 public void testConverter(String stringValue, Class klass, Object expected, Object format) { 298 Converter converter = Converters.get(klass); 299 Object newValue = null; 300 try { 301 newValue = converter.decode(stringValue, format); 302 assertEquals(expected, newValue); 303 } 304 catch (ConversionException ex) { 305 throw new RuntimeException ("Error converting to " + klass.getName() + 306 " Object: " + stringValue, ex); 307 } 308 } 309 310 public void testConverterFailure(Object objectValue, Class klass) { 311 testConverterFailure(objectValue, klass, null); 312 } 313 314 public void testConverterFailure(Object objectValue, Class klass, Object format) { 315 Converter converter = Converters.get(klass); 316 try { 317 String 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 } 324 } 325 326 public void testConverterFailure(String stringValue, Class klass) { 327 testConverterFailure(stringValue, klass, null); 328 } 329 330 public void testConverterFailure(String stringValue, Class klass, Object format) { 331 Converter converter = Converters.get(klass); 332 try { 333 Object value = converter.decode(stringValue, format); 334 fail("converter for " + klass.getName() + " should not convert string \""+ 335 stringValue + "\""); 336 } catch (ConversionException e) { 337 } 339 } 340 341 } 342 | Popular Tags |