Skip to content

Commit

Permalink
(yegor256#1022) Refactored TempFolder with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igor committed Jan 29, 2019
1 parent 05faf1d commit f4dd94c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/main/java/org/cactoos/io/TempFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -48,21 +51,48 @@ public final class TempFolder implements Scalar<Path>, Closeable {
*/
private final Scalar<Path> 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.
* @param path Relative path to new directory.
* @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(
Paths.get(
new JoinedText(
File.separator,
System.getProperty("java.io.tmpdir"),
path
path.asString()
).asString()
)
)
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/org/cactoos/io/TempFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f4dd94c

Please sign in to comment.