org.dbunit.dataset.datatype.DataType Java Examples

The following examples show how to use org.dbunit.dataset.datatype.DataType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONDataSet.java    From database-rider with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the table meta data based on the rows for a table
 * 
 * @param tableName
 *            The name of the table
 * @param rows
 *            The rows of the table
 * @return The table metadata for the table
 */
private ITableMetaData getMetaData(String tableName,
		List<Map<String, Object>> rows) {
	Set<String> columns = new LinkedHashSet<String>();
	// iterate through the dataset and add the column names to a set
	for (Map<String, Object> row : rows) {
		for (Map.Entry<String, Object> column : row.entrySet()) {
			columns.add(column.getKey());
		}
	}
	List<Column> list = new ArrayList<Column>(columns.size());
	// create a list of DBUnit columns based on the column name set
	for (String s : columns) {
		list.add(new Column(s, DataType.UNKNOWN));
	}
	return new DefaultTableMetaData(tableName,
			list.toArray(new Column[list.size()]));
}
 
Example #2
Source File: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private DataType getComparisonDataType(String tableName, Column expectedColumn, Column actualColumn, FailureHandler failureHandler) {
    if (logger.isDebugEnabled())
        logger.debug("getComparisonDataType(tableName={}, expectedColumn={}, actualColumn={}, failureHandler={}) - start", new Object[] { tableName, expectedColumn, actualColumn, failureHandler });

    DataType expectedDataType = expectedColumn.getDataType();
    DataType actualDataType = actualColumn.getDataType();

    // The two columns have different data type
    if (!expectedDataType.getClass().isInstance(actualDataType)) {
        // Expected column data type is unknown, use actual column data type
        if (expectedDataType instanceof UnknownDataType) {
            return actualDataType;
        }

        // Actual column data type is unknown, use expected column data type
        if (actualDataType instanceof UnknownDataType) {
            return expectedDataType;
        }
        
        if (hackIgnoreIntBigIntMismatch) {
            if (expectedDataType instanceof IntegerDataType && actualDataType instanceof BigIntegerDataType)
                return actualDataType;
        }

        // Impossible to determine which data type to use
        String msg = "Incompatible data types: (table=" + tableName + ", col=" + expectedColumn.getColumnName() + ")";
        throw failureHandler.createFailure(msg, String.valueOf(expectedDataType), String.valueOf(actualDataType));
    }

    // Both columns have same data type, return any one of them
    return expectedDataType;
}
 
Example #3
Source File: KylinTestBase.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Override
public DataType createDataType(int sqlType, String sqlTypeName, String tableName, String columnName) throws DataTypeException {

    if ((columnName.startsWith("COL") || columnName.startsWith("col")) && sqlType == Types.BIGINT) {
        return DataType.INTEGER;
    }
    return super.createDataType(sqlType, sqlTypeName);
}
 
Example #4
Source File: BuilderUtil.java    From database-rider with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated use <code>DataType.UNKNOWN</code> instead of this method. See https://github.com/database-rider/database-rider/pull/154#issuecomment-527622138
 * @param value column value
 * @return resolved datatype
 */
public static DataType resolveColumnDataType(Object value) {
    DataType columnType = DataType.UNKNOWN;
    if(value == null) {
        return columnType;
    }
    if (value instanceof Integer) {
        columnType = DataType.INTEGER;
    }
    if (value instanceof Long) {
        columnType = DataType.BIGINT_AUX_LONG;
    }
    if (value instanceof Double) {
        columnType = DataType.DOUBLE;
    }
    if (value instanceof Float) {
        columnType = DataType.FLOAT;
    }
    if (value instanceof Date) {
        columnType = DataType.DATE;
    }
    if (value instanceof Boolean) {
        columnType = DataType.BOOLEAN;
    }
    if (value instanceof BigDecimal) {
        columnType = DataType.DECIMAL;
    }
    if (value instanceof Number) {
        columnType = DataType.NUMERIC;
    }
    return columnType;
}
 
Example #5
Source File: YamlDataSet.java    From database-rider with Apache License 2.0 5 votes vote down vote up
ITableMetaData createMeta(String name, List<String> columnNames) {
    Column[] columns = null;
    if (columnNames != null) {
        columns = new Column[columnNames.size()];
        for (int i = 0; i < columnNames.size(); i++)
            columns[i] = new Column(columnNames.get(i), DataType.UNKNOWN);
    } else {
        columns = new Column[0];
    }
    return new DefaultTableMetaData(name, columns);
}
 
