use of java.nio.channels.NonWritableChannelException in project robovm by robovm.
the class NonWritableChannelExceptionTest method test_Constructor.
/**
* @tests {@link java.nio.channels.NonWritableChannelException#NonWritableChannelException()}
*/
public void test_Constructor() {
NonWritableChannelException e = new NonWritableChannelException();
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
use of java.nio.channels.NonWritableChannelException in project robovm by robovm.
the class FileChannelTest method test_writeLByteBuffer_ReadOnly.
/**
* @tests java.nio.channels.FileChannel#write(ByteBuffer)
*/
public void test_writeLByteBuffer_ReadOnly() throws Exception {
ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
try {
readOnlyFileChannel.write(writeBuffer);
fail("should throw NonWritableChannelException");
} catch (NonWritableChannelException e) {
// expected
}
// first throws NonWriteableChannelException
writeBuffer = null;
try {
readOnlyFileChannel.write(writeBuffer);
fail("should throw NonWritableChannelException");
} catch (NonWritableChannelException e) {
// expected
}
}
use of java.nio.channels.NonWritableChannelException in project robovm by robovm.
the class FileChannelLockingTest method test_tryLockLLZ.
public void test_tryLockLLZ() throws IOException {
// It is illegal to request an exclusive lock on a read-only channel
try {
readOnlyChannel.tryLock(0, 99, false);
fail("Acquiring exclusive lock on read-only channel should fail");
} catch (NonWritableChannelException ex) {
// Expected
}
// It is invalid to request a lock starting before the file start
try {
readOnlyChannel.tryLock(-99, 0, true);
fail("Acquiring an illegal lock value should fail.");
} catch (IllegalArgumentException ex) {
// expected
}
// Acquire a valid lock
FileLock tmpLock = readOnlyChannel.tryLock(0, 10, true);
assertTrue(tmpLock.isValid());
tmpLock.release();
// Acquire another valid lock -- and don't release it yet
FileLock lock = readOnlyChannel.tryLock(10, 788, true);
assertTrue(lock.isValid());
// Overlapping locks are illegal
try {
readOnlyChannel.tryLock(1, 23, true);
fail("Acquiring an overlapping lock should fail.");
} catch (OverlappingFileLockException ex) {
// Expected
}
// Adjacent locks are legal
FileLock adjacentLock = readOnlyChannel.tryLock(1, 3, true);
assertTrue(adjacentLock.isValid());
adjacentLock.release();
// Release longer lived lock
lock.release();
}
use of java.nio.channels.NonWritableChannelException in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testRegularFileAccessAndModifiedTimeUpdates.
@Test
public void testRegularFileAccessAndModifiedTimeUpdates() throws IOException {
Path foo = path("foo");
Files.createFile(foo);
FileTimeTester tester = new FileTimeTester(foo);
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try (FileChannel channel = FileChannel.open(foo, READ)) {
// opening READ channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.read(ByteBuffer.allocate(100));
// read call on channel does
tester.assertAccessTimeChanged();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.read(ByteBuffer.allocate(100));
tester.assertAccessTimeChanged();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try {
channel.write(ByteBuffer.wrap(new byte[] { 0, 1, 2, 3 }));
} catch (NonWritableChannelException ignore) {
}
// failed write on non-readable channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
}
// closing channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try (FileChannel channel = FileChannel.open(foo, WRITE)) {
// opening WRITE channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.write(ByteBuffer.wrap(new byte[] { 0, 1, 2, 3 }));
// write call on channel does
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.write(ByteBuffer.wrap(new byte[] { 4, 5, 6, 7 }));
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try {
channel.read(ByteBuffer.allocate(100));
} catch (NonReadableChannelException ignore) {
}
// failed read on non-readable channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
}
// closing channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
}
use of java.nio.channels.NonWritableChannelException in project j2objc by google.
the class OldRandomAccessFileTest method test_ConstructorLjava_lang_StringLjava_lang_String.
/**
* java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
* java.lang.String)
*/
public void test_ConstructorLjava_lang_StringLjava_lang_String() throws IOException {
RandomAccessFile raf = null;
File tmpFile = new File(fileName);
try {
raf = new java.io.RandomAccessFile(fileName, "r");
fail("Test 1: FileNotFoundException expected.");
} catch (FileNotFoundException e) {
// Expected.
} catch (IllegalArgumentException e) {
fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
}
try {
// Checking the remaining valid mode parameters.
try {
raf = new java.io.RandomAccessFile(fileName, "rwd");
} catch (IllegalArgumentException e) {
fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
}
raf.close();
try {
raf = new java.io.RandomAccessFile(fileName, "rws");
} catch (IllegalArgumentException e) {
fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
}
raf.close();
try {
raf = new java.io.RandomAccessFile(fileName, "rw");
} catch (IllegalArgumentException e) {
fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
}
raf.close();
// Checking an invalid mode parameter.
try {
raf = new java.io.RandomAccessFile(fileName, "i");
fail("Test 6: IllegalArgumentException expected.");
} catch (IllegalArgumentException e) {
// Expected.
}
// Checking for NoWritableChannelException.
raf = new java.io.RandomAccessFile(fileName, "r");
FileChannel fcr = raf.getChannel();
try {
fcr.lock(0L, Long.MAX_VALUE, false);
fail("Test 7: NonWritableChannelException expected.");
} catch (NonWritableChannelException e) {
// Expected.
}
} finally {
if (raf != null)
raf.close();
if (tmpFile.exists())
tmpFile.delete();
}
}
Aggregations