Skip to content

Commit

Permalink
TypeSafeBuilderDatabaseTestWithRules
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Feb 22, 2012
1 parent aa285e1 commit a114a31
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/de/marcphilipp/dbunit/example/Person.java
@@ -1,4 +1,5 @@
package de.marcphilipp.dbunit.example;

public class Person {

private final String firstName;
Expand Down
4 changes: 1 addition & 3 deletions test/de/marcphilipp/dbunit/example/BuilderDatabaseTest.java
@@ -1,4 +1,5 @@
package de.marcphilipp.dbunit.example;

import static org.h2.engine.Constants.UTF8;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
Expand All @@ -18,9 +19,6 @@
import org.junit.BeforeClass;
import org.junit.Test;

import de.marcphilipp.dbunit.example.Person;
import de.marcphilipp.dbunit.example.PersonRepository;

public class BuilderDatabaseTest {

private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
Expand Down
71 changes: 71 additions & 0 deletions test/de/marcphilipp/dbunit/example/CreateSchema.java
@@ -0,0 +1,71 @@
package de.marcphilipp.dbunit.example;

import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.h2.tools.RunScript;
import org.junit.rules.ExternalResource;

public class CreateSchema extends ExternalResource implements Using {

private String sqlFilePath;
private final DataSource dataSource;

public static Using in(DataSource dataSource) {
return new CreateSchema(dataSource);
}

private CreateSchema(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
protected void before() throws Throwable {
FileReader reader = null;
Connection connection = null;
try {
reader = new FileReader(new File(sqlFilePath));
connection = dataSource.getConnection();
RunScript.execute(connection, reader);
} finally {
closeQuietly(connection);
closeQuietly(reader);
}
}

@Override
public CreateSchema using(String sqlFilePath) {
this.sqlFilePath = sqlFilePath;
return this;
}

private void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException exception) {
}
}
}

private void closeQuietly(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException exception) {
}
}
}
}

interface Using {

CreateSchema using(String sqlFilePath);

}
9 changes: 9 additions & 0 deletions test/de/marcphilipp/dbunit/example/DataSet.java
@@ -0,0 +1,9 @@
package de.marcphilipp.dbunit.example;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface DataSet {

}
22 changes: 22 additions & 0 deletions test/de/marcphilipp/dbunit/example/H2DatabaseTester.java
@@ -0,0 +1,22 @@
package de.marcphilipp.dbunit.example;

import javax.sql.DataSource;

import org.dbunit.AbstractDatabaseTester;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.ext.h2.H2Connection;

public class H2DatabaseTester extends AbstractDatabaseTester {

private final DataSource dataSource;

public H2DatabaseTester(DataSource dataSource) {
super();
this.dataSource = dataSource;
}

@Override
public IDatabaseConnection getConnection() throws Exception {
return new H2Connection(dataSource.getConnection(), getSchema());
}
}
58 changes: 58 additions & 0 deletions test/de/marcphilipp/dbunit/example/ImportDataSet.java
@@ -0,0 +1,58 @@
package de.marcphilipp.dbunit.example;

import static org.dbunit.operation.DatabaseOperation.CLEAN_INSERT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Method;

import javax.sql.DataSource;

import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DefaultDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.rules.ExternalResource;

public class ImportDataSet extends ExternalResource {

private final Object testInstance;
private final DataSource dataSource;

public ImportDataSet(Object testInstance, DataSource dataSource) {
this.testInstance = testInstance;
this.dataSource = dataSource;
}

private DefaultDataSet emptyDataSet() {
return new DefaultDataSet();
}

@Override
protected void before() throws Throwable {
for (Method method : testInstance.getClass().getMethods()) {
method.getAnnotations();
if (method.isAnnotationPresent(DataSet.class)) {
assertEquals("@DataSet method must not have parameters", 0, method.getParameterTypes().length);
assertTrue("return type of @DataSet method must be assignable from IDataSet", method.getReturnType()
.isAssignableFrom(IDataSet.class));
IDataSet dataSet = (IDataSet) method.invoke(testInstance);
execute(CLEAN_INSERT, dataSet);
}
}
emptyDataSet();
}

public ImportDataSet execute(DatabaseOperation operation, IDataSet dataSet) throws Exception {
IDatabaseTester databaseTester = createTester();
databaseTester.setSetUpOperation(operation);
databaseTester.setDataSet(dataSet);
databaseTester.onSetup();
return this;
}

protected H2DatabaseTester createTester() {
return new H2DatabaseTester(dataSource);
}

}
20 changes: 20 additions & 0 deletions test/de/marcphilipp/dbunit/example/TestDataSource.java
@@ -0,0 +1,20 @@
package de.marcphilipp.dbunit.example;

