Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ While this is incredibly useful, understanding the resulting tests is often hard
After researching and looking through the DBUnit code -- especially `FlatXmlDataSetBuilder` and the classes it uses -- for a while, I figured it possible but there was no nice, readable way to do it, yet. Therefore, I came up with a class called `DataSetBuilder` which is basically a wrapper around a `CachedDataSet` using a `BufferedConsumer`. Let's take a look at an example:

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!
ColumnSpec<String> name = ColumnSpec.newColumn("NAME")
ColumnSpec<Integer> age = ColumnSpec.newColumn("AGE");
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();

The code listed above creates three records in the `PERSON` table. It showcases two different ways of specifying columns, one using plain Strings and one using `ColumnSpec` instances, respectively.
Expand All @@ -64,3 +64,38 @@ will print

I think I found a way to create a data set directly from Java code in a readable way. In addition, creating the data sets programmatically gives to tools like refactoring, search for references, and so on for free.

## Solution - Step 2
The idea to create a data set directly from Java code is great and give new possibilities, for example use a sequence for ids.
However the code is still boiler plated. SO the second natural step is to create table specific row builder.
With them the code can be more compact.

DataSetBuilder builder = new DataSetBuilder();
newPERSON().NAME("Bob").BIRTHPLACE("NEW YORK").addTo(builder);
newPERSON().NAME("Alice").BIRTHPLACE("London").addTo(builder);


with this the code is type-safe and compact. And to make it really comfortable
there should be a generator for this builders - `CustomRowBuilderGenerator.java`.
You can use it very easily:

CustomRowBuilderGenerator rowBuilder = new CustomRowBuilderGenerator(
new File("src/test/java"), "net.sf.sze.dbunit.rowbuilder", "UTF-8");
rowBuilder.addTypeMapping(BigInteger.class, Long.class);
rowBuilder.addTypeMapping(BigDecimal.class, Double.class);
rowBuilder.generate(getConnection().createDataSet());

With this preparation you can dump dataset from the database with

BuilderDataSetWriter writer = new BuilderDataSetWriter(
new File("src/test/java"), "net.sf.sze.dbunit.dataset",
"ResultDS", "UTF-8", "net.sf.sze.dbunit.rowbuilder", true, importStatements);
writer.addTypeMapping(BigInteger.class, Long.class);
writer.addTypeMapping(BigDecimal.class, Double.class);
writer.write(getConnection().createDataSet());

You can combine this with [Validators dbunit-validation](https://github.com/opensource21/dbunit-validation)
to make your test more robust.

A further improvement is the DataSetRowChanger. This allows you to define
which rows of of a given dataset should change. To use this you must declare
identifier columns which should be a unique key.
222 changes: 156 additions & 66 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,66 +1,156 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.dbunit.datasetbuilder</groupId>
<artifactId>dbunit-datasetbuilder</artifactId>
<version>1.0-SNAPSHOT</version>

<name>DbUnit DataSetBuilder</name>
<url>https://github.com/marcphilipp/dbunit-datasetbuilder</url>
<description>DBUnit - Dynamically Creating Data Sets Using Builders</description>

<licenses>
<license>
<name>GNU Lesser General Public License, Version 2.1</name>
<url>http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt</url>
</license>
</licenses>

<scm>
<url>http://github.com/marcphilipp/dbunit-datasetbuilder</url>
<connection>scm:git:git://github.com/marcphilipp/dbunit-datasetbuilder</connection>
<developerConnection>scm:git:git@github.com:marcphilipp/dbunit-datasetbuilder.git</developerConnection>
</scm>

<developers>
<developer>
<id>marcphilipp</id>
<name>Marc Philipp</name>
<email>mail@marcphilipp.de</email>
</developer>
</developers>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>de.ppi.dbunit.datasetbuilder</groupId>
<artifactId>dbunit-datasetbuilder</artifactId>
<version>1.5.1-SNAPSHOT</version>

<name>DbUnit DataSetBuilder</name>
<url>https://github.com/marcphilipp/dbunit-datasetbuilder</url>
<description>DBUnit - Dynamically Creating Data Sets Using Builders</description>

<licenses>
<license>
<name>GNU Lesser General Public License, Version 2.1</name>
<url>http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt</url>
</license>
</licenses>

<scm>
<url>http://github.com/opensource21/dbunit-datasetbuilder</url>
<connection>scm:git:git://github.com/opensource21/dbunit-datasetbuilder</connection>
<developerConnection>scm:git:git@github.com:opensource21/dbunit-datasetbuilder.git</developerConnection>
<tag>HEAD</tag>
</scm>
<organization>
<name>PPI AG</name>
<url>http://www.ppi.de</url>
</organization>
<developers>
<developer>
<id>marcphilipp</id>
<name>Marc Philipp</name>
<email>mail@marcphilipp.de</email>
</developer>
<developer>
<id>niels</id>
<name>Niels</name>
<email>opensource21@gmail.com</email>
</developer>
</developers>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.2</version>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>maven-central</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<detectJavaApiLink>false</detectJavaApiLink>
<detectOfflineLinks>false</detectOfflineLinks>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>nexus.releases</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>nexus.snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading