1 7 package com.inversoft.util.typeconverter.converters.test; 8 9 10 import junit.framework.TestCase; 11 12 import com.inversoft.util.typeconverter.TypeConversionException; 13 import com.inversoft.util.typeconverter.TypeConverter; 14 import com.inversoft.util.typeconverter.converters.BooleanTypeConverter; 15 16 17 23 public class BooleanTypeConverterTest extends TestCase { 24 25 26 public BooleanTypeConverterTest(String name) { 27 super(name); 28 } 29 30 33 public void testNullConversions() { 34 try { 35 String stringValue = null; 36 TypeConverter converter = new BooleanTypeConverter(); 37 Object obj = converter.convert(stringValue, Boolean .class); 38 39 assertTrue("obj should be null", obj == null); 40 } catch (TypeConversionException tce) { 41 fail(tce.toString()); 42 } 43 } 44 45 48 public void testEmptyConversions() { 49 try { 50 String stringValue = " "; 51 TypeConverter converter = new BooleanTypeConverter(); 52 Object obj = converter.convert(stringValue, Boolean .class); 53 54 assertTrue("obj should be null", obj == null); 55 } catch (TypeConversionException tce) { 56 fail(tce.toString()); 57 } 58 } 59 60 63 public void testBooleanConversion() { 64 try { 65 String value = "true"; 66 TypeConverter converter = new BooleanTypeConverter(); 67 Object booleanObject = converter.convertString(value, Boolean .class); 68 69 if (booleanObject instanceof Boolean ) { 70 assertTrue("Should be true", ((Boolean ) booleanObject).booleanValue()); 71 } else { 72 fail("Type is not correct: " + booleanObject.getClass().getName()); 73 } 74 } catch (TypeConversionException tce) { 75 fail(tce.toString()); 76 } 77 78 try { 79 String value = "fals3"; 80 TypeConverter converter = new BooleanTypeConverter(); 81 converter.convertString(value, Boolean .class); 82 fail("Should have thrown an exception"); 83 } catch (TypeConversionException tce) { 84 assertTrue("Should have no root cause or target", tce.getCause() == null); 86 } 87 } 88 89 92 public void testPrimitiveFalse() { 93 try { 94 String value = " "; 95 TypeConverter converter = new BooleanTypeConverter(); 96 Object booleanObject = converter.convertString(value, Boolean.TYPE); 97 98 if (booleanObject instanceof Boolean ) { 99 assertFalse("Should be false", ((Boolean ) booleanObject).booleanValue()); 100 } else { 101 fail("Type is not correct: " + booleanObject.getClass().getName()); 102 } 103 } catch (TypeConversionException tce) { 104 fail(tce.toString()); 105 } 106 } 107 108 112 public void testStringDelegates() { 113 try { 114 String value = " ,true, false"; 115 BooleanTypeConverter converter = new BooleanTypeConverter(); 116 Object booleanObject = converter.convertString(value, new boolean[0].getClass()); 117 118 if (booleanObject instanceof Boolean []) { 119 assertEquals(3, ((Boolean []) booleanObject).length); 120 assertEquals(false, ((Boolean []) booleanObject)[0].booleanValue()); 121 assertTrue("Should be false", ((Boolean []) booleanObject)[1].booleanValue()); 122 assertFalse("Should be false", ((Boolean []) booleanObject)[2].booleanValue()); 123 } else { 124 fail("Type is not correct: " + booleanObject.getClass().getName()); 125 } 126 } catch (TypeConversionException tce) { 127 fail(tce.toString()); 128 } 129 } 130 131 135 public void testConvertArray() { 136 try { 137 Boolean [] bool = new Boolean [1]; 138 bool[0] = new Boolean (true); 139 TypeConverter converter = new BooleanTypeConverter(); 140 Object booleanObject = converter.convertArray(bool, Boolean .class); 141 142 if (booleanObject instanceof Boolean ) { 143 assertTrue("Should be true", ((Boolean ) booleanObject).booleanValue()); 144 } else { 145 fail("Type is not correct: " + booleanObject.getClass().getName()); 146 } 147 } catch (TypeConversionException tce) { 148 fail(tce.toString()); 149 } 150 151 try { 152 Boolean [] bool = new Boolean [2]; 153 TypeConverter converter = new BooleanTypeConverter(); 154 converter.convertArray(bool, Boolean .class); 155 fail("Should have thrown an exception because array is length 2"); 156 } catch (TypeConversionException tce) { 157 assertTrue("Should have no root cause or target", tce.getCause() == null); 159 } 160 161 try { 162 String [] bool = new String [1]; 163 bool[0] = new String ("true"); 164 TypeConverter converter = new BooleanTypeConverter(); 165 Object booleanObject = converter.convertArray(bool, Boolean .class); 166 167 if (booleanObject instanceof Boolean ) { 168 assertTrue("Should be true", ((Boolean ) booleanObject).booleanValue()); 169 } else { 170 fail("Type is not correct: " + booleanObject.getClass().getName()); 171 } 172 } catch (TypeConversionException tce) { 173 fail(tce.toString()); 174 } 175 176 try { 177 TypeConverter converter = new BooleanTypeConverter(); 178 Object booleanObject = converter.convertArray(null, Boolean.TYPE); 179 180 if (booleanObject instanceof Boolean ) { 181 assertFalse(((Boolean ) booleanObject).booleanValue()); 182 } else { 183 fail("Type is not correct: " + booleanObject.getClass().getName()); 184 } 185 } catch (TypeConversionException tce) { 186 fail(tce.toString()); 187 } 188 189 try { 190 TypeConverter converter = new BooleanTypeConverter(); 191 Object booleanObject = converter.convertArray(new String [0], Boolean.TYPE); 192 193 if (booleanObject instanceof Boolean ) { 194 assertFalse(((Boolean ) booleanObject).booleanValue()); 195 } else { 196 fail("Type is not correct: " + booleanObject.getClass().getName()); 197 } 198 } catch (TypeConversionException tce) { 199 fail(tce.toString()); 200 } 201 } 202 203 207 public void testConvertArrayToArray() { 208 try { 209 Boolean [] bool = new Boolean [2]; 210 bool[0] = new Boolean (true); 211 bool[1] = new Boolean (false); 212 TypeConverter converter = new BooleanTypeConverter(); 213 Object booleanObject = converter.convertArrayToArray(bool, Boolean .class); 214 215 if (booleanObject instanceof Boolean []) { 216 assertTrue("Test 1 should be true", ((Boolean []) booleanObject)[0].booleanValue()); 217 assertTrue("Test 1 should be false", !((Boolean []) booleanObject)[1].booleanValue()); 218 } else { 219 fail("Type is not correct: " + booleanObject.getClass().getName()); 220 } 221 } catch (TypeConversionException tce) { 222 fail(tce.toString()); 223 } 224 225 try { 226 String [] bool = new String [2]; 227 bool[0] = "true"; 228 bool[1] = "false"; 229 TypeConverter converter = new BooleanTypeConverter(); 230 Object booleanObject = converter.convertArrayToArray(bool, Boolean .class); 231 232 if (booleanObject instanceof Boolean []) { 233 assertTrue("Test 2 should be true", ((Boolean []) booleanObject)[0].booleanValue()); 234 assertTrue("Test 2 should be false", !((Boolean []) booleanObject)[1].booleanValue()); 235 } else { 236 fail("Type is not correct: " + booleanObject.getClass().getName()); 237 } 238 } catch (TypeConversionException tce) { 239 fail(tce.toString()); 240 } 241 242 try { 243 String [] bool = new String [2]; 244 bool[0] = "true"; 245 bool[1] = "fals3"; 246 TypeConverter converter = new BooleanTypeConverter(); 247 converter.convertArrayToArray(bool, Boolean .class); 248 fail("Should have failed because second array is fals3"); 249 } catch (TypeConversionException tce) { 250 assertTrue("Should have no root cause or target", tce.getCause() == null); 252 } 253 } 254 255 259 public void testConvertToArray() { 260 try { 261 String [] bool = new String [2]; 262 bool[0] = "true"; 263 bool[1] = "false"; 264 TypeConverter converter = new BooleanTypeConverter(); 265 Object booleanObject = converter.convertToArray(bool, Boolean .class); 266 267 if (booleanObject instanceof Boolean []) { 268 assertTrue("Should be true", ((Boolean []) booleanObject)[0].booleanValue()); 269 assertTrue("Should be false", !((Boolean []) booleanObject)[1].booleanValue()); 270 } else { 271 fail("Type is not correct: " + booleanObject.getClass().getName()); 272 } 273 } catch (TypeConversionException tce) { 274 fail(tce.toString()); 275 } 276 277 try { 278 String bool = "true,false"; 279 TypeConverter converter = new BooleanTypeConverter(); 280 Object booleanObject = converter.convertToArray(bool, Boolean .class); 281 282 if (booleanObject instanceof Boolean []) { 283 assertTrue("Should be true", ((Boolean []) booleanObject)[0].booleanValue()); 284 assertTrue("Should be false", !((Boolean []) booleanObject)[1].booleanValue()); 285 } else { 286 fail("Type is not correct: " + booleanObject.getClass().getName()); 287 } 288 } catch (TypeConversionException tce) { 289 fail(tce.toString()); 290 } 291 292 try { 293 String bool = "true,fals3"; 294 TypeConverter converter = new BooleanTypeConverter(); 295 converter.convertToArray(bool, Boolean .class); 296 fail("Should have failed because second array is fals3"); 297 } catch (TypeConversionException tce) { 298 assertTrue("Should have no root cause or target", tce.getCause() == null); 300 } 301 } 302 303 306 public void testConvertMethod() { 307 308 try { 310 Boolean [] bool = new Boolean [1]; 311 bool[0] = new Boolean (true); 312 TypeConverter converter = new BooleanTypeConverter(); 313 Object booleanObject = converter.convert(bool, Boolean .class); 314 315 if (booleanObject instanceof Boolean ) { 316 assertTrue("Should be true", ((Boolean ) booleanObject).booleanValue()); 317 } else { 318 fail("Type is not correct: " + booleanObject.getClass().getName()); 319 } 320 } catch (TypeConversionException tce) { 321 fail("Convert test 1 failed: " + tce.toString()); 322 } 323 324 try { 326 String bool = "true,false"; 327 TypeConverter converter = new BooleanTypeConverter(); 328 Object booleanObject = converter.convert(bool, Boolean [].class); 329 330 if (booleanObject instanceof Boolean []) { 331 assertTrue("Should be true", ((Boolean []) booleanObject)[0].booleanValue()); 332 assertTrue("Should be false", !((Boolean []) booleanObject)[1].booleanValue()); 333 } else { 334 fail("Type is not correct: " + booleanObject.getClass().getName()); 335 } 336 } catch (TypeConversionException tce) { 337 fail("Convert test 2 failed: " + tce.toString()); 338 } 339 340 try { 342 String [] bool = new String [2]; 343 bool[0] = "true"; 344 bool[1] = "false"; 345 TypeConverter converter = new BooleanTypeConverter(); 346 Object booleanObject = converter.convert(bool, Boolean [].class); 347 348 if (booleanObject instanceof Boolean []) { 349 assertTrue("Test 1 should be true", ((Boolean []) booleanObject)[0].booleanValue()); 350 assertTrue("Test 1 should be false", !((Boolean []) booleanObject)[1].booleanValue()); 351 } else { 352 fail("Type is not correct: " + booleanObject.getClass().getName()); 353 } 354 } catch (TypeConversionException tce) { 355 fail("Convert test 3 failed: " + tce.toString()); 356 } 357 } 358 } | Popular Tags |