Class AbstractPdoTest

java.lang.Object
org.tentackle.pdo.junit.AbstractPdoTest
All Implemented Interfaces:
DomainContextProvider

public abstract class AbstractPdoTest extends Object implements DomainContextProvider
Base class for Junit5 tests on PDO level.
Author:
harald
  • Constructor Details

    • AbstractPdoTest

      public AbstractPdoTest(AbstractPdoTest.TransactionType txType, boolean commit)
      Creates a test.
      Parameters:
      txType - the transaction type
      commit - true if commit transaction, else rollback (default for regular tests)
    • AbstractPdoTest

      public AbstractPdoTest(AbstractPdoTest.TransactionType txType)
      Creates a test with rollback.
      Parameters:
      txType - the transaction type
    • AbstractPdoTest

      public AbstractPdoTest()
      Creates a test with rollback and one transaction per class.
  • Method Details

    • isRollbackLogged

      public boolean isRollbackLogged()
      Returns whether statements rolled back are always logged.
      Returns:
      true to log even if test succeeds, false is default
    • setRollbackLogged

      public void setRollbackLogged(boolean rollbackLogged)
      Sets whether to log the statements rolled back.
      By default, the statements are not logged if the test succeeds. Sometimes, however, it is nice to log the statements that were executed during the test.

      Notice that the flag is reset to false when the transaction ends.

      Parameters:
      rollbackLogged - true to log the statements, default is false
    • openSessionsAndStartModificationTracker

      public boolean openSessionsAndStartModificationTracker()
      Opens the sessions and starts the modification tracker exactly once.
      If the database is in-memory, it will be populated before the tracker is started.
      See Also:
    • closeSessionsAndTerminateModificationTracker

      @AfterAll public static void closeSessionsAndTerminateModificationTracker()
      Terminates the modification tracker and closes the sessions.
    • beforeMethod

      @BeforeEach public void beforeMethod() throws Exception
      Begins the transaction if type is AbstractPdoTest.TransactionType.METHOD.
      Throws:
      Exception - if failed
    • afterMethod

      @AfterEach public void afterMethod() throws Exception
      Ends the transaction if type is AbstractPdoTest.TransactionType.METHOD.
      Throws:
      Exception - if failed
    • getSession

      public org.tentackle.session.Session getSession()
      Gets the session.
      Returns:
      the session
    • isCommit

      public boolean isCommit()
      Returns whether the transaction should be committed or rolled back.
      Returns:
      true if commit, false if rollback (default for regular tests)
    • getDomainContext

      public DomainContext getDomainContext()
      Gets the domain context.
      Specified by:
      getDomainContext in interface DomainContextProvider
      Returns:
      the domain context
    • runInOtherJVM

      public int runInOtherJVM(Class<?> testClass) throws IOException
      Runs the given class in another JVM.
      The test class must have a main method.
      Parameters:
      testClass - the test class
      Returns:
      the exit value
      Throws:
      IOException - if some IO operation failed
    • openSession

      protected org.tentackle.session.Session openSession()
      Opens the session.
      Returns:
      the thread-local session
    • createDatabaseTables

      protected void createDatabaseTables(org.tentackle.dbms.Db db)
      Creates the database tables.
      The default implementation loads the model from the classpath. Override this method to provide additional model sources.
      Parameters:
      db - the low-level database session
      See Also:
    • populateDatabase

      protected void populateDatabase()
      Populates the database with test data.
    • createDomainContext

      protected DomainContext createDomainContext()
      Creates the domain context.
      Returns:
      the context (usually thread-local)
    • beginTransaction

      protected void beginTransaction()
      Begins a transaction.
    • endTransaction

      protected void endTransaction()
      Commits or rolls back a transaction.
    • executeCommitTxRunnables

      protected void executeCommitTxRunnables()
      Invokes the CommitTxRunnables, if any.
      Useful for special test scenarios. Must be invoked explicitly from within the test.
    • executeRollbackTxRunnables

      protected void executeRollbackTxRunnables()
      Invokes the RollbackTxRunnables, if any.
      Useful for special test scenarios. Must be invoked explicitly from within the test.
    • runClass

      public static Process runClass(Class<?> testClass, String... args) throws IOException
      Runs the given class in another JVM and waits for termination.
      The test class must have a main method.
      Parameters:
      testClass - the test class
      args - optional arguments
      Returns:
      the process object
      Throws:
      IOException - if some IO operation failed
    • waitForProcess

      public static void waitForProcess(Process process) throws IOException
      Waits for process to terminate and write stdout and stderr to the junit log.
      Parameters:
      process - the process
      Throws:
      IOException - if some IO failed