1 21 22 package org.dbunit.dataset.datatype; 23 24 import org.dbunit.database.ExtendedMockSingleRowResultSet; 25 import org.dbunit.dataset.ITable; 26 27 import java.math.BigDecimal ; 28 import java.sql.Types ; 29 30 34 35 public class IntegerDataTypeTest extends AbstractDataTypeTest 36 { 37 private final static DataType[] TYPES = { 38 DataType.TINYINT, 39 DataType.SMALLINT, 40 DataType.INTEGER, 41 }; 42 43 public IntegerDataTypeTest(String name) 44 { 45 super(name); 46 } 47 48 51 public void testToString() throws Exception 52 { 53 String [] expected = { 54 "TINYINT", 55 "SMALLINT", 56 "INTEGER", 57 }; 58 59 assertEquals("type count", expected.length, TYPES.length); 60 for (int i = 0; i < TYPES.length; i++) 61 { 62 assertEquals("name", expected[i], TYPES[i].toString()); 63 } 64 } 65 66 public void testGetTypeClass() throws Exception 67 { 68 for (int i = 0; i < TYPES.length; i++) 69 { 70 assertEquals("class", Integer .class, TYPES[i].getTypeClass()); 71 } 72 } 73 74 public void testIsNumber() throws Exception 75 { 76 for (int i = 0; i < TYPES.length; i++) 77 { 78 assertEquals("is number", true, TYPES[i].isNumber()); 79 } 80 } 81 82 public void testIsDateTime() throws Exception 83 { 84 for (int i = 0; i < TYPES.length; i++) 85 { 86 assertEquals("is date/time", false, TYPES[i].isDateTime()); 87 } 88 } 89 90 public void testTypeCast() throws Exception 91 { 92 Object [] values = { 93 null, 94 "5", 95 new Long (Integer.MAX_VALUE - 1), 96 new Double (Integer.MIN_VALUE + 1), 97 "-7500", 98 new Long (Integer.MAX_VALUE), 99 new Double (Integer.MIN_VALUE), 100 new Float (0.666), 101 new Double (0.666), 102 new Double (5.49), 103 "-99.9", 104 new Double (1.5E2), 105 new BigDecimal (1234), 106 }; 107 108 Integer [] expected = { 109 null, 110 new Integer (5), 111 new Integer (Integer.MAX_VALUE - 1), 112 new Integer (Integer.MIN_VALUE + 1), 113 new Integer (-7500), 114 new Integer (Integer.MAX_VALUE), 115 new Integer (Integer.MIN_VALUE), 116 new Integer (0), 117 new Integer (0), 118 new Integer (5), 119 new Integer (-99), 120 new Integer (150), 121 new Integer (1234), 122 123 }; 124 125 assertEquals("actual vs expected count", values.length, expected.length); 126 127 for (int i = 0; i < TYPES.length; i++) 128 { 129 for (int j = 0; j < values.length; j++) 130 { 131 assertEquals("typecast " + j, expected[j], 132 TYPES[i].typeCast(values[j])); 133 } 134 } 135 } 136 137 public void testTypeCastNone() throws Exception 138 { 139 for (int i = 0; i < TYPES.length; i++) 140 { 141 DataType type = TYPES[i]; 142 assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE)); 143 } 144 } 145 146 public void testTypeCastInvalid() throws Exception 147 { 148 Object [] values = { 149 new Object (), 150 "bla", 151 new java.util.Date () 152 }; 153 154 for (int i = 0; i < TYPES.length; i++) 155 { 156 for (int j = 0; j < values.length; j++) 157 { 158 try 159 { 160 TYPES[i].typeCast(values[j]); 161 fail("Should throw TypeCastException"); 162 } 163 catch (TypeCastException e) 164 { 165 } 166 } 167 } 168 } 169 170 public void testCompareEquals() throws Exception 171 { 172 Object [] values1 = { 173 null, 174 "5", 175 new Long (Integer.MAX_VALUE - 1), 176 new Double (Integer.MIN_VALUE + 1), 177 "-7500", 178 new Long (Integer.MAX_VALUE), 179 new Double (Integer.MIN_VALUE), 180 new Float (0.666), 181 new Double (0.666), 182 new Double (5.49), 183 "-99.9", 184 new Double (1.5E2), 185 new BigDecimal (1234), 186 }; 187 188 Object [] values2 = { 189 null, 190 new Integer (5), 191 new Integer (Integer.MAX_VALUE - 1), 192 new Integer (Integer.MIN_VALUE + 1), 193 new Integer (-7500), 194 new Integer (Integer.MAX_VALUE), 195 new Integer (Integer.MIN_VALUE), 196 new Integer (0), 197 new Integer (0), 198 new Integer (5), 199 new Integer (-99), 200 new Integer (150), 201 new Integer (1234), 202 }; 203 204 assertEquals("values count", values1.length, values2.length); 205 206 for (int i = 0; i < TYPES.length; i++) 207 { 208 for (int j = 0; j < values1.length; j++) 209 { 210 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j])); 211 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j])); 212 } 213 } 214 } 215 216 public void testCompareInvalid() throws Exception 217 { 218 Object [] values1 = { 219 new Object (), 220 "bla", 221 new java.util.Date () 222 }; 223 Object [] values2 = { 224 null, 225 null, 226 null 227 }; 228 229 assertEquals("values count", values1.length, values2.length); 230 231 for (int i = 0; i < TYPES.length; i++) 232 { 233 for (int j = 0; j < values1.length; j++) 234 { 235 try 236 { 237 TYPES[i].compare(values1[j], values2[j]); 238 fail("Should throw TypeCastException"); 239 } 240 catch (TypeCastException e) 241 { 242 } 243 244 try 245 { 246 TYPES[i].compare(values2[j], values1[j]); 247 fail("Should throw TypeCastException"); 248 } 249 catch (TypeCastException e) 250 { 251 } 252 } 253 } 254 } 255 256 public void testCompareDifferent() throws Exception 257 { 258 Object [] less = { 259 null, 260 "-7500", 261 new Double (Float.MIN_VALUE), 262 }; 263 264 Object [] greater = { 265 "0", 266 "5.555", 267 new Float (Float.MAX_VALUE), 268 }; 269 270 assertEquals("values count", less.length, greater.length); 271 272 for (int i = 0; i < TYPES.length; i++) 273 { 274 for (int j = 0; j < less.length; j++) 275 { 276 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0); 277 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0); 278 } 279 } 280 } 281 282 public void testSqlType() throws Exception 283 { 284 int[] sqlTypes = { 285 Types.TINYINT, 286 Types.SMALLINT, 287 Types.INTEGER, 288 }; 289 290 assertEquals("count", sqlTypes.length, TYPES.length); 291 for (int i = 0; i < TYPES.length; i++) 292 { 293 assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i])); 294 assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString())); 295 assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType()); 296 } 297 } 298 299 302 public void testForObject() throws Exception 303 { 304 assertEquals(DataType.INTEGER, DataType.forObject(new Integer (1234))); 305 } 306 307 public void testAsString() throws Exception 308 { 309 Object [] values = { 310 new Integer ("1234"), 311 }; 312 313 String [] expected = { 314 "1234", 315 }; 316 317 assertEquals("actual vs expected count", values.length, expected.length); 318 319 for (int i = 0; i < values.length; i++) 320 { 321 assertEquals("asString " + i, expected[i], DataType.asString(values[i])); 322 } 323 } 324 325 public void testGetSqlValue() throws Exception 326 { 327 Integer [] expected = { 328 null, 329 new Integer (5), 330 new Integer (Integer.MAX_VALUE - 1), 331 new Integer (Integer.MIN_VALUE + 1), 332 new Integer (-7500), 333 }; 334 335 ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet(); 336 resultSet.addExpectedIndexedValues(expected); 337 338 for (int i = 0; i < expected.length; i++) 339 { 340 Object expectedValue = expected[i]; 341 342 for (int j = 0; j < TYPES.length; j++) 343 { 344 DataType dataType = TYPES[j]; 345 Object actualValue = dataType.getSqlValue(i + 1, resultSet); 346 assertEquals("value " + j, expectedValue, actualValue); 347 } 348 } 349 } 350 351 } 352 | Popular Tags |