org.jboss.seam.mock
Class DBUnitSeamTest

java.lang.Object
  extended by org.jboss.seam.mock.AbstractSeamTest
      extended by org.jboss.seam.mock.SeamTest
          extended by org.jboss.seam.mock.DBUnitSeamTest

public abstract class DBUnitSeamTest
extends SeamTest

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:

  • A TestNG test parameter named datasourceJndiName is provided by the TestNG test runner, which automatically calls setDatasourceJndiName() on the test class before a logical test runs.
  • An instance of a test class is created manually and the setDatasourceJndiName() method is called after creation and before a test runs.
  • A subclass overrides the getConnection() method and returns a custom database connection.
  • 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.

    Author:
    Christian Bauer

    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

    datasourceJndiName

    protected String datasourceJndiName

    binaryDir

    protected String binaryDir

    database

    protected DBUnitSeamTest.Database database

    beforeTestOperations

    protected List<DBUnitSeamTest.DataSetOperation> beforeTestOperations

    afterTestOperations

    protected List<DBUnitSeamTest.DataSetOperation> afterTestOperations
    Constructor Detail

    DBUnitSeamTest

    public DBUnitSeamTest()
    Method Detail

    setDatasourceJndiName

    public void setDatasourceJndiName(String datasourceJndiName)

    setBinaryDir

    public void setBinaryDir(String binaryDir)

    setDatabase

    public void setDatabase(String database)

    setupClass

    public void setupClass()
                    throws Exception
    Description copied from class: AbstractSeamTest
    Setup this test class instance Must be run for each test class instance (e.g. @BeforeClass)

    Overrides:
    setupClass in class SeamTest
    Throws:
    Exception

    prepareDataBeforeTest

    public void prepareDataBeforeTest()

    cleanDataAfterTest

    public void cleanDataAfterTest()

    getConnection

    protected org.dbunit.database.IDatabaseConnection getConnection()
    Override this method if you want to provide your own DBUnit IDatabaseConnection instance.

    If you do not override this, default behavior is to use the * configured datasource name and to obtain a connection with a JNDI lookup.

    Returns:
    a DBUnit database connection (wrapped)

    disableReferentialIntegrity

    protected void disableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
    Override this method if you aren't using HSQL DB.

    Execute whatever statement is necessary to either defer or disable foreign key constraint checking on the given database connection, which is used by DBUnit to import datasets.

    Parameters:
    con - A DBUnit connection wrapper, which is used afterwards for dataset operations

    enableReferentialIntegrity

    protected void enableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
    Override this method if you aren't using HSQL DB.

    Execute whatever statement is necessary to enable integrity constraint checks after dataset operations.

    Parameters:
    con - A DBUnit connection wrapper, before it is used by the application again

    editConfig

    protected void editConfig(org.dbunit.database.DatabaseConfig config)
    Override this method if you require DBUnit configuration features or additional properties.

    Called after a connection has been obtaind and before the connection is used. Can be a NOOP method if no additional settings are necessary for your DBUnit/DBMS setup.

    Parameters:
    config - A DBUnit DatabaseConfig object for setting properties and features

    getBinaryDirFullpath

    protected URL getBinaryDirFullpath()
    Resolves the binary dir location with the help of the classloader, we need the absolute full path of that directory.

    Returns:
    URL full absolute path of the binary directory

    getResourceURL

    protected URL getResourceURL(String resource)

    getBinaryFile

    protected byte[] getBinaryFile(String filename)
                            throws Exception
    Throws:
    Exception

    prepareDBUnitOperations

    protected abstract void prepareDBUnitOperations()
    Implement this in a subclass.

    Use it to stack DBUnit DataSetOperation's with the beforeTestOperations and afterTestOperations lists.