com.android.repository.testframework
Class MockFileOp

java.lang.Object
  extended by com.android.repository.testframework.MockFileOp
All Implemented Interfaces:
FileOp

public class MockFileOp
extends java.lang.Object
implements FileOp

Mock version of FileOpImpl that wraps some common File operations on files and folders.

This version does not perform any file operation. Instead it records a textual representation of all the file operations performed.

To avoid cross-platform path issues (e.g. Windows path), the methods here should always use rooted (aka absolute) unix-looking paths, e.g. "/dir1/dir2/file3". When processing File, you can convert them using getAgnosticAbsPath(File).


Nested Class Summary
 class MockFileOp.StringOutputStream
          An OutputStream that will capture the stream as an UTF-8 string once properly closed and associate it to the given File.
 
Field Summary
 
Fields inherited from interface com.android.repository.io.FileOp
EMPTY_FILE_ARRAY
 
Constructor Summary
MockFileOp()
           
 
Method Summary
 boolean canExecute(java.io.File file)
          

Note: this mock version does nothing.

 boolean canWrite(java.io.File file)
          Invokes File.canWrite() on the given file.
 void copyFile(java.io.File source, java.io.File dest)
          Copies a binary file.
 boolean createNewFile(java.io.File file)
          Creates a new file.
 boolean delete(java.io.File file)
          Invokes File.delete() on the given file.
 void deleteFileOrFolder(java.io.File fileOrFolder)
          Helper to delete a file or a directory.
 void deleteOnExit(java.io.File file)
           
 java.io.File ensureRealFile(java.io.File in)
          If in is an in-memory file, write it out as a proper file and return it.
 boolean exists(java.io.File file)
          Invokes File.exists() on the given file.
 java.lang.String getAgnosticAbsPath(java.io.File file)
           
 java.lang.String getAgnosticAbsPath(java.lang.String path)
           
 java.lang.String[] getExistingFiles()
          Returns the list of paths added using recordExistingFile(String) and eventually updated by delete(File) operations.
 java.lang.String[] getExistingFolders()
          Returns the list of folder paths added using recordExistingFolder(String) and eventually updated delete(File) or mkdirs(File) operations.
 java.lang.String[] getOutputStreams()
          Returns the MockFileOp.StringOutputStream.toString() as an array, in creation order.
 boolean hasRecordedExistingFile(java.io.File file)
          Returns true if a file with the given path has been recorded.
 boolean hasRecordedExistingFolder(java.io.File folder)
          Returns true if a folder with the given path has been recorded.
 boolean isDirectory(java.io.File file)
          Invokes File.isDirectory() on the given file.
 boolean isFile(java.io.File file)
          Invokes File.isFile() on the given file.
 boolean isSameFile(java.io.File file1, java.io.File file2)
          Checks whether 2 binary files are the same.
 boolean isWindows()
          Returns true if we're on windows, false otherwise.
 long lastModified(java.io.File file)
          Returns the lastModified attribute of the file.
 long length(java.io.File file)
          Invokes File.length() on the given file.
 java.lang.String[] list(java.io.File folder, java.io.FilenameFilter filenameFilter)
           
 java.io.File[] listFiles(java.io.File file)
          Invokes File.listFiles() on the given file.
 java.io.File[] listFiles(java.io.File folder, java.io.FilenameFilter filenameFilter)
           
 java.util.Properties loadProperties(java.io.File file)
          Load Properties from a file.
 boolean mkdirs(java.io.File file)
          Invokes File.mkdirs() on the given file.
 java.io.InputStream newFileInputStream(java.io.File file)
          Creates a new InputStream for the given file.
 java.io.OutputStream newFileOutputStream(java.io.File file)
          Creates a new OutputStream for the given file.
 void recordExistingFile(java.io.File file)
          Records a new absolute file path.
 void recordExistingFile(java.lang.String absFilePath)
          Records a new absolute file path.
 void recordExistingFile(java.lang.String absFilePath, byte[] inputStream)
          Records a new absolute file path & its input stream content.
 void recordExistingFile(java.lang.String absFilePath, long lastModified, byte[] inputStream)
          Records a new absolute file path & its input stream content.
 void recordExistingFile(java.lang.String absFilePath, long lastModified, java.lang.String content)
          Records a new absolute file path & its input stream content.
 void recordExistingFile(java.lang.String absFilePath, java.lang.String content)
          Records a new absolute file path & its input stream content.
 void recordExistingFolder(java.io.File folder)
          Records a new absolute folder path.
 void recordExistingFolder(java.lang.String absFolderPath)
          Records a new absolute folder path.
 boolean renameTo(java.io.File oldFile, java.io.File newFile)
          Invokes File.renameTo(File) on the given files.
 void reset()
          Resets the internal state, as if the object had been newly created.
 void saveProperties(java.io.File file, java.util.Properties props, java.lang.String comments)
          Saves (write, store) the given Properties into the given File.
 void setExecutablePermission(java.io.File file)
          Sets the executable Unix permission (+x) on a file or folder.
 void setIsWindows(boolean isWindows)
           
 void setReadOnly(java.io.File file)
          Sets the file or directory as read-only.
 java.lang.String toString(java.io.File f, java.nio.charset.Charset c)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockFileOp

