public class MockDirectoryManager extends Object implements DirectoryManager
DirectoryManager
that simulates file access
without touching the file system.Modifier and Type | Field and Description |
---|---|
static HashMap<Path,byte[]> |
files
The mocked file system.
|
Constructor and Description |
---|
MockDirectoryManager(Path directory,
Boolean readOnly)
Creates a new object but retains all previously stored files.
|
MockDirectoryManager(Path directory,
boolean resetFileSystem,
boolean readOnly)
Creates a new object and clears all previously stored mock files if
requested.
|
MockDirectoryManager(String baseDirectory,
Boolean readOnly)
Constructor
|
Modifier and Type | Method and Description |
---|---|
long |
createFile(String fileName,
InputStream inputStream)
Creates a new file in the current directory, and fill it with the data
from the given input stream.
|
void |
createFile(String fileName,
String fileContents)
Creates a new file in the current directory, and fill it with the given
data, encoded in UTF-8.
|
long |
createFileAtomic(String fileName,
InputStream inputStream)
Creates a new file in the current directory, and fill it with the data
from the given input stream.
|
InputStream |
getInputStreamForFile(String fileName,
CompressionType compressionType)
Returns an input stream to access file of the given name within the
current directory, possibly uncompressing it if required.
|
static byte[] |
getMockedFileContents(Path filePath)
Returns the byte contents of the mocked file for the given path.
|
OutputStream |
getOutputStreamForFile(String fileName)
Opens and returns an output stream that can be used to write to the file
of the given name within the current directory.
|
List<String> |
getSubdirectories(String glob)
Returns a list of the names of all subdirectories of the base directory.
|
DirectoryManager |
getSubdirectoryManager(String subdirectoryName)
Returns a new directory manager for the subdirectory of the given name.
|
boolean |
hasFile(String fileName)
Checks if there is a file of the given name.
|
boolean |
hasSubdirectory(String subdirectoryName)
Checks if there is a subdirectory of the given name.
|
void |
setDirectory(Path path)
Create the given directory and all parent directories in our mocked view
of the file system.
|
void |
setFileContents(Path path,
String contents)
Sets the contents of the file at the given path and creates all parent
directories in our mocked view of the file system.
|
void |
setFileContents(Path path,
String contents,
CompressionType compressionType)
Sets the contents of the file at the given path and creates all parent
directories in our mocked view of the file system.
|
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.
|
String |
toString() |
public MockDirectoryManager(Path directory, Boolean readOnly) throws IOException
directory
- initial directory that is managedreadOnly
- if false, the directory manager will attempt to create
directories when changing to a location that does not existIOException
public MockDirectoryManager(String baseDirectory, Boolean readOnly) throws IOException
baseDirectory
- the directory where the file manager should point initially;
will be created if not existingreadOnly
- if false, the directory manager will attempt to create
directories when changing to a location that does not existIOException
- if there was a problem creating the directorypublic MockDirectoryManager(Path directory, boolean resetFileSystem, boolean readOnly) throws IOException
directory
- initial directory that is managedresetFileSystem
- 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 existIOException
public void setReturnFailingReaders(boolean returnFailingReaders)
The property is inherited by any submanagers that are created by this object.
returnFailingReaders
- whether read operations should failpublic void setFileContents(Path path, String contents) throws IOException
This method is used for mocking and is always successful, even if the object is in read-only mode otherwise.
path
- contents
- IOException
public void setFileContents(Path path, String contents, CompressionType compressionType) throws IOException
This method is used for mocking and is always successful, even if the object is in read-only mode otherwise.
path
- contents
- compressionType
- IOException
public void setDirectory(Path path) throws IOException
This method is used for mocking and is always successful, even if the object is in read-only mode otherwise.
path
- IOException
public DirectoryManager getSubdirectoryManager(String subdirectoryName) throws IOException
DirectoryManager
DirectoryManager.hasSubdirectory(String)
first (ignoring the fact that there
might be race conditions when accessing the file system).getSubdirectoryManager
in interface DirectoryManager
subdirectoryName
- the string name of the subdirectoryIOException
- if directory could not be createdpublic boolean hasSubdirectory(String subdirectoryName)
DirectoryManager
hasSubdirectory
in interface DirectoryManager
subdirectoryName
- the name of the subdirectorypublic boolean hasFile(String fileName)
DirectoryManager
hasFile
in interface DirectoryManager
fileName
- the name of the filepublic long createFile(String fileName, InputStream inputStream) throws IOException
DirectoryManager
createFile
in interface DirectoryManager
fileName
- the name of the fileinputStream
- the input stream from which to load the fileIOException
public long createFileAtomic(String fileName, InputStream inputStream) throws IOException
DirectoryManager
If the stream encodes a string, then it should generally be encoded in UTF-8, since access methods assume this.
createFileAtomic
in interface DirectoryManager
fileName
- the name of the fileinputStream
- the input stream from which to load the fileIOException
public void createFile(String fileName, String fileContents) throws IOException
DirectoryManager
createFile
in interface DirectoryManager
fileName
- the name of the filefileContents
- the data to write into the fileIOException
public OutputStream getOutputStreamForFile(String fileName) throws IOException
DirectoryManager
getOutputStreamForFile
in interface DirectoryManager
fileName
- the name of the fileIOException
public InputStream getInputStreamForFile(String fileName, CompressionType compressionType) throws IOException
DirectoryManager
It is important to close the stream after using it to free memory.
getInputStreamForFile
in interface DirectoryManager
fileName
- the name of the filecompressionType
- for types other than CompressionType.NONE
, the file
will be uncompressed appropriately and the returned input
stream will provide access to the uncompressed contentIOException
public List<String> getSubdirectories(String glob)
DirectoryManager
getSubdirectories
in interface DirectoryManager
glob
- pattern to filter directoy namespublic static byte[] getMockedFileContents(Path filePath)
DIRECTORY_MARKER
are returned.filePath
- the path of the mocked fileCopyright © 2014–2024 Wikidata Toolkit Developers. Generated from source code published under the Apache License 2.0. For more information, see the Wikidata Toolkit homepage