org.mockftpserver.fake.filesystem
Class AbstractFakeFileSystem

java.lang.Object
  extended by org.mockftpserver.fake.filesystem.AbstractFakeFileSystem
All Implemented Interfaces:
FileSystem
Direct Known Subclasses:
UnixFakeFileSystem, WindowsFakeFileSystem

public abstract class AbstractFakeFileSystem
extends Object
implements FileSystem

Abstract superclass for implementation of the FileSystem interface that manage the files and directories in memory, simulating a real file system.

If the createParentDirectoriesAutomatically property is set to true, then creating a directory or file will automatically create any parent directories (recursively) that do not already exist. If false, then creating a directory or file throws an exception if its parent directory does not exist. This value defaults to true.

The directoryListingFormatter property holds an instance of DirectoryListingFormatter , used by the formatDirectoryListing method to format directory listings in a filesystem-specific manner. This property must be initialized by concrete subclasses.

Version:
$Revision: 264 $ - $Date: 2012-07-17 21:19:23 -0400 (Tue, 17 Jul 2012) $
Author:
Chris Mair

Constructor Summary
AbstractFakeFileSystem()
           
 
Method Summary
 void add(FileSystemEntry entry)
          Add the specified file system entry (file or directory) to this file system
protected  void checkForInvalidFilename(String path)
          Throw an InvalidFilenameException if the specified path is not valid.
protected  String componentsToPath(List components)
          Build a path from the specified list of path components
 boolean delete(String path)
          Delete the file or directory specified by the path.
 boolean exists(String path)
          Return true if there exists a file or directory at the specified path
 String formatDirectoryListing(FileSystemEntry fileSystemEntry)
          Return the formatted directory listing entry for the file represented by the specified FileSystemEntry
 DirectoryListingFormatter getDirectoryListingFormatter()
           
 FileSystemEntry getEntry(String path)
          Returns the FileSystemEntry object representing the file system entry at the specified path, or null if the path does not specify an existing file or directory within this file system.
protected  String getFileSystemEntryKey(String path)
          Return the normalized and unique key used to access the file system entry
 String getName(String path)
          Returns the name of the file or directory denoted by this abstract pathname.
 String getParent(String path)
          Return the parent path of the specified path.
protected  FileSystemEntry getRequiredEntry(String path)
          Return the FileSystemEntry for the specified path.
protected  String getSeparator()
           
protected abstract  char getSeparatorChar()
           
 boolean isAbsolute(String path)
          Return true if the specified path designates an absolute file path.
 boolean isCreateParentDirectoriesAutomatically()
           
 boolean isDirectory(String path)
          Return true if the specified path designates an existing directory, false otherwise
 boolean isFile(String path)
          Return true if the specified path designates an existing file, false otherwise
protected abstract  boolean isRoot(String pathComponent)
           
protected abstract  boolean isSeparator(char c)
          Return true if the specified char is a separator character for this filesystem
protected abstract  boolean isValidName(String path)
           
 List listFiles(String path)
          Return the List of FileSystemEntry objects for the files in the specified directory or group of files.
 List listNames(String path)
          Return the List of filenames in the specified directory path or file path.
protected  String normalize(String path)
          Return the standard, normalized form of the path.
protected  List normalizedComponents(String path)
          Return the components of the specified path as a List.
 String path(String path1, String path2)
          Build a path from the two path components.
 void rename(String fromPath, String toPath)
          Rename the file or directory.
protected  void renamePath(FileSystemEntry entry, String toPath)
          Rename the file system entry to the specified path name
 void setCreateParentDirectoriesAutomatically(boolean createParentDirectoriesAutomatically)
           
 void setDirectoryListingFormatter(DirectoryListingFormatter directoryListingFormatter)
           
 void setEntries(List entriesToAdd)
          Add each of the entries in the specified List to this filesystem.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractFakeFileSystem

public AbstractFakeFileSystem()
Method Detail

isCreateParentDirectoriesAutomatically

public boolean isCreateParentDirectoriesAutomatically()

setCreateParentDirectoriesAutomatically

