[BAEL-3601] - Fix code review comments.
This commit is contained in:
@@ -8,5 +8,4 @@ This module contains articles about core Java non-blocking input and output (IO)
|
||||
- [Create a Symbolic Link with Java](https://www.baeldung.com/java-symlink)
|
||||
- [Introduction to the Java NIO Selector](https://www.baeldung.com/java-nio-selector)
|
||||
- [Using Java MappedByteBuffer](https://www.baeldung.com/java-mapped-byte-buffer)
|
||||
- [How to Lock a File in Java](https://www.baeldung.com/how-to-lock-a-file-in-java)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-nio)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-nio)
|
||||
|
||||
+107
-207
File diff suppressed because it is too large
Load Diff
@@ -1,87 +0,0 @@
|
||||
package com.baeldung.lock;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
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 java.io.IOException;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.channels.NonReadableChannelException;
|
||||
import java.nio.channels.NonWritableChannelException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FileLocksTest {
|
||||
|
||||
// Exclusive locks
|
||||
/**
|
||||
* Getting an exclusive (write) lock on the entire file.
|
||||
* Fails when getting the lock from a FileChannel obtained through a FileInputStream.
|
||||
*/
|
||||
@Test
|
||||
void givenAnInputStream_whenGetWriteLock_throwNonWritableChannelException() {
|
||||
assertThrows(NonWritableChannelException.class,
|
||||
() -> FileLocks.getExclusiveLockFromInputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting an exclusive lock from a RandomAccessFile
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenARandomAccessFileWithReadWriteAccess_whenGetWriteLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getExclusiveLockFromRandomAccessFile(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertFalse(lock.isShared());
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting an exclusive lock from FileChannel::open
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenAPath_whenGetExclusiveLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getExclusiveLockFromFileChannelOpen(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertFalse(lock.isShared());
|
||||
}
|
||||
|
||||
|
||||
// Shared locks
|
||||
|
||||
/**
|
||||
* Getting a shared (read) lock on the entire file.
|
||||
* Fails when getting the lock from a FileChannel obtained through a FileOutputStream.
|
||||
*/
|
||||
@Test
|
||||
void givenAFileOutputStream_whenGetSharedLock_throwNonReadableChannelException() {
|
||||
assertThrows(NonReadableChannelException.class,
|
||||
() -> FileLocks.getReadLockFromOutputStream(0, 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting a shared (read) lock works fine when getting the lock from a FileChannel obtained through a FileInputStream.
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenAnInputStream_whenGetSharedLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getReadLockFromInputStream(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertTrue(lock.isShared());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting a shared lock from a RandomAccessFile
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenARandomAccessFile_whenGetSharedLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getReadLockFromRandomAccessFile(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertTrue(lock.isShared());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.lock;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
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 java.io.IOException;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.channels.NonReadableChannelException;
|
||||
import java.nio.channels.NonWritableChannelException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FileLocksUnitTest {
|
||||
|
||||
@Test void givenAnInputStream_whenGetWriteLock_throwNonWritableChannelException() {
|
||||
assertThrows(NonWritableChannelException.class, () -> FileLocks.getExclusiveLockFromInputStream());
|
||||
}
|
||||
|
||||
@Test void givenARandomAccessFileWithReadWriteAccess_whenGetWriteLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getExclusiveLockFromRandomAccessFile(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertFalse(lock.isShared());
|
||||
}
|
||||
|
||||
@Test void givenAPath_whenGetExclusiveLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getExclusiveLockFromFileChannelOpen(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertFalse(lock.isShared());
|
||||
}
|
||||
|
||||
@Test void givenAFileOutputStream_whenGetSharedLock_throwNonReadableChannelException() {
|
||||
assertThrows(NonReadableChannelException.class, FileLocks::getReadLockFromOutputStream);
|
||||
}
|
||||
|
||||
@Test void givenAnInputStream_whenGetSharedLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getReadLockFromInputStream(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertTrue(lock.isShared());
|
||||
}
|
||||
|
||||
@Test void givenARandomAccessFile_whenGetSharedLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getReadLockFromRandomAccessFile(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertTrue(lock.isShared());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user