1 21 package org.dbunit.dataset.filter; 22 23 import org.dbunit.database.AmbiguousTableNameException; 24 import org.dbunit.dataset.*; 25 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 30 35 public class SequenceTableFilterTest extends AbstractTableFilterTest 36 { 37 38 public SequenceTableFilterTest(String s) 39 { 40 super(s); 41 } 42 43 public void testAccept() throws Exception 44 { 45 String [] validNames = getExpectedNames(); 46 ITableFilter filter = new SequenceTableFilter(validNames); 47 48 for (int i = 0; i < validNames.length; i++) 49 { 50 String validName = validNames[i]; 51 assertEquals(validName, true, filter.accept(validName)); 52 } 53 } 54 55 public void testIsCaseInsensitiveValidName() throws Exception 56 { 57 String [] validNames = getExpectedNames(); 58 ITableFilter filter = new SequenceTableFilter(validNames); 59 60 for (int i = 0; i < validNames.length; i++) 61 { 62 String validName = validNames[i]; 63 assertEquals(validName, true, filter.accept(validName)); 64 } 65 } 66 67 public void testIsValidNameAndInvalid() throws Exception 68 { 69 String [] invalidNames = new String [] { 70 "INVALID_TABLE", 71 "UNKNOWN_TABLE", 72 }; 73 String [] validNames = getExpectedNames(); 74 ITableFilter filter = new SequenceTableFilter(validNames); 75 76 for (int i = 0; i < invalidNames.length; i++) 77 { 78 String invalidName = invalidNames[i]; 79 assertEquals(invalidName, false, filter.accept(invalidName)); 80 } 81 } 82 83 public void testGetTableNames() throws Exception 84 { 85 String [] expectedNames = getExpectedNames(); 86 ITableFilter filter = new SequenceTableFilter(expectedNames); 87 88 IDataSet dataSet = createDataSet(); 89 assertTrue("dataset names count", 90 dataSet.getTableNames().length > expectedNames.length); 91 92 String [] actualNames = filter.getTableNames(dataSet); 93 assertEquals("name count", expectedNames.length, actualNames.length); 94 assertEquals("names", 95 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 96 } 97 98 public void testGetTableNamesAndTableNotInDecoratedDataSet() throws Exception 99 { 100 String [] expectedNames = getExpectedNames(); 101 102 List filterNameList = new ArrayList (Arrays.asList(expectedNames)); 103 filterNameList.add("UNKNOWN_TABLE"); 104 String [] filterNames = (String [])filterNameList.toArray(new String [0]); 105 ITableFilter filter = new SequenceTableFilter(filterNames); 106 107 IDataSet dataSet = createDataSet(); 108 assertTrue("dataset names count", 109 dataSet.getTableNames().length > expectedNames.length); 110 111 String [] actualNames = filter.getTableNames(dataSet); 112 assertEquals("name count", expectedNames.length, actualNames.length); 113 assertEquals("names", 114 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 115 } 116 117 public void testGetDuplicateTableNames() throws Exception 118 { 119 String [] expectedNames = getExpectedDuplicateNames(); 120 ITableFilter filter = new SequenceTableFilter(expectedNames); 121 122 IDataSet dataSet = createDuplicateDataSet(); 123 assertTrue("dataset names count", 124 dataSet.getTableNames().length > expectedNames.length); 125 126 try 127 { 128 filter.getTableNames(dataSet); 129 fail("Should not be here!"); 130 } 131 catch (AmbiguousTableNameException e) 132 { 133 134 } 135 } 136 137 public void testGetCaseInsensitiveTableNames() throws Exception 138 { 139 String [] filterNames = getExpectedNames(); 140 ITableFilter filter = new SequenceTableFilter(filterNames); 141 142 String [] expectedNames = getExpectedLowerNames(); 143 IDataSet dataSet = new LowerCaseDataSet(createDataSet()); 144 assertTrue("dataset names count", 145 dataSet.getTableNames().length > expectedNames.length); 146 147 String [] actualNames = filter.getTableNames(dataSet); 148 assertEquals("name count", expectedNames.length, actualNames.length); 149 assertEquals("names", 150 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 151 } 152 153 public void testGetReverseTableNames() throws Exception 154 { 155 String [] expectedNames = DataSetUtils.reverseStringArray(getExpectedNames()); 156 ITableFilter filter = new SequenceTableFilter(expectedNames); 157 158 IDataSet dataSet = createDataSet(); 159 assertTrue("dataset names count", 160 dataSet.getTableNames().length > expectedNames.length); 161 162 String [] actualNames = filter.getTableNames(dataSet); 163 assertEquals("name count", expectedNames.length, actualNames.length); 164 assertEquals("names", 165 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 166 } 167 168 public void testIterator() throws Exception 169 { 170 String [] expectedNames = getExpectedNames(); 171 ITableFilter filter = new SequenceTableFilter(expectedNames); 172 173 IDataSet dataSet = createDataSet(); 174 assertTrue("dataset names count", 175 dataSet.getTableNames().length > expectedNames.length); 176 177 ITable[] actualTables = DataSetUtils.getTables( 178 filter.iterator(dataSet, false)); 179 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 180 assertEquals("table count", expectedNames.length, actualTables.length); 181 assertEquals("table names", 182 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 183 } 184 185 public void testIteratorWithDuplicateTables() throws Exception 186 { 187 String [] expectedNames = getExpectedDuplicateNames(); 188 ITableFilter filter = new SequenceTableFilter(expectedNames); 189 190 IDataSet dataSet = createDuplicateDataSet(); 191 assertTrue("dataset names count", 192 dataSet.getTableNames().length > expectedNames.length); 193 194 try 195 { 196 DataSetUtils.getTables(filter.iterator(dataSet, false)); 197 fail("Should not be here!"); 198 } 199 catch (AmbiguousTableNameException e) 200 { 201 } 202 } 203 204 public void testCaseInsensitiveIterator() throws Exception 205 { 206 ITableFilter filter = new SequenceTableFilter(getExpectedNames()); 207 String [] lowerNames = getExpectedLowerNames(); 208 209 IDataSet dataSet = new LowerCaseDataSet(createDataSet()); 210 assertTrue("dataset names count", 211 dataSet.getTableNames().length > lowerNames.length); 212 213 ITable[] actualTables = DataSetUtils.getTables( 214 filter.iterator(dataSet, false)); 215 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 216 assertEquals("table count", lowerNames.length, actualTables.length); 217 assertEquals("table names", 218 Arrays.asList(lowerNames), Arrays.asList(actualNames)); 219 } 220 221 public void testReverseIterator() throws Exception 222 { 223 String [] filterNames = getExpectedNames(); 224 String [] expectedNames = DataSetUtils.reverseStringArray(filterNames); 225 ITableFilter filter = new SequenceTableFilter(filterNames); 226 227 IDataSet dataSet = createDataSet(); 228 assertTrue("dataset names count", 229 dataSet.getTableNames().length > expectedNames.length); 230 231 ITable[] actualTables = DataSetUtils.getTables( 232 filter.iterator(dataSet, true)); 233 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 234 assertEquals("table count", expectedNames.length, actualTables.length); 235 assertEquals("table names", 236 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 237 } 238 239 public void testIteratorAndTableNotInDecoratedDataSet() throws Exception 240 { 241 String [] expectedNames = getExpectedNames(); 242 243 List filterNameList = new ArrayList (Arrays.asList(expectedNames)); 244 filterNameList.add("UNKNOWN_TABLE"); 245 String [] filterNames = (String [])filterNameList.toArray(new String [0]); 246 ITableFilter filter = new SequenceTableFilter(filterNames); 247 248 IDataSet dataSet = createDataSet(); 249 assertTrue("dataset names count", 250 dataSet.getTableNames().length > expectedNames.length); 251 252 ITable[] actualTables = DataSetUtils.getTables( 253 filter.iterator(dataSet, false)); 254 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 255 assertEquals("table count", expectedNames.length, actualTables.length); 256 assertEquals("table names", 257 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 258 } 259 260 262 public void testIteratorWithDifferentSequence() throws Exception 263 { 264 String [] expectedNames = DataSetUtils.reverseStringArray(getExpectedNames()); 265 ITableFilter filter = new SequenceTableFilter(expectedNames); 266 267 IDataSet dataSet = createDataSet(); 268 assertTrue("dataset names count", 269 dataSet.getTableNames().length > expectedNames.length); 270 271 ITable[] actualTables = DataSetUtils.getTables( 272 filter.iterator(dataSet, false)); 273 String [] actualNames = new DefaultDataSet(actualTables).getTableNames(); 274 assertEquals("table count", expectedNames.length, actualTables.length); 275 assertEquals("table names", 276 Arrays.asList(expectedNames), Arrays.asList(actualNames)); 277 } 278 279 } 280 | Popular Tags |