Created
February 24, 2014 20:07
-
-
Save kingargyle/9195975 to your computer and use it in GitHub Desktop.
Update of the DBUnit Test Case to work with JUnit 4 instead of relying on JUnit 3.
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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.dbunit.DefaultDatabaseTester; | |
import org.dbunit.DefaultOperationListener; | |
import org.dbunit.IDatabaseTester; | |
import org.dbunit.IOperationListener; | |
import org.dbunit.database.DatabaseConfig; | |
import org.dbunit.database.IDatabaseConnection; | |
import org.dbunit.dataset.IDataSet; | |
import org.dbunit.operation.DatabaseOperation; | |
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
/** | |
* Convenience class for writing JUnit tests with dbunit easily. | |
* <br /> | |
* Note that there are some even more convenient classes available such | |
* as {@link DBTestCase}. | |
* | |
* @author Manuel Laflamme | |
* @author davidcarver - updatd to Junit 4. | |
* | |
* Original code converted to play nicely with JUnit 4. | |
* | |
*/ | |
public abstract class DatabaseTestCase | |
{ | |
/** | |
* Logger for this class | |
*/ | |
private static final Logger logger = LoggerFactory.getLogger(DatabaseTestCase.class); | |
private IDatabaseTester tester; | |
private IOperationListener operationListener; | |
/** | |
* Returns the test database connection. | |
*/ | |
protected abstract IDatabaseConnection getConnection() throws Exception; | |
/** | |
* Returns the test dataset. | |
*/ | |
protected abstract IDataSet getDataSet() throws Exception; | |
/** | |
* Creates a IDatabaseTester for this testCase.<br> | |
* | |
* A {@link DefaultDatabaseTester} is used by default. | |
* @throws Exception | |
*/ | |
protected IDatabaseTester newDatabaseTester() throws Exception{ | |
logger.debug("newDatabaseTester() - start"); | |
final IDatabaseConnection connection = getConnection(); | |
getOperationListener().connectionRetrieved(connection); | |
final IDatabaseTester tester = new DefaultDatabaseTester(connection); | |
return tester; | |
} | |
/** | |
* Designed to be overridden by subclasses in order to set additional configuration | |
* parameters for the {@link IDatabaseConnection}. | |
* @param config The settings of the current {@link IDatabaseConnection} to be configured | |
*/ | |
protected void setUpDatabaseConfig(DatabaseConfig config) | |
{ | |
// Designed to be overridden. | |
} | |
/** | |
* Gets the IDatabaseTester for this testCase.<br> | |
* If the IDatabaseTester is not set yet, this method calls | |
* newDatabaseTester() to obtain a new instance. | |
* @throws Exception | |
*/ | |
protected IDatabaseTester getDatabaseTester() throws Exception { | |
if ( this.tester == null ) { | |
this.tester = newDatabaseTester(); | |
} | |
return this.tester; | |
} | |
/** | |
* Close the specified connection. Override this method of you want to | |
* keep your connection alive between tests. | |
* @deprecated since 2.4.4 define a user defined {@link #getOperationListener()} in advance | |
*/ | |
protected void closeConnection(IDatabaseConnection connection) throws Exception | |
{ | |
logger.debug("closeConnection(connection={}) - start", connection); | |
Assert.assertNotNull( "DatabaseTester is not set", getDatabaseTester() ); | |
getDatabaseTester().closeConnection( connection ); | |
} | |
/** | |
* Returns the database operation executed in test setup. | |
*/ | |
protected DatabaseOperation getSetUpOperation() throws Exception | |
{ | |
return DatabaseOperation.CLEAN_INSERT; | |
} | |
/** | |
* Returns the database operation executed in test cleanup. | |
*/ | |
protected DatabaseOperation getTearDownOperation() throws Exception | |
{ | |
return DatabaseOperation.NONE; | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
// TestCase class | |
@Before | |
public void setUp() throws Exception | |
{ | |
logger.debug("setUp() - start"); | |
final IDatabaseTester databaseTester = getDatabaseTester(); | |
Assert.assertNotNull( "DatabaseTester is not set", databaseTester ); | |
databaseTester.setSetUpOperation( getSetUpOperation() ); | |
databaseTester.setDataSet( getDataSet() ); | |
databaseTester.setOperationListener(getOperationListener()); | |
databaseTester.onSetup(); | |
} | |
@After | |
public void tearDown() throws Exception | |
{ | |
logger.debug("tearDown() - start"); | |
try { | |
final IDatabaseTester databaseTester = getDatabaseTester(); | |
Assert.assertNotNull( "DatabaseTester is not set", databaseTester ); | |
databaseTester.setTearDownOperation( getTearDownOperation() ); | |
databaseTester.setDataSet( getDataSet() ); | |
databaseTester.setOperationListener(getOperationListener()); | |
databaseTester.onTearDown(); | |
} finally { | |
tester = null; | |
} | |
} | |
/** | |
* @return The {@link IOperationListener} to be used by the {@link IDatabaseTester}. | |
* @since 2.4.4 | |
*/ | |
protected IOperationListener getOperationListener() | |
{ | |
logger.debug("getOperationListener() - start"); | |
if(this.operationListener==null){ | |
this.operationListener = new DefaultOperationListener(){ | |
public void connectionRetrieved(IDatabaseConnection connection) { | |
super.connectionRetrieved(connection); | |
// When a new connection has been created then invoke the setUp method | |
// so that user defined DatabaseConfig parameters can be set. | |
setUpDatabaseConfig(connection.getConfig()); | |
} | |
}; | |
} | |
return this.operationListener; | |
} | |
} |
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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.dbunit.IDatabaseTester; | |
import org.dbunit.PropertiesBasedJdbcDatabaseTester; | |
import org.dbunit.database.IDatabaseConnection; | |
import org.junit.Assert; | |
/** | |
* Base testCase for database testing.<br> | |
* Subclasses may override {@link #newDatabaseTester()} to plug-in a different | |
* implementation of IDatabaseTester.<br> | |
* Default implementation uses a {@link PropertiesBasedJdbcDatabaseTester}. | |
* | |
* Based on original DBTestCase for DBUnit, updated to work with JUnit 4 | |
* | |
* @author david carver | |
*/ | |
public abstract class DBTestCase extends DatabaseTestCase { | |
/** | |
* Logger for this class | |
*/ | |
private static final Logger logger = LoggerFactory | |
.getLogger(DBTestCase.class); | |
protected final IDatabaseConnection getConnection() throws Exception { | |
logger.debug("getConnection() - start"); | |
final IDatabaseTester databaseTester = getDatabaseTester(); | |
Assert.assertNotNull("DatabaseTester is not set", databaseTester); | |
IDatabaseConnection connection = databaseTester.getConnection(); | |
// Ensure that users have the possibility to configure the connection's | |
// configuration | |
setUpDatabaseConfig(connection.getConfig()); | |
return connection; | |
} | |
/** | |
* Creates a new IDatabaseTester. Default implementation returns a | |
* {@link PropertiesBasedJdbcDatabaseTester}. | |
*/ | |
protected IDatabaseTester newDatabaseTester() throws Exception { | |
return new PropertiesBasedJdbcDatabaseTester(); | |
} | |
} |
yes, it has to be under the same license according to the LGPL terms of use. Personally, if I had written and not modified the existing DBUnit Test Case, then it would be under an MIT License. @hyandell.
Thanks :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this gist licensed under LGPL 2.1 (same license as DBUnit)?