org.unitils.io
Class IOUnitils

java.lang.Object
  extended by org.unitils.io.IOUnitils

public class IOUnitils
extends Object

Since:
3.3
Author:
Jeroen Horemans, Tim Ducheyne, Thomas De Rycke

Constructor Summary
IOUnitils()
           
 
Method Summary
static File createTempDir(String dirName)
          Creates a temporary directory with the given name.
static File createTempFile(String fileName)
          Creates a temporary file with the given name.
static void deleteTempFileOrDir(File fileOrDir)
          Deletes the given file or directory.
static
<T> T
readFileContent(Class<T> targetType, Object testInstance)
          Loads the content of 'test-class'.'
static
<T> T
readFileContent(Class<T> targetType, String encoding, Object testInstance)
          Loads the content of 'test-class'.'
static
<T> T
readFileContent(String fileName, Class<T> targetType, Object testInstance)
          Loads the content of the file with the given name and converts it to the given target type using the default encoding.
static
<T> T
readFileContent(String fileName, Class<T> targetType, String encoding, Object testInstance)
          Loads the content of the file with the given name and converts it to the given target type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUnitils

public IOUnitils()
Method Detail

readFileContent

public static <T> T readFileContent(Class<T> targetType,
                                    Object testInstance)
Loads the content of 'test-class'.'target-type-default-extension' and converts it to the given target type using the default encoding. e.g.

 String result1 = readFileContent(String.class, this);
 Properties result2 = readFileContent(Properties.class, this);
 
If this is an instance of MyTest, this will read-in file name org/myPackage/MyTest.txt (txt is the default extension for the String converter) and return the result as a string.

The second line will read-in file name org/myPackage/MyTest.properties (properties is the default extension of the Properties converter) and return the result as a Properties instance.

See FileContent for more information on how the file is resolved.

Parameters:
targetType - The target type to convert the content to, not null
testInstance - The test instance that will be used to construct the file name and locate the file, not null
Returns:
The file content converted to the target type, not null

readFileContent

public static <T> T readFileContent(Class<T> targetType,
                                    String encoding,
                                    Object testInstance)
Loads the content of 'test-class'.'target-type-default-extension' and converts it to the given target type.

See FileContent for more information on how the file is resolved.

Parameters:
targetType - The target type to convert the content to, not null
encoding - The encoding to use when reading the file, null for the default encoding
testInstance - The test instance that will be used to construct the file name and locate the file, not null
Returns:
The file content converted to the target type, not null
See Also:
FileContent

readFileContent

public static <T> T readFileContent(String fileName,
                                    Class<T> targetType,
                                    Object testInstance)
Loads the content of the file with the given name and converts it to the given target type using the default encoding. e.g.

 String result1 = readFileContent("myFile.csv", String.class, this);
 Properties result2 = readFileContent("/myFile.map", Properties.class, this);
 
If this is an instance of MyTest, this will read-in file name org/myPackage/myFile.csv and return the result as a string.

The second line will read-in file name myFile.properties and return the result as a Properties instance.

See FileContent for more information on how the file is resolved.

Parameters:
fileName - The name of the file, not null
targetType - The target type to convert the content to, not null
testInstance - The test instance that will be used to locate the file, not null
Returns:
The file content converted to the target type, not null

readFileContent

public static <T> T readFileContent(String fileName,
                                    Class<T> targetType,
                                    String encoding,
                                    Object testInstance)
Loads the content of the file with the given name and converts it to the given target type.

See FileContent for more information on how the file is resolved.

Parameters:
fileName - The name of the file, not null
targetType - The target type to convert the content to, not null
encoding - The encoding to use when reading the file, null for the default encoding
testInstance - The test instance that will be used to locate the file, not null
Returns:
The file content converted to the target type, not null

createTempFile

public static File createTempFile(String fileName)
Creates a temporary file with the given name. The parent directory for this file can be specified by setting the DefaultTempServiceFactory.ROOT_TEMP_DIR property. If no root temp dir is specified the default user temp dir will be used.

Watch out: if the file already exists, it will first be deleted.

The file will not be removed after the test. You can use deleteTempFileOrDir(java.io.File), if you want to perform cleanup after the test.

Parameters:
fileName - The name of the temp file, not null
Returns:
The temp file, not null

createTempDir

public static File createTempDir(String dirName)
Creates a temporary directory with the given name. The parent directory for this directory can be specified by setting the DefaultTempServiceFactory.ROOT_TEMP_DIR property. If no root temp dir is specified the default user temp dir will be used.

Watch out: if the directory already exists, it will first be deleted. If the directory was not empty, all files in the directory will be deleted.

The directory will not be removed after the test. You can use deleteTempFileOrDir(java.io.File), if you want to perform cleanup after the test.

Parameters:
dirName - The name of the temp dir, not null
Returns:
The temp dir, not null

deleteTempFileOrDir

public static void deleteTempFileOrDir(File fileOrDir)
Deletes the given file or directory.

Watch out: if the directory is not empty, all files in the directory will be deleted.

Nothing will be done if the file or directory is null or does not exist.

Parameters:
fileOrDir - The file or directory to delete, can be null


Copyright © 2011. All Rights Reserved.