org.jboss.seam.mock
Class DBUnitSeamTest

java.lang.Object
  extended by org.jboss.seam.mock.BaseSeamTest
      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 every 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 datasourceJndiName is passed as a constructor argument.
  • 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.
  • 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. Referential integrity checks are enabled again after the connection has been used.

    Note that the methods disableReferentialIntegrity(), enableReferentialIntegrity(), and editConfig() are implemented for HSQL DB. If you want to run unit tests on any other DBMS, you need to override these methods and implement them for your DBMS.

    Author:
    Christian Bauer

    Nested Class Summary
    protected  class DBUnitSeamTest.DataSetOperation
               
     
    Nested classes/interfaces inherited from class org.jboss.seam.mock.SeamTest
    SeamTest.FacesRequest, SeamTest.NonFacesRequest, SeamTest.Request, SeamTest.Script
     
    Nested classes/interfaces inherited from class org.jboss.seam.mock.BaseSeamTest
    BaseSeamTest.ComponentTest
     
    Field Summary
    protected  List<DBUnitSeamTest.DataSetOperation> afterTestOperations
               
    protected  List<DBUnitSeamTest.DataSetOperation> beforeTestOperations
               
    protected  String datasourceJndiName
               
     
    Constructor Summary
    protected DBUnitSeamTest()
               
    protected DBUnitSeamTest(String datasourceJndiName)
               
     
    Method Summary
     void begin()
               
    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.
     void end()
               
    protected  org.dbunit.database.IDatabaseConnection getConnection()
              Override this method if you want to provide your own DBUnit IDatabaseConnection instance.
    protected abstract  void prepareDBUnitOperations()
              Implement this in a subclass.
     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
    cleanup, init
     
    Methods inherited from class org.jboss.seam.mock.BaseSeamTest
    cleanupClass, createSeamFilter, getELResolvers, getField, getInitialContext, getInstance, getInstance, getSession, getUserTransaction, initServletContext, installMockTransport, isLongRunningConversation, isSessionInvalid, lookup, setField, startJbossEmbeddedIfNecessary, startSeam, stopSeam
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    datasourceJndiName

    protected String datasourceJndiName

    beforeTestOperations

    protected List<DBUnitSeamTest.DataSetOperation> beforeTestOperations

    afterTestOperations

    protected List<DBUnitSeamTest.DataSetOperation> afterTestOperations
    Constructor Detail

    DBUnitSeamTest

    protected DBUnitSeamTest()

    DBUnitSeamTest

    protected DBUnitSeamTest(String datasourceJndiName)
    Method Detail

    setDatasourceJndiName

    public void setDatasourceJndiName(String datasourceJndiName)

    setupClass

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

    Overrides:
    setupClass in class BaseSeamTest
    Throws:
    Exception

    begin

    public void begin()
    Overrides:
    begin in class SeamTest

    end

    public void end()
    Overrides:
    end in class SeamTest

    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

    prepareDBUnitOperations

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

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