/external/apache-harmony/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/FileChannelTest.java
https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 1520 lines · 1026 code · 192 blank · 302 comment · 37 complexity · b310b0ae3fca48bd69fd7eb5f06f3cdc MD5 · raw file
- /* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.apache.harmony.nio.tests.java.nio.channels;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.io.UnsupportedEncodingException;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.nio.BufferOverflowException;
- import java.nio.ByteBuffer;
- import java.nio.MappedByteBuffer;
- import java.nio.ReadOnlyBufferException;
- import java.nio.channels.ClosedChannelException;
- import java.nio.channels.DatagramChannel;
- import java.nio.channels.FileChannel;
- import java.nio.channels.FileLock;
- import java.nio.channels.NonReadableChannelException;
- import java.nio.channels.NonWritableChannelException;
- import java.nio.channels.OverlappingFileLockException;
- import java.nio.channels.Pipe;
- import java.nio.channels.ReadableByteChannel;
- import java.nio.channels.ServerSocketChannel;
- import java.nio.channels.SocketChannel;
- import java.nio.channels.WritableByteChannel;
- import java.nio.channels.FileChannel.MapMode;
- import java.util.Arrays;
- import junit.framework.TestCase;
- public class FileChannelTest extends TestCase {
- private static final int CAPACITY = 100;
- private static final int LIMITED_CAPACITY = 2;
- private static final int TIME_OUT = 10000;
- private static final String CONTENT = "MYTESTSTRING needs to be a little long";
- private static final byte[] TEST_BYTES;
- private static final byte[] CONTENT_AS_BYTES;
- private static final int CONTENT_AS_BYTES_LENGTH;
- static {
- try {
- TEST_BYTES = "test".getBytes("iso8859-1");
- CONTENT_AS_BYTES = CONTENT.getBytes("iso8859-1");
- CONTENT_AS_BYTES_LENGTH = CONTENT_AS_BYTES.length;
- } catch (UnsupportedEncodingException e) {
- throw new Error(e);
- }
- }
- private static final int CONTENT_LENGTH = CONTENT.length();
- private FileChannel readOnlyFileChannel;
- private FileChannel writeOnlyFileChannel;
- private FileChannel readWriteFileChannel;
- private File fileOfReadOnlyFileChannel;
- private File fileOfWriteOnlyFileChannel;
- private File fileOfReadWriteFileChannel;
- private ReadableByteChannel readByteChannel;
- private WritableByteChannel writableByteChannel;
- private DatagramChannel datagramChannelSender;
- private DatagramChannel datagramChannelReceiver;
- private ServerSocketChannel serverSocketChannel;
- private SocketChannel socketChannelSender;
- private SocketChannel socketChannelReceiver;
- private Pipe pipe;
- // to read content from FileChannel
- private FileInputStream fis;
- private FileLock fileLock;
- protected void setUp() throws Exception {
- fileOfReadOnlyFileChannel = File.createTempFile(
- "File_of_readOnlyFileChannel", "tmp");
- fileOfReadOnlyFileChannel.deleteOnExit();
- fileOfWriteOnlyFileChannel = File.createTempFile(
- "File_of_writeOnlyFileChannel", "tmp");
- fileOfWriteOnlyFileChannel.deleteOnExit();
- fileOfReadWriteFileChannel = File.createTempFile(
- "File_of_readWriteFileChannel", "tmp");
- fileOfReadWriteFileChannel.deleteOnExit();
- fis = null;
- fileLock = null;
- readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel)
- .getChannel();
- writeOnlyFileChannel = new FileOutputStream(fileOfWriteOnlyFileChannel)
- .getChannel();
- readWriteFileChannel = new RandomAccessFile(fileOfReadWriteFileChannel,
- "rw").getChannel();
- }
- protected void tearDown() {
- if (null != readOnlyFileChannel) {
- try {
- readOnlyFileChannel.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != writeOnlyFileChannel) {
- try {
- writeOnlyFileChannel.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != readWriteFileChannel) {
- try {
- readWriteFileChannel.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != fis) {
- try {
- fis.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != fileLock) {
- try {
- fileLock.release();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != fileOfReadOnlyFileChannel) {
- fileOfReadOnlyFileChannel.delete();
- }
- if (null != fileOfWriteOnlyFileChannel) {
- fileOfWriteOnlyFileChannel.delete();
- }
- if (null != fileOfReadWriteFileChannel) {
- fileOfReadWriteFileChannel.delete();
- }
- if (null != datagramChannelSender) {
- try {
- datagramChannelSender.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != datagramChannelReceiver) {
- try {
- datagramChannelReceiver.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != serverSocketChannel) {
- try {
- serverSocketChannel.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != socketChannelSender) {
- try {
- socketChannelSender.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != socketChannelReceiver) {
- try {
- socketChannelReceiver.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != pipe) {
- if (null != pipe.source()) {
- try {
- pipe.source().close();
- } catch (IOException e) {
- // do nothing
- }
- }
- if (null != pipe.sink()) {
- try {
- pipe.sink().close();
- } catch (IOException e) {
- // do nothing
- }
- }
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#force(boolean)
- */
- public void test_forceJ() throws Exception {
- ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
- writeOnlyFileChannel.write(writeBuffer);
- writeOnlyFileChannel.force(true);
- byte[] readBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
- fis = new FileInputStream(fileOfWriteOnlyFileChannel);
- fis.read(readBuffer);
- assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer));
- }
- /**
- * @tests java.nio.channels.FileChannel#force(boolean)
- */
- public void test_forceJ_closed() throws Exception {
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.force(true);
- fail();
- } catch (ClosedChannelException expected) {
- }
- try {
- writeOnlyFileChannel.force(false);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#force(boolean)
- */
- public void test_forceJ_ReadOnlyChannel() throws Exception {
- // force on a read only file channel has no effect.
- readOnlyFileChannel.force(true);
- readOnlyFileChannel.force(false);
- }
- /**
- * @tests java.nio.channels.FileChannel#position()
- */
- public void test_position_Init() throws Exception {
- assertEquals(0, readOnlyFileChannel.position());
- assertEquals(0, writeOnlyFileChannel.position());
- assertEquals(0, readWriteFileChannel.position());
- }
- /**
- * @tests java.nio.channels.FileChannel#position()
- */
- public void test_position_ReadOnly() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- assertEquals(0, readOnlyFileChannel.position());
- ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
- readOnlyFileChannel.read(readBuffer);
- assertEquals(CONTENT_LENGTH, readOnlyFileChannel.position());
- }
- /**
- * Initializes test file.
- *
- * @param file
- * @throws FileNotFoundException
- * @throws IOException
- */
- private void writeDataToFile(File file) throws FileNotFoundException,
- IOException {
- FileOutputStream fos = new FileOutputStream(file);
- try {
- fos.write(CONTENT_AS_BYTES);
- } finally {
- fos.close();
- }
- }
- /**
- * Initializes large test file.
- *
- * @param file the file to be written
- * @param size the content size to be written
- * @throws FileNotFoundException
- * @throws IOException
- */
- private void writeLargeDataToFile(File file, int size) throws FileNotFoundException,
- IOException {
- FileOutputStream fos = new FileOutputStream(file);
- byte[] buf = new byte[size];
- try {
- // we don't care about content - just need a particular file size
- fos.write(buf);
- } finally {
- fos.close();
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#position()
- */
- public void test_position_WriteOnly() throws Exception {
- ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
- writeOnlyFileChannel.write(writeBuffer);
- assertEquals(CONTENT_LENGTH, writeOnlyFileChannel.position());
- }
- /**
- * @tests java.nio.channels.FileChannel#position()
- */
- public void test_position_ReadWrite() throws Exception {
- writeDataToFile(fileOfReadWriteFileChannel);
- assertEquals(0, readWriteFileChannel.position());
- ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
- readWriteFileChannel.read(readBuffer);
- assertEquals(CONTENT_LENGTH, readWriteFileChannel.position());
- ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
- readWriteFileChannel.write(writeBuffer);
- assertEquals(CONTENT_LENGTH * 2, readWriteFileChannel.position());
- }
- /**
- * @tests java.nio.channels.FileChannel#position()
- */
- public void test_position_Closed() throws Exception {
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.position();
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException expected) {
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.position();
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException expected) {
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.position();
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException expected) {
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#position(long)
- */
- public void test_positionJ_Closed() throws Exception {
- final long POSITION = 100;
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.position(POSITION);
- fail();
- } catch (ClosedChannelException expected) {
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.position(POSITION);
- fail();
- } catch (ClosedChannelException expected) {
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.position(POSITION);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#position(long)
- */
- public void test_positionJ_Negative() throws Exception {
- final long NEGATIVE_POSITION = -1;
- try {
- readOnlyFileChannel.position(NEGATIVE_POSITION);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- writeOnlyFileChannel.position(NEGATIVE_POSITION);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- readWriteFileChannel.position(NEGATIVE_POSITION);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#position(long)
- */
- public void test_positionJ_ReadOnly() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- // set the position of the read only file channel to POSITION
- final int POSITION = 4;
- readOnlyFileChannel.position(POSITION);
- // reads the content left to readBuffer through read only file channel
- ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
- int count = readOnlyFileChannel.read(readBuffer);
- assertEquals(CONTENT_LENGTH - POSITION, count);
- // asserts the content read is the part which stays beyond the POSITION
- readBuffer.flip();
- int i = POSITION;
- while (readBuffer.hasRemaining()) {
- assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
- i++;
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#position(long)
- */
- public void test_positionJ_WriteOnly() throws Exception {
- writeDataToFile(fileOfWriteOnlyFileChannel);
- // init data to write
- ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
- // set the position of the write only file channel to POSITION
- final int POSITION = 4;
- writeOnlyFileChannel.position(POSITION);
- // writes to the write only file channel
- writeOnlyFileChannel.write(writeBuffer);
- // force to write out.
- writeOnlyFileChannel.close();
- // gets the result of the write only file channel
- byte[] result = new byte[POSITION + CONTENT_LENGTH];
- fis = new FileInputStream(fileOfWriteOnlyFileChannel);
- fis.read(result);
- // constructs the expected result which has content[0... POSITION] plus
- // content[0...length()]
- byte[] expectedResult = new byte[POSITION + CONTENT_LENGTH];
- System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, POSITION);
- System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, POSITION,
- CONTENT_LENGTH);
- // asserts result of the write only file channel same as expected
- assertTrue(Arrays.equals(expectedResult, result));
- }
- /**
- * @tests java.nio.channels.FileChannel#size()
- */
- public void test_size_Init() throws Exception {
- assertEquals(0, readOnlyFileChannel.size());
- assertEquals(0, writeOnlyFileChannel.size());
- assertEquals(0, readWriteFileChannel.size());
- }
- /**
- * @tests java.nio.channels.FileChannel#size()
- */
- public void test_size() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- assertEquals(fileOfReadOnlyFileChannel.length(), readOnlyFileChannel
- .size());
- // REGRESSION test for read(ByteBuffer[], int, int) on special files
- try {
- FileChannel specialFile =
- new FileInputStream("/dev/zero").getChannel();
- assertEquals(0, specialFile.size());
- ByteBuffer buf = ByteBuffer.allocate(8);
- assertEquals(8, specialFile.read(buf));
- ByteBuffer[] bufs = { ByteBuffer.allocate(8) };
- assertEquals(8, specialFile.read(bufs, 0, 1));
- specialFile.close();
- } catch (FileNotFoundException e) {
- // skip test if special file doesn't exist
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#size()
- */
- public void test_size_Closed() throws Exception {
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.size();
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.size();
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.size();
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#truncate(long)
- */
- public void test_truncateJ_Closed() throws Exception {
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.truncate(0);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.truncate(0);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.truncate(-1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#truncate(long)
- */
- public void test_truncateJ_IllegalArgument() throws Exception {
- // regression test for Harmony-941
- try {
- readOnlyFileChannel.truncate(-1);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- writeOnlyFileChannel.truncate(-1);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- readWriteFileChannel.truncate(-1);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#truncate(long)
- */
- public void test_truncateJ_ReadOnly() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- try {
- readOnlyFileChannel.truncate(readOnlyFileChannel.size());
- fail("should throw NonWritableChannelException.");
- } catch (NonWritableChannelException e) {
- // expected
- }
- try {
- readOnlyFileChannel.truncate(0);
- fail("should throw NonWritableChannelException.");
- } catch (NonWritableChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#truncate(long)
- */
- public void test_truncateJ() throws Exception {
- writeDataToFile(fileOfReadWriteFileChannel);
- int truncateLength = CONTENT_LENGTH + 2;
- assertEquals(readWriteFileChannel, readWriteFileChannel
- .truncate(truncateLength));
- assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
- truncateLength = CONTENT_LENGTH;
- assertEquals(readWriteFileChannel, readWriteFileChannel
- .truncate(truncateLength));
- assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
- truncateLength = CONTENT_LENGTH / 2;
- assertEquals(readWriteFileChannel, readWriteFileChannel
- .truncate(truncateLength));
- assertEquals(truncateLength, fileOfReadWriteFileChannel.length());
- }
- /**
- * @tests java.nio.channels.FileChannel#lock()
- */
- public void test_lock() throws Exception {
- MockFileChannel mockFileChannel = new MockFileChannel();
- // Verify that calling lock() leads to the method
- // lock(long, long, boolean) being called with a 0 for the
- // first parameter, Long.MAX_VALUE as the second parameter and false
- // as the third parameter.
- mockFileChannel.lock();
- assertTrue(mockFileChannel.isLockCalled);
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_Closed() throws Exception {
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.lock(0, 10, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.lock(0, 10, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.lock(0, 10, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- // throws ClosedChannelException before IllegalArgumentException
- try {
- readWriteFileChannel.lock(-1, 0, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_IllegalArgument() throws Exception {
- try {
- writeOnlyFileChannel.lock(0, -1, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- writeOnlyFileChannel.lock(-1, 0, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- readWriteFileChannel.lock(-1, -1, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- readWriteFileChannel.lock(Long.MAX_VALUE, 1, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_NonWritable() throws Exception {
- try {
- readOnlyFileChannel.lock(0, 10, false);
- fail("should throw NonWritableChannelException");
- } catch (NonWritableChannelException e) {
- // expected
- }
- // throws NonWritableChannelException before IllegalArgumentException
- try {
- readOnlyFileChannel.lock(-1, 0, false);
- fail("should throw NonWritableChannelException");
- } catch (NonWritableChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_NonReadable() throws Exception {
- try {
- writeOnlyFileChannel.lock(0, 10, true);
- fail("should throw NonReadableChannelException");
- } catch (NonReadableChannelException e) {
- // expected
- }
- // throws NonReadableChannelException before IllegalArgumentException
- try {
- writeOnlyFileChannel.lock(-1, 0, true);
- fail("should throw NonReadableChannelException");
- } catch (NonReadableChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_Shared() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true);
- assertTrue(fileLock.isValid());
- // fileLock.isShared depends on whether the underlying platform support
- // shared lock, but it works on Windows & Linux.
- assertTrue(fileLock.isShared());
- assertSame(readOnlyFileChannel, fileLock.channel());
- assertEquals(POSITION, fileLock.position());
- assertEquals(SIZE, fileLock.size());
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_NotShared() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
- assertTrue(fileLock.isValid());
- assertFalse(fileLock.isShared());
- assertSame(writeOnlyFileChannel, fileLock.channel());
- assertEquals(POSITION, fileLock.position());
- assertEquals(SIZE, fileLock.size());
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_Long_MAX_VALUE() throws Exception {
- final long POSITION = 0;
- final long SIZE = Long.MAX_VALUE;
- fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true);
- assertTrue(fileLock.isValid());
- assertTrue(fileLock.isShared());
- assertEquals(POSITION, fileLock.position());
- assertEquals(SIZE, fileLock.size());
- assertSame(readOnlyFileChannel, fileLock.channel());
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_Overlapping() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
- assertTrue(fileLock.isValid());
- try {
- writeOnlyFileChannel.lock(POSITION + 1, SIZE, false);
- fail("should throw OverlappingFileLockException");
- } catch (OverlappingFileLockException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
- */
- public void test_lockJJZ_NotOverlapping() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- FileLock fileLock1 = writeOnlyFileChannel.lock(POSITION, SIZE, false);
- assertTrue(fileLock1.isValid());
- FileLock fileLock2 = writeOnlyFileChannel.lock(POSITION + SIZE, SIZE,
- false);
- assertTrue(fileLock2.isValid());
- }
- /**
- * @tests java.nio.channels.FileChannel#lock(long,long,boolean)
- */
- public void test_lockJJZ_After_Release() throws Exception {
- fileLock = writeOnlyFileChannel.lock(0, 10, false);
- fileLock.release();
- // after release file lock can be obtained again.
- fileLock = writeOnlyFileChannel.lock(0, 10, false);
- assertTrue(fileLock.isValid());
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock()
- */
- public void test_tryLock() throws Exception {
- MockFileChannel mockFileChannel = new MockFileChannel();
- // Verify that calling tryLock() leads to the method
- // tryLock(long, long, boolean) being called with a 0 for the
- // first parameter, Long.MAX_VALUE as the second parameter and false
- // as the third parameter.
- mockFileChannel.tryLock();
- assertTrue(mockFileChannel.isTryLockCalled);
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_Closed() throws Exception {
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.tryLock(0, 10, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.tryLock(0, 10, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.tryLock(0, 10, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- // throws ClosedChannelException before IllegalArgumentException
- try {
- readWriteFileChannel.tryLock(-1, 0, false);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_IllegalArgument() throws Exception {
- try {
- writeOnlyFileChannel.tryLock(0, -1, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- writeOnlyFileChannel.tryLock(-1, 0, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- readWriteFileChannel.tryLock(-1, -1, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- readWriteFileChannel.tryLock(Long.MAX_VALUE, 1, false);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_NonWritable() throws Exception {
- try {
- readOnlyFileChannel.tryLock(0, 10, false);
- fail("should throw NonWritableChannelException");
- } catch (NonWritableChannelException e) {
- // expected
- }
- // throws NonWritableChannelException before IllegalArgumentException
- try {
- readOnlyFileChannel.tryLock(-1, 0, false);
- fail("should throw NonWritableChannelException");
- } catch (NonWritableChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_NonReadable() throws Exception {
- try {
- writeOnlyFileChannel.tryLock(0, 10, true);
- fail("should throw NonReadableChannelException");
- } catch (NonReadableChannelException e) {
- // expected
- }
- // throws NonReadableChannelException before IllegalArgumentException
- try {
- writeOnlyFileChannel.tryLock(-1, 0, true);
- fail("should throw NonReadableChannelException");
- } catch (NonReadableChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_Shared() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true);
- assertTrue(fileLock.isValid());
- // fileLock.isShared depends on whether the underlying platform support
- // shared lock, but it works on Windows & Linux.
- assertTrue(fileLock.isShared());
- assertSame(readOnlyFileChannel, fileLock.channel());
- assertEquals(POSITION, fileLock.position());
- assertEquals(SIZE, fileLock.size());
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_NotShared() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- fileLock = writeOnlyFileChannel.tryLock(POSITION, SIZE, false);
- assertTrue(fileLock.isValid());
- assertFalse(fileLock.isShared());
- assertSame(writeOnlyFileChannel, fileLock.channel());
- assertEquals(POSITION, fileLock.position());
- assertEquals(SIZE, fileLock.size());
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_Long_MAX_VALUE() throws Exception {
- final long POSITION = 0;
- final long SIZE = Long.MAX_VALUE;
- fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true);
- assertTrue(fileLock.isValid());
- assertTrue(fileLock.isShared());
- assertEquals(POSITION, fileLock.position());
- assertEquals(SIZE, fileLock.size());
- assertSame(readOnlyFileChannel, fileLock.channel());
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_Overlapping() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
- assertTrue(fileLock.isValid());
- try {
- writeOnlyFileChannel.lock(POSITION + 1, SIZE, false);
- fail("should throw OverlappingFileLockException");
- } catch (OverlappingFileLockException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
- */
- public void test_tryLockJJZ_NotOverlapping() throws Exception {
- final long POSITION = 100;
- final long SIZE = 200;
- FileLock fileLock1 = writeOnlyFileChannel
- .tryLock(POSITION, SIZE, false);
- assertTrue(fileLock1.isValid());
- FileLock fileLock2 = writeOnlyFileChannel.tryLock(POSITION + SIZE,
- SIZE, false);
- assertTrue(fileLock2.isValid());
- }
- /**
- * @tests java.nio.channels.FileChannel#tryLock(long,long,boolean)
- */
- public void test_tryLockJJZ_After_Release() throws Exception {
- fileLock = writeOnlyFileChannel.tryLock(0, 10, false);
- fileLock.release();
- // after release file lock can be obtained again.
- fileLock = writeOnlyFileChannel.tryLock(0, 10, false);
- assertTrue(fileLock.isValid());
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer)
- */
- public void test_readLByteBuffer_Null() throws Exception {
- ByteBuffer readBuffer = null;
- try {
- readOnlyFileChannel.read(readBuffer);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- try {
- readWriteFileChannel.read(readBuffer);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer)
- */
- public void test_readLByteBuffer_Closed() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.read(readBuffer);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.read(readBuffer);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.read(readBuffer);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- // should throw ClosedChannelException first
- readBuffer = null;
- try {
- readWriteFileChannel.read(readBuffer);
- fail();
- } catch (ClosedChannelException expected) {
- } catch (NullPointerException expected) {
- }
- }
- public void test_readLByteBuffer_WriteOnly() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- try {
- writeOnlyFileChannel.read(readBuffer);
- fail();
- } catch (NonReadableChannelException expected) {
- }
- readBuffer = null;
- try {
- writeOnlyFileChannel.read(readBuffer);
- fail();
- } catch (NonReadableChannelException expected) {
- } catch (NullPointerException expected) {
- }
- }
- public void test_readLByteBuffer_EmptyFile() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- int result = readOnlyFileChannel.read(readBuffer);
- assertEquals(-1, result);
- assertEquals(0, readBuffer.position());
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer)
- */
- public void test_readLByteBuffer_LimitedCapacity() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- ByteBuffer readBuffer = ByteBuffer.allocate(LIMITED_CAPACITY);
- int result = readOnlyFileChannel.read(readBuffer);
- assertEquals(LIMITED_CAPACITY, result);
- assertEquals(LIMITED_CAPACITY, readBuffer.position());
- readBuffer.flip();
- for (int i = 0; i < LIMITED_CAPACITY; i++) {
- assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
- }
- }
- public void test_readLByteBuffer() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_AS_BYTES_LENGTH);
- int result = readOnlyFileChannel.read(readBuffer);
- assertEquals(CONTENT_AS_BYTES_LENGTH, result);
- assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffer.position());
- readBuffer.flip();
- for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
- assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
- }
- }
- public void test_readLByteBufferJ_Null() throws Exception {
- try {
- readOnlyFileChannel.read(null, 0);
- fail();
- } catch (NullPointerException expected) {
- }
- try {
- readWriteFileChannel.read(null, 0);
- fail();
- } catch (NullPointerException expected) {
- }
- }
- public void test_readLByteBufferJ_Closed() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.read(readBuffer, 0);
- fail();
- } catch (ClosedChannelException expected) {
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.read(readBuffer, 0);
- fail();
- } catch (ClosedChannelException expected) {
- }
- }
- public void test_readLByteBufferJ_IllegalArgument() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- try {
- readOnlyFileChannel.read(readBuffer, -1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- try {
- writeOnlyFileChannel.read(readBuffer, -1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- try {
- readWriteFileChannel.read(readBuffer, -1);
- fail();
- } catch (IllegalArgumentException expected) {
- }
- }
- public void test_readLByteBufferJ_WriteOnly() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- try {
- writeOnlyFileChannel.read(readBuffer, 0);
- fail();
- } catch (NonReadableChannelException expected) {
- }
- }
- public void test_readLByteBufferJ_Emptyfile() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- int result = readOnlyFileChannel.read(readBuffer, 0);
- assertEquals(-1, result);
- assertEquals(0, readBuffer.position());
- }
- public void test_readLByteBufferJ_Position_BeyondFileLimit() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- int result = readOnlyFileChannel.read(readBuffer,
- CONTENT_AS_BYTES.length);
- assertEquals(-1, result);
- assertEquals(0, readBuffer.position());
- }
- public void test_readLByteBufferJ_Position_As_Long() throws Exception {
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- try {
- readOnlyFileChannel.read(readBuffer, Long.MAX_VALUE);
- } catch (IOException expected) {
- }
- }
- public void test_readLByteBufferJ() throws Exception {
- writeDataToFile(fileOfReadOnlyFileChannel);
- ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
- final int BUFFER_POSITION = 1;
- readBuffer.position(BUFFER_POSITION);
- final int POSITION = 2;
- int result = readOnlyFileChannel.read(readBuffer, POSITION);
- assertEquals(CONTENT_AS_BYTES_LENGTH - POSITION, result);
- assertEquals(BUFFER_POSITION + result, readBuffer.position());
- readBuffer.flip();
- readBuffer.position(BUFFER_POSITION);
- for (int i = POSITION; i < CONTENT_AS_BYTES_LENGTH; i++) {
- assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
- */
- public void test_read$LByteBuffer() throws Exception {
- // regression test for Harmony-849
- writeDataToFile(fileOfReadOnlyFileChannel);
- ByteBuffer[] readBuffers = new ByteBuffer[2];
- readBuffers[0] = ByteBuffer.allocate(CAPACITY);
- readBuffers[1] = ByteBuffer.allocate(CAPACITY);
- long readCount = readOnlyFileChannel.read(readBuffers);
- assertEquals(CONTENT_AS_BYTES_LENGTH, readCount);
- assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[0].position());
- assertEquals(0, readBuffers[1].position());
- readBuffers[0].flip();
- for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
- assertEquals(CONTENT_AS_BYTES[i], readBuffers[0].get());
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
- */
- public void test_read$LByteBuffer_mock() throws Exception {
- FileChannel mockChannel = new MockFileChannel();
- ByteBuffer[] buffers = new ByteBuffer[2];
- mockChannel.read(buffers);
- // Verify that calling read(ByteBuffer[] dsts) leads to the method
- // read(dsts, 0, dsts.length)
- assertTrue(((MockFileChannel)mockChannel).isReadCalled);
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
- */
- public void test_read$LByteBufferII_Null() throws Exception {
- ByteBuffer[] readBuffers = null;
- try {
- readOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- try {
- readOnlyFileChannel.read(readBuffers, 1, 11);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- try {
- readWriteFileChannel.read(readBuffers, 0, 1);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- // first throws NullPointerException
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
- */
- public void test_read$LByteBufferII_Closed() throws Exception {
- ByteBuffer[] readBuffers = new ByteBuffer[2];
- readBuffers[0] = ByteBuffer.allocate(CAPACITY);
- readOnlyFileChannel.close();
- try {
- readOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- writeOnlyFileChannel.close();
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.read(readBuffers, 0, 1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- // regression test for Harmony-902
- readBuffers[0] = null;
- try {
- readOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- try {
- readWriteFileChannel.read(readBuffers, 0, 1);
- fail("should throw ClosedChannelException");
- } catch (ClosedChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
- */
- public void test_read$LByteBufferII_WriteOnly() throws Exception {
- ByteBuffer[] readBuffers = new ByteBuffer[2];
- readBuffers[0] = ByteBuffer.allocate(CAPACITY);
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw NonReadableChannelException");
- } catch (NonReadableChannelException e) {
- // expected
- }
- // first throws NonReadableChannelException.
- readBuffers[0] = null;
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 1);
- fail("should throw NonReadableChannelException");
- } catch (NonReadableChannelException e) {
- // expected
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
- */
- public void test_read$LByteBufferII_IndexOutOfBound() throws Exception {
- ByteBuffer[] readBuffers = new ByteBuffer[2];
- readBuffers[0] = ByteBuffer.allocate(CAPACITY);
- readBuffers[1] = ByteBuffer.allocate(CAPACITY);
- try {
- readOnlyFileChannel.read(readBuffers, 2, 1);
- fail();
- } catch (IndexOutOfBoundsException expected) {
- }
- try {
- readWriteFileChannel.read(null, -1, 0);
- fail();
- } catch (IndexOutOfBoundsException expected) {
- } catch (NullPointerException expected) {
- }
- try {
- writeOnlyFileChannel.read(readBuffers, 0, 3);
- fail();
- } catch (IndexOutOfBoundsException expected) {
- }
- try {
- readWriteFileChannel.read(readBuffers, -1, 0);
- fail();
- } catch (IndexOutOfBoundsException expected) {
- }
- readWriteFileChannel.close();
- try {
- readWriteFileChannel.read(readBuffers, 0, 3);
- fail();
- } catch (IndexOutOfBoundsException expected) {
- }
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
- */
- public void test_read$LByteBufferII_EmptyFile() throws Exception {
- ByteBuffer[] readBuffers = new ByteBuffer[2];
- readBuffers[0] = ByteBuffer.allocate(CAPACITY);
- readBuffers[1] = ByteBuffer.allocate(CAPACITY);
- long result = readOnlyFileChannel.read(readBuffers, 0, 2);
- assertEquals(-1, result);
- assertEquals(0, readBuffers[0].position());
- assertEquals(0, readBuffers[1].position());
- }
- /**
- * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
- */
- public void test_read$LByteBufferII_EmptyBuffers() throws Exception {
- ByteBuffer[] readBuffers = new ByteBuffer[2];
- try {
- readOnlyFileChannel.read(readBuffers, 0, 2);
- } catch (NullPointerException e) {
- // expected
- }
- writeDataToFile(fileOfReadOnlyFileChannel);
- readBuffers[0] = ByteBuffer.allocate(CAPACITY);
- try {
- readOnlyFileChannel.read(readBuffers, 0, 2);
- } catch (NullPointerException e) {
- // expected