Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1022 TempFolder #1049

Merged
merged 5 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/main/java/org/cactoos/io/TempFolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
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.Randomized;
import org.cactoos.text.TextOf;

/**
* A temporary folder.
* This is ephemeral folder to be used in small scopes.
* The physical folder is deleted from the filesystem when the temp folder is
* closed.
* @since 1.0
*/
public final class TempFolder implements Scalar<Path>, Closeable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Iprogrammerr Please, create a new secondary ctor which create a TempFolder with a random name by default. This name should start with tmp prefix and more 5 random chars (it sholdn't exceed 8 chars size).


/**
* Creates the temporary folder, returning its path.
*/
private final Scalar<Path> folder;

/**
* Ctor.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Iprogrammerr Create a new secondary ctor which signature should be TempFolder(final String path).

* Creates new folder in temporary directory
* with a random name.
* @since 1.0
*/
public TempFolder() {
this(
new JoinedText(
new TextOf(""),
new TextOf("tmp"),
new Randomized(
// @checkstyle MagicNumber (1 line)
5,
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
)
)
);
}

/**
* Ctor.
* Creates new folder in temporary directory.
* @param path Relative path to new directory.
* @since 1.0
*/
public TempFolder(final String path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Iprogrammerr This ctor should be final Text 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.asString()
).asString()
)
)
)
);
}

/**
* Primary ctor.
* @param flr Creates the folder and returns the path to it
* @since 1.0
*/
private TempFolder(final Scalar<Path> flr) {
this.folder = flr;
}

@Override
public Path value() throws Exception {
return this.folder.value();
}

@Override
public void close() throws IOException {
Files.delete(new IoCheckedScalar<>(this).value());
}
}
64 changes: 64 additions & 0 deletions src/test/java/org/cactoos/io/TempFolderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.io.File;
import org.cactoos.text.Randomized;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsTrue;

/**
* Test case for {@link TempFolder}.
*
* @since 1.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class TempFolderTest {

@Test
public void createsDirectory() {
new Assertion<>(
"Can not create new directory",
() -> {
final File dir = new TempFolder().value().toFile();
return dir.exists() && dir.isDirectory();
},
new IsTrue()
).affirm();
}

@Test
public void deletesDirectory() throws Exception {
final TempFolder dir = new TempFolder(
new Randomized('d', 'e', 'g').asString()
);
dir.close();
new Assertion<>(
"Can't delete folder while closing",
() -> !dir.value().toFile().exists(),
new IsTrue()
).affirm();
}
}