com.ninja_squad.dbsetup
Class DbSetupTracker

java.lang.Object
  extended by com.ninja_squad.dbsetup.DbSetupTracker

public final class DbSetupTracker
extends java.lang.Object

This class allows speeding up test execution, by avoiding re-executing the same sequence of database operations before each test method even if each of these test methods leaves the database as it is (and only performs read-only operations, which is the most frequent case).

Example usage:

 // the tracker is static because JUnit uses a separate Test instance for every test method.
 private static DbSetupTracker dbSetupTracker = new DbSetupTracker();

 @Before
 public void setUp() throws Exception {
     Operation operation =
         Operations.sequenceOf(
             CommonOperations.DELETE_ALL,
             CommonOperations.INSERT_REFERENCE_DATA,
             Operations.insertInto("CLIENT")
                       .columns("CLIENT_ID", "FIRST_NAME", "LAST_NAME", "DATE_OF_BIRTH", "COUNTRY_ID")
                       .values(1L, "John", "Doe", "1975-07-19", 1L)
                       .values(2L, "Jack", "Smith", "1969-08-22", 2L)
                       .build());
     DbSetup dbSetup = new DbSetup(new DataSourceDestination(dataSource), operation);
     dbSetupTracker.launchIfNecessary(dbSetup);
 }

 @Test
 public void readOnlyTest1() {
     dbSetupTracker.skipNextLaunch();
     ...
 }

 @Test
 public void readOnlyTest2() {
     dbSetupTracker.skipNextLaunch();
     ...
 }

 @Test
 public void readOnlyTest3() {
     dbSetupTracker.skipNextLaunch();
     ...
 }

 @Test
 public void readWriteTest1() {
     // No call to dbSetupTracker.skipNextLaunch();
     ...
 }
 


Constructor Summary
DbSetupTracker()
           
 
Method Summary
 void launchIfNecessary(DbSetup dbSetup)
          Executes the given DbSetup unless all the following conditions are true: skipNextLaunch() has been called since the last call to this method; the given dbSetup is equal to the last DbSetup launched by this method This method resets the skipNextLaunch flag to false.
 void skipNextLaunch()
          Marks the current test method as read-only, and thus the need for the next test method to re-execute the same sequence of database setup operations.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DbSetupTracker

public DbSetupTracker()
Method Detail

launchIfNecessary

public void launchIfNecessary(@Nonnull
                              DbSetup dbSetup)
Executes the given DbSetup unless all the following conditions are true: This method resets the skipNextLaunch flag to false.

Parameters:
dbSetup - the DbSetup to execute (or skip)

skipNextLaunch

public void skipNextLaunch()
Marks the current test method as read-only, and thus the need for the next test method to re-execute the same sequence of database setup operations.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object