Package net.officefloor.frame.test
Class FileTestSupport
- java.lang.Object
-
- net.officefloor.frame.test.FileTestSupport
-
- All Implemented Interfaces:
TestSupport
public class FileTestSupport extends java.lang.Object implements TestSupport
FileTestSupport.- Author:
- Daniel Sagenschneider
-
-
Constructor Summary
Constructors Constructor Description FileTestSupport()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearDirectory(java.io.File directory)Creates the input directory.voidcopyDirectory(java.io.File source, java.io.File target)Copies the contents of thesourcedirectory to thetargetdirectory.voidcreateFile(java.io.File target, java.io.InputStream content)Creates the target file with the content.voiddeleteDirectory(java.io.File directory)Deletes the input directory.java.io.FilefindFile(java.lang.Class<?> packageClass, java.lang.String fileName)Obtains the file by the input file name located in the package of the input class.java.io.FilefindFile(java.lang.String relativePath)Obtains the file at the relative path.java.io.InputStreamfindInputStream(java.lang.Class<?> packageClass, java.lang.String fileName)Obtains the input stream to the file by the input file name located in the package of the input class.java.lang.StringgetFileContents(java.io.File file)Obtains the contents of the output file.java.lang.StringgetFileLocation(java.lang.Class<?> packageClass, java.lang.String fileName)Obtains the file location of the input file located in the package of the input class.java.lang.StringgetPackageRelativePath(java.lang.Class<?> packageClass)Obtains the relative path of the package of the class.voidinit(org.junit.jupiter.api.extension.ExtensionContext context)Intialise.
-
-
-
Method Detail
-
init
public void init(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.ExceptionDescription copied from interface:TestSupportIntialise.- Specified by:
initin interfaceTestSupport- Parameters:
context-ExtensionContext.- Throws:
java.lang.Exception- If fails to init.
-
findFile
public java.io.File findFile(java.lang.String relativePath) throws java.io.FileNotFoundExceptionObtains the file at the relative path.- Parameters:
relativePath- Relative path to the file.- Returns:
File.- Throws:
java.io.FileNotFoundException- If file could not be found.
-
findFile
public java.io.File findFile(java.lang.Class<?> packageClass, java.lang.String fileName) throws java.io.FileNotFoundExceptionObtains the file by the input file name located in the package of the input class.- Parameters:
packageClass- Class to obtain the relative path from for its package.fileName- Name of file within the package directory.- Returns:
- File within the package directory.
- Throws:
java.io.FileNotFoundException- Should the file not be found.
-
getFileLocation
public java.lang.String getFileLocation(java.lang.Class<?> packageClass, java.lang.String fileName)Obtains the file location of the input file located in the package of the input class.- Parameters:
packageClass- Class to obtain the relative path from for its package.fileName- Name of the file within the package directory.- Returns:
- Path to the file.
-
clearDirectory
public void clearDirectory(java.io.File directory)
Creates the input directory.- Parameters:
directory- Directory to be cleared.
-
deleteDirectory
public void deleteDirectory(java.io.File directory)
Deletes the input directory.- Parameters:
directory- Directory to be deleted.
-
copyDirectory
public void copyDirectory(java.io.File source, java.io.File target) throws java.io.IOExceptionCopies the contents of thesourcedirectory to thetargetdirectory.- Parameters:
source- Source directory.target- Target directory.- Throws:
java.io.IOException- If fails to copy the directory.
-
findInputStream
public java.io.InputStream findInputStream(java.lang.Class<?> packageClass, java.lang.String fileName) throws java.io.FileNotFoundExceptionObtains the input stream to the file by the input file name located in the package of the input class.
Note: this also searches the class path for the file.
- Parameters:
packageClass- Class to obtain the relative path from for its package.fileName- Name of file within the package directory.- Returns:
- File within the package directory.
- Throws:
java.io.FileNotFoundException- Should the file not be found.
-
getPackageRelativePath
public java.lang.String getPackageRelativePath(java.lang.Class<?> packageClass)
Obtains the relative path of the package of the class.- Parameters:
packageClass- Class to obtain the relative path from for its package.- Returns:
- Relative path of class's package.
-
getFileContents
public java.lang.String getFileContents(java.io.File file) throws java.io.FileNotFoundException, java.io.IOExceptionObtains the contents of the output file.- Parameters:
file- File to obtain contents from.- Returns:
- Contents of the output file.
- Throws:
java.io.FileNotFoundException- Should output file not yet be created.java.io.IOException- Should fail to read from output file.
-
createFile
public void createFile(java.io.File target, java.io.InputStream content) throws java.io.IOExceptionCreates the target file with the content.- Parameters:
content- Content for the file.target- Target file.- Throws:
java.io.IOException- If fails to create.
-
-