/**
   * For the specified expected and actual tables (and excluding and including the specified
   * columns), verify the actual data is as expected.
   *
   * @param expectedTable The expected table to compare the actual table to.
   * @param actualTable The actual table to compare to the expected table.
   * @param excludeColumns The column names to exclude from comparison. See {@link
   *     org.dbunit.dataset.filter.DefaultColumnFilter#excludeColumn(String)} .
   * @param includeColumns The column names to only include in comparison. See {@link
   *     org.dbunit.dataset.filter.DefaultColumnFilter#includeColumn(String)} .
   * @throws DatabaseUnitException
   */
  public void verifyData(
      ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns)
      throws DatabaseUnitException {
    final String method = "verifyData: ";
    // Filter out the columns from the expected and actual results
    LOG.debug(method + "Applying filters to expected table");
    ITable expectedFilteredTable =
        applyColumnFilters(expectedTable, excludeColumns, includeColumns);
    LOG.debug(method + "Applying filters to actual table");
    ITable actualFilteredTable = applyColumnFilters(actualTable, excludeColumns, includeColumns);

    LOG.debug(method + "Sorting expected table");
    SortedTable expectedSortedTable = new SortedTable(expectedFilteredTable);
    LOG.debug(method + "Sorted expected table={}", expectedSortedTable);

    LOG.debug(method + "Sorting actual table");
    SortedTable actualSortedTable =
        new SortedTable(actualFilteredTable, expectedFilteredTable.getTableMetaData());
    LOG.debug(method + "Sorted actual table={}", actualSortedTable);

    LOG.debug(method + "Comparing expected table to actual table");
    Column[] additionalColumnInfo = expectedTable.getTableMetaData().getColumns();

    Assertion.assertEquals(expectedSortedTable, actualSortedTable, additionalColumnInfo);
  }
  private void testExecute(IDataSet dataSet) throws Exception {
    ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
    //        InsertIdentityOperation.CLEAN_INSERT.execute(_connection, dataSet);
    InsertIdentityOperation.INSERT.execute(_connection, dataSet);
    ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());

    assertEquals("table count", tablesBefore.length, tablesAfter.length);

    // Verify tables after
    for (int i = 0; i < tablesAfter.length; i++) {
      ITable tableBefore = tablesBefore[i];
      ITable tableAfter = tablesAfter[i];

      String name = tableAfter.getTableMetaData().getTableName();
      if (name.startsWith("IDENTITY")) {
        assertEquals("row count before: " + name, 0, tableBefore.getRowCount());
        if (dataSet instanceof ForwardOnlyDataSet) {
          assertTrue(name, tableAfter.getRowCount() > 0);
        } else {
          Assertion.assertEquals(dataSet.getTable(name), tableAfter);
        }
      } else {
        // Other tables should have not been affected
        Assertion.assertEquals(tableBefore, tableAfter);
      }
    }
  }
Beispiel #3
0
 /**
  * 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;
 }
  /**
   * Extract column names from {@link #resultTable}.
   *
   * @throws DataSetException
   * @see ITable#getTableMetaData()
   * @see ITableMetaData#getColumns()
   */
  private String[] extractColumnNames() throws DataSetException {
    final Column[] columns = resultTable.getTableMetaData().getColumns();
    final String[] columnNames = new String[columns.length]; // NOPMD

    for (int i = 0; i < columns.length; i++) {
      columnNames[i] = columns[i].getColumnName(); // NOPMD
    }
    return columnNames;
  } /* extractColumnNames */
Beispiel #5
0
  /** Creates a new XlsDataSet object that loads the specified Excel document. */
  public XlsDataSet(InputStream in) throws IOException, DataSetException {
    _tables = super.createTableNameMap();

    HSSFWorkbook workbook = new HSSFWorkbook(in);
    int sheetCount = workbook.getNumberOfSheets();
    for (int i = 0; i < sheetCount; i++) {
      ITable table = new XlsTable(workbook.getSheetName(i), workbook.getSheetAt(i));
      _tables.add(table.getTableMetaData().getTableName(), table);
    }
  }
Beispiel #6
0
  /**
   * Adds the tables of the DbUnit dataset to the given schema.
   *
   * @param dbUnitDataSet The DbUnit dataset containing the tables, not null
   * @param schema The schema to add the tables to, not null
   */
  protected void addTables(IDataSet dbUnitDataSet, Schema schema) throws DataSetException {
    ITableIterator dbUnitTableIterator = dbUnitDataSet.iterator();
    while (dbUnitTableIterator.next()) {
      ITable dbUnitTable = dbUnitTableIterator.getTable();
      String tableName = dbUnitTable.getTableMetaData().getTableName();

      List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(dbUnitTable);

      Table table = schema.getTable(tableName);
      if (table == null) {
        table = new Table(tableName);
        schema.addTable(table);
      }
      addRows(dbUnitTable, table, primaryKeyColumnNames);
    }
  }
 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);
  }
  public void testSetCustomIdentityColumnFilter() throws Exception {
    _connection
        .getConfig()
        .setProperty(
            InsertIdentityOperation.PROPERTY_IDENTITY_COLUMN_FILTER,
            InsertIdentityOperation.IDENTITY_FILTER_EXTENDED);
    try {
      IDataSet dataSet = _connection.createDataSet();
      ITable table = dataSet.getTable("IDENTITY_TABLE");

      InsertIdentityOperation op = new InsertIdentityOperation(DatabaseOperation.INSERT);
      boolean hasIdentityColumn = op.hasIdentityColumn(table.getTableMetaData(), _connection);
      assertTrue("Identity column not recognized", hasIdentityColumn);
    } finally {
      // Reset property
      _connection
          .getConfig()
          .setProperty(InsertIdentityOperation.PROPERTY_IDENTITY_COLUMN_FILTER, null);
    }
  }
Beispiel #10
0
  /**
   * 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);
        }
      }
    }
  }
  /* test case was added to validate the bug that tables with Identity columns that are not
  one of the primary keys are able to figure out if an IDENTITY_INSERT is needed.
  Thanks to Gaetano Di Gregorio for finding the bug.
  */
  public void testIdentityInsertNoPK() throws Exception {
    Reader in = TestUtils.getFileReader("xml/insertIdentityOperationTestNoPK.xml");
    IDataSet xmlDataSet = new FlatXmlDataSetBuilder().build(in);

    ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
    InsertIdentityOperation.CLEAN_INSERT.execute(_connection, xmlDataSet);
    ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());

    // Verify tables after
    for (int i = 0; i < tablesAfter.length; i++) {
      ITable tableBefore = tablesBefore[i];
      ITable tableAfter = tablesAfter[i];

      String name = tableAfter.getTableMetaData().getTableName();
      if (name.equals("TEST_IDENTITY_NOT_PK")) {
        assertEquals("row count before: " + name, 0, tableBefore.getRowCount());
        Assertion.assertEquals(xmlDataSet.getTable(name), tableAfter);
      } else {
        // Other tables should have not been affected
        Assertion.assertEquals(tableBefore, tableAfter);
      }
    }
  }
  /** {@inheritDoc} */
  public ITableMetaData getTableMetaData() {

    return wrapped.getTableMetaData();
  }