org.unitils.core.util
Class FileResolver

java.lang.Object
  extended by org.unitils.core.util.FileResolver

public class FileResolver
extends Object

Resolves the location for a file with a certain name.

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

If a file name starts with a / it will not be prefixed with the package name.
E.g. /MyFile.xml remains /MyFile.xml

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

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

pathPrefix=myPathPrefix: MyFile.xml becomes myPathPrefix/com/myPackage/MyFile.xml
pathPrefix=myPathPrefix: /MyFile.xml becomes myPathPrefix/MyFile.xml

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

Examples:

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

Author:
Tim Ducheyne, Filip Neven, Tuomas Jormola

Field Summary
protected  String pathPrefix
           
protected  boolean prefixWithPackageName
           
 
Constructor Summary
FileResolver()
          Creates a resolver with package prefixing enabled and no path prefix.
FileResolver(boolean prefixWithPackageName, String pathPrefix)
          Creates a resolver.
 
Method Summary
protected  String constructFullFileName(String fileName, Class<?> testClass)
          Get the full file name depending on the package prefixing and path prefix.
protected  String getDefaultFileName(String extension, Class<?> testClass)
          The default name is constructed as follows: 'classname without packagename'.'
 String getPathPrefix()
           
 boolean isPrefixWithPackageName()
           
protected  String prefixPackageNameFilePath(String fileName, Class<?> testClass)
          Prefix the package name of the test to the name of the file (replacing . with /).
 URI resolveDefaultFileName(String extension, Class<?> testClass)
          Resolves the location for a file with the default name: 'classname'.'
 URI resolveFileName(String fileName, Class<?> testClass)
          Resolves the location for a file with a certain name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

prefixWithPackageName

protected boolean prefixWithPackageName

pathPrefix

protected String pathPrefix
Constructor Detail

FileResolver

public FileResolver()
Creates a resolver with package prefixing enabled and no path prefix.


FileResolver

public FileResolver(boolean prefixWithPackageName,
                    String pathPrefix)
Creates a resolver.

Parameters:
prefixWithPackageName - True to enable to prefixing of the file name with the package name of the test class
pathPrefix - path prefix to add to the file name, null if there is no prefix
Method Detail

resolveDefaultFileName

public URI resolveDefaultFileName(String extension,
                                  Class<?> testClass)
Resolves the location for a file with the default name: 'classname'.'extension'. An exception is raised if the file could not be found.

Parameters:
extension - The extension of the file, not null
testClass - The test class, not null
Returns:
The file, not null

resolveFileName

public URI resolveFileName(String fileName,
                           Class<?> testClass)
Resolves the location for a file with a certain name. An exception is raised if the file could not be found.

Parameters:
fileName - The name of the file, not null
testClass - The test class, not null
Returns:
The file, not null

isPrefixWithPackageName

public boolean isPrefixWithPackageName()

getPathPrefix

public String getPathPrefix()

constructFullFileName

protected String constructFullFileName(String fileName,
                                       Class<?> testClass)
Get the full file name depending on the package prefixing and path prefix.

Parameters:
fileName - The file name, not null
testClass - The test class, not null
Returns:
The file name, not null

prefixPackageNameFilePath

protected String prefixPackageNameFilePath(String fileName,
                                           Class<?> testClass)
Prefix the package name of the test to the name of the file (replacing . with /).

Parameters:
fileName - The file name, not null
testClass - The test, not null
Returns:
The file name with the package name prefix, not null

getDefaultFileName

protected String getDefaultFileName(String extension,
                                    Class<?> testClass)
The default name is constructed as follows: 'classname without packagename'.'extension'

Parameters:
extension - The extension of the file
testClass - The test class, not null
Returns:
The default filename, not null


Copyright © 2011. All Rights Reserved.