Example #6
Source File: ContainsFilterTable.java    From database-rider with Apache License 2.0 5 votes vote down vote up
/**
 * Searches for full match in original table by values from expected table
 * @param columns column names
 * @param values column values
 * @param filteredRowIndexes list of row indexes already found by previous runs
 * @return row index of original table containing all requested values
 * @throws DataSetException throws DataSetException
 */
private Integer tableContains(List<String> columns, List<Object> values, List<Integer> filteredRowIndexes, List<String> ignoredCols) throws DataSetException {
    int fullSize = this.originalTable.getRowCount();

    for ( int row=0; row<fullSize; row++ ) {
        boolean match = true;
        for (int column = 0; column < columns.size(); column++) {
            if(ignoredCols != null && ignoredCols.contains(columns.get(column).toUpperCase())) {
                continue;
            }
            if (values.get(column) != null && values.get(column).toString().startsWith("regex:")) {
                if (!regexMatches(values.get(column).toString(), this.originalTable.getValue(row, columns.get(column)).toString())) {
                    match = false;
                    break;
                }
                continue;
            }

            int columnIndex = this.originalTable.getTableMetaData().getColumnIndex(columns.get(column));
            DataType dataType = this.originalTable.getTableMetaData().getColumns()[columnIndex].getDataType();
            if (dataType.compare(values.get(column), this.originalTable.getValue(row, columns.get(column))) != 0) {
                match = false;
                break;
            }
        }

        if (match && !filteredRowIndexes.contains(row)) {
            return row;
        }
    }
    return null;
}
 
Example #7
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public DataType createDataType(int sqlType, String sqlTypeName, String tableName, String columnName)
        throws DataTypeException {

    if ((columnName.startsWith("COL") || columnName.startsWith("col")) && sqlType == Types.BIGINT) {
        return DataType.INTEGER;
    }
    return super.createDataType(sqlType, sqlTypeName);
}
 
Example #8
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 5 votes vote down vote up
private DataType getComparisonDataType(String tableName, Column expectedColumn, Column actualColumn, FailureHandler failureHandler) {
    if (logger.isDebugEnabled())
        logger.debug("getComparisonDataType(tableName={}, expectedColumn={}, actualColumn={}, failureHandler={}) - start", new Object[] { tableName, expectedColumn, actualColumn, failureHandler });

    DataType expectedDataType = expectedColumn.getDataType();
    DataType actualDataType = actualColumn.getDataType();

    // The two columns have different data type
    if (!expectedDataType.getClass().isInstance(actualDataType)) {
        // Expected column data type is unknown, use actual column data type
        if (expectedDataType instanceof UnknownDataType) {
            return actualDataType;
        }

        // Actual column data type is unknown, use expected column data type
        if (actualDataType instanceof UnknownDataType) {
            return expectedDataType;
        }
        
        if (hackIgnoreIntBigIntMismatch) {
            if (expectedDataType instanceof IntegerDataType && actualDataType instanceof BigIntegerDataType)
                return actualDataType;
        }

        // Impossible to determine which data type to use
        String msg = "Incompatible data types: (table=" + tableName + ", col=" + expectedColumn.getColumnName() + ")";
        throw failureHandler.createFailure(msg, String.valueOf(expectedDataType), String.valueOf(actualDataType));
    }

    // Both columns have same data type, return any one of them
    return expectedDataType;
}
 
Example #9
Source File: KylinTestBase.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public DataType createDataType(int sqlType, String sqlTypeName, String tableName, String columnName)
        throws DataTypeException {

    if ((columnName.startsWith("COL") || columnName.startsWith("col")) && sqlType == Types.BIGINT) {
        return DataType.INTEGER;
    }
    return super.createDataType(sqlType, sqlTypeName);
}
 
Example #10
Source File: DBUnitConfigTest.java    From database-rider with Apache License 2.0 4 votes vote down vote up
@Override
public DataType createDataType(int i, String s) {
    throw new UnsupportedOperationException("only for configuration tests");
}
 
