Skip to content

Commit

Permalink
use assertThrows
Browse files Browse the repository at this point in the history
  • Loading branch information
marschall committed Dec 29, 2023
1 parent f52e47c commit afc9aae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedWriter;
import java.io.IOException;
Expand Down Expand Up @@ -145,17 +144,9 @@ void overLappingLocking() throws IOException {
assertNull(secondChannel.tryLock(1L, 8L, true));
assertNull(secondChannel.tryLock(1L, 8L, false));

try {
secondChannel.lock(1L, 8L, true);
} catch (OverlappingFileLockException e) {
// should reach here
}
assertThrows(OverlappingFileLockException.class, () -> secondChannel.lock(1L, 8L, true));

try {
secondChannel.lock(1L, 8L, false);
} catch (OverlappingFileLockException e) {
// should reach here
}
assertThrows(OverlappingFileLockException.class, () -> secondChannel.lock(1L, 8L, false));

FileLock secondLock = secondChannel.lock(0L, 2L, true);
assertNotNull(secondLock);
Expand Down Expand Up @@ -2033,11 +2024,7 @@ void unsupportedViews() {
void channelWriteTruncateExisting() throws IOException {
Path file = this.extension.getFileSystem().getPath("/file.txt");
Files.createFile(file);
try (SeekableByteChannel channel = Files.newByteChannel(file, WRITE, APPEND, TRUNCATE_EXISTING)) {
fail("APPEND and TRUNCATE_EXISTING and should not work");
} catch (IllegalArgumentException e) {
// should reach here
}
assertThrows(IllegalArgumentException.class, () -> Files.newByteChannel(file, WRITE, APPEND, TRUNCATE_EXISTING), "APPEND and TRUNCATE_EXISTING and should not work");
}

@Test
Expand All @@ -2053,11 +2040,7 @@ void channelNormalizePath() throws IOException {
void outputStreamWriteTruncateExisting() throws IOException {
Path file = this.extension.getFileSystem().getPath("/file.txt");
Files.createFile(file);
try (OutputStream outputStream = Files.newOutputStream(file, APPEND, TRUNCATE_EXISTING)) {
fail("APPEND and TRUNCATE_EXISTING and should not work");
} catch (IllegalArgumentException e) {
// should reach here
}
assertThrows(IllegalArgumentException.class, () -> Files.newOutputStream(file, APPEND, TRUNCATE_EXISTING), "APPEND and TRUNCATE_EXISTING and should not work");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.nio.channels.ByteChannel;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
Expand Down Expand Up @@ -123,27 +122,14 @@ void jdk8066915() throws IOException {
Path directory = fileSystem.getPath("directory");
Files.createDirectory(directory);

try (ByteChannel channel = Files.newByteChannel(directory)) {
fail("should not be able to create channel on directory");
} catch (FileSystemException e) {
// should reach here
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}

try (ByteChannel channel = Files.newByteChannel(directory, READ)) {
fail("should not be able to create channel on directory");

} catch (FileSystemException e) {
// should reach here
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}

try (ByteChannel channel = Files.newByteChannel(directory, WRITE)) {
fail("should not be able to create channel on directory");
} catch (FileSystemException e) {
// should reach here
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}
FileSystemException e = assertThrows(FileSystemException.class, () -> Files.newByteChannel(directory), "should not be able to create channel on directory");
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");

e = assertThrows(FileSystemException.class, () -> Files.newByteChannel(directory, READ), "should not be able to create channel on directory");
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");

e = assertThrows(FileSystemException.class, () -> Files.newByteChannel(directory, WRITE), "should not be able to create channel on directory");
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.AccessMode;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -61,11 +59,7 @@ void directoryRead() throws IOException {
Files.setAttribute(directory, "posix:permissions", asSet(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, OTHERS_READ, OTHERS_WRITE));
UserPrincipal user = fileSystem.getUserPrincipalLookupService().lookupPrincipalByName(PosixPermissionFileSystemExtension.OTHER);
CurrentUser.useDuring(user, () -> {
try (DirectoryStream<?> stream = Files.newDirectoryStream(directory)) {
fail("should not be allowd to open a directory stram");
} catch (AccessDeniedException e) {
// should reach here
}
assertThrows(AccessDeniedException.class, () -> Files.newDirectoryStream(directory), "should not be allowd to open a directory stram");
});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.net.URI;
import java.nio.channels.ByteChannel;
import java.nio.file.AccessDeniedException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
Expand Down Expand Up @@ -336,27 +334,14 @@ void jdk8066915() throws IOException {
Path directory = fileSystem.getPath("directory");
Files.createDirectory(directory);

try (ByteChannel channel = Files.newByteChannel(directory)) {
fail("should not be able to create channel on directory");
} catch (FileSystemException e) {
// should reach here
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}

try (ByteChannel channel = Files.newByteChannel(directory, StandardOpenOption.READ)) {
fail("should not be able to create channel on directory");
FileSystemException e = assertThrows(FileSystemException.class, () -> Files.newByteChannel(directory), "should not be able to create channel on directory");
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");

} catch (FileSystemException e) {
// should reach here
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}
e = assertThrows(FileSystemException.class, () -> Files.newByteChannel(directory, StandardOpenOption.READ), "should not be able to create channel on directory");
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");

try (ByteChannel channel = Files.newByteChannel(directory, StandardOpenOption.WRITE)) {
fail("should not be able to create channel on directory");
} catch (FileSystemException e) {
// should reach here
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}
e = assertThrows(FileSystemException.class, () -> Files.newByteChannel(directory, StandardOpenOption.WRITE), "should not be able to create channel on directory");
assertEquals(directory.toAbsolutePath().toString(), e.getFile(), "file");
}

// https://bugs.openjdk.java.net/browse/JDK-8072495
Expand Down

0 comments on commit afc9aae

Please sign in to comment.