Class CachingFileExtensionDataFileLoader

All Implemented Interfaces:
DataFileLoader

public class CachingFileExtensionDataFileLoader extends FileExtensionDataFileLoader
FileExtensionDataFileLoader that parses each file once and shares the parsed dataset with every later AbstractDataFileLoader.load(String) of the same resource, instead of parsing it again for each test. A suite whose tests share the same reference files otherwise pays for a full parse of every prep and expected file in every test: a SAX parse and the building of the tables for flat XML, and much more for Excel files.

Only the parsed dataset is shared. Every load still wraps it in a new ReplacementDataSet built from the replacement objects and substrings of the loader instance that was asked, so those stay per instance.

The annotation runtime builds a new loader instance for each test, so the parsed datasets are held per loader class, not per instance, and every instance of a class reuses what the others parsed. A subclass keeps its own. They stay until clearCache() drops them or the loader class is unloaded, so for the class itself, for the rest of the run. Share one loader class only among tests that read the same resources, and keep the number of distinct files in mind when they are large.

This suits immutable classpath fixtures: a resource that changes while the JVM keeps running, as in a test daemon or an IDE runner, is not read again until clearCache(). The shared datasets must be treated as read-only, as they are shared by every test.

Use it by passing the class to @DbUnitConfig(dataFileLoader = ...), which needs the public no-argument constructor, or by setting an instance on a PrepAndExpectedTestCase.

Since:
3.6.0
Author:
Jeff Jensen
  • Constructor Details

    • CachingFileExtensionDataFileLoader

      public CachingFileExtensionDataFileLoader()
      Create new instance.
    • CachingFileExtensionDataFileLoader

      public CachingFileExtensionDataFileLoader(Map<?,?> ro)
      Create new instance with replacement objects.
      Parameters:
      ro - The replacement objects for use with ReplacementDataSet.
    • CachingFileExtensionDataFileLoader

      public CachingFileExtensionDataFileLoader(Map<?,?> ro, Map<?,?> rs)
      Create new instance with replacement objects and replacement substrings.
      Parameters:
      ro - The replacement objects for use with ReplacementDataSet.
      rs - The replacement substrings for use with ReplacementDataSet.
  • Method Details

    • loadDataSet

      public IDataSet loadDataSet(URL url) throws DataSetException, IOException
      Load the specified URL file into a dbUnit dataset. The type of dbUnit dataset created is delegated to the implementing subclass.

      Returns the dataset an earlier call parsed from the same resource, parsing it with loadUncachedDataSet(URL) the first time. The cache is keyed by the URL's string form, as URL.equals(Object) and URL.hashCode() can do blocking name lookups. A resource that fails to parse is not remembered.

      Specified by:
      loadDataSet in interface DataFileLoader
      Overrides:
      loadDataSet in class FileExtensionDataFileLoader
      Parameters:
      url - The dbUnit data file url.
      Returns:
      dbUnit dataset of the corresponding input file type.
      Throws:
      DataSetException - On data errors.
      IOException - On file errors.
    • loadUncachedDataSet

      protected IDataSet loadUncachedDataSet(URL url) throws DataSetException, IOException
      Parses the resource without consulting or filling the cache, the way FileExtensionDataFileLoader does.
      Parameters:
      url - The resource to parse.
      Returns:
      The parsed dataset.
      Throws:
      DataSetException - If the resource cannot be parsed as a dataset.
      IOException - If the resource cannot be read.
    • clearCache

      public void clearCache()
      Drops the parsed datasets of this loader's class, so the next load of each resource parses it again. For a test that changes a resource it loads, or a daemon that outlives the change.