1 package org.dbunit.database;
2
3 import java.sql.SQLException;
4 import java.util.Arrays;
5 import java.util.HashSet;
6 import java.util.Set;
7 import java.util.SortedSet;
8 import java.util.TreeSet;
9
10 import org.dbunit.AbstractHSQLTestCase;
11 import org.dbunit.database.PrimaryKeyFilter.PkTableMap;
12 import org.dbunit.dataset.DataSetException;
13 import org.dbunit.dataset.IDataSet;
14 import org.dbunit.dataset.ITable;
15 import org.dbunit.dataset.ITableIterator;
16 import org.dbunit.util.CollectionsHelper;
17 import org.dbunit.util.search.SearchException;
18
19 public abstract class AbstractSearchCallbackFilteredByPKsTestCase extends AbstractHSQLTestCase {
20
21 private static final char FIRST_TABLE = 'A';
22
23 private PkTableMap fInput = new PkTableMap();
24 private PkTableMap fOutput = new PkTableMap();
25
26 public AbstractSearchCallbackFilteredByPKsTestCase(String testName, String sqlFile) {
27 super(testName, sqlFile);
28 }
29
30 protected abstract int[] setupTablesSizeFixture();
31
32 protected IDataSet setupTablesDataSetFixture() throws SQLException {
33 IDatabaseConnection connection = getConnection();
34 IDataSet allDataSet = connection.createDataSet();
35 return allDataSet;
36 }
37
38 protected void addInput(String tableName, String[] ids) {
39
40 SortedSet idsSet = new TreeSet(Arrays.asList(ids));
41 this.fInput.put( tableName, idsSet );
42 }
43 protected void addOutput(String tableName, String[] ids) {
44
45
46 SortedSet idsSet = new TreeSet(Arrays.asList(ids));
47 this.fOutput.put( tableName, idsSet );
48 }
49
50 protected abstract IDataSet getDataset() throws SQLException, SearchException, DataSetException;
51
52 protected void doIt() throws SQLException, DataSetException, SearchException {
53 IDataSet dataset = getDataset();
54 assertNotNull( dataset );
55
56
57 String[] outputTables = dataset.getTableNames();
58 assertTablesSize( outputTables );
59 assertTablesName( outputTables );
60 assertRows( dataset );
61 }
62
63 protected void assertTablesSize(String[] actualTables) {
64 int expectedSize = this.fOutput.size();
65 int actualSize = actualTables.length;
66 if ( expectedSize != actualSize ) {
67 super.logger.error( "Expected tables: " + dump(this.fOutput.getTableNames()) );
68 super.logger.error( "Actual tables: " + dump(actualTables) );
69 fail( "I number of returned tables did not match: " + actualSize + " instead of " + expectedSize );
70 }
71 }
72 protected void assertTablesName(String[] outputTables) {
73 Set expectedTables = CollectionsHelper.objectsToSet(this.fOutput.getTableNames());
74 Set notExpectedTables = new HashSet();
75 boolean ok = true;
76
77 for (int i = 0; i < outputTables.length; i++) {
78 String table = outputTables[i];
79 if ( expectedTables.contains(table) ) {
80 expectedTables.remove(table);
81 } else {
82 notExpectedTables.add(table);
83 }
84 }
85 if ( ! notExpectedTables.isEmpty() ) {
86 ok = false;
87 super.logger.error( "Returned tables not waited: " + dump(notExpectedTables) );
88 }
89 if ( ! expectedTables.isEmpty() ) {
90 ok = false;
91 super.logger.error( "Waited tables not returned: " + dump(expectedTables) );
92 }
93 if ( ! ok ) {
94 fail( "Returned tables do not match the expectation; check error output" );
95 }
96 }
97
98 protected void assertRows(IDataSet dataset) throws DataSetException {
99 ITableIterator iterator = dataset.iterator();
100 while (iterator.next()) {
101 ITable table = iterator.getTable();
102 String tableName = table.getTableMetaData().getTableName();
103 String idField = "PK" + tableName;
104 Set expectedIds = this.fOutput.get( tableName );
105 Set actualIds = new HashSet();
106 int rowCount = table.getRowCount();
107 for( int row=0; row<rowCount; row++ ) {
108 String id = (String) table.getValue( row, idField );
109 actualIds.add( id );
110 if ( super.logger.isDebugEnabled() ) {
111 super.logger.debug( "T:" + tableName + " row: " + row + " id: " + id );
112 }
113 }
114
115
116 assertEquals( "ids of table " + tableName + " do not match", expectedIds, actualIds );
117 }
118 }
119
120 public void testSetupTables() throws SQLException, DataSetException {
121 int[] sizes = setupTablesSizeFixture();
122 IDataSet allDataSet = setupTablesDataSetFixture();
123 assertNotNull( allDataSet );
124 for (short i = 0; i < sizes.length; i++) {
125 char table = (char) (FIRST_TABLE + i);
126 if ( super.logger.isDebugEnabled() ) {
127 super.logger.debug( "Getting table " + table );
128 }
129 ITable itable = allDataSet.getTable( ""+table );
130 assertNotNull( "did not find table " + table, itable );
131 assertEquals( "size did not match for table " + table, sizes[i], itable.getRowCount());
132 }
133 }
134
135 protected PkTableMap getInput() {
136 return this.fInput;
137 }
138
139 protected PkTableMap getOutput() {
140 return this.fOutput;
141 }
142
143
144 }