View Javadoc
1   package org.dbunit.database;
2   
3   import java.sql.Connection;
4   import java.util.ArrayList;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import org.dbunit.AbstractDatabaseIT;
9   import org.dbunit.HypersonicEnvironment;
10  import org.dbunit.dataset.Column;
11  import org.dbunit.dataset.Columns;
12  import org.dbunit.dataset.IDataSet;
13  import org.dbunit.dataset.ITableMetaData;
14  import org.dbunit.testutil.TestUtils;
15  
16  /**
17   * @author gommma (gommma AT users.sourceforge.net)
18   * @author Last changed by: $Author$
19   * @version $Revision$ $Date$
20   * @since 2.4.0
21   */
22  public class ResultSetTableMetaDataIT extends AbstractDatabaseIT
23  {
24      
25      public ResultSetTableMetaDataIT(String s)
26      {
27          super(s);
28      }
29  
30      protected IDataSet createDataSet() throws Exception
31      {
32          return _connection.createDataSet();
33      }
34  
35      /**
36       * Tests the pattern-like column retrieval from the database. DbUnit
37       * should not interpret any table names as regex patterns. 
38       * @throws Exception
39       */
40      public void testGetColumnsForTablesMatchingSamePattern() throws Exception
41      {
42          Connection jdbcConnection = HypersonicEnvironment.createJdbcConnection("tempdb");
43          HypersonicEnvironment.executeDdlFile(TestUtils.getFile("sql/hypersonic_dataset_pattern_test.sql"),
44                  jdbcConnection);
45          IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
46  
47          try {
48              String tableName = "PATTERN_LIKE_TABLE_X_";
49              String[] columnNames = {"VARCHAR_COL_XUNDERSCORE"};
50      
51              String sql = "select * from " + tableName;
52              ForwardOnlyResultSetTable resultSetTable = new ForwardOnlyResultSetTable(tableName, sql, connection);
53              ResultSetTableMetaData metaData = (ResultSetTableMetaData) resultSetTable.getTableMetaData();
54              
55              Column[] columns = metaData.getColumns();
56      
57              assertEquals("column count", columnNames.length, columns.length);
58      
59              for (int i = 0; i < columnNames.length; i++)
60              {
61                  Column column = Columns.getColumn(columnNames[i], columns);
62                  assertEquals(columnNames[i], columnNames[i], column.getColumnName());
63              }
64          }
65          finally {
66              HypersonicEnvironment.shutdown(jdbcConnection);
67              jdbcConnection.close();
68              HypersonicEnvironment.deleteFiles("tempdb");
69          }
70      }
71      
72      /**
73       * Test that the column metadata generated by ResultSetTableMetaData for a given 
74       * query is consistent with the metadata obtained directly for the source tables.  
75       * @throws Exception
76       */
77      public void testMetadataCoherence() throws Exception 
78      {
79          Connection jdbcConnection = HypersonicEnvironment.createJdbcConnection("tempdb");
80          HypersonicEnvironment.executeDdlFile(TestUtils.getFile("sql/hypersonic_dataset_metadata_coherence.sql"), jdbcConnection);
81          IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
82  
83          try {
84              ITableMetaData tableAMetadata = new DatabaseTableMetaData("A", connection, true);
85              ITableMetaData tableBMetadata = new DatabaseTableMetaData("B", connection, true);
86              List<Column> dbTableColumns = new ArrayList<Column>();
87              for (Column col : tableAMetadata.getColumns()) {
88              	dbTableColumns.add(col);
89              }
90              for (Column col : tableBMetadata.getColumns()) {
91              	dbTableColumns.add(col);
92              }
93      
94              String sql = "select * from A inner join B on B.FK_A = A.PK_A";
95              ForwardOnlyResultSetTable resultSetTable = new ForwardOnlyResultSetTable("A", sql, connection);
96              ResultSetTableMetaData resultSetMetadata = (ResultSetTableMetaData) resultSetTable.getTableMetaData();
97              Column[] resultSetColumns = resultSetMetadata.getColumns();
98              
99              Iterator<Column> tableColIterator = dbTableColumns.iterator();
100             for (int i = 0; i < resultSetColumns.length; i++) {
101             	Column tableCol = tableColIterator.next();
102             	String msg = "Comparing col i: " + resultSetColumns[i].getColumnName() + " vs " + tableCol.getColumnName();
103             	//System.out.println(msg);
104             	assertEquals(msg, tableCol, resultSetColumns[i]);
105             }
106         }
107         finally {
108             HypersonicEnvironment.shutdown(jdbcConnection);
109             jdbcConnection.close();
110             HypersonicEnvironment.deleteFiles("tempdb");
111         }
112     }
113 
114 }