org.apache.lucene.store
Class MockDirectoryWrapper

java.lang.Object
  extended by org.apache.lucene.store.Directory
      extended by org.apache.lucene.store.FilterDirectory
          extended by org.apache.lucene.store.BaseDirectoryWrapper
              extended by org.apache.lucene.store.MockDirectoryWrapper
All Implemented Interfaces:
Closeable

public class MockDirectoryWrapper
extends BaseDirectoryWrapper

This is a Directory Wrapper that adds methods intended to be used only by unit tests. It also adds a number of features useful for testing:


Nested Class Summary
static class MockDirectoryWrapper.Failure
          Objects that represent fail-able conditions.
static class MockDirectoryWrapper.FakeIOException
          Use this when throwing fake IOException, e.g.
static class MockDirectoryWrapper.Throttling
          Enum for controlling hard disk throttling.
 
Nested classes/interfaces inherited from class org.apache.lucene.store.Directory
Directory.IndexInputSlicer
 
Field Summary
protected  LockFactory lockFactory
           
 
Fields inherited from class org.apache.lucene.store.BaseDirectoryWrapper
isOpen
 
Fields inherited from class org.apache.lucene.store.FilterDirectory
in
 
Constructor Summary
MockDirectoryWrapper(Random random, Directory delegate)
           
 
Method Summary
 void clearCrash()
           
 void clearLock(String name)
           
 void close()
           
 void copy(Directory to, String src, String dest, IOContext context)
           
 void crash()
          Simulates a crash of OS or machine by overwriting unsynced files.
 IndexOutput createOutput(String name, IOContext context)
           
 Directory.IndexInputSlicer createSlicer(String name, IOContext context)
           
 void deleteFile(String name)
           
 void failOn(MockDirectoryWrapper.Failure fail)
          add a Failure object to the list of objects to be evaluated at every potential failure point
 boolean fileExists(String name)
           
 long fileLength(String name)
           
 boolean getAssertNoDeleteOpenFile()
           
 int getInputCloneCount()
           
 LockFactory getLockFactory()
           
 String getLockID()
           
 long getMaxSizeInBytes()
           
 long getMaxUsedSizeInBytes()
          Returns the peek actual storage used (bytes) in this directory.
 boolean getNoDeleteOpenFile()
           
 Set<String> getOpenDeletedFiles()
           
 double getRandomIOExceptionRate()
           
 double getRandomIOExceptionRateOnOpen()
           
 long getRecomputedActualSizeInBytes()
          Like getRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest RAMOutputStream.BUFFER_SIZE (now 1024) bytes.
 long getRecomputedSizeInBytes()
          Provided for testing purposes.
 String[] listAll()
           
 Lock makeLock(String name)
           
 IndexInput openInput(String name, IOContext context)
           
 void removeIndexInput(IndexInput in, String name)
           
 void removeIndexOutput(IndexOutput out, String name)
           
 void resetMaxUsedSizeInBytes()
           
 void setAssertNoDeleteOpenFile(boolean value)
          Trip a test assert if there is an attempt to delete an open file.
 void setAssertNoUnrefencedFilesOnClose(boolean v)
           
 void setFailOnCreateOutput(boolean v)
           
 void setFailOnOpenInput(boolean v)
           
 void setLockFactory(LockFactory lockFactory)
           
 void setMaxSizeInBytes(long maxSize)
           
 void setNoDeleteOpenFile(boolean value)
          Emulate windows whereby deleting an open file is not allowed (raise IOException).
 void setPreventDoubleWrite(boolean value)
          If set to true, we throw an IOException if the same file is opened by createOutput, ever.
 void setRandomIOExceptionRate(double rate)
          If 0.0, no exceptions will be thrown.
 void setRandomIOExceptionRateOnOpen(double rate)
          If 0.0, no exceptions will be thrown during openInput and createOutput.
 void setThrottling(MockDirectoryWrapper.Throttling throttling)
           
 void setTrackDiskUsage(boolean v)
           
 void setWrapLockFactory(boolean v)
          Set to false if you want to return the pure lockfactory and not wrap it with MockLockFactoryWrapper.
 long sizeInBytes()
           
 void sync(Collection<String> names)
           
 
Methods inherited from class org.apache.lucene.store.BaseDirectoryWrapper
getCheckIndexOnClose, getCrossCheckTermVectorsOnClose, isOpen, setCheckIndexOnClose, setCrossCheckTermVectorsOnClose
 
Methods inherited from class org.apache.lucene.store.FilterDirectory
getDelegate, toString
 
Methods inherited from class org.apache.lucene.store.Directory
ensureOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

lockFactory

protected LockFactory lockFactory
Constructor Detail

MockDirectoryWrapper

public MockDirectoryWrapper(Random random,
                            Directory delegate)
Method Detail

getInputCloneCount

public int getInputCloneCount()

setTrackDiskUsage

public void setTrackDiskUsage(boolean v)

setPreventDoubleWrite

public void setPreventDoubleWrite(boolean value)
If set to true, we throw an IOException if the same file is opened by createOutput, ever.


setThrottling

public void setThrottling(MockDirectoryWrapper.Throttling throttling)

sync

public void sync(Collection<String> names)
          throws IOException
Overrides:
sync in class FilterDirectory
Throws:
IOException

