Class FileTestSupport

java.lang.Object
net.officefloor.frame.test.FileTestSupport
All Implemented Interfaces:
TestSupport

public class FileTestSupport extends Object implements TestSupport
Author:
Daniel Sagenschneider
  • Constructor Details

    • FileTestSupport

      public FileTestSupport()
      Default constructor.
  • Method Details

    • init

      public void init(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Description copied from interface: TestSupport
      Intialise.
      Specified by:
      init in interface TestSupport
      Parameters:
      context - ExtensionContext.
      Throws:
      Exception - If fails to init.
    • findFile

      public File findFile(String relativePath) throws FileNotFoundException
      Obtains the file at the relative path.
      Parameters:
      relativePath - Relative path to the file.
      Returns:
      File.
      Throws:
      FileNotFoundException - If file could not be found.
    • findFile

      public File findFile(Class<?> packageClass, String fileName) throws FileNotFoundException
      Obtains 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:
      FileNotFoundException - Should the file not be found.
    • getFileLocation

      public String getFileLocation(Class<?> packageClass, 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.
    • findDirectoryRecursive

      public File findDirectoryRecursive(File root, String name)
      Recursively searches root for the first directory whose name equals name.
      Parameters:
      root - Directory to search within.
      name - Directory name to find.
      Returns:
      Matching File directory, or null if not found.
    • clearDirectory

      public void clearDirectory(File directory)
      Creates the input directory.
      Parameters:
      directory - Directory to be cleared.
    • deleteDirectory

      public void deleteDirectory(File directory)
      Deletes the input directory.
      Parameters:
      directory - Directory to be deleted.
    • copyDirectory

      public void copyDirectory(File source, File target) throws IOException
      Copies the contents of the source directory to the target directory.
      Parameters:
      source - Source directory.
      target - Target directory.
      Throws:
      IOException - If fails to copy the directory.
    • findInputStream

      public InputStream findInputStream(Class<?> packageClass, String fileName) throws FileNotFoundException

      Obtains 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:
      FileNotFoundException - Should the file not be found.
    • getPackageRelativePath

      public String getPackageRelativePath(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 String getFileContents(File file) throws FileNotFoundException, IOException
      Obtains the contents of the output file.
      Parameters:
      file - File to obtain contents from.
      Returns:
      Contents of the output file.
      Throws:
      FileNotFoundException - Should output file not yet be created.
      IOException - Should fail to read from output file.
    • createFile

      public void createFile(File target, InputStream content) throws IOException
      Creates the target file with the content.
      Parameters:
      content - Content for the file.
      target - Target file.
      Throws:
      IOException - If fails to create.