org.dbunit.dataset.Column Java Examples

The following examples show how to use org.dbunit.dataset.Column. 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: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public HackedComparisonColumn(String tableName, Column expectedColumn, Column actualColumn, FailureHandler failureHandler) {
    
    // super class is actually useless, all public methods are overridden below
    super(tableName, expectedColumn, expectedColumn, failureHandler);
    
    this.columnName = expectedColumn.getColumnName();
    this.dataType = getComparisonDataType(tableName, expectedColumn, actualColumn, failureHandler);
}
 
Example #2
Source File: DataSetProducer.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Override
public void produce() throws DataSetException {

    consumer.startDataSet();

    final Map<String, List<Map<String, String>>> dataset = loadDataSet();

    for (final Map.Entry<String, List<Map<String, String>>> entry : dataset.entrySet()) {
        // an entry represents a table
        final List<Map<String, String>> rows = entry.getValue();
        // each row represents a record in a table
        final Collection<String> columnNames = extractColumnNames(rows);
        final ITableMetaData tableMetaData = new DefaultTableMetaData(entry.getKey(), createColumns(columnNames));

        consumer.startTable(tableMetaData);

        for (final Map<String, String> row : rows) {
            final List<String> values = new ArrayList<>();
            for (final Column column : tableMetaData.getColumns()) {
                final Object rawValue = row.get(column.getColumnName());
                final String value = rawValue == null ? null : String.valueOf(rawValue);
                values.add(value);
            }
            consumer.row(values.toArray());
        }

        consumer.endTable();

    }

    consumer.endDataSet();
}
 
Example #3
Source File: DataSetComparator.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Collection<String> extractColumnNames(final Column[] columns) {
    final List<String> names = new ArrayList<>(columns.length);
    for (final Column column : columns) {
        names.add(column.getColumnName().toLowerCase());
    }
    return names;
}
 
Example #4
Source File: JsonDataSetProducerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Map<String, String> rebuildRecord(final List<Column> columns, final Object[] entries) {
    final Map<String, String> record = new HashMap<>();
    for (int i = 0; i < columns.size(); i++) {
        record.put(columns.get(i).getColumnName(), (String) entries[i]);
    }
    return record;
}
 
Example #5
Source File: BasicRowBuilder.java    From database-rider with Apache License 2.0 5 votes vote down vote up
protected ITableMetaData toMetaData() {
    Column[] columns = new Column[numberOfColumns()];
    int index = 0;
    for (String columnName : columnNameToValue.keySet()) {
        columns[index++] = createColumn(columnName);
    }
    return createMetaData(columns);
}
 
Example #6
Source File: YamlDataSetProducerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Map<String, String> rebuildRecord(final List<Column> columns, final Object[] entries) {
    final Map<String, String> record = new HashMap<>();
    for (int i = 0; i < columns.size(); i++) {
        final Object entry = entries[i];
        record.put(columns.get(i).getColumnName(), entry == null ? null : String.valueOf(entry));
    }
    return record;
}
 
Example #7
Source File: SchemaUpgradeTestBase.java    From development with Apache License 2.0 5 votes vote down vote up
private Column[] filterColumns(Column[] allColumns) {
    List<Column> result = new ArrayList<Column>();
    for (Column column : allColumns) {
        if (!column.getColumnName().equals("moddate")) {
            result.add(column);
        }
    }
    return result.toArray(new Column[result.size()]);
}
 
Example #8
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
protected ComparisonColumn[] getComparisonColumns(String expectedTableName, Column[] expectedColumns, Column[] actualColumns, FailureHandler failureHandler) {
    ComparisonColumn[] result = new ComparisonColumn[expectedColumns.length];

    for (int j = 0; j < expectedColumns.length; j++) {
        Column expectedColumn = expectedColumns[j];
        Column actualColumn = actualColumns[j];
        result[j] = new HackedComparisonColumn(expectedTableName, expectedColumn, actualColumn, failureHandler);
    }
    return result;
}
 
Example #9
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 5 votes vote down vote up
public HackedComparisonColumn(String tableName, Column expectedColumn, Column actualColumn, FailureHandler failureHandler) {
    
    // super class is actually useless, all public methods are overridden below
    super(tableName, expectedColumn, expectedColumn, failureHandler);
    
    this.columnName = expectedColumn.getColumnName();
    this.dataType = getComparisonDataType(tableName, expectedColumn, actualColumn, failureHandler);
}
 
