1 21 22 package org.dbunit; 23 24 import org.dbunit.dataset.Column; 25 import org.dbunit.dataset.CompositeDataSet; 26 import org.dbunit.dataset.CompositeTable; 27 import org.dbunit.dataset.DataSetUtils; 28 import org.dbunit.dataset.DefaultDataSet; 29 import org.dbunit.dataset.DefaultTable; 30 import org.dbunit.dataset.FilteredDataSet; 31 import org.dbunit.dataset.IDataSet; 32 import org.dbunit.dataset.ITable; 33 import org.dbunit.dataset.datatype.DataType; 34 import org.dbunit.dataset.xml.FlatXmlDataSet; 35 36 import junit.framework.AssertionFailedError; 37 import junit.framework.TestCase; 38 39 import java.io.FileReader ; 40 import java.math.BigDecimal ; 41 42 47 public class AssertionTest extends TestCase 48 { 49 public AssertionTest(String s) 50 { 51 super(s); 52 } 53 54 private IDataSet getDataSet() throws Exception 55 { 56 return new FlatXmlDataSet(new FileReader ( 57 "src/xml/assertionTest.xml")); 58 } 59 60 63 public void testAssertTablesEquals() throws Exception 64 { 65 IDataSet dataSet = getDataSet(); 66 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 67 dataSet.getTable("TEST_TABLE_WITH_SAME_VALUE")); 68 } 69 70 public void testAssertTablesEqualsColumnNamesCaseInsensitive() throws Exception 71 { 72 IDataSet dataSet = getDataSet(); 73 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 74 dataSet.getTable("TEST_TABLE_WITH_LOWER_COLUMN_NAMES")); 75 } 76 77 public void testAssertTablesAndNamesNotEquals() throws Exception 78 { 79 IDataSet dataSet = getDataSet(); 80 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 81 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_NAME")); 82 } 83 84 public void testAssertTablesAndColumnCountNotEquals() throws Exception 85 { 86 IDataSet dataSet = getDataSet(); 87 88 try 89 { 90 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 91 dataSet.getTable("TEST_TABLE_WITH_3_COLUMNS")); 92 throw new IllegalStateException ("Should throw an AssertionFailedError"); 93 } 94 catch (AssertionFailedError e) 95 { 96 } 97 } 98 99 public void testAssertTablesAndColumnSequenceNotEquals() throws Exception 100 { 101 IDataSet dataSet = getDataSet(); 102 103 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 104 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_COLUMN_SEQUENCE")); 105 } 106 107 public void testAssertTablesAndColumnNamesNotEquals() throws Exception 108 { 109 IDataSet dataSet = getDataSet(); 110 111 try 112 { 113 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 114 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_COLUMN_NAMES")); 115 throw new IllegalStateException ("Should throw an AssertionFailedError"); 116 } 117 catch (AssertionFailedError e) 118 { 119 } 120 } 121 122 public void testAssertTablesAndRowCountNotEquals() throws Exception 123 { 124 IDataSet dataSet = getDataSet(); 125 126 try 127 { 128 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 129 dataSet.getTable("TEST_TABLE_WITH_ONE_ROW")); 130 throw new IllegalStateException ("Should throw an AssertionFailedError"); 131 } 132 catch (AssertionFailedError e) 133 { 134 } 135 } 136 137 public void testAssertTablesAndValuesNotEquals() throws Exception 138 { 139 IDataSet dataSet = getDataSet(); 140 141 try 142 { 143 Assertion.assertEquals(dataSet.getTable("TEST_TABLE"), 144 dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE")); 145 throw new IllegalStateException ("Should throw an AssertionFailedError"); 146 } 147 catch (AssertionFailedError e) 148 { 149 } 150 } 151 152 public void testAssertTablesEqualsAndIncompatibleDataType() throws Exception 153 { 154 String tableName = "TABLE_NAME"; 155 156 Column[] actualColumns = new Column[] { 158 new Column("BOOLEAN", DataType.BOOLEAN), 159 }; 160 Object [] actualRow = new Object [] { 161 Boolean.TRUE, 162 }; 163 DefaultTable actualTable = new DefaultTable(tableName, 164 actualColumns); 165 actualTable.addRow(actualRow); 166 167 Column[] expectedColumns = new Column[] { 169 new Column("BOOLEAN", DataType.VARCHAR), 170 }; 171 Object [] expectedRow = new Object [] { 172 "1", 173 }; 174 DefaultTable expectedTable = new DefaultTable(tableName, 175 expectedColumns); 176 expectedTable.addRow(expectedRow); 177 178 179 try 180 { 181 Assertion.assertEquals(expectedTable, actualTable); 182 } 183 catch (AssertionFailedError e) 184 { 185 186 } 187 } 188 189 public void testAssertTablesEqualsAndCompatibleDataType() throws Exception 190 { 191 String tableName = "TABLE_NAME"; 192 java.sql.Timestamp now = new java.sql.Timestamp ( 193 System.currentTimeMillis()); 194 195 Column[] actualColumns = new Column[] { 197 new Column("BOOLEAN", DataType.BOOLEAN), 198 new Column("TIMESTAMP", DataType.TIMESTAMP), 199 new Column("STRING", DataType.CHAR), 200 new Column("NUMERIC", DataType.NUMERIC), 201 }; 202 Object [] actualRow = new Object [] { 203 Boolean.TRUE, 204 now, 205 "0", 206 new BigDecimal ("123.4"), 207 }; 208 DefaultTable actualTable = new DefaultTable(tableName, 209 actualColumns); 210 actualTable.addRow(actualRow); 211 212 213 Column[] expectedColumns = new Column[] { 215 new Column("BOOLEAN", DataType.UNKNOWN), 216 new Column("TIMESTAMP", DataType.UNKNOWN), 217 new Column("STRING", DataType.UNKNOWN), 218 new Column("NUMERIC", DataType.UNKNOWN), 219 }; 220 Object [] expectedRow = new Object [] { 221 "1", 222 new Long (now.getTime()), 223 new Integer ("0"), 224 "123.4000", 225 }; 226 DefaultTable expectedTable = new DefaultTable(tableName, 227 expectedColumns); 228 expectedTable.addRow(expectedRow); 229 230 Assertion.assertEquals(expectedTable, actualTable); 231 } 232 233 public void testAssertDataSetsEquals() throws Exception 234 { 235 IDataSet dataSet1 = getDataSet(); 236 237 String [] names = DataSetUtils.getReverseTableNames(dataSet1); 239 IDataSet dataSet2 = new FilteredDataSet(names, dataSet1); 240 241 assertTrue("assert not same", dataSet1 != dataSet2); 242 Assertion.assertEquals(dataSet1, dataSet2); 243 } 244 245 public void testAssertDataSetsEqualsTableNamesCaseInsensitive() throws Exception 246 { 247 IDataSet dataSet1 = getDataSet(); 248 249 String [] names = dataSet1.getTableNames(); 251 for (int i = 0; i < names.length; i++) 252 { 253 names[i] = names[i].toLowerCase(); 254 } 255 IDataSet dataSet2 = new FilteredDataSet(names, dataSet1); 256 257 assertTrue("assert not same", dataSet1 != dataSet2); 258 Assertion.assertEquals(dataSet1, dataSet2); 259 } 260 261 public void testAssertDataSetsAndTableCountNotEquals() throws Exception 262 { 263 IDataSet dataSet1 = getDataSet(); 264 265 String [] names = new String []{dataSet1.getTableNames()[0]}; 267 IDataSet dataSet2 = new FilteredDataSet(names, dataSet1); 268 269 assertTrue("assert not same", dataSet1 != dataSet2); 270 271 try 272 { 273 Assertion.assertEquals(dataSet1, dataSet2); 274 throw new IllegalStateException ("Should throw an AssertionFailedError"); 275 } 276 catch (AssertionFailedError e) 277 { 278 } 279 } 280 281 public void testAssertDataSetsAndTableNamesNotEquals() throws Exception 282 { 283 IDataSet dataSet1 = getDataSet(); 284 285 String [] names = dataSet1.getTableNames(); 287 ITable[] tables = new ITable[names.length]; 288 for (int i = 0; i < names.length; i++) 289 { 290 String reversedName = new StringBuffer (names[i]).reverse().toString(); 291 tables[i] = new CompositeTable(reversedName, 292 dataSet1.getTable(names[i])); 293 } 294 IDataSet dataSet2 = new DefaultDataSet(tables); 295 296 assertTrue("assert not same", dataSet1 != dataSet2); 297 assertEquals("table count", dataSet1.getTableNames().length, 298 dataSet2.getTableNames().length); 299 300 try 301 { 302 Assertion.assertEquals(dataSet1, dataSet2); 303 throw new IllegalStateException ("Should throw an AssertionFailedError"); 304 } 305 catch (AssertionFailedError e) 306 { 307 } 308 } 309 310 public void testAssertDataSetsAndTablesNotEquals() throws Exception 311 { 312 IDataSet dataSet1 = getDataSet(); 313 314 IDataSet dataSet2 = new CompositeDataSet(dataSet1, dataSet1); 316 317 assertTrue("assert not same", dataSet1 != dataSet2); 318 assertEquals("table count", dataSet1.getTableNames().length, 319 dataSet2.getTableNames().length); 320 321 try 322 { 323 Assertion.assertEquals(dataSet1, dataSet2); 324 throw new IllegalStateException ("Should throw an AssertionFailedError"); 325 } 326 catch (AssertionFailedError e) 327 { 328 } 329 } 330 } 331 332 333 334 335 336 | Popular Tags |