public void setCreateParentDirectoriesAutomatically(boolean createParentDirectoriesAutomatically)

getDirectoryListingFormatter

public DirectoryListingFormatter getDirectoryListingFormatter()

setDirectoryListingFormatter

public void setDirectoryListingFormatter(DirectoryListingFormatter directoryListingFormatter)

setEntries

public void setEntries(List entriesToAdd)
Add each of the entries in the specified List to this filesystem. Note that this does not affect entries already existing within this filesystem.

Parameters:
entriesToAdd - - the List of FileSystemEntry entries to add

add

public void add(FileSystemEntry entry)
Add the specified file system entry (file or directory) to this file system

Specified by:
add in interface FileSystem
Parameters:
entry - - the FileSystemEntry to add

delete

public boolean delete(String path)
Delete the file or directory specified by the path. Return true if the file is successfully deleted, false otherwise. If the path refers to a directory, it must be empty. Return false if the path does not refer to a valid file or directory or if it is a non-empty directory.

Specified by:
delete in interface FileSystem
Parameters:
path - - the path of the file or directory to delete
Returns:
true if the file or directory is successfully deleted
Throws:
AssertFailedException - - if path is null
See Also:
FileSystem.delete(java.lang.String)

exists

public boolean exists(String path)
Return true if there exists a file or directory at the specified path

Specified by:
exists in interface FileSystem
Parameters:
path - - the path
Returns:
true if the file/directory exists
Throws:
AssertionError - - if path is null
See Also:
FileSystem.exists(java.lang.String)

isDirectory

public boolean isDirectory(String path)
Return true if the specified path designates an existing directory, false otherwise

Specified by:
isDirectory in interface FileSystem
Parameters:
path - - the path
Returns:
true if path is a directory, false otherwise
Throws:
AssertionError - - if path is null
See Also:
FileSystem.isDirectory(java.lang.String)

isFile

public boolean isFile(String path)
Return true if the specified path designates an existing file, false otherwise

Specified by:
isFile in interface FileSystem
Parameters:
path - - the path
Returns:
true if path is a file, false otherwise
Throws:
AssertionError - - if path is null
See Also:
FileSystem.isFile(java.lang.String)

listFiles

public List listFiles(String path)
Return the List of FileSystemEntry objects for the files in the specified directory or group of files. If the path specifies a single file, then return a list with a single FileSystemEntry object representing that file. If the path does not refer to an existing directory or group of files, then an empty List is returned.

Specified by:
listFiles in interface FileSystem
Parameters:
path - - the path specifying a directory or group of files; may contain wildcards (? or *)
Returns:
the List of FileSystemEntry objects for the specified directory or file; may be empty
See Also:
FileSystem.listFiles(java.lang.String)

listNames

public List listNames(String path)
Return the List of filenames in the specified directory path or file path. If the path specifies a single file, then return that single filename. The returned filenames do not include a path. If the path does not refer to a valid directory or file path, then an empty List is returned.

Specified by:
listNames in interface FileSystem
Parameters:
path - - the path specifying a directory or group of files; may contain wildcards (? or *)
Returns:
the List of filenames (not including paths) for all files in the specified directory or file path; may be empty
Throws:
AssertionError - - if path is null
See Also:
FileSystem.listNames(java.lang.String)

rename

public void rename(String fromPath,
                   String toPath)
Rename the file or directory. Specify the FROM path and the TO path. Throw an exception if the FROM path or the parent directory of the TO path do not exist; or if the rename fails for another reason.

Specified by:
rename in interface FileSystem
Parameters:
fromPath - - the source (old) path + filename
toPath - - the target (new) path + filename
Throws:
AssertionError - - if fromPath or toPath is null
FileSystemException - - if the rename fails.

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()

formatDirectoryListing

public String formatDirectoryListing(FileSystemEntry fileSystemEntry)
Return the formatted directory listing entry for the file represented by the specified FileSystemEntry

Specified by:
formatDirectoryListing in interface FileSystem
Parameters:
fileSystemEntry - - the FileSystemEntry representing the file or directory entry to be formatted
Returns:
the the formatted directory listing entry

