View Javadoc
1   package org.dbunit.dataset;
2   
3   import java.sql.Connection;
4   import java.sql.DatabaseMetaData;
5   import java.sql.ResultSet;
6   import java.sql.Types;
7   
8   import org.dbunit.AbstractDatabaseIT;
9   import org.dbunit.HypersonicEnvironment;
10  import org.dbunit.database.DatabaseConfig;
11  import org.dbunit.database.DatabaseConnection;
12  import org.dbunit.database.IDatabaseConnection;
13  import org.dbunit.database.IMetadataHandler;
14  import org.dbunit.testutil.TestUtils;
15  import org.junit.Test;
16  
17  public class ColumnMetaDataTest extends AbstractDatabaseIT {
18  
19  	public ColumnMetaDataTest(String s) {
20  		super(s);
21  	}
22  
23  	@Test
24  	public void testAllColumns() throws Exception {
25          Connection jdbcConnection = HypersonicEnvironment.createJdbcConnection("tempdb");
26          HypersonicEnvironment.executeDdlFile(TestUtils.getFile("sql/hypersonic_dataset_column_metadata.sql"), jdbcConnection);
27          IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
28  
29          try {
30              DatabaseMetaData databaseMetaData = jdbcConnection.getMetaData();
31              
32              DatabaseConfig config = connection.getConfig();
33              
34              IMetadataHandler metadataHandler = (IMetadataHandler)config.getProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER);
35              ResultSet colMetaRS = metadataHandler.getColumns(databaseMetaData, "PUBLIC", "%");
36              // PK_A INTEGER
37              colMetaRS.next();
38              ColumnMetaData pkaMetadata = new ColumnMetaData(colMetaRS);
39              assertEquals("PK_A.columnName", "PK_A", pkaMetadata.getColumnName());
40              assertEquals("PK_A.sqlType", Types.INTEGER, pkaMetadata.getSQLType().intValue());
41              assertEquals("PK_A.sqlTypeName", "INTEGER", pkaMetadata.getSqlTypeName());
42              //DEC_COL_A2 DECIMAL(9, 2)
43              colMetaRS.next();
44              ColumnMetaData colA2Metadata = new ColumnMetaData(colMetaRS);
45              assertEquals("DEC_COL_A2.columnName", "DEC_COL_A2", colA2Metadata.getColumnName());
46              assertEquals("DEC_COL_A2.sqlType", Types.DECIMAL, colA2Metadata.getSQLType().intValue());
47              assertEquals("DEC_COL_A2.sqlTypeName", "DECIMAL", colA2Metadata.getSqlTypeName());
48              assertEquals("DEC_COL_A2.decimalDigits", 2, colA2Metadata.getDecimalDigits().intValue());
49              //CHAR_COL_A3 CHAR(30)
50              colMetaRS.next();
51              ColumnMetaData colA3Metadata = new ColumnMetaData(colMetaRS);
52              assertEquals("CHAR_COL_A3.columnName", "CHAR_COL_A3", colA3Metadata.getColumnName());
53              assertEquals("CHAR_COL_A3.sqlType", Types.CHAR, colA3Metadata.getSQLType().intValue());
54              assertEquals("CHAR_COL_A3.sqlTypeName", "CHAR", colA3Metadata.getSqlTypeName());
55              assertEquals("CHAR_COL_A3.columnSize", 30, colA3Metadata.getColumnSize().intValue());
56              //DATE_COL_A4 DATE
57              colMetaRS.next();
58              ColumnMetaData colA4Metadata = new ColumnMetaData(colMetaRS);
59              assertEquals("DATE_COL_A4.columnName", "DATE_COL_A4", colA4Metadata.getColumnName());
60              assertEquals("DATE_COL_A4.sqlType", Types.DATE, colA4Metadata.getSQLType().intValue());
61              assertEquals("DATE_COL_A4.sqlTypeName", "DATE", colA4Metadata.getSqlTypeName());
62          } finally {
63              HypersonicEnvironment.shutdown(jdbcConnection);
64              jdbcConnection.close();
65              HypersonicEnvironment.deleteFiles("tempdb");
66          }
67  	}
68  
69  }