Package org.apache.lucene.store
Class MockDirectoryWrapper
- java.lang.Object
-
- org.apache.lucene.store.Directory
-
- org.apache.lucene.store.MockDirectoryWrapper
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class MockDirectoryWrapper extends org.apache.lucene.store.DirectoryThis 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:- Instances created by
LuceneTestCase.newDirectory()are tracked to ensure they are closed by the test. - When a MockDirectoryWrapper is closed, it will throw an exception if it has any open files against it (with a stacktrace indicating where they were opened from).
- When a MockDirectoryWrapper is closed, it runs CheckIndex to test if the index was corrupted.
- MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMockDirectoryWrapper.FailureObjects that represent fail-able conditions.static classMockDirectoryWrapper.Throttling
-
Constructor Summary
Constructors Constructor Description MockDirectoryWrapper(Random random, org.apache.lucene.store.Directory delegate)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclearCrash()voidclearLock(String name)voidclose()voidcopy(org.apache.lucene.store.Directory to, String src, String dest)voidcrash()Simulates a crash of OS or machine by overwriting unsynced files.org.apache.lucene.store.IndexOutputcreateOutput(String name)voiddeleteFile(String name)voidfailOn(MockDirectoryWrapper.Failure fail)add a Failure object to the list of objects to be evaluated at every potential failure pointbooleanfileExists(String name)longfileLength(String name)longfileModified(String name)booleangetCheckIndexOnClose()intgetInputCloneCount()org.apache.lucene.store.LockFactorygetLockFactory()StringgetLockID()longgetMaxSizeInBytes()longgetMaxUsedSizeInBytes()Returns the peek actual storage used (bytes) in this directory.booleangetNoDeleteOpenFile()Set<String>getOpenDeletedFiles()doublegetRandomIOExceptionRate()longgetRecomputedActualSizeInBytes()Like getRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest RAMOutputStream.BUFFER_SIZE (now 1024) bytes.longgetRecomputedSizeInBytes()Provided for testing purposes.booleanisOpen()String[]listAll()org.apache.lucene.store.LockmakeLock(String name)org.apache.lucene.store.IndexInputopenInput(String name)voidremoveIndexInput(org.apache.lucene.store.IndexInput in, String name)voidremoveIndexOutput(org.apache.lucene.store.IndexOutput out, String name)voidresetMaxUsedSizeInBytes()voidsetAssertNoUnrefencedFilesOnClose(boolean v)voidsetCheckIndexOnClose(boolean value)Set whether or not checkindex should be run on closevoidsetFailOnCreateOutput(boolean v)voidsetFailOnOpenInput(boolean v)voidsetLockFactory(org.apache.lucene.store.LockFactory lockFactory)voidsetMaxSizeInBytes(long maxSize)voidsetNoDeleteOpenFile(boolean value)Emulate windows whereby deleting an open file is not allowed (raise IOException).voidsetPreventDoubleWrite(boolean value)If set to true, we throw an IOException if the same file is opened by createOutput, ever.voidsetRandomIOExceptionRate(double rate)If 0.0, no exceptions will be thrown.voidsetThrottling(MockDirectoryWrapper.Throttling throttling)voidsetTrackDiskUsage(boolean v)longsizeInBytes()voidsync(String name)Deprecated.voidsync(Collection<String> names)StringtoString()voidtouchFile(String name)Deprecated.
-
-
-
Constructor Detail
-
MockDirectoryWrapper
public MockDirectoryWrapper(Random random, org.apache.lucene.store.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.
-
sync
@Deprecated public void sync(String name) throws IOException
Deprecated.- Overrides:
syncin classorg.apache.lucene.store.Directory- Throws:
IOException
-
setThrottling
public void setThrottling(MockDirectoryWrapper.Throttling throttling)
-
sync
public void sync(Collection<String> names) throws IOException
- Overrides:
syncin classorg.apache.lucene.store.Directory- Throws:
IOException
-
toString
public String toString()
- Overrides:
toStringin classorg.apache.lucene.store.Directory
-
sizeInBytes
public final long sizeInBytes() throws IOException- Throws:
IOException
-
crash
public void crash() throws IOExceptionSimulates a crash of OS or machine by overwriting unsynced files.- Throws:
IOException
-
clearCrash
public void clearCrash() throws IOException- Throws:
IOException
-
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()
-
setCheckIndexOnClose
public void setCheckIndexOnClose(boolean value)
Set whether or not checkindex should be run on close
-
getCheckIndexOnClose
public boolean getCheckIndexOnClose()
-
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()
-
deleteFile
public void deleteFile(String name) throws IOException
- Specified by:
deleteFilein classorg.apache.lucene.store.Directory- Throws:
IOException
-
setFailOnCreateOutput
public void setFailOnCreateOutput(boolean v)
-
createOutput
public org.apache.lucene.store.IndexOutput createOutput(String name) throws IOException
- Specified by:
createOutputin classorg.apache.lucene.store.Directory- Throws:
IOException
-
setFailOnOpenInput
public void setFailOnOpenInput(boolean v)
-
openInput
public org.apache.lucene.store.IndexInput openInput(String name) throws IOException
- Specified by:
openInputin classorg.apache.lucene.store.Directory- Throws:
IOException
-
getRecomputedSizeInBytes
public final long getRecomputedSizeInBytes() throws IOExceptionProvided for testing purposes. Use sizeInBytes() instead.- Throws:
IOException
-
getRecomputedActualSizeInBytes
public final long getRecomputedActualSizeInBytes() throws IOExceptionLike 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)
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classorg.apache.lucene.store.Directory- Throws:
IOException
-
removeIndexOutput
public void removeIndexOutput(org.apache.lucene.store.IndexOutput out, String name)
-
removeIndexInput
public void removeIndexInput(org.apache.lucene.store.IndexInput in, String name)
-
isOpen
public boolean isOpen()
-
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
- Specified by:
listAllin classorg.apache.lucene.store.Directory- Throws:
IOException
-
fileExists
public boolean fileExists(String name) throws IOException
- Specified by:
fileExistsin classorg.apache.lucene.store.Directory- Throws:
IOException
-
fileModified
public long fileModified(String name) throws IOException
- Specified by:
fileModifiedin classorg.apache.lucene.store.Directory- Throws:
IOException
-
touchFile
@Deprecated public void touchFile(String name) throws IOException
Deprecated.- Specified by:
touchFilein classorg.apache.lucene.store.Directory- Throws:
IOException
-
fileLength
public long fileLength(String name) throws IOException
- Specified by:
fileLengthin classorg.apache.lucene.store.Directory- Throws:
IOException
-
makeLock
public org.apache.lucene.store.Lock makeLock(String name)
- Overrides:
makeLockin classorg.apache.lucene.store.Directory
-
clearLock
public void clearLock(String name) throws IOException
- Overrides:
clearLockin classorg.apache.lucene.store.Directory- Throws:
IOException
-
setLockFactory
public void setLockFactory(org.apache.lucene.store.LockFactory lockFactory) throws IOException- Overrides:
setLockFactoryin classorg.apache.lucene.store.Directory- Throws:
IOException
-
getLockFactory
public org.apache.lucene.store.LockFactory getLockFactory()
- Overrides:
getLockFactoryin classorg.apache.lucene.store.Directory
-
getLockID
public String getLockID()
- Overrides:
getLockIDin classorg.apache.lucene.store.Directory
-
copy
public void copy(org.apache.lucene.store.Directory to, String src, String dest) throws IOException- Overrides:
copyin classorg.apache.lucene.store.Directory- Throws:
IOException
-
-