Package org.tentackle.pdo.testng
Class AbstractPdoTest
java.lang.Object
org.tentackle.pdo.testng.AbstractPdoTest
- All Implemented Interfaces:
DomainContextProvider
Base class for TestNG tests on PDO level.
- Author:
- harald
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe transaction handling for the test class. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a test with rollback and one transaction per class.Creates a test with rollback.AbstractPdoTest(AbstractPdoTest.TransactionType txType, boolean commit) Creates a test. -
Method Summary
Modifier and TypeMethodDescriptionvoidEnds the transaction if type isAbstractPdoTest.TransactionType.CLASS.voidafterMethod(org.testng.ITestResult result) Ends the transaction if type isAbstractPdoTest.TransactionType.METHOD.voidBegins the transaction if type isAbstractPdoTest.TransactionType.CLASS.voidBegins the transaction if type isAbstractPdoTest.TransactionType.METHOD.protected voidBegins a transaction.voidTerminates the modification tracker and closes the sessions.protected voidcreateDatabaseTables(org.tentackle.dbms.Db db) Creates the database tables.
The default implementation loads the model from the classpath.protected DomainContextCreates the domain context.protected voidCommits or rolls back a transaction.protected voidInvokes theCommitTxRunnables, if any.
Useful for special test scenarios.protected voidInvokes theRollbackTxRunnables, if any.
Useful for special test scenarios.Gets the domain context.org.tentackle.session.SessionGets the session.booleanisCommit()Returns whether the transaction should be committed or rolled back.booleanReturns whether statements rolled back are always logged.protected org.tentackle.session.SessionOpens the session.voidOpens the sessions and starts the modification tracker.
If the database is in-memory, it will be populated before the tracker is started.protected voidPopulates the database with test data.static ProcessRuns the given class in another JVM and waits for termination.
The test class must have a main method.intrunInOtherJVM(Class<?> testClass) Runs the given class in another JVM.
The test class must have a main method.voidsetRollbackLogged(boolean rollbackLogged) Sets whether to log the statements rolled back.
By default, the statements are not logged if the test succeeds.static voidwaitForProcess(Process process) Waits for process to terminate and write stdout and stderr to the reporter log.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.tentackle.pdo.DomainContextProvider
isWithinContext, isWithinContext, on, on, op, op
-
Constructor Details
-
AbstractPdoTest
Creates a test.- Parameters:
txType- the transaction typecommit- true if commit transaction, else rollback (default for regular tests)
-
AbstractPdoTest
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
Opens the sessions and starts the modification tracker.
If the database is in-memory, it will be populated before the tracker is started.- Throws:
Exception- if failed- See Also:
-
closeSessionsAndTerminateModificationTracker
@AfterSuite(alwaysRun=true) public void closeSessionsAndTerminateModificationTracker() throws ExceptionTerminates the modification tracker and closes the sessions.- Throws:
Exception- if failed
-
beforeClass
Begins the transaction if type isAbstractPdoTest.TransactionType.CLASS.- Throws:
Exception- if failed
-
afterClass
Ends the transaction if type isAbstractPdoTest.TransactionType.CLASS.- Throws:
Exception- if failed
-
beforeMethod
Begins the transaction if type isAbstractPdoTest.TransactionType.METHOD.- Throws:
Exception- if failed
-
afterMethod
@AfterMethod(alwaysRun=true) public void afterMethod(org.testng.ITestResult result) throws Exception Ends the transaction if type isAbstractPdoTest.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
Gets the domain context.- Specified by:
getDomainContextin interfaceDomainContextProvider- Returns:
- the domain context
-
runInOtherJVM
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
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 theCommitTxRunnables, if any.
Useful for special test scenarios. Must be invoked explicitly from within the test. -
executeRollbackTxRunnables
protected void executeRollbackTxRunnables()Invokes theRollbackTxRunnables, if any.
Useful for special test scenarios. Must be invoked explicitly from within the test. -
runClass
Runs the given class in another JVM and waits for termination.
The test class must have a main method.- Parameters:
testClass- the test classargs- optional arguments- Returns:
- the process object
- Throws:
IOException- if some IO operation failed
-
waitForProcess
Waits for process to terminate and write stdout and stderr to the reporter log.- Parameters:
process- the process- Throws:
IOException- if some IO failed
-