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.math.BigInteger ; 29 import java.sql.Types ; 30 31 35 36 public class NumberDataTypeTest extends AbstractDataTypeTest 37 { 38 private final static DataType[] TYPES = {DataType.NUMERIC, DataType.DECIMAL}; 39 40 public NumberDataTypeTest(String name) 41 { 42 super(name); 43 } 44 45 public void testToString() throws Exception 46 { 47 String [] expected = {"NUMERIC", "DECIMAL"}; 48 49 assertEquals("type count", expected.length, TYPES.length); 50 for (int i = 0; i < TYPES.length; i++) 51 { 52 assertEquals("name", expected[i], TYPES[i].toString()); 53 } 54 } 55 56 public void testGetTypeClass() throws Exception 57 { 58 for (int i = 0; i < TYPES.length; i++) 59 { 60 assertEquals("class", BigDecimal .class, TYPES[i].getTypeClass()); 61 } 62 } 63 64 public void testIsNumber() throws Exception 65 { 66 for (int i = 0; i < TYPES.length; i++) 67 { 68 assertEquals("is number", true, TYPES[i].isNumber()); 69 } 70 } 71 72 public void testIsDateTime() throws Exception 73 { 74 for (int i = 0; i < TYPES.length; i++) 75 { 76 assertEquals("is date/time", false, TYPES[i].isDateTime()); 77 } 78 } 79 80 public void testTypeCast() throws Exception 81 { 82 Object [] values = { 83 null, 84 new BigDecimal (1234), 85 "1234", 86 "12.34", 87 Boolean.TRUE, 88 Boolean.FALSE, 89 }; 90 BigDecimal [] expected = { 91 null, 92 new BigDecimal (1234), 93 new BigDecimal (1234), 94 new BigDecimal ("12.34"), 95 new BigDecimal ("1"), 96 new BigDecimal ("0"), 97 }; 98 99 assertEquals("actual vs expected count", values.length, expected.length); 100 101 for (int i = 0; i < TYPES.length; i++) 102 { 103 for (int j = 0; j < values.length; j++) 104 { 105 assertEquals("typecast " + j, expected[j], 106 TYPES[i].typeCast(values[j])); 107 } 108 } 109 } 110 111 public void testTypeCastNone() throws Exception 112 { 113 for (int i = 0; i < TYPES.length; i++) 114 { 115 DataType type = TYPES[i]; 116 assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE)); 117 } 118 } 119 120 public void testTypeCastInvalid() throws Exception 121 { 122 Object [] values = {new Object (), "bla"}; 123 124 for (int i = 0; i < TYPES.length; i++) 125 { 126 for (int j = 0; j < values.length; j++) 127 { 128 try 129 { 130 TYPES[i].typeCast(values[j]); 131 fail("Should throw TypeCastException"); 132 } 133 catch (TypeCastException e) 134 { 135 } 136 } 137 } 138 } 139 140 public void testCompareEquals() throws Exception 141 { 142 Object [] values1 = { 143 null, 144 new BigDecimal (1234), 145 "1234", 146 "12.34", 147 Boolean.TRUE, 148 Boolean.FALSE, 149 new BigDecimal (123.4), 150 "123", 151 }; 152 Object [] values2 = { 153 null, 154 new BigDecimal (1234), 155 new BigDecimal (1234), 156 new BigDecimal ("12.34"), 157 new BigDecimal ("1"), 158 new BigDecimal ("0"), 159 new BigDecimal (123.4000), 160 new BigDecimal ("123.0"), 161 }; 162 163 assertEquals("values count", values1.length, values2.length); 164 165 for (int i = 0; i < TYPES.length; i++) 166 { 167 for (int j = 0; j < values1.length; j++) 168 { 169 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j])); 170 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j])); 171 } 172 } 173 } 174 175 public void testCompareInvalid() throws Exception 176 { 177 Object [] values1 = { 178 new Object (), 179 "bla", 180 new java.util.Date () 181 }; 182 Object [] values2 = { 183 null, 184 null, 185 null 186 }; 187 188 assertEquals("values count", values1.length, values2.length); 189 190 for (int i = 0; i < TYPES.length; i++) 191 { 192 for (int j = 0; j < values1.length; j++) 193 { 194 try 195 { 196 TYPES[i].compare(values1[j], values2[j]); 197 fail("Should throw TypeCastException"); 198 } 199 catch (TypeCastException e) 200 { 201 } 202 203 try 204 { 205 TYPES[i].compare(values2[j], values1[j]); 206 fail("Should throw TypeCastException"); 207 } 208 catch (TypeCastException e) 209 { 210 } 211 } 212 } 213 } 214 215 public void testCompareDifferent() throws Exception 216 { 217 Object [] less = { 218 null, 219 "-7500", 220 new BigDecimal ("-0.01"), 221 new BigInteger ("1234"), 222 }; 223 224 Object [] greater = { 225 "0", 226 "5.555", 227 new BigDecimal ("0.01"), 228 new BigDecimal ("1234.5"), 229 }; 230 231 assertEquals("values count", less.length, greater.length); 232 233 for (int i = 0; i < TYPES.length; i++) 234 { 235 for (int j = 0; j < less.length; j++) 236 { 237 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0); 238 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0); 239 } 240 } 241 } 242 243 public void testSqlType() throws Exception 244 { 245 int[] sqlTypes = {Types.NUMERIC, Types.DECIMAL}; 246 247 assertEquals("count", sqlTypes.length, TYPES.length); 248 for (int i = 0; i < TYPES.length; i++) 249 { 250 assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i])); 251 assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString())); 252 assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType()); 253 } 254 } 255 256 public void testForObject() throws Exception 257 { 258 assertEquals(DataType.NUMERIC, DataType.forObject(new BigDecimal (1234))); 259 } 260 261 public void testAsString() throws Exception 262 { 263 BigDecimal [] values = { 264 new BigDecimal ("1234"), 265 }; 266 267 String [] expected = { 268 "1234", 269 }; 270 271 assertEquals("actual vs expected count", values.length, expected.length); 272 273 for (int i = 0; i < values.length; i++) 274 { 275 assertEquals("asString " + i, expected[i], DataType.asString(values[i])); 276 } 277 } 278 279 public void testGetSqlValue() throws Exception 280 { 281 BigDecimal [] expected = { 282 null, 283 new BigDecimal ("12.34"), 284 }; 285 286 ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet(); 287 resultSet.addExpectedIndexedValues(expected); 288 289 for (int i = 0; i < expected.length; i++) 290 { 291 Object expectedValue = expected[i]; 292 293 for (int j = 0; j < TYPES.length; j++) 294 { 295 DataType dataType = TYPES[j]; 296 Object actualValue = dataType.getSqlValue(i + 1, resultSet); 297 assertEquals("value " + j, expectedValue, actualValue); 298 } 299 } 300 } 301 302 } 303 | Popular Tags |