From a2372358040d586e1a3cccf33fe151e6c5c566fb Mon Sep 17 00:00:00 2001 From: Rui Vilao Date: Wed, 26 Aug 2020 13:18:37 +0100 Subject: [PATCH] BAEL-4132: How to create a temporary directory/folder in Java? --- .../TemporaryDirectoriesUnitTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 core-java-modules/core-java-io-3/src/test/java/com/baeldung/tempdirectory/TemporaryDirectoriesUnitTest.java diff --git a/core-java-modules/core-java-io-3/src/test/java/com/baeldung/tempdirectory/TemporaryDirectoriesUnitTest.java b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/tempdirectory/TemporaryDirectoriesUnitTest.java new file mode 100644 index 0000000000..470e06ef96 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/tempdirectory/TemporaryDirectoriesUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.tempdirectory; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.PosixFileAttributes; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.Set; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * Tests several possibilities on how to create + * temporary directories. + * + * @author Rui Vilao (rpvilao@gmail.com) + */ +public class TemporaryDirectoriesUnitTest { + + @Test + public void givenTempDirWithPrefixNoTargetSpecified_whenCreateWithPlainJava_thenInsideOSTempDirStructure() throws IOException { + final String tmpdir = Files.createTempDirectory("tmpDirPrefix").toFile().getAbsolutePath(); + final String tmpDirsLocation = System.getProperty("java.io.tmpdir"); + + assertThat(tmpdir).startsWith(tmpDirsLocation); + } + + @Test + public void givenTempDirWithPrefixNoTargetSpecified_whenCreateWithGuava_thenInsideOSTempDirStructure() throws IOException { + final String tmpdir = com.google.common.io.Files.createTempDir().getAbsolutePath(); + final String tmpDirsLocation = System.getProperty("java.io.tmpdir"); + + assertThat(tmpdir).startsWith(tmpDirsLocation); + } + + @Test + public void givenTempDirWithPrefixNoTargetSpecified_whenCreateWithApacheCommonsIo_thenInsideOSTempDirStructure() throws IOException { + final String tmpDirsLocation = System.getProperty("java.io.tmpdir"); + final Path path = Paths.get(FileUtils.getTempDirectory().getAbsolutePath(), UUID.randomUUID().toString()); + final String tmpdir = Files.createDirectories(path).toFile().getAbsolutePath(); + + assertThat(tmpdir).startsWith(tmpDirsLocation); + } + + @Test + public void givenTempDirWithPrefixWithTargetSpecified_whenCreatePlainJava_thenInsideTarget() throws IOException { + final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix"); + assertThat(tmpdir.toFile().getPath()).startsWith("target"); + } + + @Test + public void givenTempDirWithPrefixWithTargetSpecifiedWithDeleteOnExit_whenCreatePlainJava_thenInsideTarget() throws IOException { + final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix"); + assertThat(tmpdir.toFile().getPath()).startsWith("target"); + tmpdir.toFile().deleteOnExit(); + // we can really assert this test, just as an example. + } + + @Test + public void givenTempDirWithPrefixWithFileAttrs_whenCreatePlainJava_thenAttributesAreSet() throws IOException { + final FileAttribute> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------")); + + final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs); + assertThat(tmpdir.toFile().getPath()).startsWith("target"); + assertThat(tmpdir.toFile().canWrite()).isFalse(); + } +}