1 21 package org.dbunit.dataset.filter; 22 23 import org.dbunit.dataset.*; 24 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.List ; 28 29 34 public class IncludeTableFilterTest extends AbstractTableFilterTest 35 { 36 static final String MATCHING_NAME = "aBcDe"; 37 static final String [] MATCHING_PATTERNS = { 38 "?bcde", 39 "?bc*", 40 "*", 41 "a?cde", 42 "abcd?", 43 "*e", 44 "a*", 45 "a*e", 46 "a*d*e", 47 "a**e", 48 "abcde*", 49 "*abcde", 50 "?????", 51 }; 52 static final String [] NONMATCHING_PATTERNS = { 53 "?abcde", 54 "abcde?", 55 "*f*", 56 "??????", 57 "????", 58 }; 59 60 61 public IncludeTableFilterTest(String s) 62 { 63 super(s); 64 } 65 66 public void testAccept() throws Exception 67 { 68 String [] validNames = getExpectedNames(); 69 ITableFilter filter = new IncludeTableFilter(validNames); 70 71 for (int i = 0; i < validNames.length; i++) 72 { 73 String validName = validNames[i]; 74 assertEquals(validName, true, filter.accept(validName)); 75 } 76 } 77 78 public void testIsCaseInsensitiveValidName() throws Exception 79 { 80 String [] validNames = getExpectedNames(); 81 ITableFilter filter = new IncludeTableFilter(validNames); 82 83 for (int i = 0; i < validNames.length; i++) 84 { 85 String validName = validNames[i]; 86 assertEquals(validName, true, filter.accept(validName)); 87 } 88 } 89 90 public void testIsValidNameAndInvalid() throws Exception 91 { 92 String [] invalidNames = new String [] { 93 "INVALID_TABLE", 94 "UNKNOWN_TABLE", 95 }; 96 String [] validNames = getExpectedNames(); 97 ITableFilter filter = new IncludeTableFilter(validNames); 98 99 for (int i = 0; i < invalidNames.length; i++) 100 { 101 String invalidName = invalidNames[i]; 102 assertEquals(invalidName, false, filter.accept(invalidName)); 103 } 104 } 105 106 public void testGetTableNames() throws Exception 107 { 108 String [] expectedNames = getExpectedNames(); 109 ITableFilter filter = new IncludeTableFilter(expectedNames); 110 111 IDataSet dataSet = createDataSet(); 112 assertTrue("dataset names count", 113 dataSet.getTableNames().length > expectedNames.length); 114 115 String [] actualNames = filter.getTableNames(dataSet); 116 assertEquals("name count", expectedNames.length, actualNames.length); 117 assertEquals("names", 118 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 119 } 120 121 public void testGetTableNamesAndTableNotInDecoratedDataSet() throws Exception 122 { 123 String [] expectedNames = getExpectedNames(); 124 125 List filterNameList = new ArrayList (Arrays.asList(expectedNames)); 126 filterNameList.add("UNKNOWN_TABLE"); 127 String [] filterNames = (String [])filterNameList.toArray(new String [0]); 128 ITableFilter filter = new IncludeTableFilter(filterNames); 129 130 IDataSet dataSet = createDataSet(); 131 assertTrue("dataset names count", 132 dataSet.getTableNames().length > expectedNames.length); 133 134 String [] actualNames = filter.getTableNames(dataSet); 135 assertEquals("name count", expectedNames.length, actualNames.length); 136 assertEquals("names", 137 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 138 } 139 140 public void testGetDuplicateTableNames() throws Exception 141 { 142 String [] expectedNames = getExpectedDuplicateNames(); 143 ITableFilter filter = new IncludeTableFilter(expectedNames); 144 145 IDataSet dataSet = createDuplicateDataSet(); 146 assertTrue("dataset names count", 147 dataSet.getTableNames().length > expectedNames.length); 148 149 String [] actualNames = filter.getTableNames(dataSet); 150 assertEquals("name count", expectedNames.length, actualNames.length); 151 assertEquals("names", 152 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 153 } 154 155 public void testGetCaseInsensitiveTableNames() throws Exception 156 { 157 String [] filterNames = getExpectedNames(); 158 ITableFilter filter = new IncludeTableFilter(filterNames); 159 160 String [] expectedNames = getExpectedLowerNames(); 161 IDataSet dataSet = new LowerCaseDataSet(createDataSet()); 162 assertTrue("dataset names count", 163 dataSet.getTableNames().length > expectedNames.length); 164 165 String [] actualNames = filter.getTableNames(dataSet); 166 assertEquals("name count", expectedNames.length, actualNames.length); 167 assertEquals("names", 168 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 169 } 170 171 public void testGetReverseTableNames() throws Exception 172 { 173 String [] expectedNames = getExpectedNames(); 174 String [] filterNames = DataSetUtils.reverseStringArray(expectedNames); 175 ITableFilter filter = new IncludeTableFilter(filterNames); 176 177 IDataSet dataSet = createDataSet(); 178 assertTrue("dataset names count", 179 dataSet.getTableNames().length > expectedNames.length); 180 181 String [] actualNames = filter.getTableNames(dataSet); 182 assertEquals("name count", expectedNames.length, actualNames.length); 183 assertEquals("names", 184 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 185 } 186 187 public void testIterator() throws Exception 188 { 189 String [] expectedNames = getExpectedNames(); 190 ITableFilter filter = new IncludeTableFilter(expectedNames); 191 192 IDataSet dataSet = createDataSet(); 193 assertTrue("dataset names count", 194 dataSet.getTableNames().length > expectedNames.length); 195 196 ITable[] actualTables = DataSetUtils.getTables( 197 filter.iterator(dataSet, false)); 198 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 199 assertEquals("table count", expectedNames.length, actualTables.length); 200 assertEquals("table names", 201 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 202 } 203 204 public void testIteratorWithDuplicateTables() throws Exception 205 { 206 String [] expectedNames = getExpectedDuplicateNames(); 207 ITableFilter filter = new IncludeTableFilter(expectedNames); 208 209 IDataSet dataSet = createDuplicateDataSet(); 210 assertTrue("dataset names count", 211 dataSet.getTableNames().length > expectedNames.length); 212 213 ITable[] actualTables = DataSetUtils.getTables( 214 filter.iterator(dataSet, false)); 215 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 216 assertEquals("table count", expectedNames.length, actualTables.length); 217 assertEquals("table names", 218 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 219 } 220 221 public void testCaseInsensitiveIterator() throws Exception 222 { 223 ITableFilter filter = new IncludeTableFilter(getExpectedNames()); 224 String [] lowerNames = getExpectedLowerNames(); 225 226 IDataSet dataSet = new LowerCaseDataSet(createDataSet()); 227 assertTrue("dataset names count", 228 dataSet.getTableNames().length > lowerNames.length); 229 230 ITable[] actualTables = DataSetUtils.getTables( 231 filter.iterator(dataSet, false)); 232 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 233 assertEquals("table count", lowerNames.length, actualTables.length); 234 assertEquals("table names", 235 Arrays.asList(lowerNames), Arrays.asList(actualNames)); 236 } 237 238 public void testReverseIterator() throws Exception 239 { 240 String [] filterNames = getExpectedNames(); 241 String [] expectedNames = DataSetUtils.reverseStringArray(filterNames); 242 ITableFilter filter = new IncludeTableFilter(filterNames); 243 244 IDataSet dataSet = createDataSet(); 245 assertTrue("dataset names count", 246 dataSet.getTableNames().length > expectedNames.length); 247 248 ITable[] actualTables = DataSetUtils.getTables( 249 filter.iterator(dataSet, true)); 250 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 251 assertEquals("table count", expectedNames.length, actualTables.length); 252 assertEquals("table names", 253 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 254 } 255 256 public void testIteratorAndTableNotInDecoratedDataSet() throws Exception 257 { 258 String [] expectedNames = getExpectedNames(); 259 260 List filterNameList = new ArrayList (Arrays.asList(expectedNames)); 261 filterNameList.add("UNKNOWN_TABLE"); 262 String [] filterNames = (String [])filterNameList.toArray(new String [0]); 263 ITableFilter filter = new IncludeTableFilter(filterNames); 264 265 IDataSet dataSet = createDataSet(); 266 assertTrue("dataset names count", 267 dataSet.getTableNames().length > expectedNames.length); 268 269 ITable[] actualTables = DataSetUtils.getTables( 270 filter.iterator(dataSet, false)); 271 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 272 assertEquals("table count", expectedNames.length, actualTables.length); 273 assertEquals("table names", 274 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 275 } 276 277 279 public void testIsValidNameWithPatterns() throws Exception 280 { 281 String validName = MATCHING_NAME; 282 283 String [] patterns = MATCHING_PATTERNS; 284 for (int i = 0; i < patterns.length; i++) 285 { 286 String pattern = patterns[i]; 287 IncludeTableFilter filter = new IncludeTableFilter(); 288 filter.includeTable(pattern); 289 assertEquals(pattern, true, filter.accept(validName)); 290 } 291 } 292 293 public void testIsValidNameInvalidWithPatterns() throws Exception 294 { 295 String validName = MATCHING_NAME; 296 297 String [] patterns = NONMATCHING_PATTERNS; 298 for (int i = 0; i < patterns.length; i++) 299 { 300 String pattern = patterns[i]; 301 IncludeTableFilter filter = new IncludeTableFilter(); 302 filter.includeTable(pattern); 303 assertEquals(pattern, false, filter.accept(validName)); 304 } 305 } 306 307 public void testGetTableNamesWithPatterns() throws Exception 308 { 309 String [] expectedNames = new String [] {MATCHING_NAME}; 310 IDataSet dataSet = new DefaultDataSet(new ITable[] { 311 new DefaultTable(MATCHING_NAME), 312 new DefaultTable("toto"), 313 new DefaultTable("1234"), 314 new DefaultTable("fedcba"), 315 }); 316 assertTrue("dataset names count", 317 dataSet.getTableNames().length > expectedNames.length); 318 319 String [] patterns = MATCHING_PATTERNS; 320 for (int i = 0; i < patterns.length; i++) 321 { 322 String pattern = patterns[i]; 323 IncludeTableFilter filter = new IncludeTableFilter(); 324 filter.includeTable(pattern); 325 326 if (pattern.equals("*")) 328 { 329 String [] actualNames = filter.getTableNames(dataSet); 330 assertEquals("name count - " + pattern, 331 dataSet.getTableNames().length, actualNames.length); 332 assertEquals("names - " + pattern, 333 Arrays.asList(dataSet.getTableNames()), 334 Arrays.asList(actualNames)); 335 } 336 else 337 { 338 String [] actualNames = filter.getTableNames(dataSet); 339 assertEquals("name count - " + pattern, 340 expectedNames.length, actualNames.length); 341 assertEquals("names - " + pattern, 342 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 343 } 344 } 345 } 346 347 public void testGetTableNamesWithNonMatchingPatterns() throws Exception 348 { 349 IDataSet dataSet = new DefaultDataSet(new ITable[] { 350 new DefaultTable(MATCHING_NAME), 351 }); 352 assertTrue("dataset names count", 353 dataSet.getTableNames().length > 0); 354 355 String [] patterns = NONMATCHING_PATTERNS; 356 for (int i = 0; i < patterns.length; i++) 357 { 358 String pattern = patterns[i]; 359 IncludeTableFilter filter = new IncludeTableFilter(); 360 filter.includeTable(pattern); 361 362 String [] actualNames = filter.getTableNames(dataSet); 363 assertEquals("name count - " + pattern, 0, actualNames.length); 364 } 365 } 366 367 public void testGetTablesWithPatterns() throws Exception 368 { 369 String [] expectedNames = new String [] {MATCHING_NAME}; 370 IDataSet dataSet = new DefaultDataSet(new ITable[] { 371 new DefaultTable(MATCHING_NAME), 372 new DefaultTable("toto"), 373 new DefaultTable("1234"), 374 new DefaultTable("fedcba"), 375 }); 376 assertTrue("dataset names count", 377 dataSet.getTableNames().length > expectedNames.length); 378 379 String [] patterns = MATCHING_PATTERNS; 380 for (int i = 0; i < patterns.length; i++) 381 { 382 String pattern = patterns[i]; 383 IncludeTableFilter filter = new IncludeTableFilter(); 384 filter.includeTable(pattern); 385 386 if (pattern.equals("*")) 388 { 389 ITable[] actualTables = DataSetUtils.getTables( 390 filter.iterator(dataSet, false)); 391 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 392 assertEquals("table count - " + pattern, 393 dataSet.getTableNames().length, actualNames.length); 394 assertEquals("table names - " + pattern, 395 Arrays.asList(dataSet.getTableNames()), 396 Arrays.asList(actualNames)); 397 } 398 else 399 { 400 ITable[] actualTables = DataSetUtils.getTables( 401 filter.iterator(dataSet, false)); 402 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 403 assertEquals("table count - " + pattern, 404 expectedNames.length, actualTables.length); 405 assertEquals("table names - " + pattern, 406 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 407 } 408 } 409 } 410 411 public void testGetTablesWithNonMatchingPatterns() throws Exception 412 { 413 IDataSet dataSet = new DefaultDataSet(new ITable[] { 414 new DefaultTable(MATCHING_NAME), 415 }); 416 assertTrue("dataset names count", 417 dataSet.getTableNames().length > 0); 418 419 String [] patterns = NONMATCHING_PATTERNS; 420 for (int i = 0; i < patterns.length; i++) 421 { 422 String pattern = patterns[i]; 423 IncludeTableFilter filter = new IncludeTableFilter(); 424 filter.includeTable(pattern); 425 426 ITable[] actualTables = DataSetUtils.getTables( 427 filter.iterator(dataSet, false)); 428 assertEquals("table count - " + pattern, 0, actualTables.length); 429 } 430 } 431 432 } 433 | Popular Tags |