1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.assertion;
23
24 import java.io.FileReader;
25 import java.io.StringReader;
26 import java.math.BigDecimal;
27
28 import junit.framework.ComparisonFailure;
29 import junit.framework.TestCase;
30
31 import org.dbunit.DatabaseEnvironment;
32 import org.dbunit.database.IDatabaseConnection;
33 import org.dbunit.dataset.Column;
34 import org.dbunit.dataset.CompositeDataSet;
35 import org.dbunit.dataset.CompositeTable;
36 import org.dbunit.dataset.DataSetException;
37 import org.dbunit.dataset.DataSetUtils;
38 import org.dbunit.dataset.DefaultDataSet;
39 import org.dbunit.dataset.DefaultTable;
40 import org.dbunit.dataset.FilteredDataSet;
41 import org.dbunit.dataset.IDataSet;
42 import org.dbunit.dataset.ITable;
43 import org.dbunit.dataset.ITableMetaData;
44 import org.dbunit.dataset.datatype.DataType;
45 import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
46 import org.dbunit.dataset.xml.XmlDataSet;
47 import org.dbunit.operation.DatabaseOperation;
48 import org.dbunit.testutil.TestUtils;
49
50
51
52
53
54
55 public class DbUnitAssertIT extends TestCase
56 {
57 public static final String FILE_PATH = "xml/assertionTest.xml";
58
59 private DbUnitAssert assertion = new DbUnitAssert();
60
61
62 public DbUnitAssertIT(String s)
63 {
64 super(s);
65 }
66
67 private IDataSet getDataSet() throws Exception
68 {
69 return new FlatXmlDataSetBuilder().build(TestUtils.getFileReader(FILE_PATH));
70 }
71
72
73
74
75 public void testAssertTablesEquals() throws Exception
76 {
77 IDataSet dataSet = getDataSet();
78 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
79 dataSet.getTable("TEST_TABLE_WITH_SAME_VALUE"),
80 new Column[] {new Column("COLUMN0", DataType.VARCHAR)} );
81 }
82
83 public void testAssertTablesEmtpyEquals() throws Exception
84 {
85 IDataSet empty1 = new XmlDataSet(TestUtils.getFileReader("xml/assertionTest-empty1.xml"));
86 IDataSet empty2 = new FlatXmlDataSetBuilder().build(TestUtils.getFileReader("xml/assertionTest-empty2.xml"));
87 assertion.assertEquals(empty1, empty2);
88 }
89
90
91 public void testAssertTablesEqualsColumnNamesCaseInsensitive() throws Exception
92 {
93 IDataSet dataSet = getDataSet();
94 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
95 dataSet.getTable("TEST_TABLE_WITH_LOWER_COLUMN_NAMES"));
96 }
97
98 public void testAssertTablesAndNamesNotEquals() throws Exception
99 {
100 IDataSet dataSet = getDataSet();
101 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
102 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_NAME"));
103 }
104
105 public void testAssertTablesAndColumnCountNotEquals() throws Exception
106 {
107 IDataSet dataSet = getDataSet();
108
109 try
110 {
111 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
112 dataSet.getTable("TEST_TABLE_WITH_3_COLUMNS"));
113 throw new IllegalStateException("Should throw an AssertionFailedError");
114 }
115 catch (ComparisonFailure expected)
116 {
117 assertEquals("[COLUMN0, COLUMN1, COLUMN2, COLUMN3]", expected.getExpected());
118 assertEquals("[COLUMN0, COLUMN1, COLUMN2]", expected.getActual());
119 String expectedMsg = "column count (table=TEST_TABLE, expectedColCount=4, actualColCount=3) expected:<...N0, COLUMN1, COLUMN2[, COLUMN3]]> but was:<...N0, COLUMN1, COLUMN2[]]>";
120 assertEquals(expectedMsg, expected.getMessage());
121 }
122 }
123
124 public void testAssertTablesAndColumnSequenceNotEquals() throws Exception
125 {
126 IDataSet dataSet = getDataSet();
127
128 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
129 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_COLUMN_SEQUENCE"));
130 }
131
132 public void testAssertTablesAndColumnNamesNotEquals() throws Exception
133 {
134 IDataSet dataSet = getDataSet();
135
136 try
137 {
138 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
139 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_COLUMN_NAMES"));
140 throw new IllegalStateException("Should throw an AssertionFailedError");
141 }
142 catch (ComparisonFailure expected)
143 {
144 assertEquals("[COLUMN0, COLUMN1, COLUMN2, COLUMN3]", expected.getExpected());
145 assertEquals("[COLUMN4, COLUMN5, COLUMN6, COLUMN7]", expected.getActual());
146 String expectedMsg = "column mismatch (table=TEST_TABLE) expected:<[COLUMN[0, COLUMN1, COLUMN2, COLUMN3]]> but was:<[COLUMN[4, COLUMN5, COLUMN6, COLUMN7]]>";
147 assertEquals(expectedMsg, expected.getMessage());
148 }
149 }
150
151 public void testAssertTablesAndRowCountNotEquals() throws Exception
152 {
153 IDataSet dataSet = getDataSet();
154
155 try
156 {
157 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
158 dataSet.getTable("TEST_TABLE_WITH_ONE_ROW"));
159 throw new IllegalStateException("Should throw an AssertionFailedError");
160 }
161 catch (ComparisonFailure expected)
162 {
163 assertEquals("2", expected.getExpected());
164 assertEquals("1", expected.getActual());
165 String expectedMsg = "row count (table=TEST_TABLE) expected:<[2]> but was:<[1]>";
166 assertEquals(expectedMsg, expected.getMessage());
167 }
168 }
169
170 public void testAssertTablesAndValuesNotEquals() throws Exception
171 {
172 IDataSet dataSet = getDataSet();
173
174 try
175 {
176 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
177 dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"));
178 throw new IllegalStateException("Should throw an AssertionFailedError");
179 }
180 catch (ComparisonFailure expected)
181 {
182 assertEquals("row 1 col 2", expected.getExpected());
183 assertEquals("wrong value", expected.getActual());
184 String expectedMsg = "value (table=TEST_TABLE, row=1, col=COLUMN2) expected:<[row 1 col 2]> but was:<[wrong value]>";
185 assertEquals(expectedMsg, expected.getMessage());
186 }
187 }
188
189 public void testAssertTablesWithColFilterAndValuesNotEqualExcluded() throws Exception
190 {
191 IDataSet dataSet = getDataSet();
192
193
194 String[] allColumnsThatAreNotEqual = new String[] {"COLUMN2"};
195 assertion.assertEqualsIgnoreCols(dataSet.getTable("TEST_TABLE"),
196 dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"),
197 allColumnsThatAreNotEqual );
198 }
199
200 public void testAssertTablesWithColFilterAndValuesNotEqualNotExcluded() throws Exception
201 {
202 IDataSet dataSet = getDataSet();
203
204
205
206 String[] filteredColumns = new String[] {"COLUMN0"};
207 try {
208 assertion.assertEqualsIgnoreCols(dataSet.getTable("TEST_TABLE"),
209 dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"),
210 filteredColumns );
211 throw new IllegalStateException("Should throw an AssertionFailedError");
212 }
213 catch (ComparisonFailure expected)
214 {
215 assertEquals("row 1 col 2", expected.getExpected());
216 assertEquals("wrong value", expected.getActual());
217 String expectedMsg = "value (table=TEST_TABLE, row=1, col=COLUMN2) expected:<[row 1 col 2]> but was:<[wrong value]>";
218 assertEquals(expectedMsg, expected.getMessage());
219 }
220 }
221
222 public void testAssertTablesAndValuesNotEquals_AdditionalColumnInfo() throws Exception
223 {
224 IDataSet dataSet = getDataSet();
225
226 try
227 {
228 Column[] additionalColInfo = new Column[]{
229 new Column("COLUMN0", DataType.VARCHAR)
230 };
231 assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
232 dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"),
233 additionalColInfo);
234 throw new IllegalStateException("Should throw an AssertionFailedError");
235 }
236 catch (ComparisonFailure expected)
237 {
238 String expectedMsg = "junit.framework.ComparisonFailure: value (table=TEST_TABLE, row=1, col=COLUMN2, " +
239 "Additional row info: ('COLUMN0': expected=<row 1 col 0>, actual=<row 1 col 0>)) " +
240 "expected:<[row 1 col 2]> but was:<[wrong value]>";
241 String actualMsg = expected.toString();
242 assertEquals("row 1 col 2", expected.getExpected());
243 assertEquals("wrong value", expected.getActual());
244 assertEquals("Exception message did not match the expected one.", expectedMsg, actualMsg);
245 }
246 }
247
248
249 public void testAssertTablesEqualsAndIncompatibleDataType() throws Exception
250 {
251 String tableName = "TABLE_NAME";
252
253
254 Column[] actualColumns = new Column[] {
255 new Column("BOOLEAN", DataType.BOOLEAN),
256 };
257 Object[] actualRow = new Object[] {
258 Boolean.TRUE,
259 };
260 DefaultTable actualTable = new DefaultTable(tableName,
261 actualColumns);
262 actualTable.addRow(actualRow);
263
264
265 Column[] expectedColumns = new Column[] {
266 new Column("BOOLEAN", DataType.VARCHAR),
267 };
268 Object[] expectedRow = new Object[] {
269 "1",
270 };
271 DefaultTable expectedTable = new DefaultTable(tableName,
272 expectedColumns);
273 expectedTable.addRow(expectedRow);
274
275
276 try
277 {
278 assertion.assertEquals(expectedTable, actualTable);
279 }
280 catch (ComparisonFailure expected)
281 {
282 assertEquals("VARCHAR", expected.getExpected());
283 assertEquals("BOOLEAN", expected.getActual());
284 String expectedMsg = "Incompatible data types: (table=TABLE_NAME, col=BOOLEAN) expected:<[VARCHAR]> but was:<[BOOLEAN]>";
285 assertEquals(expectedMsg, expected.getMessage());
286 }
287 }
288
289 public void testAssertTablesByQueryWithColFilterAndValuesNotEqualExcluded() throws Exception
290 {
291 DatabaseEnvironment env = DatabaseEnvironment.getInstance();
292 IDatabaseConnection connection = env.getConnection();
293
294 IDataSet dataSet = env.getInitDataSet();
295 ITable expectedTable = dataSet.getTable("TEST_TABLE");
296
297 ITable table = dataSet.getTable("TEST_TABLE");
298 ITable filteredTable = new ModifyingTable(table, "COLUMN2");
299 DatabaseOperation.CLEAN_INSERT.execute(connection, new DefaultDataSet(filteredTable));
300
301
302
303 String[] ignoreCols = new String[] {"COLUMN2"};
304 assertion.assertEqualsByQuery(expectedTable, connection, "TEST_TABLE", "select * from TEST_TABLE order by 1", ignoreCols);
305 }
306
307 public void testAssertTablesByQueryWithColFilterAndValuesNotEqualNotExcluded() throws Exception
308 {
309 DatabaseEnvironment env = DatabaseEnvironment.getInstance();
310 IDatabaseConnection connection = env.getConnection();
311
312 IDataSet dataSet = env.getInitDataSet();
313 ITable expectedTable = dataSet.getTable("TEST_TABLE");
314
315 ITable table = dataSet.getTable("TEST_TABLE");
316 ITable filteredTable = new ModifyingTable(table, "COLUMN2");
317 DatabaseOperation.CLEAN_INSERT.execute(connection, new DefaultDataSet(filteredTable));
318
319
320
321 String[] ignoreCols = new String[] {"COLUMN1"};
322 try {
323 assertion.assertEqualsByQuery(expectedTable, connection, "TEST_TABLE", "select * from TEST_TABLE order by 1", ignoreCols);
324 fail("The assertion should not work");
325 }
326 catch (ComparisonFailure expected)
327 {
328 assertEquals("row 0 col 2", expected.getExpected());
329 assertEquals("row 0 col 2 (modified COLUMN2)", expected.getActual());
330 String expectedMsg = "value (table=TEST_TABLE, row=0, col=COLUMN2) expected:<row 0 col 2[]> but was:<row 0 col 2[ (modified COLUMN2)]>";
331 assertEquals(expectedMsg, expected.getMessage());
332 }
333 }
334
335
336
337 public void testAssertTablesEqualsAndCompatibleDataType() throws Exception
338 {
339 String tableName = "TABLE_NAME";
340 java.sql.Timestamp now = new java.sql.Timestamp(
341 System.currentTimeMillis());
342
343
344 Column[] actualColumns = new Column[] {
345 new Column("BOOLEAN", DataType.BOOLEAN),
346 new Column("TIMESTAMP", DataType.TIMESTAMP),
347 new Column("STRING", DataType.CHAR),
348 new Column("NUMERIC", DataType.NUMERIC),
349 };
350 Object[] actualRow = new Object[] {
351 Boolean.TRUE,
352 now,
353 "0",
354 new BigDecimal("123.4"),
355 };
356 DefaultTable actualTable = new DefaultTable(tableName,
357 actualColumns);
358 actualTable.addRow(actualRow);
359
360
361
362 Column[] expectedColumns = new Column[] {
363 new Column("BOOLEAN", DataType.UNKNOWN),
364 new Column("TIMESTAMP", DataType.UNKNOWN),
365 new Column("STRING", DataType.UNKNOWN),
366 new Column("NUMERIC", DataType.UNKNOWN),
367 };
368 Object[] expectedRow = new Object[] {
369 "1",
370 new Long(now.getTime()),
371 new Integer("0"),
372 "123.4000",
373 };
374 DefaultTable expectedTable = new DefaultTable(tableName,
375 expectedColumns);
376 expectedTable.addRow(expectedRow);
377
378 assertion.assertEquals(expectedTable, actualTable);
379 }
380
381 public void testAssertDataSetsEquals() throws Exception
382 {
383 IDataSet dataSet1 = getDataSet();
384
385
386 String[] names = DataSetUtils.getReverseTableNames(dataSet1);
387 IDataSet dataSet2 = new FilteredDataSet(names, dataSet1);
388
389 assertTrue("assert not same", dataSet1 != dataSet2);
390 assertion.assertEquals(dataSet1, dataSet2);
391 }
392
393 public void testAssertDataSetsEqualsTableNamesCaseInsensitive() throws Exception
394 {
395 IDataSet dataSet1 = getDataSet();
396
397
398 String[] names = dataSet1.getTableNames();
399 for (int i = 0; i < names.length; i++)
400 {
401 names[i] = names[i].toLowerCase();
402 }
403 IDataSet dataSet2 = new FilteredDataSet(names, dataSet1);
404
405 assertTrue("assert not same", dataSet1 != dataSet2);
406 assertion.assertEquals(dataSet1, dataSet2);
407 }
408
409 public void testAssertDataSetsAndTableCountNotEquals() throws Exception
410 {
411 IDataSet dataSet1 = getDataSet();
412
413
414 String[] names = new String[]{dataSet1.getTableNames()[0]};
415 IDataSet dataSet2 = new FilteredDataSet(names, dataSet1);
416
417 assertTrue("assert not same", dataSet1 != dataSet2);
418
419 try
420 {
421 assertion.assertEquals(dataSet1, dataSet2);
422 throw new IllegalStateException("Should throw an AssertionFailedError");
423 }
424 catch (ComparisonFailure expected)
425 {
426 assertEquals("9", expected.getExpected());
427 assertEquals("1", expected.getActual());
428 assertEquals("table count expected:<[9]> but was:<[1]>", expected.getMessage());
429 }
430 }
431
432
433 public void testAssertDataSetsAndTableNamesNotEquals() throws Exception
434 {
435 IDataSet dataSet1 = getDataSet();
436
437
438 String[] names = dataSet1.getTableNames();
439 ITable[] tables = new ITable[names.length];
440 for (int i = 0; i < names.length; i++)
441 {
442 String reversedName = new StringBuffer(names[i]).reverse().toString();
443 tables[i] = new CompositeTable(reversedName,
444 dataSet1.getTable(names[i]));
445 }
446 IDataSet dataSet2 = new DefaultDataSet(tables);
447
448 assertTrue("assert not same", dataSet1 != dataSet2);
449 assertEquals("table count", dataSet1.getTableNames().length,
450 dataSet2.getTableNames().length);
451
452 try
453 {
454 assertion.assertEquals(dataSet1, dataSet2);
455 throw new IllegalStateException("Should throw an AssertionFailedError");
456 }
457 catch (ComparisonFailure expected)
458 {
459 }
460 }
461
462 public void testAssertDataSetsAndTablesNotEquals() throws Exception
463 {
464 IDataSet dataSet1 = getDataSet();
465
466
467 IDataSet dataSet2 = new CompositeDataSet(dataSet1, dataSet1);
468
469 assertTrue("assert not same", dataSet1 != dataSet2);
470 assertEquals("table count", dataSet1.getTableNames().length,
471 dataSet2.getTableNames().length);
472
473 try
474 {
475 assertion.assertEquals(dataSet1, dataSet2);
476 throw new IllegalStateException("Should throw an AssertionFailedError");
477 }
478 catch (ComparisonFailure expected)
479 {
480 assertEquals("2", expected.getExpected());
481 assertEquals("4", expected.getActual());
482 assertEquals("row count (table=TEST_TABLE) expected:<[2]> but was:<[4]>", expected.getMessage());
483 }
484 }
485
486 public void testAssertDataSetsWithFailureHandler() throws Exception
487 {
488 DiffCollectingFailureHandler fh = new DiffCollectingFailureHandler();
489
490 String xml1 =
491 "<dataset>\n"+
492 "<TEST_TABLE COLUMN0='row 0 col 0' COLUMN1='row 0 col 1'/>\n" +
493 "</dataset>\n";
494 IDataSet dataSet1 = new FlatXmlDataSetBuilder().build(new StringReader(xml1));
495 String xml2 =
496 "<dataset>\n"+
497 "<TEST_TABLE COLUMN0='row 0 col somthing' COLUMN1='row 0 col something mysterious'/>\n" +
498 "</dataset>\n";
499 IDataSet dataSet2 = new FlatXmlDataSetBuilder().build(new StringReader(xml2));
500
501
502 assertion.assertEquals(dataSet1, dataSet2, fh);
503
504
505 assertEquals(2, fh.getDiffList().size());
506 }
507
508
509
510 public void testGetComparisonDataType_ExpectedTypeUnknown()
511 {
512 Column expectedColumn = new Column("COL1", DataType.UNKNOWN);
513 Column actualColumn = new Column("COL1", DataType.VARCHAR);
514 DataType dataType = new DbUnitAssert.ComparisonColumn("BLABLA_TABLE_NOT_NEEDED_HERE", expectedColumn, actualColumn, assertion.getDefaultFailureHandler()).getDataType();
515 assertEquals(DataType.VARCHAR, dataType);
516 }
517
518 public void testGetComparisonDataType_ActualTypeUnknown()
519 {
520 Column expectedColumn = new Column("COL1", DataType.VARCHAR);
521 Column actualColumn = new Column("COL1", DataType.UNKNOWN);
522 DataType dataType = new DbUnitAssert.ComparisonColumn("BLABLA_TABLE_NOT_NEEDED_HERE", expectedColumn, actualColumn, assertion.getDefaultFailureHandler()).getDataType();
523 assertEquals(DataType.VARCHAR, dataType);
524 }
525
526 public void testGetComparisonDataType_BothTypesSetIncompatible()
527 {
528 Column expectedColumn = new Column("COL1", DataType.VARCHAR);
529 Column actualColumn = new Column("COL1", DataType.NUMERIC);
530 try {
531 new DbUnitAssert.ComparisonColumn("BLABLA_TABLE_NOT_NEEDED_HERE", expectedColumn, actualColumn, assertion.getDefaultFailureHandler()).getDataType();
532 fail("Incompatible datatypes should not work");
533 }
534 catch(ComparisonFailure expected){
535 assertEquals("VARCHAR", expected.getExpected());
536 assertEquals("NUMERIC", expected.getActual());
537 String expectedMsg = "Incompatible data types: (table=BLABLA_TABLE_NOT_NEEDED_HERE, col=COL1) expected:<[VARCHAR]> but was:<[NUMERIC]>";
538 assertEquals(expectedMsg, expected.getMessage());
539 }
540 }
541
542 public void testGetComparisonDataType_BothTypesSetToSame()
543 {
544 Column expectedColumn = new Column("COL1", DataType.VARCHAR);
545 Column actualColumn = new Column("COL1", DataType.VARCHAR);
546 DataType dataType = new DbUnitAssert.ComparisonColumn("BLABLA_TABLE_NOT_NEEDED_HERE", expectedColumn, actualColumn, assertion.getDefaultFailureHandler()).getDataType();
547 assertEquals(DataType.VARCHAR, dataType);
548 }
549
550 public void testGetComparisonDataType_BothTypesUnknown()
551 {
552 Column expectedColumn = new Column("COL1", DataType.UNKNOWN);
553 Column actualColumn = new Column("COL1", DataType.UNKNOWN);
554 DataType dataType = new DbUnitAssert.ComparisonColumn("BLABLA_TABLE_NOT_NEEDED_HERE", expectedColumn, actualColumn, assertion.getDefaultFailureHandler()).getDataType();
555 assertEquals(DataType.UNKNOWN, dataType);
556 }
557
558
559
560
561
562
563 protected static class ModifyingTable implements ITable
564 {
565 private ITable _wrappedTable;
566 private String _columnToModify;
567
568 public ModifyingTable(ITable originalTable, String columnToModify)
569 {
570 this._wrappedTable = originalTable;
571 this._columnToModify = columnToModify;
572 }
573
574 public int getRowCount() {
575 return this._wrappedTable.getRowCount();
576 }
577
578 public ITableMetaData getTableMetaData() {
579 return this._wrappedTable.getTableMetaData();
580 }
581
582 public Object getValue(int row, String column) throws DataSetException {
583 Object originalValue = _wrappedTable.getValue(row, column);
584
585
586 if(column.equalsIgnoreCase(this._columnToModify)) {
587 return String.valueOf(originalValue) + " (modified "+_columnToModify +")";
588 }
589 return originalValue;
590 }
591
592
593 }
594
595 }
596
597
598
599
600