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