Seam Core JSF2 2.3.1.Final

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 static 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
               
    protected  boolean replaceNull
               
     
    Fields inherited from class org.jboss.seam.mock.AbstractSeamTest
    seamFilter, servletContext, session
     
    Constructor Summary
    DBUnitSeamTest()
               
     
    Method Summary
    protected  void afterExecution(org.dbunit.database.IDatabaseConnection con, DBUnitSeamTest.DataSetOperation operation)
              Callback for each operation, useful if extra preparation of data/tables is necessary.
     void cleanDataAfterTest()
               
    protected  void disableReferentialIntegrity(org.dbunit.database.IDatabaseConnection con)
              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.
    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)
              Execute whatever statement is necessary to enable integrity constraint checks after dataset operations.
     String getBinaryDir()
               
    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)
              Load a file and return it as a byte[].
    protected  org.dbunit.database.IDatabaseConnection getConnection()
              Override this method if you want to provide your own DBUnit IDatabaseConnection instance.
     String getDatasourceJndiName()
               
    protected  URL getResourceURL(String resource)
               
     Boolean isReplaceNull()
               
     void prepareDataBeforeTest()
               
    protected abstract  void prepareDBUnitOperations()
              Implement this in a subclass.
    protected  void prepareExecution(org.dbunit.database.IDatabaseConnection con, DBUnitSeamTest.DataSetOperation operation)
              Callback for each operation before DBUnit executes the operation, useful if extra preparation of data/tables is necessary, e.g.
     void setBinaryDir(String binaryDir)
               
     void setDatabase(String database)
               
     void setDatasourceJndiName(String datasourceJndiName)
               
     void setReplaceNull(Boolean replaceNull)
               
     
    Methods inherited from class org.jboss.seam.mock.SeamTest
    begin, cleanupClass, end, reset, setupClass, startSeam, stopSeam
     
    Methods inherited from class org.jboss.seam.mock.AbstractSeamTest
    createSeamFilter, createServletContext, getConversationIdParameter, getELResolvers, getField, getInitialContext, getInstance, getInstance, getSession, getUserTransaction, initServletContext, installMockTransport, isLongRunningConversation, isSessionInvalid, lookup, setField
     
    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

    replaceNull

    protected boolean replaceNull

    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)

    getDatasourceJndiName

    public String getDatasourceJndiName()

    setBinaryDir

    public void setBinaryDir(String binaryDir)

    getBinaryDir

    public String getBinaryDir()

    setDatabase

    public void setDatabase(String database)

    setReplaceNull

    public void setReplaceNull(Boolean replaceNull)

    isReplaceNull

    public Boolean isReplaceNull()

    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)
    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)
    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

    prepareExecution

    protected void prepareExecution(org.dbunit.database.IDatabaseConnection con,
                                    DBUnitSeamTest.DataSetOperation operation)
    Callback for each operation before DBUnit executes the operation, useful if extra preparation of data/tables is necessary, e.g. additional SQL commands on a per-operation (per table?) granularity on the given database connection.

    Parameters:
    con - A DBUnit connection wrapper
    operation - The operation to be executed, call getDataSet() to access the data.

    afterExecution

    protected void afterExecution(org.dbunit.database.IDatabaseConnection con,
                                  DBUnitSeamTest.DataSetOperation operation)
    Callback for each operation, useful if extra preparation of data/tables is necessary.

    Parameters:
    con - A DBUnit connection wrapper
    operation - The operation that was executed, call getDataSet() to access the data.

    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
    Load a file and return it as a byte[]. Useful for comparison operations in an actual unit test, e.g. to compare an imported database record against a known file state.

    Parameters:
    filename - the path of the file on the classpath, relative to configured binaryDir base path
    Returns:
    the file content as bytes
    Throws:
    Exception - when the file could not be found or read

    prepareDBUnitOperations

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

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


    Seam Core JSF2 2.3.1.Final

    Copyright © 2013 Seam Framework. All Rights Reserved.