Example #10
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 #11
Source File: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
protected ComparisonColumn[] getComparisonColumns(String expectedTableName, Column[] expectedColumns, Column[] actualColumns, FailureHandler failureHandler) {
    ComparisonColumn[] result = new ComparisonColumn[expectedColumns.length];

    for (int j = 0; j < expectedColumns.length; j++) {
        Column expectedColumn = expectedColumns[j];
        Column actualColumn = actualColumns[j];
        result[j] = new HackedComparisonColumn(expectedTableName, expectedColumn, actualColumn, failureHandler);
    }
    return result;
}
 
Example #12
Source File: YMLWriter.java    From database-rider with Apache License 2.0 5 votes vote down vote up
@Override
public void row(Object[] values) throws DataSetException {
    try {
        for (int i = 0; i < values.length; i++) {

            if(values[i] == null){
                continue;
            }

            if (i == 0) {
                out.write(DOUBLE_SPACES + "- ");
            } else{
                out.write(DOUBLE_SPACES + DOUBLE_SPACES);
            }
            
            Column currentColumn = metaData.getColumns()[i];
            out.write(metaData.getColumns()[i].getColumnName()+": ");
            boolean isNumber = currentColumn.getDataType().isNumber();
            if(!isNumber){
                out.write("\"");
            }
            if(values[i] != null){
                out.write(values[i].toString());
            }
            if(!isNumber){
                out.write("\"");
            }
            out.write(NEW_LINE);
        }
    }catch (Exception e){
    	logger.warn("Could not write row.",e);
    }
}
 
Example #13
Source File: JSONWriter.java    From database-rider with Apache License 2.0 5 votes vote down vote up
private String createSetFromValues(Object[] values) throws DataSetException {
	StringBuilder sb = new StringBuilder();
	for (int i = 0; i < values.length; i++) {
		Object currentValue = values[i];
		if(currentValue == null){
				continue;
		}

		Column currentColumn = metaData.getColumns()[i];
		sb.append(FOUR_SPACES + DOUBLE_SPACES + '"').append(metaData.getColumns()[i].getColumnName()).append("\": ");
		boolean isNumber = currentColumn.getDataType().isNumber();
		if (!isNumber) {
			sb.append('"');
		}

		sb.append(currentValue.toString().replaceAll(NEW_LINE, "\\\\n"));

		if (!isNumber) {
			sb.append('"');
		}
		if(i != values.length-1){
			sb.append(',');
		}
		sb.append(NEW_LINE);

	}
	return replaceExtraCommaInTheEnd(sb);
}
 
Example #14
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 #15
Source File: DataSetComparatorTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final String tableName, final Column column) {
    return false;
}
 
Example #16
Source File: DataSetComparatorTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final String tableName, final Column column) {
    // reject column 1
    return column.getColumnName() != TABLE_1_COLUMN_1.getColumnName();
}
 
