1 21 22 package org.dbunit.dataset; 23 24 import org.dbunit.database.AmbiguousTableNameException; 25 import org.dbunit.dataset.xml.XmlDataSet; 26 27 import java.io.FileReader ; 28 29 34 public class FilteredDataSetTest extends AbstractDataSetTest 35 { 36 public FilteredDataSetTest(String s) 37 { 38 super(s); 39 } 40 41 protected IDataSet createDataSet() throws Exception 42 { 43 IDataSet dataSet1 = new XmlDataSet( 44 new FileReader ("src/xml/dataSetTest.xml")); 45 IDataSet dataSet2 = new XmlDataSet( 46 new FileReader ("src/xml/filteredDataSetTest.xml")); 47 48 IDataSet dataSet = new CompositeDataSet(dataSet1, dataSet2); 49 assertEquals("count before filter", getExpectedNames().length + 1, 50 dataSet.getTableNames().length); 51 return new FilteredDataSet(getExpectedNames(), dataSet); 52 } 53 54 protected IDataSet createDuplicateDataSet() throws Exception 55 { 56 IDataSet dataSet1 = new XmlDataSet( 57 new FileReader ("src/xml/xmlDataSetDuplicateTest.xml")); 58 IDataSet dataSet2 = new XmlDataSet( 59 new FileReader ("src/xml/filteredDataSetTest.xml")); 60 61 IDataSet dataSet = new CompositeDataSet(dataSet1, dataSet2, false); 62 assertEquals("count before filter", getExpectedDuplicateNames().length + 1, 63 dataSet.getTableNames().length); 64 return new FilteredDataSet(getExpectedDuplicateNames(), dataSet); 65 } 66 67 protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 68 { 69 String [] names = getExpectedDuplicateNames(); 70 names[0] = names[0].toLowerCase(); 71 72 return new FilteredDataSet(names, createDuplicateDataSet()); 73 } 74 75 public void testGetFilteredTableNames() throws Exception 76 { 77 String [] originalNames = getExpectedNames(); 78 String expectedName = originalNames[0]; 79 IDataSet dataSet = createDataSet(); 80 assertTrue("original count", dataSet.getTableNames().length > 1); 81 82 IDataSet filteredDataSet = new FilteredDataSet(new String []{expectedName}, dataSet); 83 assertEquals("filtered count", 1, filteredDataSet.getTableNames().length); 84 assertEquals("filtered names", expectedName, filteredDataSet.getTableNames()[0]); 85 } 86 87 public void testGetFilteredTable() throws Exception 88 { 89 String [] originalNames = getExpectedNames(); 90 IDataSet filteredDataSet = new FilteredDataSet( 91 new String []{originalNames[0]}, createDataSet()); 92 93 94 for (int i = 0; i < originalNames.length; i++) 95 { 96 String name = originalNames[i]; 97 if (i == 0) 98 { 99 assertEquals("table " + i, name, 100 filteredDataSet.getTable(name).getTableMetaData().getTableName()); 101 } 102 else 103 { 104 try 105 { 106 filteredDataSet.getTable(name); 107 fail("Should throw a NoSuchTableException"); 108 } 109 catch (NoSuchTableException e) 110 { 111 } 112 } 113 } 114 } 115 116 public void testGetFilteredTableMetaData() throws Exception 117 { 118 String [] originalNames = getExpectedNames(); 119 IDataSet filteredDataSet = new FilteredDataSet( 120 new String []{originalNames[0]}, createDataSet()); 121 122 123 for (int i = 0; i < originalNames.length; i++) 124 { 125 String name = originalNames[i]; 126 if (i == 0) 127 { 128 assertEquals("table " + i, name, 129 filteredDataSet.getTableMetaData(name).getTableName()); 130 } 131 else 132 { 133 try 134 { 135 filteredDataSet.getTableMetaData(name); 136 fail("Should throw a NoSuchTableException"); 137 } 138 catch (NoSuchTableException e) 139 { 140 } 141 } 142 } 143 } 144 145 public void testGetDuplicateTableNames() throws Exception 146 { 147 IDataSet dataSet = createDuplicateDataSet(); 148 try 149 { 150 dataSet.getTableNames(); 151 fail("Should throw AmbiguousTableNameException"); 152 } 153 catch (AmbiguousTableNameException e) 154 { 155 } 156 } 157 158 public void testGetDuplicateTables() throws Exception 159 { 160 IDataSet dataSet = createDuplicateDataSet(); 161 try 162 { 163 dataSet.getTables(); 164 fail("Should throw AmbiguousTableNameException"); 165 } 166 catch (AmbiguousTableNameException e) 167 { 168 } 169 } 170 171 public void testReverseIteratorAndDuplicateTable() throws Exception 172 { 173 IDataSet dataSet = createDuplicateDataSet(); 174 try 175 { 176 dataSet.iterator(); 177 fail("Should throw AmbiguousTableNameException"); 178 } 179 catch (AmbiguousTableNameException e) 180 { 181 } 182 } 183 184 public void testIteratorAndDuplicateTable() throws Exception 185 { 186 IDataSet dataSet = createDuplicateDataSet(); 187 try 188 { 189 dataSet.reverseIterator(); 190 fail("Should throw AmbiguousTableNameException"); 191 } 192 catch (AmbiguousTableNameException e) 193 { 194 } 195 } 196 } 197 198 199 200 201 202 | Popular Tags |