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