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.converters.CharacterTypeConverter; 14 15 16 22 public class CharacterTypeConverterTest extends TestCase { 23 24 public static final char DEFAULT = 0; 25 26 27 30 public void testString() { 31 try { 32 CharacterTypeConverter ctc = new CharacterTypeConverter(); 33 34 Character c = (Character ) ctc.convertString("a", Character.TYPE); 36 assertEquals(new Character ('a'), c); 37 38 c = (Character ) ctc.convertString("b", Character .class); 40 assertEquals(new Character ('b'), c); 41 42 c = (Character ) ctc.convertString(null, Character.TYPE); 44 assertEquals(new Character (DEFAULT), c); 45 46 c = (Character ) ctc.convertString(null, Character .class); 48 assertEquals(null, c); 49 50 c = (Character ) ctc.convertString(" ", Character.TYPE); 52 assertEquals(new Character (DEFAULT), c); 53 54 c = (Character ) ctc.convertString(" ", Character .class); 56 assertEquals(null, c); 57 } catch (TypeConversionException tce) { 58 fail(tce.toString()); 59 } 60 61 try { 63 CharacterTypeConverter ctc = new CharacterTypeConverter(); 64 ctc.convertString("ab", Character.TYPE); 65 fail("Should have failed because string is two characters"); 66 } catch (TypeConversionException e) { 67 System.out.println(e.toString()); 69 } 70 71 try { 73 CharacterTypeConverter ctc = new CharacterTypeConverter(); 74 ctc.convertString("ab", Character .class); 75 fail("Should have failed because string is two characters"); 76 } catch (TypeConversionException e) { 77 System.out.println(e.toString()); 79 } 80 } 81 82 85 public void testStringToArray() { 86 try { 87 CharacterTypeConverter ctc = new CharacterTypeConverter(); 88 89 Character [] c = (Character []) ctc.convertStringToArray("a,b", ",", 91 char[].class); 92 assertEquals(new Character ('a'), c[0]); 93 assertEquals(new Character ('b'), c[1]); 94 95 c = (Character []) ctc.convertStringToArray("b,c", ",", 97 Character [].class); 98 assertEquals(new Character ('b'), c[0]); 99 assertEquals(new Character ('c'), c[1]); 100 101 c = (Character []) ctc.convertStringToArray(null, ",", char[].class); 103 assertEquals(null, c); 104 105 c = (Character []) ctc.convertStringToArray(null, ",", Character [].class); 107 assertEquals(null, c); 108 109 c = (Character []) ctc.convertStringToArray(" , ", ",", char[].class); 111 assertEquals(new Character (DEFAULT), c[0]); 112 assertEquals(new Character (DEFAULT), c[1]); 113 114 c = (Character []) ctc.convertStringToArray(" , ", ",", Character [].class); 116 assertEquals(null, c[0]); 117 assertEquals(null, c[1]); 118 119 c = (Character []) ctc.convertString("a,b", char[].class); 121 assertEquals(new Character ('a'), c[0]); 122 assertEquals(new Character ('b'), c[1]); 123 124 c = (Character []) ctc.convertString("a,b", Character [].class); 126 assertEquals(new Character ('a'), c[0]); 127 assertEquals(new Character ('b'), c[1]); 128 } catch (TypeConversionException tce) { 129 fail(tce.toString()); 130 } 131 132 try { 134 CharacterTypeConverter ctc = new CharacterTypeConverter(); 135 ctc.convertStringToArray("ab,cd", ",", char[].class); 136 fail("Should have failed because string is two characters"); 137 } catch (TypeConversionException e) { 138 System.out.println(e.toString()); 140 } 141 142 try { 144 CharacterTypeConverter ctc = new CharacterTypeConverter(); 145 ctc.convertStringToArray("ab, cd", ",", Character [].class); 146 fail("Should have failed because string is two characters"); 147 } catch (TypeConversionException e) { 148 System.out.println(e.toString()); 150 } 151 } 152 153 156 public void testArray() { 157 try { 158 CharacterTypeConverter ctc = new CharacterTypeConverter(); 159 160 Character c = (Character ) ctc.convertArray(new String []{"a"}, 162 Character.TYPE); 163 assertEquals(new Character ('a'), c); 164 165 c = (Character ) ctc.convertArray(new String []{"b"}, Character .class); 167 assertEquals(new Character ('b'), c); 168 169 c = (Character ) ctc.convertArray(null, Character.TYPE); 171 assertEquals(new Character (DEFAULT), c); 172 173 c = (Character ) ctc.convertArray(null, Character .class); 175 assertEquals(null, c); 176 177 c = (Character ) ctc.convertArray(new String []{" "}, Character.TYPE); 179 assertEquals(new Character (DEFAULT), c); 180 181 c = (Character ) ctc.convertArray(new String []{" "}, Character .class); 183 assertEquals(null, c); 184 185 c = (Character ) ctc.convertArray(new String [0], Character.TYPE); 187 assertEquals(new Character (DEFAULT), c); 188 189 c = (Character ) ctc.convertArray(new String [0], Character .class); 191 assertEquals(null, c); 192 } catch (TypeConversionException tce) { 193 fail(tce.toString()); 194 } 195 196 try { 198 CharacterTypeConverter ctc = new CharacterTypeConverter(); 199 ctc.convertArray(new String []{"ab"}, Character.TYPE); 200 fail("Should have failed because string is two characters"); 201 } catch (TypeConversionException e) { 202 System.out.println(e.toString()); 204 } 205 206 try { 208 CharacterTypeConverter ctc = new CharacterTypeConverter(); 209 ctc.convertArray(new String []{"ab"}, Character .class); 210 fail("Should have failed because string is two characters"); 211 } catch (TypeConversionException e) { 212 System.out.println(e.toString()); 214 } 215 } 216 217 220 public void testArrayToArray() { 221 try { 222 CharacterTypeConverter ctc = new CharacterTypeConverter(); 223 224 Character [] c = (Character []) ctc.convertArrayToArray(new String []{"a","b"}, 226 Character.TYPE); 227 assertEquals(new Character ('a'), c[0]); 228 assertEquals(new Character ('b'), c[1]); 229 230 c = (Character []) ctc.convertArrayToArray(new String []{"b", "c"}, 232 Character .class); 233 assertEquals(new Character ('b'), c[0]); 234 assertEquals(new Character ('c'), c[1]); 235 236 c = (Character []) ctc.convertArrayToArray(null, Character.TYPE); 238 assertEquals(null, c); 239 240 c = (Character []) ctc.convertArrayToArray(null, Character .class); 242 assertEquals(null, c); 243 244 c = (Character []) ctc.convertArrayToArray(new Character [2], 246 Character.TYPE); 247 assertEquals(new Character (DEFAULT), c[0]); 248 assertEquals(new Character (DEFAULT), c[1]); 249 250 c = (Character []) ctc.convertArrayToArray(new Character [2], 252 Character .class); 253 assertEquals(null, c[0]); 254 assertEquals(null, c[1]); 255 256 c = (Character []) ctc.convertArrayToArray(new String []{" ", " "}, 258 Character.TYPE); 259 assertEquals(new Character (DEFAULT), c[0]); 260 assertEquals(new Character (DEFAULT), c[1]); 261 262 c = (Character []) ctc.convertArrayToArray(new String []{" ", " "}, 264 Character .class); 265 assertEquals(null, c[0]); 266 assertEquals(null, c[1]); 267 268 c = (Character []) ctc.convertArray(new String []{"a", "b"}, 270 char[].class); 271 assertEquals(new Character ('a'), c[0]); 272 assertEquals(new Character ('b'), c[1]); 273 274 c = (Character []) ctc.convertArrayToArray(new String []{"a", "b"}, 276 Character [].class); 277 assertEquals(new Character ('a'), c[0]); 278 assertEquals(new Character ('b'), c[1]); 279 } catch (TypeConversionException tce) { 280 fail(tce.toString()); 281 } 282 283 try { 285 CharacterTypeConverter ctc = new CharacterTypeConverter(); 286 ctc.convertArrayToArray(new String []{"ab", "cd"}, Character.TYPE); 287 fail("Should have failed because string is two characters"); 288 } catch (TypeConversionException e) { 289 System.out.println(e.toString()); 291 } 292 293 try { 295 CharacterTypeConverter ctc = new CharacterTypeConverter(); 296 ctc.convertArrayToArray(new String []{"ab", "cd"}, Character .class); 297 fail("Should have failed because string is two characters"); 298 } catch (TypeConversionException e) { 299 System.out.println(e.toString()); 301 } 302 } 303 } | Popular Tags |