Interface EphemeralBaseDir

  • All Superinterfaces:
    AutoCloseable

    public interface EphemeralBaseDir
    extends AutoCloseable
    A helper for creating a base dir programmatically, typically at test time.

    This is typically used in conjunction with EmbeddedApp, when the app requires a base dir. It is however also useful for any kind of testing where a “filesystem” is needed just for the test.

    The tmpDir(), dir(File), tmpJar() and jar(File) methods create instances, controlling what file system space will be used.

    The write(String, String), mkdir(String) and path(String) methods can be used for creating and getting at content.

    The getRoot() method provides a path object for the root of the base dir.

    This type implements Closeable. Closing an embedded base dir causes the base dir to be removed from the real file system. It is generally always desirable to close the base dir in order not to leave files around. The use(Action) method allows an action to be executed before having the base dir be automatically closed.

    • Method Detail

      • tmpDir

        static EphemeralBaseDir tmpDir()
        Creates a new base dir, using a newly created dir within the JVM's assigned temp dir.
        Returns:
        a new embedded base dir
      • dir

        static EphemeralBaseDir dir​(File dir)
        Creates a new base dir, using the given dir as the root.

        Note: if the returned base dir is closed, the given dir will be deleted.

        Parameters:
        dir - the base dir root
        Returns:
        a new embedded base dir
      • dir

        static EphemeralBaseDir dir​(Path dir)
        Creates a new base dir, using the given dir as the root.

        Note: if the returned base dir is closed, the given dir will be deleted.

        Parameters:
        dir - the base dir root
        Returns:
        a new embedded base dir
      • tmpJar

        static EphemeralBaseDir tmpJar()
        Creates a new base dir which is actually a jar created within the JVM's assigned temp dir.

        This is typically used when testing Ratpack extensions to verify that they don't assume they are running from the default file system.

        Returns:
        a new embedded base dir
      • jar

        static EphemeralBaseDir jar​(File jarFile)
        Creates a new base dir which is actually a jar at the given location.

        This is typically used when testing Ratpack extensions to verify that they don't assume they are running from the default file system.

        The given file is expected to exist and be empty.

        Parameters:
        jarFile - the location of the jar to act as a base dir
        Returns:
        a new embedded base dir
      • closeOnExit

        default EphemeralBaseDir closeOnExit()
        Add's a JVM shutdown hook that will close() this base dir.
        Returns:
        this
      • use

        default void use​(Action<? super EphemeralBaseDir> action)
                  throws Exception
        Executes the given action with this base dir, then closes this base dir.
        Parameters:
        action - the action to execute before closing this base dir
        Throws:
        Exception - any thrown by action
      • path

        default Path path​(String path)
        Returns a path for the given path within the base dir.

        All parent directories will be created on demand.

        Parameters:
        path - the relative path to the path
        Returns:
        a path for the given path
      • write

        default Path write​(String path,
                           String content)
        Creates a file with the given string content at the given path within the base dir.

        All parent directories will be created on demand.

        Parameters:
        path - the relative path to the file to create
        content - the content to write to the file
        Returns:
        a path for the created file
      • mkdir

        default Path mkdir​(String path)
        Creates a directory at the given path within the base dir.

        All parent directories will be created on demand.

        Parameters:
        path - the relative path to the file to create
        Returns:
        a path for the created directory
      • getRoot

        Path getRoot()
        The root of the base dir.
        Returns:
        the root of the base dir