1 21 22 package org.dbunit.ant; 23 24 import org.dbunit.Assertion; 25 import org.dbunit.DatabaseUnitException; 26 import org.dbunit.database.IDatabaseConnection; 27 import org.dbunit.dataset.CompositeTable; 28 import org.dbunit.dataset.IDataSet; 29 import org.dbunit.dataset.ITable; 30 import org.dbunit.dataset.SortedTable; 31 import org.dbunit.dataset.ITableMetaData; 32 import org.dbunit.dataset.FilteredTableMetaData; 33 import org.dbunit.dataset.filter.DefaultColumnFilter; 34 35 import java.io.File ; 36 import java.util.ArrayList ; 37 import java.util.List ; 38 39 48 public class Compare extends AbstractStep 49 { 50 private static final String DEFAULT_FORMAT = FORMAT_FLAT; 51 52 private String _format; 53 private File _src; 54 private List _tables = new ArrayList (); 55 private boolean _sort = false; 56 57 public File getSrc() 58 { 59 return _src; 60 } 61 62 public void setSrc(File src) 63 { 64 _src = src; 65 } 66 67 public void setSort(boolean sort) 68 { 69 _sort = sort; 70 } 71 72 public String getFormat() 73 { 74 return _format != null ? _format : DEFAULT_FORMAT; 75 } 76 77 public void setFormat(String format) 78 { 79 if (format.equalsIgnoreCase(FORMAT_FLAT) 80 || format.equalsIgnoreCase(FORMAT_XML) 81 || format.equalsIgnoreCase(FORMAT_CSV) 82 ) 83 { 84 _format = format; 85 } 86 else 87 { 88 throw new IllegalArgumentException ("Type must be either 'flat'(default) csv or 'xml' but was: " + format); 89 } 90 } 91 92 public List getTables() 93 { 94 return _tables; 95 } 96 97 public void addTable(Table table) 98 { 99 _tables.add(table); 100 } 101 102 public void addQuery(Query query) 103 { 104 _tables.add(query); 105 } 106 107 public void execute(IDatabaseConnection connection) throws DatabaseUnitException 108 { 109 IDataSet expectedDataset = getSrcDataSet(_src, getFormat(), false); 110 IDataSet actualDataset = getDatabaseDataSet(connection, _tables, false); 111 112 String [] tableNames = null; 113 if (_tables.size() == 0) 114 { 115 tableNames = expectedDataset.getTableNames(); 118 } 119 else 120 { 121 tableNames = actualDataset.getTableNames(); 122 } 123 124 for (int i = 0; i < tableNames.length; i++) 125 { 126 String tableName = tableNames[i]; 127 ITable expectedTable = expectedDataset.getTable(tableName); 128 ITableMetaData expectedMetaData = expectedTable.getTableMetaData(); 129 130 ITable actualTable = actualDataset.getTable(tableName); 133 actualTable = DefaultColumnFilter.includedColumnsTable( 134 actualTable, expectedMetaData.getColumns()); 135 136 if (_sort) 137 { 138 expectedTable = new SortedTable(expectedTable); 139 actualTable = new SortedTable(actualTable); 140 } 141 Assertion.assertEquals(expectedTable, actualTable); 142 } 143 } 144 145 public String getLogMessage() 146 { 147 return "Executing compare: " 148 + "\n from file: " + ((_src == null) ? null : _src.getAbsolutePath()) 149 + "\n with format: " + _format; 150 } 151 152 public String toString() 153 { 154 StringBuffer result = new StringBuffer (); 155 result.append("Compare: "); 156 result.append(" SRC=" + _src.getAbsolutePath()); 157 result.append(", format= " + _format); 158 result.append(", tables= " + _tables); 159 160 return result.toString(); 161 } 162 } 163 164 | Popular Tags |