public MockFileOp()
Method Detail

reset

public void reset()
Resets the internal state, as if the object had been newly created.


isWindows

public boolean isWindows()
Description copied from interface: FileOp
Returns true if we're on windows, false otherwise.

Specified by:
isWindows in interface FileOp

toString

@NonNull
public java.lang.String toString(@NonNull
                                         java.io.File f,
                                         @NonNull
                                         java.nio.charset.Charset c)
                          throws java.io.IOException
Specified by:
toString in interface FileOp
Throws:
java.io.IOException
See Also:
Files.toString(File, Charset)

list

@Nullable
public java.lang.String[] list(@NonNull
                                        java.io.File folder,
                                        @Nullable
                                        java.io.FilenameFilter filenameFilter)
Specified by:
list in interface FileOp
See Also:
File.list(FilenameFilter)

listFiles

@Nullable
public java.io.File[] listFiles(@NonNull
                                         java.io.File folder,
                                         @Nullable
                                         java.io.FilenameFilter filenameFilter)
Specified by:
listFiles in interface FileOp
See Also:
File.listFiles(FilenameFilter)

deleteOnExit

public void deleteOnExit(java.io.File file)
Specified by:
deleteOnExit in interface FileOp
See Also:
File.deleteOnExit()

setIsWindows

public void setIsWindows(boolean isWindows)

getAgnosticAbsPath

@NonNull
public java.lang.String getAgnosticAbsPath(@NonNull
                                                   java.io.File file)

getAgnosticAbsPath

@NonNull
public java.lang.String getAgnosticAbsPath(@NonNull
                                                   java.lang.String path)

recordExistingFile

public void recordExistingFile(@NonNull
                               java.io.File file)
Records a new absolute file path. Parent folders are automatically created.


recordExistingFile

public void recordExistingFile(@NonNull
                               java.lang.String absFilePath)
Records a new absolute file path. Parent folders are automatically created.

The syntax should always look "unix-like", e.g. "/dir/file". On Windows that means you'll want to use getAgnosticAbsPath(File).

Parameters:
absFilePath - A unix-like file path, e.g. "/dir/file"

recordExistingFile

public void recordExistingFile(@NonNull
                               java.lang.String absFilePath,
                               @Nullable
                               byte[] inputStream)
Records a new absolute file path & its input stream content. Parent folders are automatically created.

The syntax should always look "unix-like", e.g. "/dir/file". On Windows that means you'll want to use getAgnosticAbsPath(File).

Parameters:
absFilePath - A unix-like file path, e.g. "/dir/file"
inputStream - A non-null byte array of content to return via newFileInputStream(File).

recordExistingFile

public void recordExistingFile(@NonNull
                               java.lang.String absFilePath,
                               @NonNull
                               java.lang.String content)
Records a new absolute file path & its input stream content. Parent folders are automatically created.

The syntax should always look "unix-like", e.g. "/dir/file". On Windows that means you'll want to use getAgnosticAbsPath(File).

Parameters:
absFilePath - A unix-like file path, e.g. "/dir/file"
content - A non-null UTF-8 content string to return via newFileInputStream(File).

recordExistingFile

public void recordExistingFile(@NonNull
                               java.lang.String absFilePath,
                               long lastModified,
                               @Nullable
                               byte[] inputStream)
Records a new absolute file path & its input stream content. Parent folders are automatically created.

The syntax should always look "unix-like", e.g. "/dir/file". On Windows that means you'll want to use getAgnosticAbsPath(File).

Parameters:
absFilePath - A unix-like file path, e.g. "/dir/file"
inputStream - A non-null byte array of content to return via newFileInputStream(File).

