public interface EphemeralBaseDir extends AutoCloseable
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.
Modifier and Type | Method and 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(File dir)
Creates a new base dir, using the given dir as the root.
|
Path |
getRoot()
The root of the base dir.
|
static EphemeralBaseDir |
jar(File jarFile)
Creates a new base dir which is actually a jar at the given location.
|
default Path |
mkdir(String path)
Creates a directory at the given path within the base dir.
|
default Path |
path(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(Action<? super EphemeralBaseDir> action)
Executes the given action with this base dir, then closes this base dir.
|
default Path |
write(String path,
String content)
Creates a file with the given string content at the given path within the base dir.
|
static EphemeralBaseDir tmpDir()
static EphemeralBaseDir dir(File dir)
Note: if the returned base dir is closed, the given dir will be deleted.
dir
- the base dir rootstatic EphemeralBaseDir tmpJar()
This is typically used when testing Ratpack extensions to verify that they don't assume they are running from the default file system.
static EphemeralBaseDir jar(File jarFile)
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.
jarFile
- the location of the jar to act as a base dirdefault EphemeralBaseDir closeOnExit()
close()
this base dir.this
default void use(Action<? super EphemeralBaseDir> action) throws Exception
action
- the action to execute before closing this base dirException
- any thrown by action
default Path path(String path)
All parent directories will be created on demand.
path
- the relative path to the pathdefault Path write(String path, String content)
All parent directories will be created on demand.
path
- the relative path to the file to createcontent
- the content to write to the filedefault Path mkdir(String path)
All parent directories will be created on demand.
path
- the relative path to the file to createPath getRoot()
void close() throws IOException
close
in interface AutoCloseable
IOException
- if the base dir cannot be deleted.