Interface EphemeralBaseDir

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface EphemeralBaseDir
    extends java.lang.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 Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void close()
      Deletes the base dir from the file system.
      default EphemeralBaseDir closeOnExit()
      Add's a JVM shutdown hook that will close() this base dir.
      static EphemeralBaseDir dir​(java.io.File dir)
      Creates a new base dir, using the given dir as the root.
      java.nio.file.Path getRoot()
      The root of the base dir.
      static EphemeralBaseDir jar​(java.io.File jarFile)
      Creates a new base dir which is actually a jar at the given location.
      default java.nio.file.Path mkdir​(java.lang.String path)
      Creates a directory at the given path within the base dir.
      default java.nio.file.Path path​(java.lang.String path)
      Returns a path for the given path within the base dir.
      static EphemeralBaseDir tmpDir()
      Creates a new base dir, using a newly created dir within the JVM's assigned temp dir.
      static EphemeralBaseDir tmpJar()
      Creates a new base dir which is actually a jar created within the JVM's assigned temp dir.
      default void use​(ratpack.func.Action<? super EphemeralBaseDir> action)
      Executes the given action with this base dir, then closes this base dir.
      default java.nio.file.Path write​(java.lang.String path, java.lang.String content)
      Creates a file with the given string content at the given path within the base dir.
    • 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​(java.io.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
      • 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​(java.io.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​(ratpack.func.Action<? super EphemeralBaseDir> action)
                  throws java.lang.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:
        java.lang.Exception - any thrown by action
      • path

        default java.nio.file.Path path​(java.lang.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 java.nio.file.Path write​(java.lang.String path,
                                         java.lang.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 java.nio.file.Path mkdir​(java.lang.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

        java.nio.file.Path getRoot()
        The root of the base dir.
        Returns:
        the root of the base dir
      • close

        void close()
            throws java.io.IOException
        Deletes the base dir from the file system.
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException - if the base dir cannot be deleted.