org.unitils.dbunit.datasetfactory.impl
Class DefaultDataSetResolver

java.lang.Object
  extended by org.unitils.dbunit.datasetfactory.impl.DefaultDataSetResolver
All Implemented Interfaces:
Configurable, DataSetResolver

public class DefaultDataSetResolver
extends java.lang.Object
implements DataSetResolver

Resolves the location for a data set with a certain name.

By default, the data set name is prefixed with the package name (. replaced by /).
E.g. MyDataSet.xml becomes com/myPackage/MyDataSet.xml

If a data set name starts with a / it will not be prefixed with the package name.
E.g. /MyDataSet.xml remains /MyDataSet.xml

Package name prefixing can be disabled using the PROPKEY_PREFIX_WITH_PACKAGE_NAME property.
prefixWithPackageName=false => MyDataSet.xml remains MyDataSet.xml

If a path prefix is specified using the PROPKEY_DATA_SET_PATH_PREFIX property it is added to the file name.
Examples:

pathPrefix=myPathPrefix: MyDataSet.xml becomes myPathPrefix/org/unitils/test/MyDataSet.xml
pathPrefix=myPathPrefix: /MyDataSet.xml becomes myPathPrefix/MyDataSet.xml

If the path prefix with '/', the file name is treated absolute, else it will be treated relative to the classpath.

Examples:

path prefix /c:/datasets --> looks for c:/datasets/myDataSet.xml on the file system path prefix datasets --> looks for datasets/myDataSet.xml on the classpath

Special thanks to Tuomas Jormola for the input and code contribution for resolving data sets.

Author:
Tim Ducheyne, Filip Neven, Tuomas Jormola

Field Summary
protected  java.lang.String pathPrefix
          An optional path prefix for the file name.
protected  boolean prefixWithPackageName
          True if the file name should be prefixed with the package name of the test class.
static java.lang.String PROPKEY_DATA_SET_PATH_PREFIX
          Property key for the path prefix
static java.lang.String PROPKEY_PREFIX_WITH_PACKAGE_NAME
          Property key for the path prefix
 
Constructor Summary
DefaultDataSetResolver()
           
 
Method Summary
protected  java.lang.String getDataSetFileName(java.lang.Class<?> testClass, java.lang.String dataSetName)
          Get the file name for the data set.
 void init(java.util.Properties configuration)
          Initializes the resolver with the given configuration.
protected  java.lang.String prefixPackageNameFilePath(java.lang.Class<?> testClass, java.lang.String dataSetName)
          Prefix the package name of the test to the name of the data set (replacing . with /).
 java.io.File resolve(java.lang.Class<?> testClass, java.lang.String dataSetName)
          Resolves the location for a data set with a certain name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPKEY_PREFIX_WITH_PACKAGE_NAME

public static final java.lang.String PROPKEY_PREFIX_WITH_PACKAGE_NAME
Property key for the path prefix

See Also:
Constant Field Values

PROPKEY_DATA_SET_PATH_PREFIX

public static final java.lang.String PROPKEY_DATA_SET_PATH_PREFIX
Property key for the path prefix

See Also:
Constant Field Values

prefixWithPackageName

protected boolean prefixWithPackageName
True if the file name should be prefixed with the package name of the test class.


pathPrefix

protected java.lang.String pathPrefix
An optional path prefix for the file name.

Constructor Detail

DefaultDataSetResolver

public DefaultDataSetResolver()
Method Detail

init

public void init(java.util.Properties configuration)
Initializes the resolver with the given configuration.

Specified by:
init in interface Configurable
Parameters:
configuration - The configuration, not null

resolve

public java.io.File resolve(java.lang.Class<?> testClass,
                            java.lang.String dataSetName)
Resolves the location for a data set with a certain name. An exception is raised if the file could not be found.

Specified by:
resolve in interface DataSetResolver
Parameters:
testClass - The test class, not null
dataSetName - The name of the data set, not null
Returns:
The data set file, not null

getDataSetFileName

protected java.lang.String getDataSetFileName(java.lang.Class<?> testClass,
                                              java.lang.String dataSetName)
Get the file name for the data set.

Parameters:
testClass - The test class, not null
dataSetName - The data set name, not null
Returns:
The file name, not null

prefixPackageNameFilePath

protected java.lang.String prefixPackageNameFilePath(java.lang.Class<?> testClass,
                                                     java.lang.String dataSetName)
Prefix the package name of the test to the name of the data set (replacing . with /).

Parameters:
testClass - The test, not null
dataSetName - The data set name, not null
Returns:
The data set name with the package name prefix, not null


Copyright © 2009. All Rights Reserved.