1 21 22 package org.dbunit.dataset; 23 24 import org.dbunit.database.AmbiguousTableNameException; 25 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 30 35 public abstract class AbstractDataSetTest extends AbstractTest 36 { 37 public AbstractDataSetTest(String s) 38 { 39 super(s); 40 } 41 42 protected int[] getExpectedDuplicateRows() 43 { 44 return new int[] {1, 0, 2}; 45 } 46 47 53 public static IDataSet removeExtraTestTables(IDataSet dataSet) throws Exception 54 { 55 String [] names = dataSet.getTableNames(); 56 57 List nameList = new ArrayList (Arrays.asList(names)); 60 nameList.remove("BLOB_TABLE"); 61 nameList.remove("CLOB_TABLE"); 62 nameList.remove("DBUNIT.BLOB_TABLE"); 63 nameList.remove("DBUNIT.CLOB_TABLE"); 64 68 nameList.remove("DBUNIT.dtproperties"); 69 nameList.remove("dtproperties"); 70 74 nameList.remove("DBUNIT.IDENTITY_TABLE"); 75 nameList.remove("IDENTITY_TABLE"); 76 nameList.remove("DBUNIT.TEST_IDENTITY_NOT_PK"); 77 nameList.remove("TEST_IDENTITY_NOT_PK"); 78 79 names = (String [])nameList.toArray(new String [0]); 80 81 return new FilteredDataSet(names, dataSet); 82 } 83 84 88 protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 89 { 90 IDataSet dataSet = createDuplicateDataSet(); 91 ITable[] tables = DataSetUtils.getTables(dataSet.iterator()); 92 ITable lowerTable = tables[0]; 93 dataSet = new DefaultDataSet(new ITable[]{ 94 new CompositeTable(getDuplicateTableName().toLowerCase(), lowerTable), 95 tables[1], 96 tables[2], 97 }); 98 return dataSet; 99 } 100 101 protected abstract IDataSet createDataSet() throws Exception ; 102 103 protected abstract IDataSet createDuplicateDataSet() throws Exception ; 104 105 protected void assertEqualsTableName(String mesage, String expected, 106 String actual) 107 { 108 assertEquals(mesage, expected, actual); 109 } 110 111 public void testGetTableNames() throws Exception 112 { 113 String [] expected = getExpectedNames(); 114 assertContainsIgnoreCase("minimal names subset", 115 super.getExpectedNames(), expected); 116 117 IDataSet dataSet = createDataSet(); 118 String [] names = dataSet.getTableNames(); 119 120 assertEquals("table count", expected.length, names.length); 121 for (int i = 0; i < expected.length; i++) 122 { 123 assertEqualsTableName("name " + i, expected[i], names[i]); 124 } 125 } 126 127 public void testGetTableNamesDefensiveCopy() throws Exception 128 { 129 IDataSet dataSet = createDataSet(); 130 assertTrue("Should not be same intance", 131 dataSet.getTableNames() != dataSet.getTableNames()); 132 } 133 134 public void testGetTable() throws Exception 135 { 136 String [] expected = getExpectedNames(); 137 138 IDataSet dataSet = createDataSet(); 139 for (int i = 0; i < expected.length; i++) 140 { 141 ITable table = dataSet.getTable(expected[i]); 142 assertEqualsTableName("name " + i, expected[i], table.getTableMetaData().getTableName()); 143 } 144 } 145 146 public void testGetUnknownTable() throws Exception 147 { 148 IDataSet dataSet = createDataSet(); 149 try 150 { 151 dataSet.getTable("UNKNOWN_TABLE"); 152 fail("Should throw a NoSuchTableException"); 153 } 154 catch (NoSuchTableException e) 155 { 156 } 157 } 158 159 public void testGetTableMetaData() throws Exception 160 { 161 String [] expected = getExpectedNames(); 162 163 IDataSet dataSet = createDataSet(); 164 for (int i = 0; i < expected.length; i++) 165 { 166 ITableMetaData metaData = dataSet.getTableMetaData(expected[i]); 167 assertEqualsTableName("name " + i, expected[i], metaData.getTableName()); 168 } 169 } 170 171 public void testGetUnknownTableMetaData() throws Exception 172 { 173 IDataSet dataSet = createDataSet(); 174 try 175 { 176 dataSet.getTableMetaData("UNKNOWN_TABLE"); 177 fail("Should throw a NoSuchTableException"); 178 } 179 catch (NoSuchTableException e) 180 { 181 } 182 } 183 184 public void testGetTables() throws Exception 185 { 186 String [] expected = getExpectedNames(); 187 assertContainsIgnoreCase("minimal names subset", 188 super.getExpectedNames(), expected); 189 190 IDataSet dataSet = createDataSet(); 191 ITable[] tables = dataSet.getTables(); 192 193 assertEquals("table count", expected.length, tables.length); 194 for (int i = 0; i < expected.length; i++) 195 { 196 assertEqualsTableName("name " + i, expected[i], 197 tables[i].getTableMetaData().getTableName()); 198 } 199 } 200 201 public void testGetTablesDefensiveCopy() throws Exception 202 { 203 IDataSet dataSet = createDataSet(); 204 assertTrue("Should not be same intance", 205 dataSet.getTables() != dataSet.getTables()); 206 } 207 208 public void testGetDuplicateTables() throws Exception 209 { 210 String [] expectedNames = getExpectedDuplicateNames(); 211 int[] expectedRows = getExpectedDuplicateRows(); 212 assertEquals(expectedNames.length, expectedRows.length); 213 214 IDataSet dataSet = createDuplicateDataSet(); 215 ITable[] tables = dataSet.getTables(); 216 217 assertEquals("table count", expectedNames.length, tables.length); 218 for (int i = 0; i < expectedNames.length; i++) 219 { 220 ITable table = tables[i]; 221 String name = table.getTableMetaData().getTableName(); 222 assertEqualsTableName("name " + i, expectedNames[i], name); 223 assertEquals("row count", expectedRows[i], table.getRowCount()); 224 } 225 } 226 227 public void testGetDuplicateTableNames() throws Exception 228 { 229 String [] expected = getExpectedDuplicateNames(); 230 231 IDataSet dataSet = createDuplicateDataSet(); 232 String [] names = dataSet.getTableNames(); 233 234 assertEquals("table count", expected.length, names.length); 235 for (int i = 0; i < expected.length; i++) 236 { 237 assertEqualsTableName("name " + i, expected[i], names[i]); 238 } 239 } 240 241 public void testGetDuplicateTable() throws Exception 242 { 243 IDataSet dataSet = createDuplicateDataSet(); 244 try 245 { 246 dataSet.getTable(getDuplicateTableName()); 247 fail("Should throw AmbiguousTableNameException"); 248 } 249 catch (AmbiguousTableNameException e) 250 { 251 } 252 } 253 254 public void testGetDuplicateTableMetaData() throws Exception 255 { 256 IDataSet dataSet = createDuplicateDataSet(); 257 try 258 { 259 dataSet.getTableMetaData(getDuplicateTableName()); 260 fail("Should throw AmbiguousTableNameException"); 261 } 262 catch (AmbiguousTableNameException e) 263 { 264 } 265 } 266 267 public void testGetCaseInsensitiveTable() throws Exception 268 { 269 String [] expectedNames = getExpectedLowerNames(); 270 271 IDataSet dataSet = createDataSet(); 272 for (int i = 0; i < expectedNames.length; i++) 273 { 274 String expected = expectedNames[i]; 275 ITable table = dataSet.getTable(expected); 276 String actual = table.getTableMetaData().getTableName(); 277 278 if (!expected.equalsIgnoreCase(actual)) 279 { 280 assertEquals("name " + i, expected, actual); 281 } 282 } 283 } 284 285 public void testGetCaseInsensitiveTableMetaData() throws Exception 286 { 287 String [] expectedNames = getExpectedLowerNames(); 288 IDataSet dataSet = createDataSet(); 289 290 for (int i = 0; i < expectedNames.length; i++) 291 { 292 String expected = expectedNames[i]; 293 ITableMetaData metaData = dataSet.getTableMetaData(expected); 294 String actual = metaData.getTableName(); 295 296 if (!expected.equalsIgnoreCase(actual)) 297 { 298 assertEquals("name " + i, expected, actual); 299 } 300 } 301 } 302 303 public void testGetCaseInsensitiveDuplicateTable() throws Exception 304 { 305 IDataSet dataSet = createMultipleCaseDuplicateDataSet(); 306 307 try 308 { 309 dataSet.getTable(getDuplicateTableName().toLowerCase()); 310 fail("Should throw AmbiguousTableNameException"); 311 } 312 catch (AmbiguousTableNameException e) 313 { 314 } 315 } 316 317 public void testGetCaseInsensitiveDuplicateTableMetaData() throws Exception 318 { 319 IDataSet dataSet = createMultipleCaseDuplicateDataSet(); 320 try 321 { 322 dataSet.getTableMetaData(getDuplicateTableName().toLowerCase()); 323 fail("Should throw AmbiguousTableNameException"); 324 } 325 catch (AmbiguousTableNameException e) 326 { 327 } 328 } 329 330 public void testIterator() throws Exception 331 { 332 String [] expected = getExpectedNames(); 333 assertContainsIgnoreCase("minimal names subset", 334 super.getExpectedNames(), expected); 335 336 int i = 0; 337 ITableIterator iterator = createDataSet().iterator(); 338 while(iterator.next()) 339 { 340 assertEqualsTableName("name " + i, expected[i], 341 iterator.getTableMetaData().getTableName()); 342 i++; 343 } 344 345 assertEquals("table count", expected.length, i); 346 } 347 348 public void testIteratorAndDuplicateTable() throws Exception 349 { 350 String [] expectedNames = getExpectedDuplicateNames(); 351 353 int i = 0; 354 ITableIterator iterator = createDuplicateDataSet().iterator(); 355 while(iterator.next()) 356 { 357 ITable table = iterator.getTable(); 358 String name = table.getTableMetaData().getTableName(); 359 assertEqualsTableName("name " + i, expectedNames[i], name); 360 i++; 362 } 363 364 assertEquals("table count", expectedNames.length, i); 365 } 366 367 public void testReverseIterator() throws Exception 368 { 369 String [] expected = DataSetUtils.reverseStringArray(getExpectedNames()); 370 assertContainsIgnoreCase("minimal names subset", 371 super.getExpectedNames(), expected); 372 373 int i = 0; 374 ITableIterator iterator = createDataSet().reverseIterator(); 375 while(iterator.next()) 376 { 377 assertEqualsTableName("name " + i, expected[i], 378 iterator.getTableMetaData().getTableName()); 379 i++; 380 } 381 382 assertEquals("table count", expected.length, i); 383 } 384 385 public void testReverseIteratorAndDuplicateTable() throws Exception 386 { 387 String [] expectedNames = getExpectedDuplicateNames(); 388 int[] expectedRows = getExpectedDuplicateRows(); 389 390 int i = expectedRows.length - 1; 391 int count = 0; 392 ITableIterator iterator = createDuplicateDataSet().reverseIterator(); 393 while(iterator.next()) 394 { 395 ITable table = iterator.getTable(); 396 String name = table.getTableMetaData().getTableName(); 397 assertEqualsTableName("name " + i, expectedNames[i], name); 398 assertEquals("row count", expectedRows[i], table.getRowCount()); 399 i--; 400 count++; 401 } 402 403 assertEquals("table count", expectedNames.length, count); 404 } 405 } 406 407 408 409 410 411 412 413 414 415 416 | Popular Tags |