1 21 22 package org.dbunit; 23 24 import junit.framework.Assert; 25 import org.dbunit.dataset.*; 26 import org.dbunit.dataset.datatype.DataType; 27 import org.dbunit.dataset.datatype.UnknownDataType; 28 29 import java.util.Arrays ; 30 import java.util.Comparator ; 31 32 37 public class Assertion 38 { 39 private static final ColumnComparator COLUMN_COMPARATOR = new ColumnComparator(); 40 41 private Assertion() 42 { 43 } 44 45 49 public static void assertEquals(IDataSet expectedDataSet, 50 IDataSet actualDataSet) throws DatabaseUnitException 51 { 52 if (expectedDataSet == actualDataSet) 54 { 55 return; 56 } 57 58 String [] expectedNames = getSortedUpperTableNames(expectedDataSet); 59 String [] actualNames = getSortedUpperTableNames(actualDataSet); 60 61 Assert.assertEquals("table count", expectedNames.length, actualNames.length); 63 64 65 for (int i = 0; i < expectedNames.length; i++) 67 { 68 if (!actualNames[i].equals(expectedNames[i])) 69 { 70 Assert.fail("expected tables " + Arrays.asList(expectedNames) + 71 " but was " + Arrays.asList(actualNames)); 72 } 73 74 } 75 76 for (int i = 0; i < expectedNames.length; i++) 78 { 79 String name = expectedNames[i]; 80 assertEquals(expectedDataSet.getTable(name), 81 actualDataSet.getTable(name)); 82 } 83 84 } 85 86 87 92 public static void assertEquals(ITable expectedTable, ITable actualTable) 93 throws DatabaseUnitException 94 { 95 if (expectedTable == actualTable) 97 { 98 return; 99 } 100 101 ITableMetaData expectedMetaData = expectedTable.getTableMetaData(); 102 ITableMetaData actualMetaData = actualTable.getTableMetaData(); 103 String expectedTableName = expectedMetaData.getTableName(); 104 105 109 Column[] expectedColumns = getSortedColumns(expectedMetaData); 111 Column[] actualColumns = getSortedColumns(actualMetaData); 112 Assert.assertEquals("column count (table=" + expectedTableName + ")", 113 expectedColumns.length, actualColumns.length); 114 115 for (int i = 0; i < expectedColumns.length; i++) 116 { 117 String expectedName = expectedColumns[i].getColumnName(); 118 String actualName = actualColumns[i].getColumnName(); 119 if (!expectedName.equalsIgnoreCase(actualName)) 120 { 121 Assert.fail("expected columns " + getColumnNamesAsString(expectedColumns) + 122 " but was " + getColumnNamesAsString(actualColumns) + 123 " (table=" + expectedTableName + ")"); 124 } 125 } 126 127 Assert.assertEquals("row count (table=" + expectedTableName + ")", 129 expectedTable.getRowCount(), actualTable.getRowCount()); 130 131 for (int i = 0; i < expectedTable.getRowCount(); i++) 133 { 134 for (int j = 0; j < expectedColumns.length; j++) 135 { 136 Column expectedColumn = expectedColumns[j]; 137 Column actualColumn = actualColumns[j]; 138 139 String columnName = expectedColumn.getColumnName(); 140 Object expectedValue = expectedTable.getValue(i, columnName); 141 Object actualValue = actualTable.getValue(i, columnName); 142 143 DataType dataType = getComparisonDataType( 144 expectedTableName, expectedColumn, actualColumn); 145 if (dataType.compare(expectedValue, actualValue) != 0) 146 { 147 Assert.fail("value (table=" + expectedTableName + ", " + 148 "row=" + i + ", col=" + columnName + "): expected:<" + 149 expectedValue + "> but was:<" + actualValue + ">"); 150 } 151 } 152 } 153 } 154 155 static DataType getComparisonDataType(String tableName, Column expectedColumn, 156 Column actualColumn) 157 { 158 DataType expectedDataType = expectedColumn.getDataType(); 159 DataType actualDataType = actualColumn.getDataType(); 160 161 if (!expectedDataType.getClass().isInstance(actualDataType)) 163 { 164 if (expectedDataType instanceof UnknownDataType) 166 { 167 return actualDataType; 168 } 169 170 if (actualDataType instanceof UnknownDataType) 172 { 173 return expectedDataType; 174 } 175 176 Assert.fail("Incompatible data types: " + expectedDataType + ", " + 178 actualDataType + " (table=" + tableName + ", col=" + 179 expectedColumn.getColumnName() + ")"); 180 } 181 187 return expectedDataType; 189 } 190 191 private static Column[] getSortedColumns(ITableMetaData metaData) 192 throws DataSetException 193 { 194 Column[] columns = metaData.getColumns(); 195 Column[] sortColumns = new Column[columns.length]; 196 System.arraycopy(columns, 0, sortColumns, 0, columns.length); 197 Arrays.sort(sortColumns, COLUMN_COMPARATOR); 198 return sortColumns; 199 } 200 201 private static String getColumnNamesAsString(Column[] columns) 202 { 203 String [] names = new String [columns.length]; 204 for (int i = 0; i < columns.length; i++) 205 { 206 Column column = columns[i]; 207 names[i] = column.getColumnName(); 208 } 209 return Arrays.asList(names).toString(); 210 } 211 212 private static String [] getSortedUpperTableNames(IDataSet dataSet) 213 throws DataSetException 214 { 215 String [] names = dataSet.getTableNames(); 216 for (int i = 0; i < names.length; i++) 217 { 218 names[i] = names[i].toUpperCase(); 219 } 220 Arrays.sort(names); 221 return names; 222 } 223 224 227 private static class ColumnComparator implements Comparator 228 { 229 public int compare(Object o1, Object o2) 230 { 231 Column column1 = (Column)o1; 232 Column column2 = (Column)o2; 233 234 String columnName1 = column1.getColumnName(); 235 String columnName2 = column2.getColumnName(); 236 return columnName1.compareToIgnoreCase(columnName2); 237 } 238 } 239 } 240 241 242 243 244 | Popular Tags |