import javax.sql.DataSource;

import org.h2.jdbcx.JdbcDataSource;

public class TestDataSource {

public static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
public static final String USER = "sa";
public static final String PASSWORD = "";

public static DataSource dataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(JDBC_URL);
dataSource.setUser(USER);
dataSource.setPassword(PASSWORD);
return dataSource;
}
}
Expand Up @@ -12,7 +12,6 @@
import javax.sql.DataSource;

import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.builder.DataSetBuilder;
Expand All @@ -23,12 +22,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import de.marcphilipp.dbunit.example.Person;
import de.marcphilipp.dbunit.example.PersonRepository;

public class TypeSafeBuilderDatabaseTest {

private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
private static final String USER = "sa";
private static final String PASSWORD = "";
Expand All @@ -53,7 +48,7 @@ private IDataSet buildDataSet() throws DataSetException {
}

private void cleanlyInsertDataset(IDataSet dataSet) throws ClassNotFoundException, Exception {
IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
IDatabaseTester databaseTester = new H2DatabaseTester(dataSource());
databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
databaseTester.setDataSet(dataSet);
databaseTester.onSetup();
Expand Down
@@ -0,0 +1,54 @@
package de.marcphilipp.dbunit.example;

import static de.marcphilipp.dbunit.example.Schema.PersonTable.AGE;
import static de.marcphilipp.dbunit.example.Schema.PersonTable.LAST_NAME;
import static de.marcphilipp.dbunit.example.Schema.PersonTable.NAME;
import static de.marcphilipp.dbunit.example.Schema.Tables.PERSON;
import static de.marcphilipp.dbunit.example.TestDataSource.dataSource;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.builder.DataSetBuilder;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

public class TypeSafeBuilderDatabaseTestWithRules {

@ClassRule
public static TestRule schema = CreateSchema.in(dataSource()).using("schema.sql");

@Rule
public ImportDataSet database = new ImportDataSet(this, dataSource());

@DataSet
public IDataSet dataSet() throws DataSetException {
DataSetBuilder builder = new DataSetBuilder();
builder.newRow(PERSON).with(NAME, "Bob").with(LAST_NAME, "Doe").with(AGE, 18).add();
builder.newRow(PERSON).with(NAME, "Alice").with(LAST_NAME, "Foo").with(AGE, 23).add();
builder.newRow(PERSON).with(NAME, "Charlie").with(LAST_NAME, "Brown").with(AGE, 42).add();
return builder.build();
}

@Test
public void findsAndReadsExistingPersonByFirstName() throws Exception {
PersonRepository repository = new PersonRepository(dataSource());
Person charlie = repository.findPersonByFirstName("Charlie");

assertThat(charlie.getFirstName(), is("Charlie"));
assertThat(charlie.getLastName(), is("Brown"));
assertThat(charlie.getAge(), is(42));
}

@Test
public void returnsNullWhenPersonCannotBeFoundByFirstName() throws Exception {
PersonRepository repository = new PersonRepository(dataSource());
Person person = repository.findPersonByFirstName("iDoNotExist");

assertThat(person, is(nullValue()));
}
}
3 changes: 0 additions & 3 deletions test/de/marcphilipp/dbunit/example/XmlDatabaseTest.java
Expand Up @@ -22,9 +22,6 @@
import org.junit.BeforeClass;
import org.junit.Test;

import de.marcphilipp.dbunit.example.Person;
import de.marcphilipp.dbunit.example.PersonRepository;

public class XmlDatabaseTest {

private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
Expand Down

0 comments on commit a114a31

Please sign in to comment.