Example #11
Source File: XlsTable.java    From Leo with Apache License 2.0 4 votes vote down vote up
static ITableMetaData createMetaData(String tableName, HSSFRow sampleRow, HSSFWorkbook workbook) {
    logger.debug("createMetaData(tableName={}, sampleRow={}) - start", tableName, sampleRow);

    List<Column> columnList = new ArrayList<Column>();
    List<String> primaryKeyList = new ArrayList<String>();
    for (int i = 0;; i++) {
        HSSFCell cell = sampleRow.getCell(i);
        if (cell == null) {
            break;
        }

        String columnName = cell.getRichStringCellValue().getString();
        if (columnName != null) {
            columnName = columnName.trim();
        }

        // Bugfix for issue ID 2818981 - if a cell has a formatting but no
        // name also ignore it
        if (columnName.length() <= 0) {
            logger
                    .debug(
                            "The column name of column # {} is empty - will skip here assuming the last column was reached",
                            String.valueOf(i));
            break;
        }

        Column column = new Column(columnName, DataType.UNKNOWN);
        columnList.add(column);
        
        // Unique identification key
        byte underline = cell.getCellStyle().getFont(workbook).getUnderline();
        if (underline == 1) {
            primaryKeyList.add(columnName);
        } 

    }
    Column[] columns = columnList.toArray(new Column[0]);
    
    if(!primaryKeyList.isEmpty()){
    	return new DefaultTableMetaData(tableName, columns,primaryKeyList.toArray(new String[primaryKeyList.size()]));
    }else{
    	return new DefaultTableMetaData(tableName, columns);
    }
    
}
 
Example #12
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 4 votes vote down vote up
private boolean findRowInExpectedTable(ITable expectedTable, ITable actualTable, ComparisonColumn[] comparisonCols, FailureHandler failureHandler, int index) throws DataSetException {

        // iterate over all rows
        for (int i = 0; i < expectedTable.getRowCount(); i++) {

            // iterate over all columns of the current row
            for (int j = 0; j < comparisonCols.length; j++) {
                ComparisonColumn compareColumn = comparisonCols[j];

                String columnName = compareColumn.getColumnName();
                DataType dataType = compareColumn.getDataType();

                Object expectedValue = expectedTable.getValue(i, columnName);
                Object actualValue = actualTable.getValue(index, columnName);

                // Compare the values
                if (skipCompare(columnName, expectedValue, actualValue)) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("ignoring comparison " + expectedValue + "=" + actualValue + " on column " + columnName);
                    }
                    continue;
                }

                if (dataType.compare(expectedValue, actualValue) != 0) {
                    break;

                    //                    Difference diff = new Difference(expectedTable, actualTable, i, columnName, expectedValue, actualValue);
                    //
                    //                    // Handle the difference (throw error immediately or something else)
                    //                    failureHandler.handle(diff);
                } else {
                    if (j == comparisonCols.length - 1) {
                        return true;
                    } else {
                        continue;
                    }
                }
            }
        }
        return false;
    }
 
Example #13
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public DataType getDataType() {
    return this.dataType;
}
 
Example #14
Source File: DataSetProducer.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
private Column[] createColumns(final Collection<String> columnNames) {
    final List<Column> columns = columnNames.stream().map(e -> new Column(e, DataType.UNKNOWN)).collect(toList());
    return columns.toArray(new Column[columns.size()]);
}
 
Example #15
Source File: DBUnitConfigTest.java    From database-rider with Apache License 2.0 4 votes vote down vote up
@Override
public DataType createDataType(int i, String s, String s1, String s2) {
    throw new UnsupportedOperationException("only for configuration tests");
}
 
Example #16
Source File: BasicRowBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
protected Column createColumn(String columnName) {
    DataType columnType = DataType.UNKNOWN;
    return new Column(columnName, columnType);
}
 
Example #17
Source File: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private boolean findRowInExpectedTable(ITable expectedTable, ITable actualTable, ComparisonColumn[] comparisonCols, FailureHandler failureHandler, int index) throws DataSetException {

        // iterate over all rows
        for (int i = 0; i < expectedTable.getRowCount(); i++) {

            // iterate over all columns of the current row
            for (int j = 0; j < comparisonCols.length; j++) {
                ComparisonColumn compareColumn = comparisonCols[j];

                String columnName = compareColumn.getColumnName();
                DataType dataType = compareColumn.getDataType();

                Object expectedValue = expectedTable.getValue(i, columnName);
                Object actualValue = actualTable.getValue(index, columnName);

                // Compare the values
                if (skipCompare(columnName, expectedValue, actualValue)) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("ignoring comparison " + expectedValue + "=" + actualValue + " on column " + columnName);
                    }
                    continue;
                }

                if (dataType.compare(expectedValue, actualValue) != 0) {
                    break;

                    //                    Difference diff = new Difference(expectedTable, actualTable, i, columnName, expectedValue, actualValue);
                    //
                    //                    // Handle the difference (throw error immediately or something else)
                    //                    failureHandler.handle(diff);
                } else {
                    if (j == comparisonCols.length - 1) {
                        return true;
                    } else {
                        continue;
                    }
                }
            }
        }
        return false;
    }
 
Example #18
Source File: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public DataType getDataType() {
    return this.dataType;
}