|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.jboss.seam.mock.AbstractSeamTest
org.jboss.seam.mock.SeamTest
org.jboss.seam.mock.DBUnitSeamTest
public abstract class DBUnitSeamTest
Utility for integration testing with Seam and DBUnit datasets.
Subclass this class instead of SeamTest if you need to insert or clean data in your database before and after a test. You need to implement prepareDBUnitOperations() and add instances of DataSetOperations to the beforeTestOperations and afterTestOperations lists. An example:
public class MyTest extends DBUnitSeamTest {
protected void prepareDBUnitOperations() {
beforeTestOperations.add(
new DataSetOperation("my/datasets/BaseData.xml")
);
beforeTestOperations.add(
new DataSetOperation("my/datasets/AdditionalData.xml", DatabaseOperation.INSERT)
);
}
... // Various test methods with @Test annotation
}
Note that DataSetOperation defaults to DatabaseOperation.CLEAN_INSERT if no other operation is specified as a constructor argument. The above example cleans all tables defined in BaseData.xml, then inserts all rows declared in BaseData.xml, then inserts all the rows declared in AdditionalData.xml. This executes before each test method is invoked. If you require extra cleanup after a test method executes, add operations to the afterTestOperations list.
A test class obtains the database connection for loading and cleaning of datasets in one of the following ways:
Binary files can be imported into the database from a binary directory, configured with the TestNG parameter binaryDir or by calling setBinaryDir() before a test runs. The binary directory is a classpath reference, e.g. my/org/test/package/binarydir. In your DBUnit XML flat dataset, declare the path of your file as follows: <MYTABLE MYCOLUMN="[BINARY_DIR]/mytestfile.png"/>
Referential integrity checks (foreign keys) will be or have to be disabled on the database connection used for DBUnit operations. This makes adding circular references in datasets easier (especially for nullable foreign key columns). Referential integrity checks are enabled again after the connection has been used.
IMPORTANT: The methods disableReferentialIntegrity(), enableReferentialIntegrity(), and editConfig() are implemented for HSQL and MySQL. You need to configure the DBMS you are using with the database TestNG parameter or by calling setDatabase() before the the test run. If you want to run unit tests on any other DBMS, you need to override the disableReferentialIntegrity() and enableReferentialIntegrity() methods and implement them for your DBMS. Also note that by default, if no database TestNG parameter has been set or if the setDatabase() method has not been called before test runs, HSQL DB will be used as the default.
| Nested Class Summary | |
|---|---|
static class |
DBUnitSeamTest.Database
|
protected class |
DBUnitSeamTest.DataSetOperation
|
| Nested classes/interfaces inherited from class org.jboss.seam.mock.AbstractSeamTest |
|---|
AbstractSeamTest.ComponentTest, AbstractSeamTest.FacesRequest, AbstractSeamTest.NonFacesRequest |
| Field Summary | |
|---|---|
protected List<DBUnitSeamTest.DataSetOperation> |
afterTestOperations
|
protected List<DBUnitSeamTest.DataSetOperation> |
beforeTestOperations
|
protected String |
binaryDir
|
protected DBUnitSeamTest.Database |
database
|
protected String |
datasourceJndiName
|
| Fields inherited from class org.jboss.seam.mock.AbstractSeamTest |
|---|
seamFilter, servletContext, session |
| Constructor Summary | |
|---|---|
DBUnitSeamTest()
|
|
| Method Summary | |
|---|---|
void |
cleanDataAfterTest()
|
protected void |
disableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
Override this method if you aren't using HSQL DB. |
protected void |
editConfig(org.dbunit.database.DatabaseConfig config)
Override this method if you require DBUnit configuration features or additional properties. |
protected void |
enableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
Override this method if you aren't using HSQL DB. |
protected URL |
getBinaryDirFullpath()
Resolves the binary dir location with the help of the classloader, we need the absolute full path of that directory. |
protected byte[] |
getBinaryFile(String filename)
|
protected org.dbunit.database.IDatabaseConnection |
getConnection()
Override this method if you want to provide your own DBUnit IDatabaseConnection instance. |
protected URL |
getResourceURL(String resource)
|
void |
prepareDataBeforeTest()
|
protected abstract void |
prepareDBUnitOperations()
Implement this in a subclass. |
void |
setBinaryDir(String binaryDir)
|
void |
setDatabase(String database)
|
void |
setDatasourceJndiName(String datasourceJndiName)
|
void |
setupClass()
Setup this test class instance Must be run for each test class instance (e.g. |
| Methods inherited from class org.jboss.seam.mock.SeamTest |
|---|
begin, cleanupClass, end, startSeam, stopSeam |
| Methods inherited from class org.jboss.seam.mock.AbstractSeamTest |
|---|
createSeamFilter, createServletContext, getELResolvers, getField, getInitialContext, getInstance, getInstance, getSession, getUserTransaction, initServletContext, installMockTransport, isLongRunningConversation, isSessionInvalid, lookup, setField, startJbossEmbeddedIfNecessary |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected String datasourceJndiName
protected String binaryDir
protected DBUnitSeamTest.Database database
protected List<DBUnitSeamTest.DataSetOperation> beforeTestOperations
protected List<DBUnitSeamTest.DataSetOperation> afterTestOperations
| Constructor Detail |
|---|
public DBUnitSeamTest()
| Method Detail |
|---|
public void setDatasourceJndiName(String datasourceJndiName)
public void setBinaryDir(String binaryDir)
public void setDatabase(String database)
public void setupClass()
throws Exception
AbstractSeamTest
setupClass in class SeamTestExceptionpublic void prepareDataBeforeTest()
public void cleanDataAfterTest()
protected org.dbunit.database.IDatabaseConnection getConnection()
protected void disableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
con - A DBUnit connection wrapper, which is used afterwards for dataset operationsprotected void enableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
con - A DBUnit connection wrapper, before it is used by the application againprotected void editConfig(org.dbunit.database.DatabaseConfig config)
config - A DBUnit DatabaseConfig object for setting properties and featuresprotected URL getBinaryDirFullpath()
protected URL getResourceURL(String resource)
protected byte[] getBinaryFile(String filename)
throws Exception
Exceptionprotected abstract void prepareDBUnitOperations()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||