Browse files

Initial commit

  • Loading branch information...
0 parents commit a0dccf4b65b349687f2b93185056e1020459447e @marcphilipp committed Mar 26, 2011
11 .classpath
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="test"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="lib" path="lib/dbunit-2.4.8.jar" sourcepath="lib/dbunit-2.4.8-sources.jar"/>
+ <classpathentry kind="lib" path="lib/slf4j-api-1.6.1.jar"/>
+ <classpathentry kind="lib" path="lib/slf4j-simple-1.6.1.jar"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
1 .gitignore
@@ -0,0 +1 @@
+/bin
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>DataSetBuilder</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
12 .settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Sat Feb 12 17:09:59 CET 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
BIN lib/dbunit-2.4.8-sources.jar
Binary file not shown.
BIN lib/dbunit-2.4.8.jar
Binary file not shown.
BIN lib/slf4j-api-1.6.1.jar
Binary file not shown.
BIN lib/slf4j-simple-1.6.1.jar
Binary file not shown.
15 src/org/dbunit/dataset/builder/CaseInsensitiveStringPolicy.java
@@ -0,0 +1,15 @@
+package org.dbunit.dataset.builder;
+
+public class CaseInsensitiveStringPolicy implements StringPolicy {
+
+ @Override
+ public boolean areEqual(String first, String second) {
+ return first.equalsIgnoreCase(second);
+ }
+
+ @Override
+ public String toKey(String value) {
+ return value.toUpperCase();
+ }
+
+}
15 src/org/dbunit/dataset/builder/CaseSensitiveStringPolicy.java
@@ -0,0 +1,15 @@
+package org.dbunit.dataset.builder;
+
+public class CaseSensitiveStringPolicy implements StringPolicy {
+
+ @Override
+ public boolean areEqual(String first, String second) {
+ return first.equals(second);
+ }
+
+ @Override
+ public String toKey(String value) {
+ return value;
+ }
+
+}
19 src/org/dbunit/dataset/builder/ColumnSpec.java
@@ -0,0 +1,19 @@
+package org.dbunit.dataset.builder;
+
+public class ColumnSpec<T> {
+
+ private final String name;
+
+ public static <T> ColumnSpec<T> newColumn(String name) {
+ return new ColumnSpec<T>(name);
+ }
+
+ protected ColumnSpec(String name) {
+ this.name = name;
+ }
+
+ public String name() {
+ return name;
+ }
+
+}
88 src/org/dbunit/dataset/builder/DataRowBuilder.java
@@ -0,0 +1,88 @@
+package org.dbunit.dataset.builder;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.dbunit.dataset.Column;
+import org.dbunit.dataset.DataSetException;
+import org.dbunit.dataset.DefaultTableMetaData;
+import org.dbunit.dataset.ITableMetaData;
+import org.dbunit.dataset.datatype.DataType;
+
+public class DataRowBuilder {
+
+ private final DataSetBuilder dataSet;
+ private final String tableName;
+ private final Map<String, Object> columnNameToValue = new LinkedHashMap<String, Object>();
+
+ protected DataRowBuilder(DataSetBuilder dataSet, String tableName) {
+ this.dataSet = dataSet;
+ this.tableName = tableName;
+ }
+
+ public <T> DataRowBuilder with(ColumnSpec<T> column, T value) {
+ return with(column.name(), value);
+ }
+
+ public DataRowBuilder with(String columnName, Object value) {
+ put(columnName, value);
+ return this;
+ }
+
+ public DataSetBuilder add() throws DataSetException {
+ dataSet.add(this);
+ return dataSet;
+ }
+
+ public Object[] values(Column[] columns) {
+ Object[] values = new Object[columns.length];
+ int index = 0;
+ for (Column column : columns) {
+ values[index++] = getValue(column);
+ }
+ return values;
+ }
+
+ public ITableMetaData toMetaData() {
+ Column[] columns = new Column[numberOfColumns()];
+ int index = 0;
+ for (String columnName : allColumnNames()) {
+ columns[index++] = createColumn(columnName);
+ }
+ return createMetaData(columns);
+ }
+
+ protected int numberOfColumns() {
+ return columnNameToValue.size();
+ }
+
+ protected Set<String> allColumnNames() {
+ return columnNameToValue.keySet();
+ }
+
+ protected ITableMetaData createMetaData(Column[] columns) {
+ return new DefaultTableMetaData(tableName, columns);
+ }
+
+ protected Column createColumn(String columnName) {
+ return new Column(columnName, DataType.UNKNOWN);
+ }
+
+ protected String tableName() {
+ return tableName;
+ }
+
+ protected void put(String columnName, Object value) {
+ columnNameToValue.put(columnName, value);
+ }
+
+ protected Object getValue(Column column) {
+ return getValue(column.getColumnName());
+ }
+
+ protected Object getValue(String columnName) {
+ return columnNameToValue.get(columnName);
+ }
+
+}
138 src/org/dbunit/dataset/builder/DataSetBuilder.java
@@ -0,0 +1,138 @@
+package org.dbunit.dataset.builder;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.dbunit.dataset.CachedDataSet;
+import org.dbunit.dataset.DataSetException;
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.ITableMetaData;
+import org.dbunit.dataset.stream.BufferedConsumer;
+import org.dbunit.dataset.stream.IDataSetConsumer;
+
+public class DataSetBuilder {
+
+ private final CachedDataSet dataSet = new CachedDataSet();
+ private final IDataSetConsumer consumer = new BufferedConsumer(dataSet);
+ private final Map<String, TableMetaDataBuilder> tableNameToMetaData = new HashMap<String, TableMetaDataBuilder>();
+ private final StringPolicy stringPolicy;
+
+ private String currentTableName;
+
+ private static StringPolicy stringPolicy(boolean ignoreCase) {
+ return ignoreCase ? new CaseInsensitiveStringPolicy() : new CaseSensitiveStringPolicy();
+ }
+
+ public DataSetBuilder() throws DataSetException {
+ this(true);
+ }
+
+ public DataSetBuilder(boolean ignoreCase) throws DataSetException {
+ this(stringPolicy(ignoreCase));
+ }
+
+ public DataSetBuilder(StringPolicy stringPolicy) throws DataSetException {
+ this.stringPolicy = stringPolicy;
+ consumer.startDataSet();
+ }
+
+ public void ensureTableIsPresent(String tableName) throws DataSetException {
+ if (containsTable(tableName)) {
+ return;
+ }
+ endTableIfNecessary();
+ startTable(metaDataBuilderFor(tableName).build());
+ }
+
+ public DataRowBuilder newRow(String tableName) {
+ return new DataRowBuilder(this, tableName);
+ }
+
+ public IDataSet build() throws DataSetException {
+ endTableIfNecessary();
+ consumer.endDataSet();
+ return dataSet;
+ }
+
+ protected void add(DataRowBuilder row) throws DataSetException {
+ ITableMetaData metaData = updateTableMetaData(row);
+ Object[] values = extractValues(row, metaData);
+ notifyConsumer(values);
+ }
+
+ private Object[] extractValues(DataRowBuilder row, ITableMetaData metaData) throws DataSetException {
+ return row.values(metaData.getColumns());
+ }
+
+ private void notifyConsumer(Object[] values) throws DataSetException {
+ consumer.row(values);
+ }
+
+ private ITableMetaData updateTableMetaData(DataRowBuilder row) throws DataSetException {
+ TableMetaDataBuilder builder = metaDataBuilderFor(row.tableName());
+ int previousNumberOfColumns = builder.numberOfColumns();
+
+ ITableMetaData metaData = builder.with(row.toMetaData()).build();
+ int newNumberOfColumns = metaData.getColumns().length;
+
+ boolean addedNewColumn = newNumberOfColumns > previousNumberOfColumns;
+ handleTable(metaData, addedNewColumn);
+
+ return metaData;
+ }
+
+ private void handleTable(ITableMetaData metaData, boolean addedNewColumn) throws DataSetException {
+ if (isNewTable(metaData.getTableName())) {
+ endTableIfNecessary();
+ startTable(metaData);
+ } else if (addedNewColumn) {
+ startTable(metaData);
+ }
+ }
+
+ private void startTable(ITableMetaData metaData) throws DataSetException {
+ currentTableName = metaData.getTableName();
+ consumer.startTable(metaData);
+ }
+
+ private void endTable() throws DataSetException {
+ consumer.endTable();
+ currentTableName = null;
+ }
+
+ private void endTableIfNecessary() throws DataSetException {
+ if (hasCurrentTable()) {
+ endTable();
+ }
+ }
+
+ private boolean hasCurrentTable() {
+ return currentTableName != null;
+ }
+
+ private boolean isNewTable(String tableName) {
+ return currentTableName == null || !stringPolicy.areEqual(currentTableName, tableName);
+ }
+
+ private TableMetaDataBuilder metaDataBuilderFor(String tableName) {
+ String key = stringPolicy.toKey(tableName);
+ if (containsKey(key)) {
+ return tableNameToMetaData.get(key);
+ }
+ TableMetaDataBuilder builder = createNewTableMetaDataBuilder(tableName);
+ tableNameToMetaData.put(key, builder);
+ return builder;
+ }
+
+ protected TableMetaDataBuilder createNewTableMetaDataBuilder(String tableName) {
+ return new TableMetaDataBuilder(tableName, stringPolicy);
+ }
+
+ public boolean containsTable(String tableName) {
+ return containsKey(stringPolicy.toKey(tableName));
+ }
+
+ private boolean containsKey(String key) {
+ return tableNameToMetaData.containsKey(key);
+ }
+}
9 src/org/dbunit/dataset/builder/StringPolicy.java
@@ -0,0 +1,9 @@
+package org.dbunit.dataset.builder;
+
+public interface StringPolicy {
+
+ boolean areEqual(String first, String second);
+
+ String toKey(String value);
+
+}
67 src/org/dbunit/dataset/builder/TableMetaDataBuilder.java
@@ -0,0 +1,67 @@
+package org.dbunit.dataset.builder;
+
+import java.util.LinkedHashMap;
+
+import org.dbunit.dataset.Column;
+import org.dbunit.dataset.DataSetException;
+import org.dbunit.dataset.DefaultTableMetaData;
+import org.dbunit.dataset.ITableMetaData;
+
+public class TableMetaDataBuilder {
+
+ private final String tableName;
+ private final StringPolicy policy;
+ private final LinkedHashMap<String, Column> keysToColumns = new LinkedHashMap<String, Column>();
+
+ public TableMetaDataBuilder(String tableName, StringPolicy policy) {
+ this.tableName = tableName;
+ this.policy = policy;
+ }
+
+ public TableMetaDataBuilder with(ITableMetaData metaData) throws DataSetException {
+ return with(metaData.getColumns());
+ }
+
+ public TableMetaDataBuilder with(Column... columns) {
+ for (Column column : columns) {
+ with(column);
+ }
+ return this;
+ }
+
+ public TableMetaDataBuilder with(Column column) {
+ if (isUnknown(column)) {
+ add(column);
+ }
+ return this;
+ }
+
+ public int numberOfColumns() {
+ return keysToColumns.size();
+ }
+
+ public ITableMetaData build() {
+ return new DefaultTableMetaData(tableName, columns());
+ }
+
+ private void add(Column column) {
+ keysToColumns.put(toKey(column), column);
+ }
+
+ private String toKey(Column column) {
+ return policy.toKey(column.getColumnName());
+ }
+
+ private boolean isUnknown(Column column) {
+ return !isKnown(column);
+ }
+
+ private boolean isKnown(Column column) {
+ return keysToColumns.containsKey(toKey(column));
+ }
+
+ private Column[] columns() {
+ return keysToColumns.values().toArray(new Column[keysToColumns.size()]);
+ }
+
+}
35 src/org/dbunit/example/DataSetBuilderExample.java
@@ -0,0 +1,35 @@
+package org.dbunit.example;
+
+import static org.dbunit.dataset.builder.ColumnSpec.newColumn;
+
+import java.io.PrintWriter;
+
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.builder.ColumnSpec;
+import org.dbunit.dataset.builder.DataSetBuilder;
+import org.dbunit.dataset.xml.XmlDataSetWriter;
+
+public class DataSetBuilderExample {
+
+ private static final ColumnSpec<String> NAME = newColumn("NAME");
+ private static final ColumnSpec<Integer> AGE = ColumnSpec.newColumn("AGE");
+
+ public static void main(String[] args) throws Exception {
+
+ DataSetBuilder builder = new DataSetBuilder();
+
+ // Using strings as column names, not type-safe
+ builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();
+
+ // Using ColumnSpecs to identify columns, type-safe!
+ builder.newRow("PERSON").with(NAME, "Alice").with(AGE, 23).add();
+
+ // New columns are added on the fly
+ builder.newRow("PERSON").with(NAME, "Charlie").with("LAST_NAME", "Brown").add();
+
+ IDataSet dataSet = builder.build();
+
+ new XmlDataSetWriter(new PrintWriter(System.out)).write(dataSet);
+ }
+
+}
21 test/org/dbunit/dataset/builder/CaseInsensitiveStringPolicyTest.java
@@ -0,0 +1,21 @@
+package org.dbunit.dataset.builder;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class CaseInsensitiveStringPolicyTest {
+
+ private CaseInsensitiveStringPolicy policy = new CaseInsensitiveStringPolicy();
+
+ @Test
+ public void keyIsStringInUpperCase() {
+ assertEquals("KEY1", policy.toKey("kEy1"));
+ }
+
+ @Test
+ public void caseIsIgnoredWhenComparingTwoStrings() throws Exception {
+ assertTrue(policy.areEqual("KEY1", "kEy1"));
+ assertFalse(policy.areEqual("KEY1", "KEY2"));
+ }
+}
23 test/org/dbunit/dataset/builder/CaseSensitiveStringPolicyTest.java
@@ -0,0 +1,23 @@
+package org.dbunit.dataset.builder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class CaseSensitiveStringPolicyTest {
+
+ private CaseSensitiveStringPolicy policy = new CaseSensitiveStringPolicy();
+
+ @Test
+ public void keyIsSameAsInputString() {
+ assertEquals("kEy1", policy.toKey("kEy1"));
+ }
+
+ @Test
+ public void caseIsConsideredWhenComparingTwoStrings() throws Exception {
+ assertFalse(policy.areEqual("KEY1", "kEy1"));
+ assertTrue(policy.areEqual("KEY1", "KEY1"));
+ }
+}
109 test/org/dbunit/dataset/builder/DataSetBuilderTest.java
@@ -0,0 +1,109 @@
+package org.dbunit.dataset.builder;
+
+import static org.junit.Assert.*;
+
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.ITable;
+import org.junit.Test;
+
+public class DataSetBuilderTest {
+
+ @Test
+ public void ignoringCaseByDefault() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ builder.ensureTableIsPresent("PeRsoN");
+
+ assertTrue(builder.containsTable("Person"));
+ }
+
+ @Test
+ public void notIgnoringCaseWhenSpecified() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder(false);
+ builder.ensureTableIsPresent("PeRsoN");
+
+ assertFalse(builder.containsTable("Person"));
+ assertTrue(builder.containsTable("PeRsoN"));
+ }
+
+ @Test
+ public void ensuringPresenceOfExistingTableMakesNoHarm() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ builder.newRow("person").add();
+ builder.ensureTableIsPresent("PeRsoN");
+
+ assertTrue(builder.containsTable("Person"));
+ }
+
+ @Test
+ public void tablesAreKeptInOrder() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ builder.newRow("PERSON").add();
+ builder.newRow("ADDRESS").add();
+ builder.newRow("_TABLE_").add();
+ IDataSet dataSet = builder.build();
+
+ assertArrayEquals(new String[]{"PERSON", "ADDRESS", "_TABLE_"}, dataSet.getTableNames());
+ }
+
+ @Test
+ public void addsDataForSingleRow() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();
+
+ IDataSet dataSet = builder.build();
+ ITable table = dataSet.getTable("PERSON");
+ assertEquals(1, table.getRowCount());
+ assertEquals("Bob", table.getValue(0, "NAME"));
+ assertEquals(18, table.getValue(0, "AGE"));
+ }
+
+ @Test
+ public void addsDataWithColumnSpec() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ ColumnSpec<String> name = ColumnSpec.newColumn("NAME");
+ ColumnSpec<Integer> age = ColumnSpec.newColumn("AGE");
+ builder.newRow("PERSON").with(name, "Bob").with(age, 18).add();
+
+ IDataSet dataSet = builder.build();
+ ITable table = dataSet.getTable("PERSON");
+ assertEquals(1, table.getRowCount());
+ assertEquals("Bob", table.getValue(0, "NAME"));
+ assertEquals(18, table.getValue(0, "AGE"));
+ }
+
+ @Test
+ public void addsDataForMultipleRowsOfDifferentTables() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();
+ builder.newRow("ADDRESS").with("STREET", "Main Street").with("NUMBER", 42).add();
+ builder.newRow("PERSON").with("NAME", "Alice").with("AGE", 23).add();
+
+ IDataSet dataSet = builder.build();
+ ITable table = dataSet.getTable("PERSON");
+ assertEquals(2, table.getRowCount());
+ assertEquals("Bob", table.getValue(0, "NAME"));
+ assertEquals(18, table.getValue(0, "AGE"));
+ assertEquals("Alice", table.getValue(1, "NAME"));
+ assertEquals(23, table.getValue(1, "AGE"));
+
+ table = dataSet.getTable("ADDRESS");
+ assertEquals(1, table.getRowCount());
+ assertEquals("Main Street", table.getValue(0, "STREET"));
+ assertEquals(42, table.getValue(0, "NUMBER"));
+ }
+
+ @Test
+ public void addsNewColumnsOnTheFly() throws Exception {
+ DataSetBuilder builder = new DataSetBuilder();
+ builder.newRow("PERSON").with("NAME", "Bob").add();
+ builder.newRow("PERSON").with("AGE", 18).add();
+
+ IDataSet dataSet = builder.build();
+ ITable table = dataSet.getTable("PERSON");
+ assertEquals(2, table.getRowCount());
+ assertEquals("Bob", table.getValue(0, "NAME"));
+ assertNull(table.getValue(0, "AGE"));
+ assertNull(table.getValue(1, "NAME"));
+ assertEquals(18, table.getValue(1, "AGE"));
+ }
+}
63 test/org/dbunit/dataset/builder/TableMetaDataBuilderTest.java
@@ -0,0 +1,63 @@
+package org.dbunit.dataset.builder;
+
+import static org.junit.Assert.*;
+
+import org.dbunit.dataset.Column;
+import org.dbunit.dataset.DefaultTableMetaData;
+import org.dbunit.dataset.ITableMetaData;
+import org.dbunit.dataset.datatype.DataType;
+import org.junit.Test;
+
+public class TableMetaDataBuilderTest {
+
+ private TableMetaDataBuilder builder = new TableMetaDataBuilder("MY_TABLE_NAME", new CaseInsensitiveStringPolicy());
+
+ @Test
+ public void isEmptyInTheBeginning() throws Exception {
+ assertEquals(0, builder.numberOfColumns());
+ Column[] columns = builder.build().getColumns();
+ assertEquals(0, columns.length);
+ }
+
+ @Test
+ public void addsSingleColumn() throws Exception {
+ Column column = new Column("someColumn", DataType.CHAR);
+
+ builder.with(column);
+
+ assertEquals(1, builder.numberOfColumns());
+ Column[] columns = builder.build().getColumns();
+ assertEquals(1, columns.length);
+ assertEquals(column, columns[0]);
+ }
+
+ @Test
+ public void preservesExistingColumn() throws Exception {
+ Column originalColumn = new Column("someColumn", DataType.CHAR);
+ Column sameColumnDifferentType = new Column("someColumn", DataType.INTEGER);
+
+ builder.with(originalColumn).with(sameColumnDifferentType);
+
+ assertEquals(1, builder.numberOfColumns());
+ Column[] columns = builder.build().getColumns();
+ assertEquals(1, columns.length);
+ assertEquals(originalColumn, columns[0]);
+ }
+
+ @Test
+ public void addsAllColumnsOfTableMetaData() throws Exception {
+ Column existingColumn = new Column("existingColumn", DataType.CHAR);
+ Column newColumn = new Column("newColumn", DataType.INTEGER);
+ Column anotherNewColumn = new Column("anotherNewColumn", DataType.INTEGER);
+ ITableMetaData metaData = new DefaultTableMetaData("MY_TABLE_NAME", new Column[]{ newColumn, anotherNewColumn });
+
+ builder.with(existingColumn).with(metaData);
+
+ assertEquals(3, builder.numberOfColumns());
+ Column[] columns = builder.build().getColumns();
+ assertEquals(3, columns.length);
+ assertEquals(existingColumn, columns[0]);
+ assertEquals(newColumn, columns[1]);
+ assertEquals(anotherNewColumn, columns[2]);
+ }
+}

0 comments on commit a0dccf4

Please sign in to comment.