/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

  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.harmony.nio.tests.java.nio.channels;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.RandomAccessFile;
  23. import java.io.UnsupportedEncodingException;
  24. import java.net.InetAddress;
  25. import java.net.InetSocketAddress;
  26. import java.nio.BufferOverflowException;
  27. import java.nio.ByteBuffer;
  28. import java.nio.MappedByteBuffer;
  29. import java.nio.ReadOnlyBufferException;
  30. import java.nio.channels.ClosedChannelException;
  31. import java.nio.channels.DatagramChannel;
  32. import java.nio.channels.FileChannel;
  33. import java.nio.channels.FileLock;
  34. import java.nio.channels.NonReadableChannelException;
  35. import java.nio.channels.NonWritableChannelException;
  36. import java.nio.channels.OverlappingFileLockException;
  37. import java.nio.channels.Pipe;
  38. import java.nio.channels.ReadableByteChannel;
  39. import java.nio.channels.ServerSocketChannel;
  40. import java.nio.channels.SocketChannel;
  41. import java.nio.channels.WritableByteChannel;
  42. import java.nio.channels.FileChannel.MapMode;
  43. import java.util.Arrays;
  44. import junit.framework.TestCase;
  45. public class FileChannelTest extends TestCase {
  46. private static final int CAPACITY = 100;
  47. private static final int LIMITED_CAPACITY = 2;
  48. private static final int TIME_OUT = 10000;
  49. private static final String CONTENT = "MYTESTSTRING needs to be a little long";
  50. private static final byte[] TEST_BYTES;
  51. private static final byte[] CONTENT_AS_BYTES;
  52. private static final int CONTENT_AS_BYTES_LENGTH;
  53. static {
  54. try {
  55. TEST_BYTES = "test".getBytes("iso8859-1");
  56. CONTENT_AS_BYTES = CONTENT.getBytes("iso8859-1");
  57. CONTENT_AS_BYTES_LENGTH = CONTENT_AS_BYTES.length;
  58. } catch (UnsupportedEncodingException e) {
  59. throw new Error(e);
  60. }
  61. }
  62. private static final int CONTENT_LENGTH = CONTENT.length();
  63. private FileChannel readOnlyFileChannel;
  64. private FileChannel writeOnlyFileChannel;
  65. private FileChannel readWriteFileChannel;
  66. private File fileOfReadOnlyFileChannel;
  67. private File fileOfWriteOnlyFileChannel;
  68. private File fileOfReadWriteFileChannel;
  69. private ReadableByteChannel readByteChannel;
  70. private WritableByteChannel writableByteChannel;
  71. private DatagramChannel datagramChannelSender;
  72. private DatagramChannel datagramChannelReceiver;
  73. private ServerSocketChannel serverSocketChannel;
  74. private SocketChannel socketChannelSender;
  75. private SocketChannel socketChannelReceiver;
  76. private Pipe pipe;
  77. // to read content from FileChannel
  78. private FileInputStream fis;
  79. private FileLock fileLock;
  80. protected void setUp() throws Exception {
  81. fileOfReadOnlyFileChannel = File.createTempFile(
  82. "File_of_readOnlyFileChannel", "tmp");
  83. fileOfReadOnlyFileChannel.deleteOnExit();
  84. fileOfWriteOnlyFileChannel = File.createTempFile(
  85. "File_of_writeOnlyFileChannel", "tmp");
  86. fileOfWriteOnlyFileChannel.deleteOnExit();
  87. fileOfReadWriteFileChannel = File.createTempFile(
  88. "File_of_readWriteFileChannel", "tmp");
  89. fileOfReadWriteFileChannel.deleteOnExit();
  90. fis = null;
  91. fileLock = null;
  92. readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel)
  93. .getChannel();
  94. writeOnlyFileChannel = new FileOutputStream(fileOfWriteOnlyFileChannel)
  95. .getChannel();
  96. readWriteFileChannel = new RandomAccessFile(fileOfReadWriteFileChannel,
  97. "rw").getChannel();
  98. }
  99. protected void tearDown() {
  100. if (null != readOnlyFileChannel) {
  101. try {
  102. readOnlyFileChannel.close();
  103. } catch (IOException e) {
  104. // do nothing
  105. }
  106. }
  107. if (null != writeOnlyFileChannel) {
  108. try {
  109. writeOnlyFileChannel.close();
  110. } catch (IOException e) {
  111. // do nothing
  112. }
  113. }
  114. if (null != readWriteFileChannel) {
  115. try {
  116. readWriteFileChannel.close();
  117. } catch (IOException e) {
  118. // do nothing
  119. }
  120. }
  121. if (null != fis) {
  122. try {
  123. fis.close();
  124. } catch (IOException e) {
  125. // do nothing
  126. }
  127. }
  128. if (null != fileLock) {
  129. try {
  130. fileLock.release();
  131. } catch (IOException e) {
  132. // do nothing
  133. }
  134. }
  135. if (null != fileOfReadOnlyFileChannel) {
  136. fileOfReadOnlyFileChannel.delete();
  137. }
  138. if (null != fileOfWriteOnlyFileChannel) {
  139. fileOfWriteOnlyFileChannel.delete();
  140. }
  141. if (null != fileOfReadWriteFileChannel) {
  142. fileOfReadWriteFileChannel.delete();
  143. }
  144. if (null != datagramChannelSender) {
  145. try {
  146. datagramChannelSender.close();
  147. } catch (IOException e) {
  148. // do nothing
  149. }
  150. }
  151. if (null != datagramChannelReceiver) {
  152. try {
  153. datagramChannelReceiver.close();
  154. } catch (IOException e) {
  155. // do nothing
  156. }
  157. }
  158. if (null != serverSocketChannel) {
  159. try {
  160. serverSocketChannel.close();
  161. } catch (IOException e) {
  162. // do nothing
  163. }
  164. }
  165. if (null != socketChannelSender) {
  166. try {
  167. socketChannelSender.close();
  168. } catch (IOException e) {
  169. // do nothing
  170. }
  171. }
  172. if (null != socketChannelReceiver) {
  173. try {
  174. socketChannelReceiver.close();
  175. } catch (IOException e) {
  176. // do nothing
  177. }
  178. }
  179. if (null != pipe) {
  180. if (null != pipe.source()) {
  181. try {
  182. pipe.source().close();
  183. } catch (IOException e) {
  184. // do nothing
  185. }
  186. }
  187. if (null != pipe.sink()) {
  188. try {
  189. pipe.sink().close();
  190. } catch (IOException e) {
  191. // do nothing
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * @tests java.nio.channels.FileChannel#force(boolean)
  198. */
  199. public void test_forceJ() throws Exception {
  200. ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
  201. writeOnlyFileChannel.write(writeBuffer);
  202. writeOnlyFileChannel.force(true);
  203. byte[] readBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
  204. fis = new FileInputStream(fileOfWriteOnlyFileChannel);
  205. fis.read(readBuffer);
  206. assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer));
  207. }
  208. /**
  209. * @tests java.nio.channels.FileChannel#force(boolean)
  210. */
  211. public void test_forceJ_closed() throws Exception {
  212. writeOnlyFileChannel.close();
  213. try {
  214. writeOnlyFileChannel.force(true);
  215. fail();
  216. } catch (ClosedChannelException expected) {
  217. }
  218. try {
  219. writeOnlyFileChannel.force(false);
  220. fail();
  221. } catch (ClosedChannelException expected) {
  222. }
  223. }
  224. /**
  225. * @tests java.nio.channels.FileChannel#force(boolean)
  226. */
  227. public void test_forceJ_ReadOnlyChannel() throws Exception {
  228. // force on a read only file channel has no effect.
  229. readOnlyFileChannel.force(true);
  230. readOnlyFileChannel.force(false);
  231. }
  232. /**
  233. * @tests java.nio.channels.FileChannel#position()
  234. */
  235. public void test_position_Init() throws Exception {
  236. assertEquals(0, readOnlyFileChannel.position());
  237. assertEquals(0, writeOnlyFileChannel.position());
  238. assertEquals(0, readWriteFileChannel.position());
  239. }
  240. /**
  241. * @tests java.nio.channels.FileChannel#position()
  242. */
  243. public void test_position_ReadOnly() throws Exception {
  244. writeDataToFile(fileOfReadOnlyFileChannel);
  245. assertEquals(0, readOnlyFileChannel.position());
  246. ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
  247. readOnlyFileChannel.read(readBuffer);
  248. assertEquals(CONTENT_LENGTH, readOnlyFileChannel.position());
  249. }
  250. /**
  251. * Initializes test file.
  252. *
  253. * @param file
  254. * @throws FileNotFoundException
  255. * @throws IOException
  256. */
  257. private void writeDataToFile(File file) throws FileNotFoundException,
  258. IOException {
  259. FileOutputStream fos = new FileOutputStream(file);
  260. try {
  261. fos.write(CONTENT_AS_BYTES);
  262. } finally {
  263. fos.close();
  264. }
  265. }
  266. /**
  267. * Initializes large test file.
  268. *
  269. * @param file the file to be written
  270. * @param size the content size to be written
  271. * @throws FileNotFoundException
  272. * @throws IOException
  273. */
  274. private void writeLargeDataToFile(File file, int size) throws FileNotFoundException,
  275. IOException {
  276. FileOutputStream fos = new FileOutputStream(file);
  277. byte[] buf = new byte[size];
  278. try {
  279. // we don't care about content - just need a particular file size
  280. fos.write(buf);
  281. } finally {
  282. fos.close();
  283. }
  284. }
  285. /**
  286. * @tests java.nio.channels.FileChannel#position()
  287. */
  288. public void test_position_WriteOnly() throws Exception {
  289. ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
  290. writeOnlyFileChannel.write(writeBuffer);
  291. assertEquals(CONTENT_LENGTH, writeOnlyFileChannel.position());
  292. }
  293. /**
  294. * @tests java.nio.channels.FileChannel#position()
  295. */
  296. public void test_position_ReadWrite() throws Exception {
  297. writeDataToFile(fileOfReadWriteFileChannel);
  298. assertEquals(0, readWriteFileChannel.position());
  299. ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
  300. readWriteFileChannel.read(readBuffer);
  301. assertEquals(CONTENT_LENGTH, readWriteFileChannel.position());
  302. ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
  303. readWriteFileChannel.write(writeBuffer);
  304. assertEquals(CONTENT_LENGTH * 2, readWriteFileChannel.position());
  305. }
  306. /**
  307. * @tests java.nio.channels.FileChannel#position()
  308. */
  309. public void test_position_Closed() throws Exception {
  310. readOnlyFileChannel.close();
  311. try {
  312. readOnlyFileChannel.position();
  313. fail("should throw ClosedChannelException");
  314. } catch (ClosedChannelException expected) {
  315. }
  316. writeOnlyFileChannel.close();
  317. try {
  318. writeOnlyFileChannel.position();
  319. fail("should throw ClosedChannelException");
  320. } catch (ClosedChannelException expected) {
  321. }
  322. readWriteFileChannel.close();
  323. try {
  324. readWriteFileChannel.position();
  325. fail("should throw ClosedChannelException");
  326. } catch (ClosedChannelException expected) {
  327. }
  328. }
  329. /**
  330. * @tests java.nio.channels.FileChannel#position(long)
  331. */
  332. public void test_positionJ_Closed() throws Exception {
  333. final long POSITION = 100;
  334. readOnlyFileChannel.close();
  335. try {
  336. readOnlyFileChannel.position(POSITION);
  337. fail();
  338. } catch (ClosedChannelException expected) {
  339. }
  340. writeOnlyFileChannel.close();
  341. try {
  342. writeOnlyFileChannel.position(POSITION);
  343. fail();
  344. } catch (ClosedChannelException expected) {
  345. }
  346. readWriteFileChannel.close();
  347. try {
  348. readWriteFileChannel.position(POSITION);
  349. fail();
  350. } catch (ClosedChannelException expected) {
  351. }
  352. }
  353. /**
  354. * @tests java.nio.channels.FileChannel#position(long)
  355. */
  356. public void test_positionJ_Negative() throws Exception {
  357. final long NEGATIVE_POSITION = -1;
  358. try {
  359. readOnlyFileChannel.position(NEGATIVE_POSITION);
  360. fail("should throw IllegalArgumentException");
  361. } catch (IllegalArgumentException e) {
  362. // expected
  363. }
  364. try {
  365. writeOnlyFileChannel.position(NEGATIVE_POSITION);
  366. fail("should throw IllegalArgumentException");
  367. } catch (IllegalArgumentException e) {
  368. // expected
  369. }
  370. try {
  371. readWriteFileChannel.position(NEGATIVE_POSITION);
  372. fail("should throw IllegalArgumentException");
  373. } catch (IllegalArgumentException e) {
  374. // expected
  375. }
  376. }
  377. /**
  378. * @tests java.nio.channels.FileChannel#position(long)
  379. */
  380. public void test_positionJ_ReadOnly() throws Exception {
  381. writeDataToFile(fileOfReadOnlyFileChannel);
  382. // set the position of the read only file channel to POSITION
  383. final int POSITION = 4;
  384. readOnlyFileChannel.position(POSITION);
  385. // reads the content left to readBuffer through read only file channel
  386. ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
  387. int count = readOnlyFileChannel.read(readBuffer);
  388. assertEquals(CONTENT_LENGTH - POSITION, count);
  389. // asserts the content read is the part which stays beyond the POSITION
  390. readBuffer.flip();
  391. int i = POSITION;
  392. while (readBuffer.hasRemaining()) {
  393. assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
  394. i++;
  395. }
  396. }
  397. /**
  398. * @tests java.nio.channels.FileChannel#position(long)
  399. */
  400. public void test_positionJ_WriteOnly() throws Exception {
  401. writeDataToFile(fileOfWriteOnlyFileChannel);
  402. // init data to write
  403. ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
  404. // set the position of the write only file channel to POSITION
  405. final int POSITION = 4;
  406. writeOnlyFileChannel.position(POSITION);
  407. // writes to the write only file channel
  408. writeOnlyFileChannel.write(writeBuffer);
  409. // force to write out.
  410. writeOnlyFileChannel.close();
  411. // gets the result of the write only file channel
  412. byte[] result = new byte[POSITION + CONTENT_LENGTH];
  413. fis = new FileInputStream(fileOfWriteOnlyFileChannel);
  414. fis.read(result);
  415. // constructs the expected result which has content[0... POSITION] plus
  416. // content[0...length()]
  417. byte[] expectedResult = new byte[POSITION + CONTENT_LENGTH];
  418. System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, POSITION);
  419. System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, POSITION,
  420. CONTENT_LENGTH);
  421. // asserts result of the write only file channel same as expected
  422. assertTrue(Arrays.equals(expectedResult, result));
  423. }
  424. /**
  425. * @tests java.nio.channels.FileChannel#size()
  426. */
  427. public void test_size_Init() throws Exception {
  428. assertEquals(0, readOnlyFileChannel.size());
  429. assertEquals(0, writeOnlyFileChannel.size());
  430. assertEquals(0, readWriteFileChannel.size());
  431. }
  432. /**
  433. * @tests java.nio.channels.FileChannel#size()
  434. */
  435. public void test_size() throws Exception {
  436. writeDataToFile(fileOfReadOnlyFileChannel);
  437. assertEquals(fileOfReadOnlyFileChannel.length(), readOnlyFileChannel
  438. .size());
  439. // REGRESSION test for read(ByteBuffer[], int, int) on special files
  440. try {
  441. FileChannel specialFile =
  442. new FileInputStream("/dev/zero").getChannel();
  443. assertEquals(0, specialFile.size());
  444. ByteBuffer buf = ByteBuffer.allocate(8);
  445. assertEquals(8, specialFile.read(buf));
  446. ByteBuffer[] bufs = { ByteBuffer.allocate(8) };
  447. assertEquals(8, specialFile.read(bufs, 0, 1));
  448. specialFile.close();
  449. } catch (FileNotFoundException e) {
  450. // skip test if special file doesn't exist
  451. }
  452. }
  453. /**
  454. * @tests java.nio.channels.FileChannel#size()
  455. */
  456. public void test_size_Closed() throws Exception {
  457. readOnlyFileChannel.close();
  458. try {
  459. readOnlyFileChannel.size();
  460. fail("should throw ClosedChannelException");
  461. } catch (ClosedChannelException e) {
  462. // expected
  463. }
  464. writeOnlyFileChannel.close();
  465. try {
  466. writeOnlyFileChannel.size();
  467. fail("should throw ClosedChannelException");
  468. } catch (ClosedChannelException e) {
  469. // expected
  470. }
  471. readWriteFileChannel.close();
  472. try {
  473. readWriteFileChannel.size();
  474. fail("should throw ClosedChannelException");
  475. } catch (ClosedChannelException e) {
  476. // expected
  477. }
  478. }
  479. /**
  480. * @tests java.nio.channels.FileChannel#truncate(long)
  481. */
  482. public void test_truncateJ_Closed() throws Exception {
  483. readOnlyFileChannel.close();
  484. try {
  485. readOnlyFileChannel.truncate(0);
  486. fail("should throw ClosedChannelException");
  487. } catch (ClosedChannelException e) {
  488. // expected
  489. }
  490. writeOnlyFileChannel.close();
  491. try {
  492. writeOnlyFileChannel.truncate(0);
  493. fail("should throw ClosedChannelException");
  494. } catch (ClosedChannelException e) {
  495. // expected
  496. }
  497. readWriteFileChannel.close();
  498. try {
  499. readWriteFileChannel.truncate(-1);
  500. fail("should throw ClosedChannelException");
  501. } catch (ClosedChannelException e) {
  502. // expected
  503. }
  504. }
  505. /**
  506. * @tests java.nio.channels.FileChannel#truncate(long)
  507. */
  508. public void test_truncateJ_IllegalArgument() throws Exception {
  509. // regression test for Harmony-941
  510. try {
  511. readOnlyFileChannel.truncate(-1);
  512. fail("should throw IllegalArgumentException");
  513. } catch (IllegalArgumentException e) {
  514. // expected
  515. }
  516. try {
  517. writeOnlyFileChannel.truncate(-1);
  518. fail("should throw IllegalArgumentException");
  519. } catch (IllegalArgumentException e) {
  520. // expected
  521. }
  522. try {
  523. readWriteFileChannel.truncate(-1);
  524. fail("should throw IllegalArgumentException");
  525. } catch (IllegalArgumentException e) {
  526. // expected
  527. }
  528. }
  529. /**
  530. * @tests java.nio.channels.FileChannel#truncate(long)
  531. */
  532. public void test_truncateJ_ReadOnly() throws Exception {
  533. writeDataToFile(fileOfReadOnlyFileChannel);
  534. try {
  535. readOnlyFileChannel.truncate(readOnlyFileChannel.size());
  536. fail("should throw NonWritableChannelException.");
  537. } catch (NonWritableChannelException e) {
  538. // expected
  539. }
  540. try {
  541. readOnlyFileChannel.truncate(0);
  542. fail("should throw NonWritableChannelException.");
  543. } catch (NonWritableChannelException e) {
  544. // expected
  545. }
  546. }
  547. /**
  548. * @tests java.nio.channels.FileChannel#truncate(long)
  549. */
  550. public void test_truncateJ() throws Exception {
  551. writeDataToFile(fileOfReadWriteFileChannel);
  552. int truncateLength = CONTENT_LENGTH + 2;
  553. assertEquals(readWriteFileChannel, readWriteFileChannel
  554. .truncate(truncateLength));
  555. assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
  556. truncateLength = CONTENT_LENGTH;
  557. assertEquals(readWriteFileChannel, readWriteFileChannel
  558. .truncate(truncateLength));
  559. assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
  560. truncateLength = CONTENT_LENGTH / 2;
  561. assertEquals(readWriteFileChannel, readWriteFileChannel
  562. .truncate(truncateLength));
  563. assertEquals(truncateLength, fileOfReadWriteFileChannel.length());
  564. }
  565. /**
  566. * @tests java.nio.channels.FileChannel#lock()
  567. */
  568. public void test_lock() throws Exception {
  569. MockFileChannel mockFileChannel = new MockFileChannel();
  570. // Verify that calling lock() leads to the method
  571. // lock(long, long, boolean) being called with a 0 for the
  572. // first parameter, Long.MAX_VALUE as the second parameter and false
  573. // as the third parameter.
  574. mockFileChannel.lock();
  575. assertTrue(mockFileChannel.isLockCalled);
  576. }
  577. /**
  578. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  579. */
  580. public void test_lockJJZ_Closed() throws Exception {
  581. readOnlyFileChannel.close();
  582. try {
  583. readOnlyFileChannel.lock(0, 10, false);
  584. fail("should throw ClosedChannelException");
  585. } catch (ClosedChannelException e) {
  586. // expected
  587. }
  588. writeOnlyFileChannel.close();
  589. try {
  590. writeOnlyFileChannel.lock(0, 10, false);
  591. fail("should throw ClosedChannelException");
  592. } catch (ClosedChannelException e) {
  593. // expected
  594. }
  595. readWriteFileChannel.close();
  596. try {
  597. readWriteFileChannel.lock(0, 10, false);
  598. fail("should throw ClosedChannelException");
  599. } catch (ClosedChannelException e) {
  600. // expected
  601. }
  602. // throws ClosedChannelException before IllegalArgumentException
  603. try {
  604. readWriteFileChannel.lock(-1, 0, false);
  605. fail("should throw ClosedChannelException");
  606. } catch (ClosedChannelException e) {
  607. // expected
  608. }
  609. }
  610. /**
  611. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  612. */
  613. public void test_lockJJZ_IllegalArgument() throws Exception {
  614. try {
  615. writeOnlyFileChannel.lock(0, -1, false);
  616. fail("should throw IllegalArgumentException");
  617. } catch (IllegalArgumentException e) {
  618. // expected
  619. }
  620. try {
  621. writeOnlyFileChannel.lock(-1, 0, false);
  622. fail("should throw IllegalArgumentException");
  623. } catch (IllegalArgumentException e) {
  624. // expected
  625. }
  626. try {
  627. readWriteFileChannel.lock(-1, -1, false);
  628. fail("should throw IllegalArgumentException");
  629. } catch (IllegalArgumentException e) {
  630. // expected
  631. }
  632. try {
  633. readWriteFileChannel.lock(Long.MAX_VALUE, 1, false);
  634. fail("should throw IllegalArgumentException");
  635. } catch (IllegalArgumentException e) {
  636. // expected
  637. }
  638. }
  639. /**
  640. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  641. */
  642. public void test_lockJJZ_NonWritable() throws Exception {
  643. try {
  644. readOnlyFileChannel.lock(0, 10, false);
  645. fail("should throw NonWritableChannelException");
  646. } catch (NonWritableChannelException e) {
  647. // expected
  648. }
  649. // throws NonWritableChannelException before IllegalArgumentException
  650. try {
  651. readOnlyFileChannel.lock(-1, 0, false);
  652. fail("should throw NonWritableChannelException");
  653. } catch (NonWritableChannelException e) {
  654. // expected
  655. }
  656. }
  657. /**
  658. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  659. */
  660. public void test_lockJJZ_NonReadable() throws Exception {
  661. try {
  662. writeOnlyFileChannel.lock(0, 10, true);
  663. fail("should throw NonReadableChannelException");
  664. } catch (NonReadableChannelException e) {
  665. // expected
  666. }
  667. // throws NonReadableChannelException before IllegalArgumentException
  668. try {
  669. writeOnlyFileChannel.lock(-1, 0, true);
  670. fail("should throw NonReadableChannelException");
  671. } catch (NonReadableChannelException e) {
  672. // expected
  673. }
  674. }
  675. /**
  676. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  677. */
  678. public void test_lockJJZ_Shared() throws Exception {
  679. final long POSITION = 100;
  680. final long SIZE = 200;
  681. fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true);
  682. assertTrue(fileLock.isValid());
  683. // fileLock.isShared depends on whether the underlying platform support
  684. // shared lock, but it works on Windows & Linux.
  685. assertTrue(fileLock.isShared());
  686. assertSame(readOnlyFileChannel, fileLock.channel());
  687. assertEquals(POSITION, fileLock.position());
  688. assertEquals(SIZE, fileLock.size());
  689. }
  690. /**
  691. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  692. */
  693. public void test_lockJJZ_NotShared() throws Exception {
  694. final long POSITION = 100;
  695. final long SIZE = 200;
  696. fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
  697. assertTrue(fileLock.isValid());
  698. assertFalse(fileLock.isShared());
  699. assertSame(writeOnlyFileChannel, fileLock.channel());
  700. assertEquals(POSITION, fileLock.position());
  701. assertEquals(SIZE, fileLock.size());
  702. }
  703. /**
  704. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  705. */
  706. public void test_lockJJZ_Long_MAX_VALUE() throws Exception {
  707. final long POSITION = 0;
  708. final long SIZE = Long.MAX_VALUE;
  709. fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true);
  710. assertTrue(fileLock.isValid());
  711. assertTrue(fileLock.isShared());
  712. assertEquals(POSITION, fileLock.position());
  713. assertEquals(SIZE, fileLock.size());
  714. assertSame(readOnlyFileChannel, fileLock.channel());
  715. }
  716. /**
  717. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  718. */
  719. public void test_lockJJZ_Overlapping() throws Exception {
  720. final long POSITION = 100;
  721. final long SIZE = 200;
  722. fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
  723. assertTrue(fileLock.isValid());
  724. try {
  725. writeOnlyFileChannel.lock(POSITION + 1, SIZE, false);
  726. fail("should throw OverlappingFileLockException");
  727. } catch (OverlappingFileLockException e) {
  728. // expected
  729. }
  730. }
  731. /**
  732. * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
  733. */
  734. public void test_lockJJZ_NotOverlapping() throws Exception {
  735. final long POSITION = 100;
  736. final long SIZE = 200;
  737. FileLock fileLock1 = writeOnlyFileChannel.lock(POSITION, SIZE, false);
  738. assertTrue(fileLock1.isValid());
  739. FileLock fileLock2 = writeOnlyFileChannel.lock(POSITION + SIZE, SIZE,
  740. false);
  741. assertTrue(fileLock2.isValid());
  742. }
  743. /**
  744. * @tests java.nio.channels.FileChannel#lock(long,long,boolean)
  745. */
  746. public void test_lockJJZ_After_Release() throws Exception {
  747. fileLock = writeOnlyFileChannel.lock(0, 10, false);
  748. fileLock.release();
  749. // after release file lock can be obtained again.
  750. fileLock = writeOnlyFileChannel.lock(0, 10, false);
  751. assertTrue(fileLock.isValid());
  752. }
  753. /**
  754. * @tests java.nio.channels.FileChannel#tryLock()
  755. */
  756. public void test_tryLock() throws Exception {
  757. MockFileChannel mockFileChannel = new MockFileChannel();
  758. // Verify that calling tryLock() leads to the method
  759. // tryLock(long, long, boolean) being called with a 0 for the
  760. // first parameter, Long.MAX_VALUE as the second parameter and false
  761. // as the third parameter.
  762. mockFileChannel.tryLock();
  763. assertTrue(mockFileChannel.isTryLockCalled);
  764. }
  765. /**
  766. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  767. */
  768. public void test_tryLockJJZ_Closed() throws Exception {
  769. readOnlyFileChannel.close();
  770. try {
  771. readOnlyFileChannel.tryLock(0, 10, false);
  772. fail("should throw ClosedChannelException");
  773. } catch (ClosedChannelException e) {
  774. // expected
  775. }
  776. writeOnlyFileChannel.close();
  777. try {
  778. writeOnlyFileChannel.tryLock(0, 10, false);
  779. fail("should throw ClosedChannelException");
  780. } catch (ClosedChannelException e) {
  781. // expected
  782. }
  783. readWriteFileChannel.close();
  784. try {
  785. readWriteFileChannel.tryLock(0, 10, false);
  786. fail("should throw ClosedChannelException");
  787. } catch (ClosedChannelException e) {
  788. // expected
  789. }
  790. // throws ClosedChannelException before IllegalArgumentException
  791. try {
  792. readWriteFileChannel.tryLock(-1, 0, false);
  793. fail("should throw ClosedChannelException");
  794. } catch (ClosedChannelException e) {
  795. // expected
  796. }
  797. }
  798. /**
  799. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  800. */
  801. public void test_tryLockJJZ_IllegalArgument() throws Exception {
  802. try {
  803. writeOnlyFileChannel.tryLock(0, -1, false);
  804. fail("should throw IllegalArgumentException");
  805. } catch (IllegalArgumentException e) {
  806. // expected
  807. }
  808. try {
  809. writeOnlyFileChannel.tryLock(-1, 0, false);
  810. fail("should throw IllegalArgumentException");
  811. } catch (IllegalArgumentException e) {
  812. // expected
  813. }
  814. try {
  815. readWriteFileChannel.tryLock(-1, -1, false);
  816. fail("should throw IllegalArgumentException");
  817. } catch (IllegalArgumentException e) {
  818. // expected
  819. }
  820. try {
  821. readWriteFileChannel.tryLock(Long.MAX_VALUE, 1, false);
  822. fail("should throw IllegalArgumentException");
  823. } catch (IllegalArgumentException e) {
  824. // expected
  825. }
  826. }
  827. /**
  828. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  829. */
  830. public void test_tryLockJJZ_NonWritable() throws Exception {
  831. try {
  832. readOnlyFileChannel.tryLock(0, 10, false);
  833. fail("should throw NonWritableChannelException");
  834. } catch (NonWritableChannelException e) {
  835. // expected
  836. }
  837. // throws NonWritableChannelException before IllegalArgumentException
  838. try {
  839. readOnlyFileChannel.tryLock(-1, 0, false);
  840. fail("should throw NonWritableChannelException");
  841. } catch (NonWritableChannelException e) {
  842. // expected
  843. }
  844. }
  845. /**
  846. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  847. */
  848. public void test_tryLockJJZ_NonReadable() throws Exception {
  849. try {
  850. writeOnlyFileChannel.tryLock(0, 10, true);
  851. fail("should throw NonReadableChannelException");
  852. } catch (NonReadableChannelException e) {
  853. // expected
  854. }
  855. // throws NonReadableChannelException before IllegalArgumentException
  856. try {
  857. writeOnlyFileChannel.tryLock(-1, 0, true);
  858. fail("should throw NonReadableChannelException");
  859. } catch (NonReadableChannelException e) {
  860. // expected
  861. }
  862. }
  863. /**
  864. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  865. */
  866. public void test_tryLockJJZ_Shared() throws Exception {
  867. final long POSITION = 100;
  868. final long SIZE = 200;
  869. fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true);
  870. assertTrue(fileLock.isValid());
  871. // fileLock.isShared depends on whether the underlying platform support
  872. // shared lock, but it works on Windows & Linux.
  873. assertTrue(fileLock.isShared());
  874. assertSame(readOnlyFileChannel, fileLock.channel());
  875. assertEquals(POSITION, fileLock.position());
  876. assertEquals(SIZE, fileLock.size());
  877. }
  878. /**
  879. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  880. */
  881. public void test_tryLockJJZ_NotShared() throws Exception {
  882. final long POSITION = 100;
  883. final long SIZE = 200;
  884. fileLock = writeOnlyFileChannel.tryLock(POSITION, SIZE, false);
  885. assertTrue(fileLock.isValid());
  886. assertFalse(fileLock.isShared());
  887. assertSame(writeOnlyFileChannel, fileLock.channel());
  888. assertEquals(POSITION, fileLock.position());
  889. assertEquals(SIZE, fileLock.size());
  890. }
  891. /**
  892. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  893. */
  894. public void test_tryLockJJZ_Long_MAX_VALUE() throws Exception {
  895. final long POSITION = 0;
  896. final long SIZE = Long.MAX_VALUE;
  897. fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true);
  898. assertTrue(fileLock.isValid());
  899. assertTrue(fileLock.isShared());
  900. assertEquals(POSITION, fileLock.position());
  901. assertEquals(SIZE, fileLock.size());
  902. assertSame(readOnlyFileChannel, fileLock.channel());
  903. }
  904. /**
  905. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  906. */
  907. public void test_tryLockJJZ_Overlapping() throws Exception {
  908. final long POSITION = 100;
  909. final long SIZE = 200;
  910. fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
  911. assertTrue(fileLock.isValid());
  912. try {
  913. writeOnlyFileChannel.lock(POSITION + 1, SIZE, false);
  914. fail("should throw OverlappingFileLockException");
  915. } catch (OverlappingFileLockException e) {
  916. // expected
  917. }
  918. }
  919. /**
  920. * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
  921. */
  922. public void test_tryLockJJZ_NotOverlapping() throws Exception {
  923. final long POSITION = 100;
  924. final long SIZE = 200;
  925. FileLock fileLock1 = writeOnlyFileChannel
  926. .tryLock(POSITION, SIZE, false);
  927. assertTrue(fileLock1.isValid());
  928. FileLock fileLock2 = writeOnlyFileChannel.tryLock(POSITION + SIZE,
  929. SIZE, false);
  930. assertTrue(fileLock2.isValid());
  931. }
  932. /**
  933. * @tests java.nio.channels.FileChannel#tryLock(long,long,boolean)
  934. */
  935. public void test_tryLockJJZ_After_Release() throws Exception {
  936. fileLock = writeOnlyFileChannel.tryLock(0, 10, false);
  937. fileLock.release();
  938. // after release file lock can be obtained again.
  939. fileLock = writeOnlyFileChannel.tryLock(0, 10, false);
  940. assertTrue(fileLock.isValid());
  941. }
  942. /**
  943. * @tests java.nio.channels.FileChannel#read(ByteBuffer)
  944. */
  945. public void test_readLByteBuffer_Null() throws Exception {
  946. ByteBuffer readBuffer = null;
  947. try {
  948. readOnlyFileChannel.read(readBuffer);
  949. fail("should throw NullPointerException");
  950. } catch (NullPointerException e) {
  951. // expected
  952. }
  953. try {
  954. readWriteFileChannel.read(readBuffer);
  955. fail("should throw NullPointerException");
  956. } catch (NullPointerException e) {
  957. // expected
  958. }
  959. }
  960. /**
  961. * @tests java.nio.channels.FileChannel#read(ByteBuffer)
  962. */
  963. public void test_readLByteBuffer_Closed() throws Exception {
  964. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  965. readOnlyFileChannel.close();
  966. try {
  967. readOnlyFileChannel.read(readBuffer);
  968. fail("should throw ClosedChannelException");
  969. } catch (ClosedChannelException e) {
  970. // expected
  971. }
  972. writeOnlyFileChannel.close();
  973. try {
  974. writeOnlyFileChannel.read(readBuffer);
  975. fail("should throw ClosedChannelException");
  976. } catch (ClosedChannelException e) {
  977. // expected
  978. }
  979. readWriteFileChannel.close();
  980. try {
  981. readWriteFileChannel.read(readBuffer);
  982. fail("should throw ClosedChannelException");
  983. } catch (ClosedChannelException e) {
  984. // expected
  985. }
  986. // should throw ClosedChannelException first
  987. readBuffer = null;
  988. try {
  989. readWriteFileChannel.read(readBuffer);
  990. fail();
  991. } catch (ClosedChannelException expected) {
  992. } catch (NullPointerException expected) {
  993. }
  994. }
  995. public void test_readLByteBuffer_WriteOnly() throws Exception {
  996. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  997. try {
  998. writeOnlyFileChannel.read(readBuffer);
  999. fail();
  1000. } catch (NonReadableChannelException expected) {
  1001. }
  1002. readBuffer = null;
  1003. try {
  1004. writeOnlyFileChannel.read(readBuffer);
  1005. fail();
  1006. } catch (NonReadableChannelException expected) {
  1007. } catch (NullPointerException expected) {
  1008. }
  1009. }
  1010. public void test_readLByteBuffer_EmptyFile() throws Exception {
  1011. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1012. int result = readOnlyFileChannel.read(readBuffer);
  1013. assertEquals(-1, result);
  1014. assertEquals(0, readBuffer.position());
  1015. }
  1016. /**
  1017. * @tests java.nio.channels.FileChannel#read(ByteBuffer)
  1018. */
  1019. public void test_readLByteBuffer_LimitedCapacity() throws Exception {
  1020. writeDataToFile(fileOfReadOnlyFileChannel);
  1021. ByteBuffer readBuffer = ByteBuffer.allocate(LIMITED_CAPACITY);
  1022. int result = readOnlyFileChannel.read(readBuffer);
  1023. assertEquals(LIMITED_CAPACITY, result);
  1024. assertEquals(LIMITED_CAPACITY, readBuffer.position());
  1025. readBuffer.flip();
  1026. for (int i = 0; i < LIMITED_CAPACITY; i++) {
  1027. assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
  1028. }
  1029. }
  1030. public void test_readLByteBuffer() throws Exception {
  1031. writeDataToFile(fileOfReadOnlyFileChannel);
  1032. ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_AS_BYTES_LENGTH);
  1033. int result = readOnlyFileChannel.read(readBuffer);
  1034. assertEquals(CONTENT_AS_BYTES_LENGTH, result);
  1035. assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffer.position());
  1036. readBuffer.flip();
  1037. for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
  1038. assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
  1039. }
  1040. }
  1041. public void test_readLByteBufferJ_Null() throws Exception {
  1042. try {
  1043. readOnlyFileChannel.read(null, 0);
  1044. fail();
  1045. } catch (NullPointerException expected) {
  1046. }
  1047. try {
  1048. readWriteFileChannel.read(null, 0);
  1049. fail();
  1050. } catch (NullPointerException expected) {
  1051. }
  1052. }
  1053. public void test_readLByteBufferJ_Closed() throws Exception {
  1054. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1055. readOnlyFileChannel.close();
  1056. try {
  1057. readOnlyFileChannel.read(readBuffer, 0);
  1058. fail();
  1059. } catch (ClosedChannelException expected) {
  1060. }
  1061. readWriteFileChannel.close();
  1062. try {
  1063. readWriteFileChannel.read(readBuffer, 0);
  1064. fail();
  1065. } catch (ClosedChannelException expected) {
  1066. }
  1067. }
  1068. public void test_readLByteBufferJ_IllegalArgument() throws Exception {
  1069. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1070. try {
  1071. readOnlyFileChannel.read(readBuffer, -1);
  1072. fail();
  1073. } catch (IllegalArgumentException expected) {
  1074. }
  1075. try {
  1076. writeOnlyFileChannel.read(readBuffer, -1);
  1077. fail();
  1078. } catch (IllegalArgumentException expected) {
  1079. }
  1080. try {
  1081. readWriteFileChannel.read(readBuffer, -1);
  1082. fail();
  1083. } catch (IllegalArgumentException expected) {
  1084. }
  1085. }
  1086. public void test_readLByteBufferJ_WriteOnly() throws Exception {
  1087. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1088. try {
  1089. writeOnlyFileChannel.read(readBuffer, 0);
  1090. fail();
  1091. } catch (NonReadableChannelException expected) {
  1092. }
  1093. }
  1094. public void test_readLByteBufferJ_Emptyfile() throws Exception {
  1095. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1096. int result = readOnlyFileChannel.read(readBuffer, 0);
  1097. assertEquals(-1, result);
  1098. assertEquals(0, readBuffer.position());
  1099. }
  1100. public void test_readLByteBufferJ_Position_BeyondFileLimit() throws Exception {
  1101. writeDataToFile(fileOfReadOnlyFileChannel);
  1102. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1103. int result = readOnlyFileChannel.read(readBuffer,
  1104. CONTENT_AS_BYTES.length);
  1105. assertEquals(-1, result);
  1106. assertEquals(0, readBuffer.position());
  1107. }
  1108. public void test_readLByteBufferJ_Position_As_Long() throws Exception {
  1109. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1110. try {
  1111. readOnlyFileChannel.read(readBuffer, Long.MAX_VALUE);
  1112. } catch (IOException expected) {
  1113. }
  1114. }
  1115. public void test_readLByteBufferJ() throws Exception {
  1116. writeDataToFile(fileOfReadOnlyFileChannel);
  1117. ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
  1118. final int BUFFER_POSITION = 1;
  1119. readBuffer.position(BUFFER_POSITION);
  1120. final int POSITION = 2;
  1121. int result = readOnlyFileChannel.read(readBuffer, POSITION);
  1122. assertEquals(CONTENT_AS_BYTES_LENGTH - POSITION, result);
  1123. assertEquals(BUFFER_POSITION + result, readBuffer.position());
  1124. readBuffer.flip();
  1125. readBuffer.position(BUFFER_POSITION);
  1126. for (int i = POSITION; i < CONTENT_AS_BYTES_LENGTH; i++) {
  1127. assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
  1128. }
  1129. }
  1130. /**
  1131. * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
  1132. */
  1133. public void test_read$LByteBuffer() throws Exception {
  1134. // regression test for Harmony-849
  1135. writeDataToFile(fileOfReadOnlyFileChannel);
  1136. ByteBuffer[] readBuffers = new ByteBuffer[2];
  1137. readBuffers[0] = ByteBuffer.allocate(CAPACITY);
  1138. readBuffers[1] = ByteBuffer.allocate(CAPACITY);
  1139. long readCount = readOnlyFileChannel.read(readBuffers);
  1140. assertEquals(CONTENT_AS_BYTES_LENGTH, readCount);
  1141. assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[0].position());
  1142. assertEquals(0, readBuffers[1].position());
  1143. readBuffers[0].flip();
  1144. for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
  1145. assertEquals(CONTENT_AS_BYTES[i], readBuffers[0].get());
  1146. }
  1147. }
  1148. /**
  1149. * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
  1150. */
  1151. public void test_read$LByteBuffer_mock() throws Exception {
  1152. FileChannel mockChannel = new MockFileChannel();
  1153. ByteBuffer[] buffers = new ByteBuffer[2];
  1154. mockChannel.read(buffers);
  1155. // Verify that calling read(ByteBuffer[] dsts) leads to the method
  1156. // read(dsts, 0, dsts.length)
  1157. assertTrue(((MockFileChannel)mockChannel).isReadCalled);
  1158. }
  1159. /**
  1160. * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
  1161. */
  1162. public void test_read$LByteBufferII_Null() throws Exception {
  1163. ByteBuffer[] readBuffers = null;
  1164. try {
  1165. readOnlyFileChannel.read(readBuffers, 0, 1);
  1166. fail("should throw NullPointerException");
  1167. } catch (NullPointerException e) {
  1168. // expected
  1169. }
  1170. try {
  1171. readOnlyFileChannel.read(readBuffers, 1, 11);
  1172. fail("should throw NullPointerException");
  1173. } catch (NullPointerException e) {
  1174. // expected
  1175. }
  1176. try {
  1177. writeOnlyFileChannel.read(readBuffers, 0, 1);
  1178. fail("should throw NullPointerException");
  1179. } catch (NullPointerException e) {
  1180. // expected
  1181. }
  1182. try {
  1183. readWriteFileChannel.read(readBuffers, 0, 1);
  1184. fail("should throw NullPointerException");
  1185. } catch (NullPointerException e) {
  1186. // expected
  1187. }
  1188. // first throws NullPointerException
  1189. writeOnlyFileChannel.close();
  1190. try {
  1191. writeOnlyFileChannel.read(readBuffers, 0, 1);
  1192. fail("should throw NullPointerException");
  1193. } catch (NullPointerException e) {
  1194. // expected
  1195. }
  1196. }
  1197. /**
  1198. * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
  1199. */
  1200. public void test_read$LByteBufferII_Closed() throws Exception {
  1201. ByteBuffer[] readBuffers = new ByteBuffer[2];
  1202. readBuffers[0] = ByteBuffer.allocate(CAPACITY);
  1203. readOnlyFileChannel.close();
  1204. try {
  1205. readOnlyFileChannel.read(readBuffers, 0, 1);
  1206. fail("should throw ClosedChannelException");
  1207. } catch (ClosedChannelException e) {
  1208. // expected
  1209. }
  1210. writeOnlyFileChannel.close();
  1211. try {
  1212. writeOnlyFileChannel.read(readBuffers, 0, 1);
  1213. fail("should throw ClosedChannelException");
  1214. } catch (ClosedChannelException e) {
  1215. // expected
  1216. }
  1217. readWriteFileChannel.close();
  1218. try {
  1219. readWriteFileChannel.read(readBuffers, 0, 1);
  1220. fail("should throw ClosedChannelException");
  1221. } catch (ClosedChannelException e) {
  1222. // expected
  1223. }
  1224. // regression test for Harmony-902
  1225. readBuffers[0] = null;
  1226. try {
  1227. readOnlyFileChannel.read(readBuffers, 0, 1);
  1228. fail("should throw ClosedChannelException");
  1229. } catch (ClosedChannelException e) {
  1230. // expected
  1231. }
  1232. try {
  1233. writeOnlyFileChannel.read(readBuffers, 0, 1);
  1234. fail("should throw ClosedChannelException");
  1235. } catch (ClosedChannelException e) {
  1236. // expected
  1237. }
  1238. try {
  1239. readWriteFileChannel.read(readBuffers, 0, 1);
  1240. fail("should throw ClosedChannelException");
  1241. } catch (ClosedChannelException e) {
  1242. // expected
  1243. }
  1244. }
  1245. /**
  1246. * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
  1247. */
  1248. public void test_read$LByteBufferII_WriteOnly() throws Exception {
  1249. ByteBuffer[] readBuffers = new ByteBuffer[2];
  1250. readBuffers[0] = ByteBuffer.allocate(CAPACITY);
  1251. try {
  1252. writeOnlyFileChannel.read(readBuffers, 0, 1);
  1253. fail("should throw NonReadableChannelException");
  1254. } catch (NonReadableChannelException e) {
  1255. // expected
  1256. }
  1257. // first throws NonReadableChannelException.
  1258. readBuffers[0] = null;
  1259. try {
  1260. writeOnlyFileChannel.read(readBuffers, 0, 1);
  1261. fail("should throw NonReadableChannelException");
  1262. } catch (NonReadableChannelException e) {
  1263. // expected
  1264. }
  1265. }
  1266. /**
  1267. * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
  1268. */
  1269. public void test_read$LByteBufferII_IndexOutOfBound() throws Exception {
  1270. ByteBuffer[] readBuffers = new ByteBuffer[2];
  1271. readBuffers[0] = ByteBuffer.allocate(CAPACITY);
  1272. readBuffers[1] = ByteBuffer.allocate(CAPACITY);
  1273. try {
  1274. readOnlyFileChannel.read(readBuffers, 2, 1);
  1275. fail();
  1276. } catch (IndexOutOfBoundsException expected) {
  1277. }
  1278. try {
  1279. readWriteFileChannel.read(null, -1, 0);
  1280. fail();
  1281. } catch (IndexOutOfBoundsException expected) {
  1282. } catch (NullPointerException expected) {
  1283. }
  1284. try {
  1285. writeOnlyFileChannel.read(readBuffers, 0, 3);
  1286. fail();
  1287. } catch (IndexOutOfBoundsException expected) {
  1288. }
  1289. try {
  1290. readWriteFileChannel.read(readBuffers, -1, 0);
  1291. fail();
  1292. } catch (IndexOutOfBoundsException expected) {
  1293. }
  1294. readWriteFileChannel.close();
  1295. try {
  1296. readWriteFileChannel.read(readBuffers, 0, 3);
  1297. fail();
  1298. } catch (IndexOutOfBoundsException expected) {
  1299. }
  1300. }
  1301. /**
  1302. * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
  1303. */
  1304. public void test_read$LByteBufferII_EmptyFile() throws Exception {
  1305. ByteBuffer[] readBuffers = new ByteBuffer[2];
  1306. readBuffers[0] = ByteBuffer.allocate(CAPACITY);
  1307. readBuffers[1] = ByteBuffer.allocate(CAPACITY);
  1308. long result = readOnlyFileChannel.read(readBuffers, 0, 2);
  1309. assertEquals(-1, result);
  1310. assertEquals(0, readBuffers[0].position());
  1311. assertEquals(0, readBuffers[1].position());
  1312. }
  1313. /**
  1314. * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
  1315. */
  1316. public void test_read$LByteBufferII_EmptyBuffers() throws Exception {
  1317. ByteBuffer[] readBuffers = new ByteBuffer[2];
  1318. try {
  1319. readOnlyFileChannel.read(readBuffers, 0, 2);
  1320. } catch (NullPointerException e) {
  1321. // expected
  1322. }
  1323. writeDataToFile(fileOfReadOnlyFileChannel);
  1324. readBuffers[0] = ByteBuffer.allocate(CAPACITY);
  1325. try {
  1326. readOnlyFileChannel.read(readBuffers, 0, 2);
  1327. } catch (NullPointerException e) {
  1328. // expected