Example #17
Source File: YamlDataSetProducerTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testProduceDataSetUsingValidStream() throws DataSetException {
    // GIVEN
    final IDataSetConsumer consumer = mock(IDataSetConsumer.class);
    final IDataSetProducer producer = new YamlDataSetProducer(yamlStream);
    producer.setConsumer(consumer);

    // WHEN
    producer.produce();

    // THEN
    verify(consumer).startDataSet();

    final ArgumentCaptor<ITableMetaData> tmdCaptor = ArgumentCaptor.forClass(ITableMetaData.class);
    verify(consumer, times(2)).startTable(tmdCaptor.capture());

    final List<ITableMetaData> tmdList = tmdCaptor.getAllValues();
    final ITableMetaData tmd1 = tmdList.get(0);
    final List<Column> table1Columns = Arrays.asList(tmd1.getColumns());
    final ITableMetaData tmd2 = tmdList.get(1);
    final List<Column> table2Columns = Arrays.asList(tmd2.getColumns());

    assertThat(tmd1.getTableName(), equalTo("YAML_TABLE_1"));
    assertThat(table1Columns.size(), equalTo(7));
    assertThat(table1Columns, hasItems(columnWithName("id"), columnWithName("version"), columnWithName("value_1"),
            columnWithName("value_2"), columnWithName("value_3"), columnWithName("value_4"), columnWithName("value_5")));

    assertThat(tmd2.getTableName(), equalTo("YAML_TABLE_2"));
    assertThat(table2Columns.size(), equalTo(4));
    assertThat(table2Columns,
            hasItems(columnWithName("id"), columnWithName("version"), columnWithName("value_6"), columnWithName("value_7")));

    final ArgumentCaptor<Object[]> rowCaptor = ArgumentCaptor.forClass(Object[].class);
    verify(consumer, times(4)).row(rowCaptor.capture());

    final List<Object[]> allRows = rowCaptor.getAllValues();

    final Map<String, String> record1 = rebuildRecord(table1Columns, allRows.get(0));
    assertThat(record1.size(), equalTo(7));
    assertThat(record1.get("id"), equalTo("1"));
    assertThat(record1.get("version"), equalTo("Record 1 version"));
    assertThat(record1.get("value_1"), equalTo("Record 1 Value 1"));
    assertThat(record1.get("value_2"), equalTo("Record 1 Value 2"));
    assertThat(record1.get("value_3"), nullValue());
    assertThat(record1.get("value_4"), equalTo("Record 1 Value 4"));
    assertThat(record1.get("value_5"), nullValue());

    final Map<String, String> record2 = rebuildRecord(table1Columns, allRows.get(1));
    assertThat(record2.size(), equalTo(7));
    assertThat(record2.get("id"), equalTo("2"));
    assertThat(record2.get("version"), equalTo("Record 2 version"));
    assertThat(record2.get("value_1"), equalTo("Record 2 Value 1"));
    assertThat(record2.get("value_2"), equalTo("Record 2 Value 2"));
    assertThat(record2.get("value_3"), equalTo("Record 2 Value 3"));
    assertThat(record2.get("value_4"), nullValue());
    assertThat(record2.get("value_5"), nullValue());

    final Map<String, String> record3 = rebuildRecord(table1Columns, allRows.get(2));
    assertThat(record3.size(), equalTo(7));
    assertThat(record3.get("id"), equalTo("3"));
    assertThat(record3.get("version"), equalTo("Record 3 version"));
    assertThat(record3.get("value_1"), nullValue());
    assertThat(record3.get("value_2"), nullValue());
    assertThat(record3.get("value_3"), nullValue());
    assertThat(record3.get("value_4"), nullValue());
    assertThat(record3.get("value_5"), equalTo("Record 3 Value 5"));

    final Map<String, String> record4 = rebuildRecord(table2Columns, allRows.get(3));
    assertThat(record4.size(), equalTo(4));
    assertThat(record4.get("id"), equalTo("4"));
    assertThat(record4.get("version"), equalTo("Record 4 version"));
    assertThat(record4.get("value_6"), equalTo("Record 4 Value 6"));
    assertThat(record4.get("value_7"), equalTo("Record 4 Value 7"));

    verify(consumer, times(2)).endTable();

    verify(consumer).endDataSet();
}
 
