YAML4DbUnit integrates JYaml with DbUnit so that you can write your data set files in YAML rather than XML. Here is an example:
customer: - id: 1 name: "John McEnroe" login: jMAC - id: 2 name: "Larry Bird" login: larryB transactions: - id: 1 customer_id: 1 item_id: 1 purchase_date: "2007-05-15 15:40:16.638" - id: 2 customer_id: 2 item_id: 2 purchase_date: "2007-03-12 15:40:16.638" item: - id: 1 name: "Wilson Tennis Racquet" price: 230.00 - id: 2 name: "Autographed Basketball Jersey" price: 100.00
This is equivalent to the following dbUnit data set file in XML:
<?xml version='1.0' encoding='UTF-8'?> <dataset> <customer id="1" name="John McEnroe" login="jMAC"/> <customer id="2" name="Larry Bird" login="larryB"/> <transactions id="1" customer_id="1" item_id="1" purchase_date="2007-05-15 15:40:16.0"/> <transactions id="2" customer_id="2" item_id="2" purchase_date="2007-03-12 15:40:16.0"/> <item id="1" name="Wilson Tennis Racquet" price="230.0"/> <item id="2" name="Autographed Basketball Jersey" price="100.0"/> </dataset>
Which is better? You decide.
To use this, all you need to do is to replace
IDataSet dataSet = new FlatXmlDataSet(new FileInputStream("dataset.xml"));
with
IDataSet dataSet = new YamlDataSet(new File("dataset.yml"));
Below is the complete source code for YAML4DbUnit.
package org.dbunit.dataset.yaml; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dbunit.dataset.Column; import org.dbunit.dataset.DataSetException; import org.dbunit.dataset.DefaultTableIterator; import org.dbunit.dataset.DefaultTableMetaData; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ITable; import org.dbunit.dataset.ITableIterator; import org.dbunit.dataset.ITableMetaData; import org.dbunit.dataset.RowOutOfBoundsException; import org.dbunit.dataset.datatype.DataType; import org.ho.yaml.Yaml; public class YamlDataSet implements IDataSet { Map<String, MyTable> tables = new HashMap<String, MyTable>(); public YamlDataSet(File file) throws FileNotFoundException{ Map<String, List<Map>> data = (Map<String, List<Map>>)Yaml.load(file); for (Map.Entry<String, List<Map>> ent: data.entrySet()){ String tableName = ent.getKey(); List<Map> rows = ent.getValue(); createTable(tableName, rows); } } class MyTable implements ITable{ String name; List<Map> data; ITableMetaData meta; MyTable(String name, List<String> columnNames){ this.name = name; this.data = new ArrayList<Map>(); meta = createMeta(name, columnNames); } 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); } return new DefaultTableMetaData(name, columns); } public int getRowCount() { return data.size(); } public ITableMetaData getTableMetaData() { return meta; } public Object getValue(int row, String column) throws DataSetException { if (data.size() <= row) throw new RowOutOfBoundsException("" +row); return data.get(row).get(column.toUpperCase()); } public void addRow(Map values){ data.add(convertMap(values)); } Map convertMap(Map<String, Object> values){ Map ret = new HashMap(); for (Map.Entry<String, Object> ent: values.entrySet()){ ret.put(ent.getKey().toUpperCase(), ent.getValue()); } return ret; } } MyTable createTable(String name, List<Map> rows){ MyTable table = new MyTable(name, rows.size() > 0? new ArrayList(rows.get(0).keySet()) : null); for (Map values: rows) table.addRow(values); tables.put(name, table); return table; } public ITable getTable(String tableName) throws DataSetException { return tables.get(tableName); } public ITableMetaData getTableMetaData(String tableName) throws DataSetException { return tables.get(tableName).getTableMetaData(); } public String[] getTableNames() throws DataSetException { return (String[])tables.keySet().toArray(new String[tables.size()]); } public ITable[] getTables() throws DataSetException { return (ITable[])tables.values().toArray(new ITable[tables.size()]); } public ITableIterator iterator() throws DataSetException { return new DefaultTableIterator(getTables()); } public ITableIterator reverseIterator() throws DataSetException { return new DefaultTableIterator(getTables(), true); } }
117 lines of code in total, not too bad, ehh?