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