diff --git a/src/main/java/org/cactoos/io/TempFolder.java b/src/main/java/org/cactoos/io/TempFolder.java index 2c826259ef..7a6938cb77 100644 --- a/src/main/java/org/cactoos/io/TempFolder.java +++ b/src/main/java/org/cactoos/io/TempFolder.java @@ -30,9 +30,12 @@ import java.nio.file.Path; import java.nio.file.Paths; import org.cactoos.Scalar; +import org.cactoos.Text; import org.cactoos.scalar.IoCheckedScalar; import org.cactoos.scalar.StickyScalar; import org.cactoos.text.JoinedText; +import org.cactoos.text.RandomText; +import org.cactoos.text.TextOf; /** * A temporary folder. @@ -48,6 +51,23 @@ public final class TempFolder implements Scalar, Closeable { */ private final Scalar folder; + /** + * Ctor. + * Creates new folder in temporary directory + * with a random name. + * @since 1.0 + */ + public TempFolder() { + this( + new JoinedText( + new TextOf(""), + new TextOf("tmp"), + // @checkstyle MagicNumber (1 line) + new RandomText(5, 'a', 'b', 'c') + ) + ); + } + /** * Ctor. * Creates new folder in temporary directory. @@ -55,6 +75,16 @@ public final class TempFolder implements Scalar, Closeable { * @since 1.0 */ public TempFolder(final String path) { + this(new TextOf(path)); + } + + /** + * Ctor. + * Creates new folder in temporary directory. + * @param path Relative path to new directory. + * @since 1.0 + */ + public TempFolder(final Text path) { this( new StickyScalar<>( () -> Files.createDirectory( @@ -62,7 +92,7 @@ public TempFolder(final String path) { new JoinedText( File.separator, System.getProperty("java.io.tmpdir"), - path + path.asString() ).asString() ) ) diff --git a/src/test/java/org/cactoos/io/TempFolderTest.java b/src/test/java/org/cactoos/io/TempFolderTest.java index 37a7bd6d81..3709cc8741 100644 --- a/src/test/java/org/cactoos/io/TempFolderTest.java +++ b/src/test/java/org/cactoos/io/TempFolderTest.java @@ -42,9 +42,7 @@ public void createsDirectory() { new Assertion<>( "Can not create new directory", () -> { - final File dir = new TempFolder( - new RandomText('a', 'b', 'c').asString() - ).value().toFile(); + final File dir = new TempFolder().value().toFile(); return dir.exists() && dir.isDirectory(); }, new IsTrue()