sizeInBytes

public final long sizeInBytes()
                       throws IOException
Throws:
IOException

crash

public void crash()
           throws IOException
Simulates a crash of OS or machine by overwriting unsynced files.

Throws:
IOException

clearCrash

public void clearCrash()

setMaxSizeInBytes

public void setMaxSizeInBytes(long maxSize)

getMaxSizeInBytes

public long getMaxSizeInBytes()

getMaxUsedSizeInBytes

public long getMaxUsedSizeInBytes()
Returns the peek actual storage used (bytes) in this directory.


resetMaxUsedSizeInBytes

public void resetMaxUsedSizeInBytes()
                             throws IOException
Throws:
IOException

setNoDeleteOpenFile

public void setNoDeleteOpenFile(boolean value)
Emulate windows whereby deleting an open file is not allowed (raise IOException).


getNoDeleteOpenFile

public boolean getNoDeleteOpenFile()

setAssertNoDeleteOpenFile

public void setAssertNoDeleteOpenFile(boolean value)
Trip a test assert if there is an attempt to delete an open file.


getAssertNoDeleteOpenFile

public boolean getAssertNoDeleteOpenFile()

setRandomIOExceptionRate

public void setRandomIOExceptionRate(double rate)
If 0.0, no exceptions will be thrown. Else this should be a double 0.0 - 1.0. We will randomly throw an IOException on the first write to an OutputStream based on this probability.


getRandomIOExceptionRate

public double getRandomIOExceptionRate()

setRandomIOExceptionRateOnOpen

public void setRandomIOExceptionRateOnOpen(double rate)
If 0.0, no exceptions will be thrown during openInput and createOutput. Else this should be a double 0.0 - 1.0 and we will randomly throw an IOException in openInput and createOutput with this probability.


getRandomIOExceptionRateOnOpen

public double getRandomIOExceptionRateOnOpen()

deleteFile

public void deleteFile(String name)
                throws IOException
Overrides:
deleteFile in class FilterDirectory
Throws:
IOException

getOpenDeletedFiles

public Set<String> getOpenDeletedFiles()

setFailOnCreateOutput

public void setFailOnCreateOutput(boolean v)

createOutput

public IndexOutput createOutput(String name,
                                IOContext context)
                         throws IOException
Overrides:
createOutput in class FilterDirectory
Throws:
IOException

setFailOnOpenInput

public void setFailOnOpenInput(boolean v)

openInput

public IndexInput openInput(String name,
                            IOContext context)
                     throws IOException
Overrides:
openInput in class FilterDirectory
Throws:
IOException

getRecomputedSizeInBytes

public final long getRecomputedSizeInBytes()
                                    throws IOException
Provided for testing purposes. Use sizeInBytes() instead.

Throws:
IOException

getRecomputedActualSizeInBytes

public final long getRecomputedActualSizeInBytes()
                                          throws IOException
Like getRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest RAMOutputStream.BUFFER_SIZE (now 1024) bytes.

Throws:
IOException

setAssertNoUnrefencedFilesOnClose

public void setAssertNoUnrefencedFilesOnClose(boolean v)

setWrapLockFactory

public void setWrapLockFactory(boolean v)
Set to false if you want to return the pure lockfactory and not wrap it with MockLockFactoryWrapper.

Be careful if you turn this off: MockDirectoryWrapper might no longer be able to detect if you forget to close an IndexWriter, and spit out horribly scary confusing exceptions instead of simply telling you that.


close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class BaseDirectoryWrapper
Throws:
IOException

removeIndexOutput

public void removeIndexOutput(IndexOutput out,
                              String name)

removeIndexInput

public void removeIndexInput(IndexInput in,
                             String name)

failOn

public void failOn(MockDirectoryWrapper.Failure fail)
add a Failure object to the list of objects to be evaluated at every potential failure point


listAll

public String[] listAll()
                 throws IOException
Overrides:
listAll in class FilterDirectory
Throws:
IOException

fileExists

public boolean fileExists(String name)
                   throws IOException
Overrides:
fileExists in class FilterDirectory
Throws:
IOException

fileLength

public long fileLength(String name)
                throws IOException
Overrides:
fileLength in class FilterDirectory
Throws:
IOException

makeLock

public Lock makeLock(String name)
Overrides:
makeLock in class FilterDirectory

clearLock

public void clearLock(String name)
               throws IOException
Overrides:
clearLock in class FilterDirectory
Throws:
IOException

setLockFactory

public void setLockFactory(LockFactory lockFactory)
                    throws IOException
Overrides:
setLockFactory in class FilterDirectory
Throws:
IOException

getLockFactory

public LockFactory getLockFactory()
Overrides:
getLockFactory in class FilterDirectory

getLockID

public String getLockID()
Overrides:
getLockID in class FilterDirectory

copy

public void copy(Directory to,
                 String src,
                 String dest,
                 IOContext context)
          throws IOException
Overrides:
copy in class BaseDirectoryWrapper
Throws:
IOException

createSlicer

public Directory.IndexInputSlicer createSlicer(String name,
                                               IOContext context)
                                        throws IOException
Overrides:
createSlicer in class BaseDirectoryWrapper
Throws:
IOException


Copyright © 2000-2014 Apache Software Foundation. All Rights Reserved.