path

public String path(String path1,
                   String path2)
Build a path from the two path components. Concatenate path1 and path2. Insert the path separator character in between if necessary (i.e., if both are non-empty and path1 does not already end with a separator character AND path2 does not begin with one).

Specified by:
path in interface FileSystem
Parameters:
path1 - - the first path component may be null or empty
path2 - - the second path component may be null or empty
Returns:
the normalized path resulting from concatenating path1 to path2

getParent

public String getParent(String path)
Return the parent path of the specified path. If path specifies a filename, then this method returns the path of the directory containing that file. If path specifies a directory, the this method returns its parent directory. If path is empty or does not have a parent component, then return an empty string.

All path separators in the returned path are converted to the system-dependent separator character.

Specified by:
getParent in interface FileSystem
Parameters:
path - - the path
Returns:
the parent of the specified path, or null if path has no parent
Throws:
AssertionError - - if path is null

getName

public String getName(String path)
Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty string is returned.

Parameters:
path - - the path
Returns:
The name of the file or directory denoted by this abstract pathname, or the empty string if this pathname's name sequence is empty

getEntry

public FileSystemEntry getEntry(String path)
Returns the FileSystemEntry object representing the file system entry at the specified path, or null if the path does not specify an existing file or directory within this file system.

Specified by:
getEntry in interface FileSystem
Parameters:
path - - the path of the file or directory within this file system
Returns:
the FileSystemEntry containing the information for the file or directory, or else null
See Also:
FileSystem.getEntry(String)

isValidName

protected abstract boolean isValidName(String path)
Parameters:
path - - the path
Returns:
true if the specified dir/file path name is valid according to the current filesystem.

getSeparatorChar

protected abstract char getSeparatorChar()
Returns:
the file system-specific file separator as a char

isRoot

protected abstract boolean isRoot(String pathComponent)
Parameters:
pathComponent - - the component (piece) of the path to check
Returns:
true if the specified path component is a root for this filesystem

isSeparator

protected abstract boolean isSeparator(char c)
Return true if the specified char is a separator character for this filesystem

Parameters:
c - - the character to test
Returns:
true if the specified char is a separator character

getSeparator

protected String getSeparator()
Returns:
the file system-specific file separator as a String

getFileSystemEntryKey

protected String getFileSystemEntryKey(String path)
Return the normalized and unique key used to access the file system entry

Parameters:
path - - the path
Returns:
the corresponding normalized key

normalize

protected String normalize(String path)
Return the standard, normalized form of the path.

Parameters:
path - - the path
Returns:
the path in a standard, unique, canonical form
Throws:
AssertionError - - if path is null

checkForInvalidFilename

protected void checkForInvalidFilename(String path)
Throw an InvalidFilenameException if the specified path is not valid.

Parameters:
path - - the path

renamePath

protected void renamePath(FileSystemEntry entry,
                          String toPath)
Rename the file system entry to the specified path name

Parameters:
entry - - the file system entry
toPath - - the TO path (normalized)

getRequiredEntry

protected FileSystemEntry getRequiredEntry(String path)
Return the FileSystemEntry for the specified path. Throw FileSystemException if the specified path does not exist.

Parameters:
path - - the path
Returns:
the FileSystemEntry
Throws:
FileSystemException - - if the specified path does not exist

normalizedComponents

protected List normalizedComponents(String path)
Return the components of the specified path as a List. The components are normalized, and the returned List does not include path separator characters.

Parameters:
path - - the path
Returns:
the List of normalized components

componentsToPath

protected String componentsToPath(List components)
Build a path from the specified list of path components

Parameters:
components - - the list of path components
Returns:
the resulting path

isAbsolute

public boolean isAbsolute(String path)
Return true if the specified path designates an absolute file path.

Specified by:
isAbsolute in interface FileSystem
Parameters:
path - - the path
Returns:
true if path is absolute, false otherwise
Throws:
AssertionError - - if path is null


Copyright © 2014. All rights reserved.