Class MockDirectoryManager

java.lang.Object
org.wikidata.wdtk.testing.MockDirectoryManager
All Implemented Interfaces:
DirectoryManager

public class MockDirectoryManager extends Object implements DirectoryManager
Mock implementation of DirectoryManager that simulates file access without touching the file system.
Author:
Markus Kroetzsch
  • Field Details

    • files

      public static HashMap<Path,byte[]> files
      The mocked file system. This is static so that it can be accessed after a test even if the directory manager that was used is created internally.
  • Constructor Details

    • MockDirectoryManager

      public MockDirectoryManager(Path directory, Boolean readOnly) throws IOException
      Creates a new object but retains all previously stored files.
      Parameters:
      directory - initial directory that is managed
      readOnly - if false, the directory manager will attempt to create directories when changing to a location that does not exist
      Throws:
      IOException
    • MockDirectoryManager

      public MockDirectoryManager(String baseDirectory, Boolean readOnly) throws IOException
      Constructor
      Parameters:
      baseDirectory - the directory where the file manager should point initially; will be created if not existing
      readOnly - if false, the directory manager will attempt to create directories when changing to a location that does not exist
      Throws:
      IOException - if there was a problem creating the directory
    • MockDirectoryManager

      public MockDirectoryManager(Path directory, boolean resetFileSystem, boolean readOnly) throws IOException
      Creates a new object and clears all previously stored mock files if requested.
      Parameters:
      directory - initial directory that is managed
      resetFileSystem - if true, the previously mocked files will be cleared; in this case, the starting directory will be created, however (even in read-only mode)
      readOnly - if false, the directory manager will attempt to create directories when changing to a location that does not exist
      Throws:
      IOException
  • Method Details

    • setReturnFailingReaders

      public void setReturnFailingReaders(boolean returnFailingReaders)
      When set to true, every operation that returns reader objects to access some file will return objects that fail with exceptions when trying to read the file. This can be used to simulate problems like insufficient access rights or files becoming inaccessible after being opened.

      The property is inherited by any submanagers that are created by this object.

      Parameters:
      returnFailingReaders - whether read operations should fail
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setFileContents

      public void setFileContents(Path path, String contents) throws IOException
      Sets the contents of the file at the given path and creates all parent directories in our mocked view of the file system.

      This method is used for mocking and is always successful, even if the object is in read-only mode otherwise.

      Parameters:
      path -
      contents -
      Throws:
      IOException
    • setFileContents

      public void setFileContents(Path path, String contents, CompressionType compressionType) throws IOException
      Sets the contents of the file at the given path and creates all parent directories in our mocked view of the file system. If a compression is chosen, the file contents is the compressed version of the given contents. Strings are encoded as UTF8.

      This method is used for mocking and is always successful, even if the object is in read-only mode otherwise.

      Parameters:
      path -
      contents -
      compressionType -
      Throws:
      IOException
    • setDirectory

      public void setDirectory(Path path) throws IOException
      Create the given directory and all parent directories in our mocked view of the file system.

      This method is used for mocking and is always successful, even if the object is in read-only mode otherwise.

      Parameters:
      path -
      Throws:
      IOException
    • getSubdirectoryManager

      public DirectoryManager getSubdirectoryManager(String subdirectoryName) throws IOException
      Description copied from interface: DirectoryManager
      Returns a new directory manager for the subdirectory of the given name. If the subdirectory does not exist yet, it will be created. If this is not desired, its existence can be checked with DirectoryManager.hasSubdirectory(String) first (ignoring the fact that there might be race conditions when accessing the file system).
      Specified by:
      getSubdirectoryManager in interface DirectoryManager
      Parameters:
      subdirectoryName - the string name of the subdirectory
      Returns:
      DirectoryManager for subdirectory
      Throws:
      IOException - if directory could not be created
    • hasSubdirectory

      public boolean hasSubdirectory(String subdirectoryName)
      Description copied from interface: DirectoryManager
      Checks if there is a subdirectory of the given name.
      Specified by:
      hasSubdirectory in interface DirectoryManager
      Parameters:
      subdirectoryName - the name of the subdirectory
      Returns:
      true if the subdirectory exists
    • hasFile

      public boolean hasFile(String fileName)
      Description copied from interface: DirectoryManager
      Checks if there is a file of the given name.
      Specified by:
      hasFile in interface DirectoryManager
      Parameters:
      fileName - the name of the file
      Returns:
      true if the file exists and is not a directory
    • createFile

      public long createFile(String fileName, InputStream inputStream) throws IOException
      Description copied from interface: DirectoryManager
      Creates a new file in the current directory, and fill it with the data from the given input stream. If the stream encodes a string, then it should generally be encoded in UTF-8, since access methods assume this.
      Specified by:
      createFile in interface DirectoryManager
      Parameters:
      fileName - the name of the file
      inputStream - the input stream from which to load the file
      Returns:
      size of the new file in bytes
      Throws:
      IOException
    • createFileAtomic

      public long createFileAtomic(String fileName, InputStream inputStream) throws IOException
      Description copied from interface: DirectoryManager
      Creates a new file in the current directory, and fill it with the data from the given input stream. This is done by first writing the data to a temporary file that uses a suffix to the file name, and then moving the completed file to the new location. This should be used when fetching larger files through from slow stream (e.g., a download) to prevent partially finished file downloads lying around if the program is terminated during download. The temporary file will still be lying around, but it will not be mistaken for the completed download by any other parts of the program.

      If the stream encodes a string, then it should generally be encoded in UTF-8, since access methods assume this.

      Specified by:
      createFileAtomic in interface DirectoryManager
      Parameters:
      fileName - the name of the file
      inputStream - the input stream from which to load the file
      Returns:
      size of the new file in bytes
      Throws:
      IOException
    • createFile

      public void createFile(String fileName, String fileContents) throws IOException
      Description copied from interface: DirectoryManager
      Creates a new file in the current directory, and fill it with the given data, encoded in UTF-8. Should only be used for short pieces of data.
      Specified by:
      createFile in interface DirectoryManager
      Parameters:
      fileName - the name of the file
      fileContents - the data to write into the file
      Throws:
      IOException
    • getOutputStreamForFile

      public OutputStream getOutputStreamForFile(String fileName) throws IOException
      Description copied from interface: DirectoryManager
      Opens and returns an output stream that can be used to write to the file of the given name within the current directory. The stream is owned by the caller and must be closed after use. If the file already exists, it will be truncated at this operation.
      Specified by:
      getOutputStreamForFile in interface DirectoryManager
      Parameters:
      fileName - the name of the file
      Returns:
      the stream to write to
      Throws:
      IOException
    • getInputStreamForFile

      public InputStream getInputStreamForFile(String fileName, CompressionType compressionType) throws IOException
      Description copied from interface: DirectoryManager
      Returns an input stream to access file of the given name within the current directory, possibly uncompressing it if required.

      It is important to close the stream after using it to free memory.

      Specified by:
      getInputStreamForFile in interface DirectoryManager
      Parameters:
      fileName - the name of the file
      compressionType - for types other than CompressionType.NONE, the file will be uncompressed appropriately and the returned input stream will provide access to the uncompressed content
      Returns:
      an InputStream to fetch data from the file
      Throws:
      IOException
    • getSubdirectories

      public List<String> getSubdirectories(String glob)
      Description copied from interface: DirectoryManager
      Returns a list of the names of all subdirectories of the base directory. The glob pattern can be used to filter the names; "*" should be used if no filtering is desired.
      Specified by:
      getSubdirectories in interface DirectoryManager
      Parameters:
      glob - pattern to filter directoy names
      Returns:
      list of subdirectory names
    • getMockedFileContents

      public static byte[] getMockedFileContents(Path filePath)
      Returns the byte contents of the mocked file for the given path. If the file is not mocked, null is returned. If the file is a mocked directory, the bytes of DIRECTORY_MARKER are returned.
      Parameters:
      filePath - the path of the mocked file
      Returns:
      byte contents of mocked file