Class FileTestSupport

  • All Implemented Interfaces:
    TestSupport

    public class FileTestSupport
    extends java.lang.Object
    implements TestSupport
    Author:
    Daniel Sagenschneider
    • Constructor Summary

      Constructors 
      Constructor Description
      FileTestSupport()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearDirectory​(java.io.File directory)
      Creates the input directory.
      void copyDirectory​(java.io.File source, java.io.File target)
      Copies the contents of the source directory to the target directory.
      void createFile​(java.io.File target, java.io.InputStream content)
      Creates the target file with the content.
      void deleteDirectory​(java.io.File directory)
      Deletes the input directory.
      java.io.File findFile​(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.File findFile​(java.lang.String relativePath)
      Obtains the file at the relative path.
      java.io.InputStream findInputStream​(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.String getFileContents​(java.io.File file)
      Obtains the contents of the output file.
      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.
      java.lang.String getPackageRelativePath​(java.lang.Class<?> packageClass)
      Obtains the relative path of the package of the class.
      void init​(org.junit.jupiter.api.extension.ExtensionContext context)
      Intialise.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileTestSupport

        public FileTestSupport()
    • Method Detail

      • init

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

        public java.io.File findFile​(java.lang.String relativePath)
                              throws java.io.FileNotFoundException
        Obtains 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.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:
        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.IOException
        Copies the contents of the source directory to the target directory.
        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.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:
        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.IOException
        Obtains 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.IOException
        Creates the target file with the content.
        Parameters:
        content - Content for the file.
        target - Target file.
        Throws:
        java.io.IOException - If fails to create.