org.apache.lucene.store

## Class MockDirectoryWrapper

- java.lang.Object
- - org.apache.lucene.store.Directory
  - - org.apache.lucene.store.FilterDirectory
    - - org.apache.lucene.store.BaseDirectoryWrapper
      - - org.apache.lucene.store.MockDirectoryWrapper

- All Implemented Interfaces:
    Closeable, AutoCloseable

      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:
  - 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 and Description
    --- | ---
    `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.

  - ### Field Summary

    - ### Fields inherited from class org.apache.lucene.store.BaseDirectoryWrapper

      `isOpen`

    - ### Fields inherited from class org.apache.lucene.store.FilterDirectory

      `in`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MockDirectoryWrapper(Random random, Directory delegate)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clearCrash()`
    `void` | `close()`
    `void` | `copyFrom(Directory from, String src, String dest, IOContext context)`
    `void` | `corruptFiles(Collection<String> files)`
    `void` | `corruptUnknownFiles()`
    `void` | `crash()` Simulates a crash of OS or machine by overwriting unsynced files.
    `IndexOutput` | `createOutput(String name, IOContext context)`
    `IndexOutput` | `createTempOutput(String prefix, String suffix, IOContext context)`
    `void` | `deleteFile(String name)`
    `protected void` | `ensureOpen()`
    `void` | `failOn(MockDirectoryWrapper.Failure fail)` add a Failure object to the list of objects to be evaluated at every potential failure point
    `long` | `fileLength(String name)`
    `boolean` | `getAssertNoDeleteOpenFile()`
    `long` | `getFileHandleCount()` returns current open file handle count
    `int` | `getInputCloneCount()`
    `long` | `getMaxSizeInBytes()`
    `long` | `getMaxUsedSizeInBytes()` Returns the peek actual storage used (bytes) in this directory.
    `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` | `obtainLock(String name)`
    `ChecksumIndexInput` | `openChecksumInput(String name, IOContext context)`
    `IndexInput` | `openInput(String name, IOContext context)`
    `void` | `removeIndexInput(IndexInput in, String name)`
    `void` | `removeIndexOutput(IndexOutput out, String name)`
    `void` | `rename(String source, String dest)`
    `void` | `resetMaxUsedSizeInBytes()`
    `void` | `setAllowRandomFileNotFoundException(boolean value)` If set to true (the default), when we throw random IOException on openInput or createOutput, we may sometimes throw FileNotFoundException or NoSuchFileException.
    `void` | `setAllowReadingFilesStillOpenForWrite(boolean value)` If set to true, you can open an inputstream on a file that is still open for writes.
    `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` | `setMaxSizeInBytes(long maxSize)`
    `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` | `setUseSlowOpenClosers(boolean v)` Add a rare small sleep to catch race conditions in open/close
    `void` | `setVerboseClone(boolean v)` If set to true, we print a fake exception with filename and stacktrace on every indexinput clone()
    `long` | `sizeInBytes()`
    `void` | `sync(Collection<String> names)`
    `void` | `syncMetaData()`
    `String` | `toString()`

    - ### Methods inherited from class org.apache.lucene.store.BaseDirectoryWrapper

      `getCheckIndexOnClose, getCrossCheckTermVectorsOnClose, isOpen, setCheckIndexOnClose, setCrossCheckTermVectorsOnClose`

    - ### Methods inherited from class org.apache.lucene.store.FilterDirectory

      `getDelegate, getPendingDeletions, unwrap`

    - ### Methods inherited from class org.apache.lucene.store.Directory

      `getTempFileName`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### MockDirectoryWrapper

          public MockDirectoryWrapper(Random random,
                                      Directory delegate)

  - ### Method Detail

    - #### getInputCloneCount

          public int getInputCloneCount()

    - #### setVerboseClone

          public void setVerboseClone(boolean v)

      If set to true, we print a fake exception with filename and stacktrace on every indexinput clone()

    - #### setTrackDiskUsage

          public void setTrackDiskUsage(boolean v)

    - #### setAllowRandomFileNotFoundException

          public void setAllowRandomFileNotFoundException(boolean value)

      If set to true (the default), when we throw random IOException on openInput or createOutput, we may sometimes throw FileNotFoundException or NoSuchFileException.

    - #### setAllowReadingFilesStillOpenForWrite

          public void setAllowReadingFilesStillOpenForWrite(boolean value)

      If set to true, you can open an inputstream on a file that is still open for writes.

    - #### setThrottling

          public void setThrottling(MockDirectoryWrapper.Throttling throttling)

    - #### setUseSlowOpenClosers

          public void setUseSlowOpenClosers(boolean v)

      Add a rare small sleep to catch race conditions in open/close

      You can enable this if you need it.

    - #### sync

          public void sync(Collection<String> names)
                    throws IOException

      Overrides:
        `sync` in class `FilterDirectory`
      Throws:
        `IOException`

    - #### rename

          public void rename(String source,
                             String dest)
                      throws IOException

      Overrides:
        `rename` in class `FilterDirectory`
      Throws:
        `IOException`

    - #### syncMetaData

          public void syncMetaData()
                            throws IOException

      Overrides:
        `syncMetaData` in class `FilterDirectory`
      Throws:
        `IOException`

    - #### sizeInBytes

          public final long sizeInBytes()
                                 throws IOException

      Throws:
        `IOException`

    - #### corruptUnknownFiles

          public void corruptUnknownFiles()
                                   throws IOException

      Throws:
        `IOException`

    - #### corruptFiles

          public void corruptFiles(Collection<String> files)
                            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`

    - #### 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()

    - #### getFileHandleCount

          public long getFileHandleCount()

      returns current open file handle count

    - #### 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`

    - #### createTempOutput

          public IndexOutput createTempOutput(String prefix,
                                              String suffix,
                                              IOContext context)
                                       throws IOException

      Overrides:
        `createTempOutput` 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)

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      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`

    - #### fileLength

          public long fileLength(String name)
                          throws IOException

      Overrides:
        `fileLength` in class `FilterDirectory`
      Throws:
        `IOException`

    - #### obtainLock

          public Lock obtainLock(String name)
                          throws IOException

      Overrides:
        `obtainLock` in class `FilterDirectory`
      Throws:
        `IOException`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `FilterDirectory`

    - #### openChecksumInput

          public final ChecksumIndexInput openChecksumInput(String name,
                                                            IOContext context)
                                                     throws IOException

      Overrides:
        `openChecksumInput` in class `Directory`
      Throws:
        `IOException`

    - #### copyFrom

          public final void copyFrom(Directory from,
                                     String src,
                                     String dest,
                                     IOContext context)
                              throws IOException

      Overrides:
        `copyFrom` in class `Directory`
      Throws:
        `IOException`

    - #### ensureOpen

          protected final void ensureOpen()
                                   throws AlreadyClosedException

      Overrides:
        `ensureOpen` in class `Directory`
      Throws:
        `AlreadyClosedException`

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