recordExistingFile

public void recordExistingFile(@NonNull
                               java.lang.String absFilePath,
                               long lastModified,
                               @NonNull
                               java.lang.String content)
Records a new absolute file path & its input stream content. Parent folders are automatically created.

The syntax should always look "unix-like", e.g. "/dir/file". On Windows that means you'll want to use getAgnosticAbsPath(File).

Parameters:
absFilePath - A unix-like file path, e.g. "/dir/file"
content - A non-null UTF-8 content string to return via newFileInputStream(File).

recordExistingFolder

public void recordExistingFolder(java.io.File folder)
Records a new absolute folder path. Parent folders are automatically created.


recordExistingFolder

public void recordExistingFolder(java.lang.String absFolderPath)
Records a new absolute folder path. Parent folders are automatically created.

The syntax should always look "unix-like", e.g. "/dir/file". On Windows that means you'll want to use getAgnosticAbsPath(File).

Parameters:
absFolderPath - A unix-like folder path, e.g. "/dir/file"

hasRecordedExistingFile

public boolean hasRecordedExistingFile(java.io.File file)
Returns true if a file with the given path has been recorded.


hasRecordedExistingFolder

public boolean hasRecordedExistingFolder(java.io.File folder)
Returns true if a folder with the given path has been recorded.


getExistingFiles

@NonNull
public java.lang.String[] getExistingFiles()
Returns the list of paths added using recordExistingFile(String) and eventually updated by delete(File) operations.

The returned list is sorted by alphabetic absolute path string.


getExistingFolders

@NonNull
public java.lang.String[] getExistingFolders()
Returns the list of folder paths added using recordExistingFolder(String) and eventually updated delete(File) or mkdirs(File) operations.

The returned list is sorted by alphabetic absolute path string.


getOutputStreams

@NonNull
public java.lang.String[] getOutputStreams()
Returns the MockFileOp.StringOutputStream.toString() as an array, in creation order. Array can be empty but not null.


deleteFileOrFolder

public void deleteFileOrFolder(@NonNull
                               java.io.File fileOrFolder)
Description copied from interface: FileOp
Helper to delete a file or a directory. For a directory, recursively deletes all of its content. Files that cannot be deleted right away are marked for deletion on exit. It's ok for the file or folder to not exist at all.

Specified by:
deleteFileOrFolder in interface FileOp

setExecutablePermission

public void setExecutablePermission(@NonNull
                                    java.io.File file)
                             throws java.io.IOException
Sets the executable Unix permission (+x) on a file or folder.

This attempts to use File#setExecutable through reflection if it's available. If this is not available, this invokes a chmod exec instead, so there is no guarantee of it being fast.

Caller must make sure to not invoke this under Windows.

Note: this mock version does nothing.

Specified by:
setExecutablePermission in interface FileOp
Parameters:
file - The file to set permissions on.
Throws:
java.io.IOException - If an I/O error occurs

canExecute

public boolean canExecute(@NonNull
                          java.io.File file)

Note: this mock version does nothing.

Specified by:
canExecute in interface FileOp
See Also:
File.canExecute()

ensureRealFile

public java.io.File ensureRealFile(@NonNull
                                   java.io.File in)
                            throws java.io.IOException
Description copied from interface: FileOp
If in is an in-memory file, write it out as a proper file and return it. Otherwise just return in.

Specified by:
ensureRealFile in interface FileOp
Throws:
java.io.IOException

setReadOnly

public void setReadOnly(@NonNull
                        java.io.File file)
Description copied from interface: FileOp
Sets the file or directory as read-only.

Specified by:
setReadOnly in interface FileOp
Parameters:
file - The file or directory to set permissions on.

copyFile

public void copyFile(@NonNull
                     java.io.File source,
                     @NonNull
                     java.io.File dest)
              throws java.io.IOException
Description copied from interface: FileOp
Copies a binary file.

Specified by:
copyFile in interface FileOp
Parameters:
source - the source file to copy.
dest - the destination file to write.
Throws:
java.io.FileNotFoundException - if the source file doesn't exist.
java.io.IOException - if there's a problem reading or writing the file.

isSameFile

public boolean isSameFile(@NonNull
                          java.io.File file1,
                          @NonNull
                          java.io.File file2)
                   throws java.io.IOException
Description copied from interface: FileOp
Checks whether 2 binary files are the same.

