Class DbSetupRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule
    Direct Known Subclasses:
    DbUnitLoadingRule

    public class DbSetupRule
    extends Object
    implements org.junit.rules.TestRule
    A rule to set up the database before any integration tests implying the persistence engine of Silverpeas.
    Author:
    mmoquillon
    • Constructor Detail

      • DbSetupRule

        protected DbSetupRule​(String... sqlScripts)
    • Method Detail

      • createTablesFrom

        public static DbSetupRule createTablesFrom​(String... sqlScripts)
        Constructs a new instance of this rule by specifying the SQL scripts containing the statements to create the different tables required by the integration test. The creation of the default tables, required by the tests, like UniqueId, are taken in charge automatically by this rule, so you don't have to specify them.

        In order to work fine, it is not recommended to insert an initial data set with these scripts. For doing a such purpose, please invoke one of the following methods: loadInitialDataSetFrom(com.ninja_squad.dbsetup.operation.Operation...) or loadInitialDataSetFrom(String...).

        Parameters:
        sqlScripts - the path of the SQL scripts in the classpath and from which the database will be set up.
        Returns:
        itself.
      • createDefaultTables

        public static DbSetupRule createDefaultTables()
        Constructs a new instance of this rule by creating only the default tables (like UniqueId) required for the integration tests on database to run.
        Returns:
        itself.
      • loadInitialDataSetFrom

        public DbSetupRule loadInitialDataSetFrom​(String... sqlScripts)
        Loads the specified SQL scripts in order to insert into the database an initial data set before any test running.
        Parameters:
        sqlScripts - the path of the SQL scripts in the classpath and from which an initial data set will be inserted in the database.
        Returns:
        itself.
      • loadInitialDataSetFrom

        public DbSetupRule loadInitialDataSetFrom​(com.ninja_squad.dbsetup.operation.Operation... insertionOperation)
        Loads the specified SQL scripts in order to insert into the database an initial data set before any test running.
        Parameters:
        insertionOperation - the operation to use for inserting an initial data set.
        Returns:
        itself.
      • apply

        public final org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement test,
                                                             org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • performBefore

        protected void performBefore​(org.junit.runner.Description description)
      • performAfter

        protected void performAfter​(org.junit.runner.Description description)
      • openSafeConnection

        protected Connection openSafeConnection()
                                         throws SQLException
        Opens a new connection to the database that will be closed automatically closed at the end of test if it is not already done.
        Returns:
        a connection to the database.
        Throws:
        SQLException - on SQL error
      • getSafeConnection

        public static Connection getSafeConnection()
                                            throws SQLException
        Gets a new connection to the database that will be closed automatically closed at the end of test if it is not already done.
        Returns:
        a connection to the database
        Throws:
        SQLException - on SQL error
      • getSafeConnectionFromDifferentThread

        public Connection getSafeConnectionFromDifferentThread()
                                                        throws SQLException
        Gets a new connection to the database that will be closed automatically closed at the end of test if it is not already done.
        Returns:
        a connection to the database
        Throws:
        SQLException - on SQL error
      • getActualDataSet

        public static org.dbunit.dataset.IDataSet getActualDataSet​(Connection connection)
        Gets the actual data set in the database so that you can check information persisted in the data source according to the operations that were performed in the behaviour of the test.
        Parameters:
        connection - a connection to the database
        Returns:
        the actual data set.
        Throws:
        Exception - if an error occurs while fetching the data set in the database.