/** * Gets the primary key column names for the given DbUnit table. * * @param dbUnitTable The DbUnit table, not null * @return The pk column names, empty if none found */ protected List<String> getPrimaryKeyColumnNames(ITable dbUnitTable) throws DataSetException { List<String> result = new ArrayList<String>(); for (org.dbunit.dataset.Column column : dbUnitTable.getTableMetaData().getPrimaryKeys()) { result.add(column.getColumnName()); } return result; }
/** * Tests the pattern-like column retrieval from the database. DbUnit should not interpret any * table names as regex patterns. * * @throws Exception */ public void testGetColumnsForTablesMatchingSamePattern() throws Exception { Connection jdbcConnection = HypersonicEnvironment.createJdbcConnection("tempdb"); HypersonicEnvironment.executeDdlFile( new File("src/sql/hypersonic_dataset_pattern_test.sql"), jdbcConnection); IDatabaseConnection connection = new DatabaseConnection(jdbcConnection); try { String tableName = "PATTERN_LIKE_TABLE_X_"; String[] columnNames = {"VARCHAR_COL_XUNDERSCORE"}; String sql = "select * from " + tableName; ForwardOnlyResultSetTable resultSetTable = new ForwardOnlyResultSetTable(tableName, sql, connection); ResultSetTableMetaData metaData = (ResultSetTableMetaData) resultSetTable.getTableMetaData(); Column[] columns = metaData.getColumns(); assertEquals("column count", columnNames.length, columns.length); for (int i = 0; i < columnNames.length; i++) { Column column = Columns.getColumn(columnNames[i], columns); assertEquals(columnNames[i], columnNames[i], column.getColumnName()); } } finally { HypersonicEnvironment.shutdown(jdbcConnection); jdbcConnection.close(); HypersonicEnvironment.deleteFiles("tempdb"); } }
protected void expTable(String table, Column... columns) { expected = new DefaultTable(table, columns); query = "SELECT * FROM " + table; StringBuilder order = new StringBuilder(); for (Column column : columns) { order.append(column.getColumnName()).append(", "); } query = query.concat(" ORDER BY ").concat(order.substring(0, order.length() - 2)); }
private List<String> extractColumnNames() { final List<String> columnNames = new ArrayList<String>(); Column[] columns; try { columns = actual.getTableMetaData().getColumns(); for (Column column : columns) { columnNames.add(column.getColumnName()); } } catch (DataSetException e) { throw new RuntimeException(e); } return columnNames; }
private static List<Map<String, Object>> createProps( Class testClass, String file, String tableName) throws IOException, DataSetException { List<Map<String, Object>> propsList = new ArrayList<Map<String, Object>>(); IDataSet expected = new XlsDataSet(testClass.getResourceAsStream(file)); ITable table = expected.getTable(tableName); Column[] columns = table.getTableMetaData().getColumns(); for (int i = 0; i < table.getRowCount(); i++) { Map<String, Object> props = new HashMap<String, Object>(); for (Column c : columns) { Object value = table.getValue(i, c.getColumnName()); String propName = underlineToCamel(c.getColumnName()); props.put(propName, value); } propsList.add(props); } return propsList; }
/** * Print the actual results of the query, for troubleshooting. (It might be little slow.) * * @param sqlChecker * @throws DataSetException */ public void printSqlResults() throws DataSetException { ITable resultTable = this.getResultTable(); List<String> columns = new ArrayList<String>(); for (Column column : resultTable.getTableMetaData().getColumns()) { columns.add(column.getColumnName()); } StringBuilder tableCsv = new StringBuilder(); tableCsv.append(columns).append('\n'); for (int rowIdx = 0; rowIdx < resultTable.getRowCount(); rowIdx++) { List<Object> values = new ArrayList<Object>(columns.size()); for (String column : columns) { values.add(resultTable.getValue(rowIdx, column)); } tableCsv.append(values).append('\n'); } System.out.format("Select results:\n%s", tableCsv); }
/** * Adds the rows of the DbUnit table to the given table. * * @param dbUnitTable The DbUnit table containing the rows, not null * @param table The table to add the rows to, not null * @param primaryKeyColumnNames The names of the pk columns, empty if there are none */ protected void addRows(ITable dbUnitTable, Table table, List<String> primaryKeyColumnNames) throws DataSetException { org.dbunit.dataset.Column[] columns = dbUnitTable.getTableMetaData().getColumns(); int rowCount = dbUnitTable.getRowCount(); for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { Row row = new Row(); table.addRow(row); for (org.dbunit.dataset.Column dbUnitColumn : columns) { String columnName = dbUnitColumn.getColumnName(); DataType columnType = dbUnitColumn.getDataType(); Object value = dbUnitTable.getValue(rowIndex, columnName); Column column = new Column(columnName, columnType, value); if (primaryKeyColumnNames.contains(columnName)) { row.addPrimaryKeyColumn(column); } else { row.addColumn(column); } } } }