Skip to content

Commit

Permalink
Replace static method with separate class (PrepareDatabase)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Mar 31, 2012
1 parent 56f9a04 commit 7bb4b3f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
19 changes: 19 additions & 0 deletions test/de/marcphilipp/dbunit/example/builder/Schema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.marcphilipp.dbunit.example.builder;

import static org.dbunit.dataset.builder.ColumnSpec.newColumn;

import org.dbunit.dataset.builder.ColumnSpec;

class Schema {

static class Tables {
static final String PERSON = "PERSON";
}

static class PersonTable {
static final ColumnSpec<String> NAME = newColumn("NAME");
static final ColumnSpec<String> LAST_NAME = newColumn("LAST_NAME");
static final ColumnSpec<Integer> AGE = newColumn("AGE");
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package de.marcphilipp.dbunit.example.rules;

import static org.junit.rules.RuleChain.outerRule;
package de.marcphilipp.dbunit.example.h2;

import javax.sql.DataSource;

import org.h2.jdbcx.JdbcDataSource;
import org.junit.rules.TestRule;


public class TestDataSource {

Expand All @@ -21,10 +17,4 @@ public static DataSource dataSource() {
dataSource.setPassword(System.getProperty("test.jdbc.password", DEFAULT_PASSWORD));
return dataSource;
}

public static TestRule prepareDatabase(DataSource dataSource, Object testInstance) {
return outerRule(new OnlyRunOracleTestsOnOracle(dataSource)).around(
new CreateSchemaIfNecessary(dataSource, "schema.sql")).around(
new ImportDataSet(dataSource, testInstance));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.marcphilipp.dbunit.example.rules;

import static de.marcphilipp.dbunit.example.rules.TestDataSource.dataSource;
import static de.marcphilipp.dbunit.example.h2.TestDataSource.dataSource;
import static de.marcphilipp.dbunit.example.wrapped.PersonRowBuilder.newPerson;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.marcphilipp.dbunit.example.rules;

import static de.marcphilipp.dbunit.example.rules.TestDataSource.dataSource;
import static de.marcphilipp.dbunit.example.rules.TestDataSource.prepareDatabase;
import static de.marcphilipp.dbunit.example.h2.TestDataSource.dataSource;
import static de.marcphilipp.dbunit.example.wrapped.PersonRowBuilder.newPerson;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
Expand All @@ -20,7 +19,7 @@
public class CustomRowBuilderDatabaseTestWithSingleRule {

@Rule
public TestRule database = prepareDatabase(dataSource(), this);
public TestRule database = new PrepareDatabase(dataSource(), this);

@DataSet
public IDataSet dataSet() throws DataSetException {
Expand Down
31 changes: 31 additions & 0 deletions test/de/marcphilipp/dbunit/example/rules/PrepareDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.marcphilipp.dbunit.example.rules;

import javax.sql.DataSource;

import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class PrepareDatabase implements TestRule {

private final DataSource dataSource;
private final Object testInstance;

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

@Override
public Statement apply(Statement base, Description description) {
return chain().apply(base, description);
}

private RuleChain chain() {
return RuleChain.outerRule(new OnlyRunOracleTestsOnOracle(dataSource))
.around(new CreateSchemaIfNecessary(dataSource, "schema.sql"))
.around(new ImportDataSet(dataSource, testInstance));
}

}

0 comments on commit 7bb4b3f

Please sign in to comment.