Class DataSourceRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class DataSourceRule
    extends java.lang.Object
    implements org.junit.rules.TestRule

    TestRule for making DataSource available.

    This is useful for in memory database implementations to keep a Connection active so the in memory database stays active for the entirety of the test.

    Author:
    Daniel Sagenschneider
    • Constructor Detail

      • DataSourceRule

        public DataSourceRule​(java.lang.String dataSourcePropertiesFilePath)
        Instantiate.
        Parameters:
        dataSourcePropertiesFilePath - Path to the DataSource properties file.
    • Method Detail

      • waitForDatabaseAvailable

        public static java.sql.Connection waitForDatabaseAvailable​(DataSourceRule.ConnectionFactory connectionFactory)
                                                            throws java.lang.Exception
        Waits for the database to be available.
        Parameters:
        connectionFactory - DataSourceRule.ConnectionFactory.
        Returns:
        Connection.
        Throws:
        java.lang.Exception - If failed waiting on database or Connection issue.
      • waitForDatabaseAvailable

        public static java.sql.Connection waitForDatabaseAvailable​(java.lang.Object lock,
                                                                   DataSourceRule.ConnectionFactory connectionFactory)
                                                            throws java.lang.Exception
        Waits for the database to be available.
        Parameters:
        lock - To wait on to allow locking setup.
        connectionFactory - DataSourceRule.ConnectionFactory.
        Returns:
        Connection.
        Throws:
        java.lang.Exception - If failed waiting on database or Connection issue.
      • getDataSource

        public javax.sql.DataSource getDataSource()
        Obtains the DataSource.
        Returns:
        DataSource;
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
        Convenience method to obtain a Connection.
        Returns:
        Connection.
        Throws:
        java.sql.SQLException - If fails to obtain Connection.
      • getActiveConnection

        public java.sql.Connection getActiveConnection()
        Obtains the active Connection.
        Returns:
        Active Connection.
      • apply

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