org.dbunit.dataset.ITableMetaData Java Examples

The following examples show how to use org.dbunit.dataset.ITableMetaData. 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: YamlDataSetProducerTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testProduceDataSetFromEmptyFile() throws DataSetException {
    // GIVEN
    final IDataSetConsumer consumer = mock(IDataSetConsumer.class);
    final IDataSetProducer producer = new YamlDataSetProducer(emptyStream);
    producer.setConsumer(consumer);

    // WHEN
    producer.produce();

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

    verify(consumer, times(0)).startTable(any(ITableMetaData.class));
    verify(consumer, times(0)).row(any(Object[].class));
    verify(consumer, times(0)).endTable();

    verify(consumer).endDataSet();
}
 
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: XlsTableWrapper.java    From Leo with Apache License 2.0 5 votes vote down vote up
public ITableMetaData getTableMetaData() {
    ITableMetaData meta = delegate.getTableMetaData();
    try {
        return new DefaultTableMetaData(tableName, meta.getColumns(), meta.getPrimaryKeys());
    } catch (DataSetException e) {
        throw new QtafException("Don't get the meta info from  " + meta, e);
    }
}
 
Example #4
Source File: JSONWriter.java    From database-rider with Apache License 2.0 5 votes vote down vote up
@Override
public void startTable(ITableMetaData metaData) throws DataSetException {
	this.metaData = metaData;
	rowCount = 0;
	try {
		out.write(DOUBLE_SPACES+"\""+metaData.getTableName() + "\": [" + NEW_LINE);
	} catch (IOException e) {
		logger.warn("Could not start table.", e);
	}
}
 
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: YMLWriter.java    From database-rider with Apache License 2.0 5 votes vote down vote up
@Override
public void startTable(ITableMetaData metaData) throws DataSetException {
    this.metaData = metaData;
    try {
        out.write(metaData.getTableName()+":"+NEW_LINE);
    } catch (IOException e) {
    	logger.warn("Could not start table.", e);
    }
}
 
Example #7
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 #8
Source File: ScriptableTable.java    From database-rider with Apache License 2.0 4 votes vote down vote up
@Override
public ITableMetaData getTableMetaData() {
    return delegate.getTableMetaData();
}
 
Example #9
Source File: ScriptableDataSetIterator.java    From database-rider with Apache License 2.0 4 votes vote down vote up
@Override
public ITableMetaData getTableMetaData() throws DataSetException {
    return delegate.getTableMetaData();
}
 
Example #10
Source File: TableMetaDataBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
public ITableMetaData build() {
    return new DefaultTableMetaData(tableName, columns());
}
 
Example #11
Source File: TableWrapper.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
public TableWrapper(final ITable table, final ITableMetaData metaData) {
    this.table = table;
    this.metaData = metaData;
}
 
Example #12
Source File: TableWrapper.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public ITableMetaData getTableMetaData() {
    return metaData;
}
 
Example #13
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 #14
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 #15
Source File: TableMetaDataBuilder.java    From database-rider with Apache License 2.0 4 votes vote down vote up
public TableMetaDataBuilder with(ITableMetaData metaData) throws DataSetException {
    return with(metaData.getColumns());
}
 
Example #16
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 #17
Source File: XlsTable.java    From Leo with Apache License 2.0 4 votes vote down vote up
public ITableMetaData getTableMetaData() {
    logger.debug("getTableMetaData() - start");

    return _metaData;
}