Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeSafeBuilderDatabaseTestWithRules
- Loading branch information
1 parent
aa285e1
commit a114a31
Showing
10 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package de.marcphilipp.dbunit.example; | ||
|
||
public class Person { | ||
|
||
private final String firstName; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
test/de/marcphilipp/dbunit/example/TypeSafeBuilderDatabaseTestWithRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters