1 21 package org.dbunit.dataset.stream; 22 23 import com.mockobjects.ExpectationList; 24 import com.mockobjects.Verifiable; 25 import org.dbunit.dataset.Column; 26 import org.dbunit.dataset.DataSetException; 27 import org.dbunit.dataset.DefaultTableMetaData; 28 import org.dbunit.dataset.ITableMetaData; 29 30 import java.util.Arrays ; 31 32 37 public class MockDataSetConsumer implements Verifiable, IDataSetConsumer 38 { 39 private static final ProducerEvent START_DATASET_EVENT = 40 new ProducerEvent("startDataSet()"); 41 private static final ProducerEvent END_DATASET_EVENT = 42 new ProducerEvent("endDataSet()"); 43 44 private final ExpectationList _expectedList = new ExpectationList(""); 45 private String _actualTableName; 46 47 public void addExpectedStartDataSet() throws Exception 48 { 49 _expectedList.addExpected(START_DATASET_EVENT); 50 } 51 52 public void addExpectedEndDataSet() throws Exception 53 { 54 _expectedList.addExpected(END_DATASET_EVENT); 55 } 56 57 public void addExpectedStartTable(ITableMetaData metaData) throws Exception 58 { 59 _expectedList.addExpected(new StartTableEvent(metaData, false)); 60 } 61 62 public void addExpectedStartTable(String tableName, Column[] columns) throws Exception 63 { 64 addExpectedStartTable(new DefaultTableMetaData(tableName, columns)); 65 } 66 67 public void addExpectedStartTableIgnoreColumns(String tableName) throws Exception 68 { 69 _expectedList.addExpected(new StartTableEvent(tableName, true)); 70 } 71 72 public void addExpectedEmptyTable(String tableName, Column[] columns) throws Exception 73 { 74 addExpectedStartTable(tableName, columns); 75 addExpectedEndTable(tableName); 76 } 77 78 public void addExpectedEmptyTableIgnoreColumns(String tableName) throws Exception 79 { 80 addExpectedStartTableIgnoreColumns(tableName); 81 addExpectedEndTable(tableName); 82 } 83 84 public void addExpectedEndTable(String tableName) throws Exception 85 { 86 _expectedList.addExpected(new EndTableEvent(tableName)); 87 } 88 89 public void addExpectedRow(String tableName, Object [] values) throws Exception 90 { 91 _expectedList.addExpected(new RowEvent(tableName, values)); 92 } 93 94 97 public void verify() 98 { 99 _expectedList.verify(); 100 } 101 102 105 public void startDataSet() throws DataSetException 106 { 107 _expectedList.addActual(START_DATASET_EVENT); 108 } 109 110 public void endDataSet() throws DataSetException 111 { 112 _expectedList.addActual(END_DATASET_EVENT); 113 } 114 115 public void startTable(ITableMetaData metaData) throws DataSetException 116 { 117 _expectedList.addActual(new StartTableEvent(metaData, false)); 118 _actualTableName = metaData.getTableName(); 119 } 120 121 public void endTable() throws DataSetException 122 { 123 _expectedList.addActual(new EndTableEvent(_actualTableName)); 124 _actualTableName = null; 125 } 126 127 public void row(Object [] values) throws DataSetException 128 { 129 _expectedList.addActual( 130 new RowEvent(_actualTableName, values)); 131 } 132 133 136 private static class ProducerEvent 137 { 138 protected final String _name; 139 140 public ProducerEvent(String name) 141 { 142 _name = name; 143 } 144 145 public boolean equals(Object o) 146 { 147 if (this == o) return true; 148 if (!(o instanceof ProducerEvent)) return false; 149 150 final ProducerEvent item = (ProducerEvent)o; 151 152 if (!_name.equals(item._name)) return false; 153 154 return true; 155 } 156 157 public int hashCode() 158 { 159 return _name.hashCode(); 160 } 161 162 public String toString() 163 { 164 return _name; 165 } 166 } 167 168 private static class StartTableEvent extends ProducerEvent 169 { 170 private final String _tableName; 171 private final Column[] _columns; 172 private final boolean _ignoreColumns; 173 174 public StartTableEvent(ITableMetaData metaData, boolean ignoreColumns) throws DataSetException 175 { 176 super("startTable()"); 177 _tableName = metaData.getTableName(); 178 _columns = metaData.getColumns(); 179 _ignoreColumns = ignoreColumns; 180 } 181 182 public StartTableEvent(String tableName, boolean ignoreColumns) throws DataSetException 183 { 184 super("startTable()"); 185 _tableName = tableName; 186 _columns = new Column[0]; 187 _ignoreColumns = ignoreColumns; 188 } 189 190 public boolean equals(Object o) 191 { 192 if (this == o) return true; 193 if (!(o instanceof StartTableEvent)) return false; 194 if (!super.equals(o)) return false; 195 196 final StartTableEvent startTableItem = (StartTableEvent)o; 197 198 if (!_tableName.equals(startTableItem._tableName)) return false; 199 if (!_ignoreColumns) 200 { 201 if (!Arrays.equals(_columns, startTableItem._columns)) return false; 202 } 203 204 return true; 205 } 206 207 public int hashCode() 208 { 209 int result = super.hashCode(); 210 result = 29 * result + _tableName.hashCode(); 211 return result; 212 } 213 214 public String toString() 215 { 216 String string = _name + ": table=" + _tableName; 217 if (!_ignoreColumns) 218 { 219 string += ", columns=" + Arrays.asList(_columns); 220 } 221 return string; 222 } 223 } 224 225 private static class EndTableEvent extends ProducerEvent 226 { 227 private final String _tableName; 228 229 public EndTableEvent(String tableName) 230 { 231 super("endTable()"); 232 _tableName = tableName; 233 } 234 235 public boolean equals(Object o) 236 { 237 if (this == o) return true; 238 if (!(o instanceof EndTableEvent)) return false; 239 if (!super.equals(o)) return false; 240 241 final EndTableEvent endTableItem = (EndTableEvent)o; 242 243 if (!_tableName.equals(endTableItem._tableName)) return false; 244 245 return true; 246 } 247 248 public int hashCode() 249 { 250 int result = super.hashCode(); 251 result = 29 * result + _tableName.hashCode(); 252 return result; 253 } 254 255 public String toString() 256 { 257 return _name + ": table=" + _tableName; 258 } 259 } 260 261 private static class RowEvent extends ProducerEvent 262 { 263 private final String _tableName; 264 private final Object [] _values; 265 266 public RowEvent(String tableName, Object [] values) 267 { 268 super("row()"); 269 _tableName = tableName; 270 _values = values; 271 } 272 273 public boolean equals(Object o) 274 { 275 if (this == o) return true; 276 if (!(o instanceof RowEvent)) return false; 277 if (!super.equals(o)) return false; 278 279 final RowEvent rowItem = (RowEvent)o; 280 281 if (!_tableName.equals(rowItem._tableName)) return false; 282 if (!Arrays.equals(_values, rowItem._values)) return false; 284 285 return true; 286 } 287 288 public int hashCode() 289 { 290 int result = super.hashCode(); 291 result = 29 * result + _tableName.hashCode(); 292 return result; 293 } 294 295 public String toString() 296 { 297 return _name + ": table=" + _tableName + ", values=" + Arrays.asList(_values); 298 } 299 300 } 301 } 302 | Popular Tags |