Example #18
Source File: JsonDataSetProducerTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testProduceDataSetUsingValidStream() throws DataSetException {
    // GIVEN
    final IDataSetConsumer consumer = mock(IDataSetConsumer.class);
    final IDataSetProducer producer = new JsonDataSetProducer(jsonStream);
    producer.setConsumer(consumer);

    // WHEN
    producer.produce();

    // THEN
    verify(consumer).startDataSet();

    final ArgumentCaptor<ITableMetaData> tmdCaptor = ArgumentCaptor.forClass(ITableMetaData.class);
    verify(consumer, times(2)).startTable(tmdCaptor.capture());

    final List<ITableMetaData> tmdList = tmdCaptor.getAllValues();
    final ITableMetaData tmd1 = tmdList.get(0);
    final List<Column> table1Columns = Arrays.asList(tmd1.getColumns());
    final ITableMetaData tmd2 = tmdList.get(1);
    final List<Column> table2Columns = Arrays.asList(tmd2.getColumns());

    assertThat(tmd1.getTableName(), equalTo("JSON_TABLE_1"));
    assertThat(table1Columns.size(), equalTo(7));
    assertThat(table1Columns, hasItems(columnWithName("id"), columnWithName("version"), columnWithName("value_1"),
            columnWithName("value_2"), columnWithName("value_3"), columnWithName("value_4"), columnWithName("value_5")));

    assertThat(tmd2.getTableName(), equalTo("JSON_TABLE_2"));
    assertThat(table2Columns.size(), equalTo(4));
    assertThat(table2Columns,
            hasItems(columnWithName("id"), columnWithName("version"), columnWithName("value_6"), columnWithName("value_7")));

    final ArgumentCaptor<Object[]> rowCaptor = ArgumentCaptor.forClass(Object[].class);
    verify(consumer, times(4)).row(rowCaptor.capture());

    final List<Object[]> allRows = rowCaptor.getAllValues();

    final Map<String, String> record1 = rebuildRecord(table1Columns, allRows.get(0));
    assertThat(record1.size(), equalTo(7));
    assertThat(record1.get("id"), equalTo("1"));
    assertThat(record1.get("version"), equalTo("Record 1 version"));
    assertThat(record1.get("value_1"), equalTo("Record 1 Value 1"));
    assertThat(record1.get("value_2"), equalTo("Record 1 Value 2"));
    assertThat(record1.get("value_3"), nullValue());
    assertThat(record1.get("value_4"), equalTo("Record 1 Value 4"));
    assertThat(record1.get("value_5"), nullValue());

    final Map<String, String> record2 = rebuildRecord(table1Columns, allRows.get(1));
    assertThat(record2.size(), equalTo(7));
    assertThat(record2.get("id"), equalTo("2"));
    assertThat(record2.get("version"), equalTo("Record 2 version"));
    assertThat(record2.get("value_1"), equalTo("Record 2 Value 1"));
    assertThat(record2.get("value_2"), equalTo("Record 2 Value 2"));
    assertThat(record2.get("value_3"), equalTo("Record 2 Value 3"));
    assertThat(record2.get("value_4"), nullValue());
    assertThat(record2.get("value_5"), nullValue());

    final Map<String, String> record3 = rebuildRecord(table1Columns, allRows.get(2));
    assertThat(record3.size(), equalTo(7));
    assertThat(record3.get("id"), equalTo("3"));
    assertThat(record3.get("version"), equalTo("Record 3 version"));
    assertThat(record3.get("value_1"), nullValue());
    assertThat(record3.get("value_2"), nullValue());
    assertThat(record3.get("value_3"), nullValue());
    assertThat(record3.get("value_4"), nullValue());
    assertThat(record3.get("value_5"), equalTo("Record 3 Value 5"));

    final Map<String, String> record4 = rebuildRecord(table2Columns, allRows.get(3));
    assertThat(record4.size(), equalTo(4));
    assertThat(record4.get("id"), equalTo("4"));
    assertThat(record4.get("version"), equalTo("Record 4 version"));
    assertThat(record4.get("value_6"), equalTo("Record 4 Value 6"));
    assertThat(record4.get("value_7"), equalTo("Record 4 Value 7"));

    verify(consumer, times(2)).endTable();

    verify(consumer).endDataSet();
}
 
Example #19
Source File: ColumnNameMatcher.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(final Object item) {
    return ((Column) item).getColumnName().equals(columnName);
}
 
Example #20
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 #21
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 #22
Source File: ExpectedDataSetsTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final String tableName, final Column column) {
    return !names.contains(column.getColumnName());
}
 
Example #23
Source File: ExpectedDataSetsTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final String tableName, final Column column) {
    return !names.contains(column.getColumnName());
}
 
Example #24
Source File: ExpectedDataSetsTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final String tableName, final Column column) {
    return !names.contains(column.getColumnName());
}
 
Example #25
Source File: ExpectedDataSetsTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final String tableName, final Column column) {
    return !names.contains(column.getColumnName());
}
 
Example #26
Source File: BasicRowBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
protected Object getValue(Column column) {
    return getValue(column.getColumnName());
}
 
Example #27
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 #28
Source File: BasicRowBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
protected ITableMetaData createMetaData(Column[] columns) {
    return new DefaultTableMetaData(tableName, columns);
}
 
Example #29
Source File: TableMetaDataBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
private Column[] columns() {
    return keysToColumns.values().toArray(new Column[keysToColumns.size()]);
}
 
Example #30
Source File: TableMetaDataBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
private boolean isKnown(Column column) {
    return keysToColumns.containsKey(toKey(column));
}