JAVA-2599: Update OS specific unit tests

This commit is contained in:
Krzysztof Woyke 2020-09-15 10:28:34 +02:00
parent 89f0f75667
commit 4f14873967
3 changed files with 15 additions and 6 deletions

View File

@ -9,4 +9,5 @@ This module contains articles about core Java input and output (IO)
- [Check If a File or Directory Exists in Java](https://www.baeldung.com/java-file-directory-exists)
- [Copy a Directory in Java](https://www.baeldung.com/java-copy-directory)
- [Java Files Open Options](https://www.baeldung.com/java-file-options)
- [Creating Temporary Directories in Java](https://www.baeldung.com/java-temp-directories)
- [[<-- Prev]](/core-java-modules/core-java-io-2)

View File

@ -4,6 +4,7 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
@ -20,8 +21,8 @@ public class DirectoryEmptinessUnitTest {
}
@Test
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException {
Path aFile = Paths.get(getClass().getResource("/notDir.txt").getPath());
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException, URISyntaxException {
Path aFile = Paths.get(getClass().getResource("/notDir.txt").toURI());
assertThat(isEmpty(aFile)).isFalse();
}

View File

@ -4,6 +4,7 @@ import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -66,10 +67,16 @@ public class TemporaryDirectoriesUnitTest {
@Test
public void givenTempDirWithPrefixWithFileAttrs_whenCreatePlainJava_thenAttributesAreSet() throws IOException {
final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------"));
boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs);
assertThat(tmpdir.toFile().getPath()).startsWith("target");
assertThat(tmpdir.toFile().canWrite()).isFalse();
if(!isPosix){
System.out.println("You must be under a Posix Compliant Filesystem to run this test.");
} else {
final FileAttribute<Set<PosixFilePermission>> 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();
}
}
}