Specified by:
isSameFile in interface FileOp
Parameters:
file1 - the source file to copy
file2 - the destination file to write
Throws:
java.io.FileNotFoundException - if the source files don't exist.
java.io.IOException - if there's a problem reading the files.

isFile

public boolean isFile(@NonNull
                      java.io.File file)
Description copied from interface: FileOp
Invokes File.isFile() on the given file.

Specified by:
isFile in interface FileOp

isDirectory

public boolean isDirectory(@NonNull
                           java.io.File file)
Description copied from interface: FileOp
Invokes File.isDirectory() on the given file.

Specified by:
isDirectory in interface FileOp

canWrite

public boolean canWrite(@NonNull
                        java.io.File file)
Description copied from interface: FileOp
Invokes File.canWrite() on the given file.

Specified by:
canWrite in interface FileOp

exists

public boolean exists(@NonNull
                      java.io.File file)
Description copied from interface: FileOp
Invokes File.exists() on the given file.

Specified by:
exists in interface FileOp

length

public long length(@NonNull
                   java.io.File file)
Description copied from interface: FileOp
Invokes File.length() on the given file.

Specified by:
length in interface FileOp

delete

public boolean delete(@NonNull
                      java.io.File file)
Description copied from interface: FileOp
Invokes File.delete() on the given file. Note: for a recursive folder version, consider FileOp.deleteFileOrFolder(File).

Specified by:
delete in interface FileOp

mkdirs

public boolean mkdirs(@NonNull
                      java.io.File file)
Description copied from interface: FileOp
Invokes File.mkdirs() on the given file.

Specified by:
mkdirs in interface FileOp

listFiles

@NonNull
public java.io.File[] listFiles(@NonNull
                                        java.io.File file)
Invokes File.listFiles() on the given file. Contrary to the Java API, this returns an empty array instead of null when the directory does not exist. TODO: this incorrectly recursively returns all files in subfolders

Specified by:
listFiles in interface FileOp

renameTo

public boolean renameTo(@NonNull
                        java.io.File oldFile,
                        @NonNull
                        java.io.File newFile)
Description copied from interface: FileOp
Invokes File.renameTo(File) on the given files.

Specified by:
renameTo in interface FileOp

loadProperties

@NonNull
public java.util.Properties loadProperties(@NonNull
                                                   java.io.File file)
Load Properties from a file. Returns an empty property set on error.

TODO: we might want to overload this to read mock properties instead of a real file.

Specified by:
loadProperties in interface FileOp
Parameters:
file - A non-null file to load from. File may not exist.
Returns:
A new Properties with the properties loaded from the file, or an empty property set in case of error.

saveProperties

public void saveProperties(@NonNull
                           java.io.File file,
                           @NonNull
                           java.util.Properties props,
                           @NonNull
                           java.lang.String comments)
                    throws java.io.IOException
Saves (write, store) the given Properties into the given File.

Note that this uses the mock version of newFileOutputStream(File) and thus records the write rather than actually performing it.

Specified by:
saveProperties in interface FileOp
Parameters:
file - A non-null file to write to.
props - The properties to write.
comments - A non-null description of the properly list, written in the file.
Throws:
java.io.IOException - if the write operation failed.

newFileOutputStream

@NonNull
public java.io.OutputStream newFileOutputStream(@NonNull
                                                        java.io.File file)
                                         throws java.io.FileNotFoundException
Description copied from interface: FileOp
Creates a new OutputStream for the given file.

Specified by:
newFileOutputStream in interface FileOp
Throws:
java.io.FileNotFoundException

newFileInputStream

@NonNull
public java.io.InputStream newFileInputStream(@NonNull
                                                      java.io.File file)
                                       throws java.io.FileNotFoundException
Description copied from interface: FileOp
Creates a new InputStream for the given file.

Specified by:
newFileInputStream in interface FileOp
Throws:
java.io.FileNotFoundException

lastModified

public long lastModified(@NonNull
                         java.io.File file)
Description copied from interface: FileOp
Returns the lastModified attribute of the file.

Specified by:
lastModified in interface FileOp
Parameters:
file - The non-null file of which to retrieve the lastModified attribute.
Returns:
The last-modified attribute of the file, in milliseconds since The Epoch.
See Also:
File.lastModified()

createNewFile

public boolean createNewFile(java.io.File file)
                      throws java.io.IOException
Description copied from interface: FileOp
Creates a new file. See File.createNewFile().

Specified by:
createNewFile in interface FileOp
Throws:
java.io.IOException