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
17package org.apache.harmony.nio.tests.java.nio.channels;
18
19import dalvik.annotation.AndroidOnly;
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.File;
26import java.io.FileInputStream;
27import java.io.FileNotFoundException;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.RandomAccessFile;
31import java.io.UnsupportedEncodingException;
32import java.net.InetAddress;
33import java.net.InetSocketAddress;
34import java.nio.BufferOverflowException;
35import java.nio.ByteBuffer;
36import java.nio.MappedByteBuffer;
37import java.nio.ReadOnlyBufferException;
38import java.nio.channels.ClosedChannelException;
39import java.nio.channels.DatagramChannel;
40import java.nio.channels.FileChannel;
41import java.nio.channels.FileLock;
42import java.nio.channels.NonReadableChannelException;
43import java.nio.channels.NonWritableChannelException;
44import java.nio.channels.OverlappingFileLockException;
45import java.nio.channels.Pipe;
46import java.nio.channels.ReadableByteChannel;
47import java.nio.channels.ServerSocketChannel;
48import java.nio.channels.SocketChannel;
49import java.nio.channels.WritableByteChannel;
50import java.nio.channels.FileChannel.MapMode;
51import java.util.Arrays;
52import org.apache.harmony.luni.platform.Platform;
53
54import junit.framework.TestCase;
55
56@TestTargetClass(
57    value = FileChannel.class,
58    untestedMethods = {
59        @TestTargetNew(
60            level = TestLevel.NOT_NECESSARY,
61            notes = "empty protected constructor",
62            method = "FileChannel",
63            args = {}
64        )
65    }
66)
67public class FileChannelTest extends TestCase {
68
69    private static final int CAPACITY = 100;
70
71    private static final int LIMITED_CAPACITY = 2;
72
73    private static final int TIME_OUT = 10000;
74
75    private static final String CONTENT = "MYTESTSTRING needs to be a little long";
76
77    private static final byte[] TEST_BYTES;
78
79    static {
80        try {
81            TEST_BYTES = "test".getBytes("iso8859-1");
82        } catch (UnsupportedEncodingException e) {
83            throw new Error(e);
84        }
85    }
86
87    private static final int CONTENT_LENGTH = CONTENT.length();
88
89    private static final byte[] CONTENT_AS_BYTES = CONTENT.getBytes();
90
91    private static final int CONTENT_AS_BYTES_LENGTH = CONTENT_AS_BYTES.length;
92
93    private FileChannel readOnlyFileChannel;
94
95    private FileChannel writeOnlyFileChannel;
96
97    private FileChannel readWriteFileChannel;
98
99    private File fileOfReadOnlyFileChannel;
100
101    private File fileOfWriteOnlyFileChannel;
102
103    private File fileOfReadWriteFileChannel;
104
105    private ReadableByteChannel readByteChannel;
106
107    private WritableByteChannel writableByteChannel;
108
109    private DatagramChannel datagramChannelSender;
110
111    private DatagramChannel datagramChannelReceiver;
112
113    private ServerSocketChannel serverSocketChannel;
114
115    private SocketChannel socketChannelSender;
116
117    private SocketChannel socketChannelReceiver;
118
119    private Pipe pipe;
120
121    // to read content from FileChannel
122    private FileInputStream fis;
123
124    private FileLock fileLock;
125
126    protected void setUp() throws Exception {
127        fileOfReadOnlyFileChannel = File.createTempFile(
128                "File_of_readOnlyFileChannel", "tmp");
129        fileOfReadOnlyFileChannel.deleteOnExit();
130        fileOfWriteOnlyFileChannel = File.createTempFile(
131                "File_of_writeOnlyFileChannel", "tmp");
132        fileOfWriteOnlyFileChannel.deleteOnExit();
133        fileOfReadWriteFileChannel = File.createTempFile(
134                "File_of_readWriteFileChannel", "tmp");
135        fileOfReadWriteFileChannel.deleteOnExit();
136        fis = null;
137        fileLock = null;
138        readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel)
139                .getChannel();
140        writeOnlyFileChannel = new FileOutputStream(fileOfWriteOnlyFileChannel)
141                .getChannel();
142        readWriteFileChannel = new RandomAccessFile(fileOfReadWriteFileChannel,
143                "rw").getChannel();
144    }
145
146    protected void tearDown() {
147        if (null != readOnlyFileChannel) {
148            try {
149                readOnlyFileChannel.close();
150            } catch (IOException e) {
151                // do nothing
152            }
153        }
154        if (null != writeOnlyFileChannel) {
155            try {
156                writeOnlyFileChannel.close();
157            } catch (IOException e) {
158                // do nothing
159            }
160        }
161        if (null != readWriteFileChannel) {
162            try {
163                readWriteFileChannel.close();
164            } catch (IOException e) {
165                // do nothing
166            }
167        }
168        if (null != fis) {
169            try {
170                fis.close();
171            } catch (IOException e) {
172                // do nothing
173            }
174        }
175
176        if (null != fileLock) {
177            try {
178                fileLock.release();
179            } catch (IOException e) {
180                // do nothing
181            }
182        }
183
184        if (null != fileOfReadOnlyFileChannel) {
185            fileOfReadOnlyFileChannel.delete();
186        }
187        if (null != fileOfWriteOnlyFileChannel) {
188            fileOfWriteOnlyFileChannel.delete();
189        }
190        if (null != fileOfReadWriteFileChannel) {
191            fileOfReadWriteFileChannel.delete();
192        }
193        if (null != datagramChannelSender) {
194            try {
195                datagramChannelSender.close();
196            } catch (IOException e) {
197                // do nothing
198            }
199        }
200        if (null != datagramChannelReceiver) {
201            try {
202                datagramChannelReceiver.close();
203            } catch (IOException e) {
204                // do nothing
205            }
206        }
207        if (null != serverSocketChannel) {
208            try {
209                serverSocketChannel.close();
210            } catch (IOException e) {
211                // do nothing
212            }
213        }
214        if (null != socketChannelSender) {
215            try {
216                socketChannelSender.close();
217            } catch (IOException e) {
218                // do nothing
219            }
220        }
221        if (null != socketChannelReceiver) {
222            try {
223                socketChannelReceiver.close();
224            } catch (IOException e) {
225                // do nothing
226            }
227        }
228        if (null != pipe) {
229            if (null != pipe.source()) {
230                try {
231                    pipe.source().close();
232                } catch (IOException e) {
233                    // do nothing
234                }
235            }
236            if (null != pipe.sink()) {
237                try {
238                    pipe.sink().close();
239                } catch (IOException e) {
240                    // do nothing
241                }
242            }
243        }
244    }
245
246    /**
247     * @tests java.nio.channels.FileChannel#force(boolean)
248     */
249    @TestTargetNew(
250        level = TestLevel.PARTIAL_COMPLETE,
251        notes = "",
252        method = "force",
253        args = {boolean.class}
254    )
255    public void test_forceZ() throws Exception {
256        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
257        writeOnlyFileChannel.write(writeBuffer);
258        writeOnlyFileChannel.force(true);
259
260        byte[] readBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
261        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
262        fis.read(readBuffer);
263        assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer));
264
265        writeOnlyFileChannel.write(writeBuffer);
266        writeOnlyFileChannel.force(false);
267
268        readBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
269        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
270        fis.read(readBuffer);
271        assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer));
272    }
273
274    /**
275     * @tests java.nio.channels.FileChannel#force(boolean)
276     */
277    @TestTargetNew(
278        level = TestLevel.PARTIAL_COMPLETE,
279        notes = "",
280        method = "force",
281        args = {boolean.class}
282    )
283    public void test_forceZ_closed() throws Exception {
284        writeOnlyFileChannel.close();
285        try {
286            writeOnlyFileChannel.force(true);
287            fail("should throw ClosedChannelException");
288        } catch (ClosedChannelException e) {
289            // expected
290        }
291
292        try {
293            writeOnlyFileChannel.force(false);
294            fail("should throw ClosedChannelException");
295        } catch (ClosedChannelException e) {
296            // expected
297        }
298    }
299
300    /**
301     * @tests java.nio.channels.FileChannel#force(boolean)
302     */
303    @TestTargetNew(
304        level = TestLevel.PARTIAL_COMPLETE,
305        notes = "",
306        method = "force",
307        args = {boolean.class}
308    )
309    public void test_forceZ_ReadOnlyChannel() throws Exception {
310        // force on a read only file channel has no effect.
311        readOnlyFileChannel.force(true);
312        readOnlyFileChannel.force(false);
313    }
314
315    /**
316     * @tests java.nio.channels.FileChannel#position()
317     */
318    @TestTargetNew(
319        level = TestLevel.PARTIAL_COMPLETE,
320        notes = "",
321        method = "position",
322        args = {}
323    )
324    public void test_position_Init() throws Exception {
325        assertEquals(0, readOnlyFileChannel.position());
326        assertEquals(0, writeOnlyFileChannel.position());
327        assertEquals(0, readWriteFileChannel.position());
328    }
329
330    /**
331     * @tests java.nio.channels.FileChannel#position()
332     */
333    @TestTargetNew(
334        level = TestLevel.PARTIAL_COMPLETE,
335        notes = "",
336        method = "position",
337        args = {}
338    )
339    public void test_position_ReadOnly() throws Exception {
340        writeDataToFile(fileOfReadOnlyFileChannel);
341
342        assertEquals(0, readOnlyFileChannel.position());
343        ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
344        readOnlyFileChannel.read(readBuffer);
345        assertEquals(CONTENT_LENGTH, readOnlyFileChannel.position());
346    }
347
348    /**
349     * Initializes test file.
350     *
351     * @param file
352     * @throws FileNotFoundException
353     * @throws IOException
354     */
355    private void writeDataToFile(File file) throws FileNotFoundException,
356            IOException {
357        FileOutputStream fos = new FileOutputStream(file);
358        try {
359            fos.write(CONTENT_AS_BYTES);
360        } finally {
361            fos.close();
362        }
363    }
364
365    /**
366     * Initializes large test file.
367     *
368     * @param file the file to be written
369     * @param size the content size to be written
370     * @throws FileNotFoundException
371     * @throws IOException
372     */
373    private void writeLargeDataToFile(File file, int size)
374            throws FileNotFoundException, IOException {
375        FileOutputStream fos = new FileOutputStream(file);
376        byte[] buf = new byte[size];
377
378        try {
379            // we don't care about content - just need a particular file size
380            fos.write(buf);
381        } finally {
382            fos.close();
383        }
384    }
385
386    /**
387     * @tests java.nio.channels.FileChannel#position()
388     */
389    @TestTargetNew(
390        level = TestLevel.PARTIAL_COMPLETE,
391        notes = "",
392        method = "position",
393        args = {}
394    )
395    public void test_position_WriteOnly() throws Exception {
396        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
397        writeOnlyFileChannel.write(writeBuffer);
398        assertEquals(CONTENT_LENGTH, writeOnlyFileChannel.position());
399    }
400
401    /**
402     * @tests java.nio.channels.FileChannel#position()
403     */
404    @TestTargetNew(
405        level = TestLevel.PARTIAL_COMPLETE,
406        notes = "",
407        method = "position",
408        args = {}
409    )
410    public void test_position_ReadWrite() throws Exception {
411        writeDataToFile(fileOfReadWriteFileChannel);
412
413        assertEquals(0, readWriteFileChannel.position());
414        ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
415        readWriteFileChannel.read(readBuffer);
416        assertEquals(CONTENT_LENGTH, readWriteFileChannel.position());
417
418        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
419        readWriteFileChannel.write(writeBuffer);
420        assertEquals(CONTENT_LENGTH * 2, readWriteFileChannel.position());
421    }
422
423    /**
424     * @tests java.nio.channels.FileChannel#position()
425     */
426    @TestTargetNew(
427        level = TestLevel.PARTIAL_COMPLETE,
428        notes = "Verifies ClosedChannelException.",
429        method = "position",
430        args = {}
431    )
432    public void test_position_Closed() throws Exception {
433        readOnlyFileChannel.close();
434        try {
435            readOnlyFileChannel.position();
436            fail("should throw ClosedChannelException");
437        } catch (ClosedChannelException e) {
438            // expected
439        }
440
441        writeOnlyFileChannel.close();
442        try {
443            writeOnlyFileChannel.position();
444            fail("should throw ClosedChannelException");
445        } catch (ClosedChannelException e) {
446            // expected
447        }
448
449        readWriteFileChannel.close();
450        try {
451            readWriteFileChannel.position();
452            fail("should throw ClosedChannelException");
453        } catch (ClosedChannelException e) {
454            // expected
455        }
456    }
457
458    /**
459     * @tests java.nio.channels.FileChannel#position(long)
460     */
461    @TestTargetNew(
462        level = TestLevel.PARTIAL_COMPLETE,
463        notes = "Verifies ClosedChannelException.",
464        method = "position",
465        args = {long.class}
466    )
467    public void test_positionJ_Closed() throws Exception {
468        final long POSITION = 100;
469
470        readOnlyFileChannel.close();
471        try {
472            readOnlyFileChannel.position(POSITION);
473            fail("should throw ClosedChannelException");
474        } catch (ClosedChannelException e) {
475            // expected
476        }
477
478        writeOnlyFileChannel.close();
479        try {
480            writeOnlyFileChannel.position(POSITION);
481            fail("should throw ClosedChannelException");
482        } catch (ClosedChannelException e) {
483            // expected
484        }
485
486        readWriteFileChannel.close();
487        try {
488            readWriteFileChannel.position(POSITION);
489            fail("should throw ClosedChannelException");
490        } catch (ClosedChannelException e) {
491            // expected
492        }
493    }
494
495    /**
496     * @tests java.nio.channels.FileChannel#position(long)
497     */
498    @TestTargetNew(
499        level = TestLevel.PARTIAL_COMPLETE,
500        notes = "Verifies IllegalArgumentException.",
501        method = "position",
502        args = {long.class}
503    )
504    public void test_positionJ_Negative() throws Exception {
505        final long NEGATIVE_POSITION = -1;
506        try {
507            readOnlyFileChannel.position(NEGATIVE_POSITION);
508            fail("should throw IllegalArgumentException");
509        } catch (IllegalArgumentException e) {
510            // expected
511        }
512
513        try {
514            writeOnlyFileChannel.position(NEGATIVE_POSITION);
515            fail("should throw IllegalArgumentException");
516        } catch (IllegalArgumentException e) {
517            // expected
518        }
519
520        try {
521            readWriteFileChannel.position(NEGATIVE_POSITION);
522            fail("should throw IllegalArgumentException");
523        } catch (IllegalArgumentException e) {
524            // expected
525        }
526    }
527
528    /**
529     * @tests java.nio.channels.FileChannel#position(long)
530     */
531    @TestTargetNew(
532        level = TestLevel.PARTIAL_COMPLETE,
533        notes = "",
534        method = "position",
535        args = {long.class}
536    )
537    public void test_positionJ_ReadOnly() throws Exception {
538        writeDataToFile(fileOfReadOnlyFileChannel);
539
540        // set the position of the read only file channel to POSITION
541        final int POSITION = 4;
542        readOnlyFileChannel.position(POSITION);
543
544        // reads the content left to readBuffer through read only file channel
545        ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
546        int count = readOnlyFileChannel.read(readBuffer);
547        assertEquals(CONTENT_LENGTH - POSITION, count);
548
549        // asserts the content read is the part which stays beyond the POSITION
550        readBuffer.flip();
551        int i = POSITION;
552        while (readBuffer.hasRemaining()) {
553            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
554            i++;
555        }
556    }
557
558    /**
559     * @tests java.nio.channels.FileChannel#position(long)
560     */
561    @TestTargetNew(
562        level = TestLevel.PARTIAL_COMPLETE,
563        notes = "",
564        method = "position",
565        args = {long.class}
566    )
567    public void test_positionJ_WriteOnly() throws Exception {
568        writeDataToFile(fileOfWriteOnlyFileChannel);
569
570        // init data to write
571        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
572
573        // set the position of the write only file channel to POSITION
574        final int POSITION = 4;
575        writeOnlyFileChannel.position(POSITION);
576
577        // writes to the write only file channel
578        writeOnlyFileChannel.write(writeBuffer);
579        // force to write out.
580        writeOnlyFileChannel.close();
581
582        // gets the result of the write only file channel
583        byte[] result = new byte[POSITION + CONTENT_LENGTH];
584        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
585        fis.read(result);
586
587        // constructs the expected result which has content[0... POSITION] plus
588        // content[0...length()]
589        byte[] expectedResult = new byte[POSITION + CONTENT_LENGTH];
590        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, POSITION);
591        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, POSITION,
592                CONTENT_LENGTH);
593
594        // asserts result of the write only file channel same as expected
595        assertTrue(Arrays.equals(expectedResult, result));
596    }
597
598    /**
599     * @tests java.nio.channels.FileChannel#size()
600     */
601    @TestTargetNew(
602        level = TestLevel.PARTIAL_COMPLETE,
603        notes = "Verifies zero size.",
604        method = "size",
605        args = {}
606    )
607    public void test_size_Init() throws Exception {
608        assertEquals(0, readOnlyFileChannel.size());
609        assertEquals(0, writeOnlyFileChannel.size());
610        assertEquals(0, readWriteFileChannel.size());
611    }
612
613    /**
614     * @tests java.nio.channels.FileChannel#size()
615     */
616    @TestTargetNew(
617        level = TestLevel.PARTIAL_COMPLETE,
618        notes = "Verifies positive case.",
619        method = "size",
620        args = {}
621    )
622    public void test_size() throws Exception {
623        writeDataToFile(fileOfReadOnlyFileChannel);
624        assertEquals(fileOfReadOnlyFileChannel.length(), readOnlyFileChannel
625                .size());
626    }
627
628    /**
629     * @tests java.nio.channels.FileChannel#size()
630     */
631    @TestTargetNew(
632        level = TestLevel.PARTIAL_COMPLETE,
633        notes = "Verifies ClosedChannelException.",
634        method = "size",
635        args = {}
636    )
637    public void test_size_Closed() throws Exception {
638        readOnlyFileChannel.close();
639        try {
640            readOnlyFileChannel.size();
641            fail("should throw ClosedChannelException");
642        } catch (ClosedChannelException e) {
643            // expected
644        }
645
646        writeOnlyFileChannel.close();
647        try {
648            writeOnlyFileChannel.size();
649            fail("should throw ClosedChannelException");
650        } catch (ClosedChannelException e) {
651            // expected
652        }
653
654        readWriteFileChannel.close();
655        try {
656            readWriteFileChannel.size();
657            fail("should throw ClosedChannelException");
658        } catch (ClosedChannelException e) {
659            // expected
660        }
661    }
662
663    /**
664     * @tests java.nio.channels.FileChannel#truncate(long)
665     */
666    @TestTargetNew(
667        level = TestLevel.PARTIAL_COMPLETE,
668        notes = "Verifies ClosedChannelException.",
669        method = "truncate",
670        args = {long.class}
671    )
672    public void test_truncateJ_Closed() throws Exception {
673        readOnlyFileChannel.close();
674        try {
675            readOnlyFileChannel.truncate(0);
676            fail("should throw ClosedChannelException");
677        } catch (ClosedChannelException e) {
678            // expected
679        }
680
681        writeOnlyFileChannel.close();
682        try {
683            writeOnlyFileChannel.truncate(0);
684            fail("should throw ClosedChannelException");
685        } catch (ClosedChannelException e) {
686            // expected
687        }
688
689        readWriteFileChannel.close();
690        try {
691            readWriteFileChannel.truncate(-1);
692            fail("should throw ClosedChannelException");
693        } catch (ClosedChannelException e) {
694            // expected
695        }
696    }
697
698    /**
699     * @tests java.nio.channels.FileChannel#truncate(long)
700     */
701    @TestTargetNew(
702        level = TestLevel.PARTIAL_COMPLETE,
703        notes = "Verifies IllegalArgumentException.",
704        method = "truncate",
705        args = {long.class}
706    )
707    public void test_truncateJ_IllegalArgument() throws Exception {
708        // regression test for Harmony-941
709        try {
710            readOnlyFileChannel.truncate(-1);
711            fail("should throw IllegalArgumentException");
712        } catch (IllegalArgumentException e) {
713            // expected
714        }
715
716        try {
717            writeOnlyFileChannel.truncate(-1);
718            fail("should throw IllegalArgumentException");
719        } catch (IllegalArgumentException e) {
720            // expected
721        }
722
723        try {
724            readWriteFileChannel.truncate(-1);
725            fail("should throw IllegalArgumentException");
726        } catch (IllegalArgumentException e) {
727            // expected
728        }
729    }
730
731    /**
732     * @tests java.nio.channels.FileChannel#truncate(long)
733     */
734    @TestTargetNew(
735        level = TestLevel.PARTIAL_COMPLETE,
736        notes = "Verifies NonWritableChannelException.",
737        method = "truncate",
738        args = {long.class}
739    )
740    public void test_truncateJ_ReadOnly() throws Exception {
741        writeDataToFile(fileOfReadOnlyFileChannel);
742        try {
743            readOnlyFileChannel.truncate(readOnlyFileChannel.size());
744            fail("should throw NonWritableChannelException.");
745        } catch (NonWritableChannelException e) {
746            // expected
747        }
748
749        try {
750            readOnlyFileChannel.truncate(0);
751            fail("should throw NonWritableChannelException.");
752        } catch (NonWritableChannelException e) {
753            // expected
754        }
755    }
756
757    /**
758     * @tests java.nio.channels.FileChannel#truncate(long)
759     */
760    @TestTargetNew(
761        level = TestLevel.PARTIAL_COMPLETE,
762        notes = "Doesn't verify exceptions.",
763        method = "truncate",
764        args = {long.class}
765    )
766    public void test_truncateJ() throws Exception {
767        writeDataToFile(fileOfReadWriteFileChannel);
768
769        int truncateLength = CONTENT_LENGTH + 2;
770        assertEquals(readWriteFileChannel, readWriteFileChannel
771                .truncate(truncateLength));
772        assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
773
774        truncateLength = CONTENT_LENGTH;
775        assertEquals(readWriteFileChannel, readWriteFileChannel
776                .truncate(truncateLength));
777        assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length());
778
779        truncateLength = CONTENT_LENGTH / 2;
780        assertEquals(readWriteFileChannel, readWriteFileChannel
781                .truncate(truncateLength));
782        assertEquals(truncateLength, fileOfReadWriteFileChannel.length());
783    }
784
785    /**
786     * @tests java.nio.channels.FileChannel#lock()
787     */
788    @TestTargetNew(
789        level = TestLevel.COMPLETE,
790        notes = "",
791        method = "lock",
792        args = {}
793    )
794    public void test_lock() throws Exception {
795        MockFileChannel mockFileChannel = new MockFileChannel();
796        // Verify that calling lock() leads to the method
797        // lock(long, long, boolean) being called with a 0 for the
798        // first parameter, Long.MAX_VALUE as the second parameter and false
799        // as the third parameter.
800        mockFileChannel.lock();
801        assertTrue(mockFileChannel.isLockCalled);
802    }
803
804    /**
805     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
806     */
807    @TestTargetNew(
808        level = TestLevel.PARTIAL_COMPLETE,
809        notes = "Verifies ClosedChannelException.",
810        method = "lock",
811        args = {long.class, long.class, boolean.class}
812    )
813    public void test_lockJJZ_Closed() throws Exception {
814        readOnlyFileChannel.close();
815        try {
816            readOnlyFileChannel.lock(0, 10, false);
817            fail("should throw ClosedChannelException");
818        } catch (ClosedChannelException e) {
819            // expected
820        }
821
822        writeOnlyFileChannel.close();
823        try {
824            writeOnlyFileChannel.lock(0, 10, false);
825            fail("should throw ClosedChannelException");
826        } catch (ClosedChannelException e) {
827            // expected
828        }
829
830        readWriteFileChannel.close();
831        try {
832            readWriteFileChannel.lock(0, 10, false);
833            fail("should throw ClosedChannelException");
834        } catch (ClosedChannelException e) {
835            // expected
836        }
837
838        // throws ClosedChannelException before IllegalArgumentException
839        try {
840            readWriteFileChannel.lock(-1, 0, false);
841            fail("should throw ClosedChannelException");
842        } catch (ClosedChannelException e) {
843            // expected
844        }
845    }
846
847    /**
848     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
849     */
850    @TestTargetNew(
851        level = TestLevel.PARTIAL_COMPLETE,
852        notes = "Verifies IllegalArgumentException.",
853        method = "lock",
854        args = {long.class, long.class, boolean.class}
855    )
856    public void test_lockJJZ_IllegalArgument() throws Exception {
857        try {
858            writeOnlyFileChannel.lock(0, -1, false);
859            fail("should throw IllegalArgumentException");
860        } catch (IllegalArgumentException e) {
861            // expected
862        }
863
864        try {
865            writeOnlyFileChannel.lock(-1, 0, false);
866            fail("should throw IllegalArgumentException");
867        } catch (IllegalArgumentException e) {
868            // expected
869        }
870
871        try {
872            readWriteFileChannel.lock(-1, -1, false);
873            fail("should throw IllegalArgumentException");
874        } catch (IllegalArgumentException e) {
875            // expected
876        }
877
878        try {
879            readWriteFileChannel.lock(Long.MAX_VALUE, 1, false);
880            fail("should throw IllegalArgumentException");
881        } catch (IllegalArgumentException e) {
882            // expected
883        }
884    }
885
886    /**
887     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
888     */
889    @TestTargetNew(
890        level = TestLevel.PARTIAL_COMPLETE,
891        notes = "Verifies NonWritableChannelException.",
892        method = "lock",
893        args = {long.class, long.class, boolean.class}
894    )
895    public void test_lockJJZ_NonWritable() throws Exception {
896        try {
897            readOnlyFileChannel.lock(0, 10, false);
898            fail("should throw NonWritableChannelException");
899        } catch (NonWritableChannelException e) {
900            // expected
901        }
902
903        // throws NonWritableChannelException before IllegalArgumentException
904        try {
905            readOnlyFileChannel.lock(-1, 0, false);
906            fail("should throw NonWritableChannelException");
907        } catch (NonWritableChannelException e) {
908            // expected
909        }
910    }
911
912    /**
913     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
914     */
915    @TestTargetNew(
916        level = TestLevel.PARTIAL_COMPLETE,
917        notes = "Verifies NonReadableChannelException.",
918        method = "lock",
919        args = {long.class, long.class, boolean.class}
920    )
921    public void test_lockJJZ_NonReadable() throws Exception {
922        try {
923            writeOnlyFileChannel.lock(0, 10, true);
924            fail("should throw NonReadableChannelException");
925        } catch (NonReadableChannelException e) {
926            // expected
927        }
928
929        // throws NonReadableChannelException before IllegalArgumentException
930        try {
931            writeOnlyFileChannel.lock(-1, 0, true);
932            fail("should throw NonReadableChannelException");
933        } catch (NonReadableChannelException e) {
934            // expected
935        }
936    }
937
938    /**
939     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
940     */
941    @TestTargetNew(
942        level = TestLevel.PARTIAL_COMPLETE,
943        notes = "Verifies shared channel.",
944        method = "lock",
945        args = {long.class, long.class, boolean.class}
946    )
947    public void test_lockJJZ_Shared() throws Exception {
948        final long POSITION = 100;
949        final long SIZE = 200;
950        fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true);
951        assertTrue(fileLock.isValid());
952        // fileLock.isShared depends on whether the underlying platform support
953        // shared lock, but it works on Windows & Linux.
954        assertTrue(fileLock.isShared());
955        assertSame(readOnlyFileChannel, fileLock.channel());
956        assertEquals(POSITION, fileLock.position());
957        assertEquals(SIZE, fileLock.size());
958    }
959
960    /**
961     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
962     */
963    @TestTargetNew(
964        level = TestLevel.PARTIAL_COMPLETE,
965        notes = "Verifies that unshared channel.",
966        method = "lock",
967        args = {long.class, long.class, boolean.class}
968    )
969    public void test_lockJJZ_NotShared() throws Exception {
970        final long POSITION = 100;
971        final long SIZE = 200;
972        fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
973        assertTrue(fileLock.isValid());
974        assertFalse(fileLock.isShared());
975        assertSame(writeOnlyFileChannel, fileLock.channel());
976        assertEquals(POSITION, fileLock.position());
977        assertEquals(SIZE, fileLock.size());
978    }
979
980    /**
981     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
982     */
983    @TestTargetNew(
984        level = TestLevel.PARTIAL_COMPLETE,
985        notes = "Verifies lock method with Long max value as a size.",
986        method = "lock",
987        args = {long.class, long.class, boolean.class}
988    )
989    public void test_lockJJZ_Long_MAX_VALUE() throws Exception {
990        final long POSITION = 0;
991        final long SIZE = Long.MAX_VALUE;
992        fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true);
993        assertTrue(fileLock.isValid());
994        assertTrue(fileLock.isShared());
995        assertEquals(POSITION, fileLock.position());
996        assertEquals(SIZE, fileLock.size());
997        assertSame(readOnlyFileChannel, fileLock.channel());
998    }
999
1000    /**
1001     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
1002     */
1003    @TestTargetNew(
1004        level = TestLevel.PARTIAL_COMPLETE,
1005        notes = "Verifies OverlappingFileLockException.",
1006        method = "lock",
1007        args = {long.class, long.class, boolean.class}
1008    )
1009    public void test_lockJJZ_Overlapping() throws Exception {
1010        final long POSITION = 100;
1011        final long SIZE = 200;
1012        fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
1013        assertTrue(fileLock.isValid());
1014
1015        try {
1016            writeOnlyFileChannel.lock(POSITION + 1, SIZE, false);
1017            fail("should throw OverlappingFileLockException");
1018        } catch (OverlappingFileLockException e) {
1019            // expected
1020        }
1021    }
1022
1023    /**
1024     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
1025     */
1026    @TestTargetNew(
1027        level = TestLevel.PARTIAL_COMPLETE,
1028        notes = "Verifies that not overlaping regions can be locked.",
1029        method = "lock",
1030        args = {long.class, long.class, boolean.class}
1031    )
1032    public void test_lockJJZ_NotOverlapping() throws Exception {
1033        final long POSITION = 100;
1034        final long SIZE = 200;
1035        FileLock fileLock1 = writeOnlyFileChannel.lock(POSITION, SIZE, false);
1036        assertTrue(fileLock1.isValid());
1037        FileLock fileLock2 = writeOnlyFileChannel.lock(POSITION + SIZE, SIZE,
1038                false);
1039        assertTrue(fileLock2.isValid());
1040    }
1041
1042    /**
1043     * @tests java.nio.channels.FileChannel#lock(long,long,boolean)
1044     */
1045    @TestTargetNew(
1046        level = TestLevel.PARTIAL_COMPLETE,
1047        notes = "Verifies functionality after release method.",
1048        method = "lock",
1049        args = {long.class, long.class, boolean.class}
1050    )
1051    public void test_lockJJZ_After_Release() throws Exception {
1052        fileLock = writeOnlyFileChannel.lock(0, 10, false);
1053        fileLock.release();
1054        // after release file lock can be obtained again.
1055        fileLock = writeOnlyFileChannel.lock(0, 10, false);
1056        assertTrue(fileLock.isValid());
1057    }
1058
1059    /**
1060     * @tests java.nio.channels.FileChannel#tryLock()
1061     */
1062    @TestTargetNew(
1063        level = TestLevel.COMPLETE,
1064        notes = "",
1065        method = "tryLock",
1066        args = {}
1067    )
1068    public void test_tryLock() throws Exception {
1069        MockFileChannel mockFileChannel = new MockFileChannel();
1070        // Verify that calling tryLock() leads to the method
1071        // tryLock(long, long, boolean) being called with a 0 for the
1072        // first parameter, Long.MAX_VALUE as the second parameter and false
1073        // as the third parameter.
1074        mockFileChannel.tryLock();
1075        assertTrue(mockFileChannel.isTryLockCalled);
1076    }
1077
1078    /**
1079     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1080     */
1081    @TestTargetNew(
1082        level = TestLevel.PARTIAL_COMPLETE,
1083        notes = "Verifies ClosedChannelException.",
1084        method = "tryLock",
1085        args = {long.class, long.class, boolean.class}
1086    )
1087    public void test_tryLockJJZ_Closed() throws Exception {
1088        readOnlyFileChannel.close();
1089        try {
1090            readOnlyFileChannel.tryLock(0, 10, false);
1091            fail("should throw ClosedChannelException");
1092        } catch (ClosedChannelException e) {
1093            // expected
1094        }
1095
1096        writeOnlyFileChannel.close();
1097        try {
1098            writeOnlyFileChannel.tryLock(0, 10, false);
1099            fail("should throw ClosedChannelException");
1100        } catch (ClosedChannelException e) {
1101            // expected
1102        }
1103
1104        readWriteFileChannel.close();
1105        try {
1106            readWriteFileChannel.tryLock(0, 10, false);
1107            fail("should throw ClosedChannelException");
1108        } catch (ClosedChannelException e) {
1109            // expected
1110        }
1111
1112        // throws ClosedChannelException before IllegalArgumentException
1113        try {
1114            readWriteFileChannel.tryLock(-1, 0, false);
1115            fail("should throw ClosedChannelException");
1116        } catch (ClosedChannelException e) {
1117            // expected
1118        }
1119    }
1120
1121    /**
1122     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1123     */
1124    @TestTargetNew(
1125        level = TestLevel.PARTIAL_COMPLETE,
1126        notes = "Verifies IllegalArgumentException.",
1127        method = "tryLock",
1128        args = {long.class, long.class, boolean.class}
1129    )
1130    public void test_tryLockJJZ_IllegalArgument() throws Exception {
1131        try {
1132            writeOnlyFileChannel.tryLock(0, -1, false);
1133            fail("should throw IllegalArgumentException");
1134        } catch (IllegalArgumentException e) {
1135            // expected
1136        }
1137
1138        try {
1139            writeOnlyFileChannel.tryLock(-1, 0, false);
1140            fail("should throw IllegalArgumentException");
1141        } catch (IllegalArgumentException e) {
1142            // expected
1143        }
1144
1145        try {
1146            readWriteFileChannel.tryLock(-1, -1, false);
1147            fail("should throw IllegalArgumentException");
1148        } catch (IllegalArgumentException e) {
1149            // expected
1150        }
1151
1152        try {
1153            readWriteFileChannel.tryLock(Long.MAX_VALUE, 1, false);
1154            fail("should throw IllegalArgumentException");
1155        } catch (IllegalArgumentException e) {
1156            // expected
1157        }
1158
1159        // BEGIN android-added
1160        // Android uses 32-bit off_t, so anything larger than a signed 32-bit int won't work...
1161        // ...except for the special case of length == Long.MAX_VALUE, which is used to mean "the
1162        // whole file". The special case is tested elsewhere.
1163        long tooBig = ((long) Integer.MAX_VALUE) + 1;
1164        try {
1165            readWriteFileChannel.tryLock(tooBig, 1, false);
1166            fail("should throw IOException");
1167        } catch (IOException e) {
1168            // expected
1169        }
1170        try {
1171            readWriteFileChannel.tryLock(0, tooBig, false);
1172            fail("should throw IOException");
1173        } catch (IOException e) {
1174            // expected
1175        }
1176        // END android-added
1177    }
1178
1179    /**
1180     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1181     */
1182    @TestTargetNew(
1183        level = TestLevel.PARTIAL_COMPLETE,
1184        notes = "Verifies NonWritableChannelException.",
1185        method = "tryLock",
1186        args = {long.class, long.class, boolean.class}
1187    )
1188    public void test_tryLockJJZ_NonWritable() throws Exception {
1189        try {
1190            readOnlyFileChannel.tryLock(0, 10, false);
1191            fail("should throw NonWritableChannelException");
1192        } catch (NonWritableChannelException e) {
1193            // expected
1194        }
1195
1196        // throws NonWritableChannelException before IllegalArgumentException
1197        try {
1198            readOnlyFileChannel.tryLock(-1, 0, false);
1199            fail("should throw NonWritableChannelException");
1200        } catch (NonWritableChannelException e) {
1201            // expected
1202        }
1203    }
1204
1205    /**
1206     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1207     */
1208    @TestTargetNew(
1209        level = TestLevel.PARTIAL_COMPLETE,
1210        notes = "Verifies NonReadableChannelException.",
1211        method = "tryLock",
1212        args = {long.class, long.class, boolean.class}
1213    )
1214    public void test_tryLockJJZ_NonReadable() throws Exception {
1215        try {
1216            writeOnlyFileChannel.tryLock(0, 10, true);
1217            fail("should throw NonReadableChannelException");
1218        } catch (NonReadableChannelException e) {
1219            // expected
1220        }
1221
1222        // throws NonReadableChannelException before IllegalArgumentException
1223        try {
1224            writeOnlyFileChannel.tryLock(-1, 0, true);
1225            fail("should throw NonReadableChannelException");
1226        } catch (NonReadableChannelException e) {
1227            // expected
1228        }
1229    }
1230
1231    /**
1232     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1233     */
1234    @TestTargetNew(
1235        level = TestLevel.PARTIAL_COMPLETE,
1236        notes = "",
1237        method = "tryLock",
1238        args = {long.class, long.class, boolean.class}
1239    )
1240    public void test_tryLockJJZ_Shared() throws Exception {
1241        final long POSITION = 100;
1242        final long SIZE = 200;
1243        fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true);
1244        assertTrue(fileLock.isValid());
1245        // fileLock.isShared depends on whether the underlying platform support
1246        // shared lock, but it works on Windows & Linux.
1247        assertTrue(fileLock.isShared());
1248        assertSame(readOnlyFileChannel, fileLock.channel());
1249        assertEquals(POSITION, fileLock.position());
1250        assertEquals(SIZE, fileLock.size());
1251    }
1252
1253    /**
1254     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1255     */
1256    @TestTargetNew(
1257        level = TestLevel.PARTIAL_COMPLETE,
1258        notes = "",
1259        method = "tryLock",
1260        args = {long.class, long.class, boolean.class}
1261    )
1262    public void test_tryLockJJZ_NotShared() throws Exception {
1263        final long POSITION = 100;
1264        final long SIZE = 200;
1265        fileLock = writeOnlyFileChannel.tryLock(POSITION, SIZE, false);
1266        assertTrue(fileLock.isValid());
1267        assertFalse(fileLock.isShared());
1268        assertSame(writeOnlyFileChannel, fileLock.channel());
1269        assertEquals(POSITION, fileLock.position());
1270        assertEquals(SIZE, fileLock.size());
1271    }
1272
1273    /**
1274     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1275     */
1276    @TestTargetNew(
1277        level = TestLevel.PARTIAL_COMPLETE,
1278        notes = "",
1279        method = "tryLock",
1280        args = {long.class, long.class, boolean.class}
1281    )
1282    public void test_tryLockJJZ_Long_MAX_VALUE() throws Exception {
1283        final long POSITION = 0;
1284        final long SIZE = Long.MAX_VALUE;
1285        fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true);
1286        assertTrue(fileLock.isValid());
1287        assertTrue(fileLock.isShared());
1288        assertEquals(POSITION, fileLock.position());
1289        assertEquals(SIZE, fileLock.size());
1290        assertSame(readOnlyFileChannel, fileLock.channel());
1291    }
1292
1293    /**
1294     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1295     */
1296    @TestTargetNew(
1297        level = TestLevel.PARTIAL_COMPLETE,
1298        notes = "Verifies OverlappingFileLockException.",
1299        method = "tryLock",
1300        args = {long.class, long.class, boolean.class}
1301    )
1302    public void test_tryLockJJZ_Overlapping() throws Exception {
1303        final long POSITION = 100;
1304        final long SIZE = 200;
1305        fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false);
1306        assertTrue(fileLock.isValid());
1307
1308        try {
1309            writeOnlyFileChannel.lock(POSITION + 1, SIZE, false);
1310            fail("should throw OverlappingFileLockException");
1311        } catch (OverlappingFileLockException e) {
1312            // expected
1313        }
1314    }
1315
1316    /**
1317     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
1318     */
1319    @TestTargetNew(
1320        level = TestLevel.PARTIAL_COMPLETE,
1321        notes = "",
1322        method = "tryLock",
1323        args = {long.class, long.class, boolean.class}
1324    )
1325    public void test_tryLockJJZ_NotOverlapping() throws Exception {
1326        final long POSITION = 100;
1327        final long SIZE = 200;
1328        FileLock fileLock1 = writeOnlyFileChannel
1329                .tryLock(POSITION, SIZE, false);
1330        assertTrue(fileLock1.isValid());
1331
1332        FileLock fileLock2 = writeOnlyFileChannel.tryLock(POSITION + SIZE,
1333                SIZE, false);
1334        assertTrue(fileLock2.isValid());
1335    }
1336
1337    /**
1338     * @tests java.nio.channels.FileChannel#tryLock(long,long,boolean)
1339     */
1340    @TestTargetNew(
1341        level = TestLevel.PARTIAL_COMPLETE,
1342        notes = "",
1343        method = "tryLock",
1344        args = {long.class, long.class, boolean.class}
1345    )
1346    public void test_tryLockJJZ_After_Release() throws Exception {
1347        fileLock = writeOnlyFileChannel.tryLock(0, 10, false);
1348        fileLock.release();
1349
1350        // after release file lock can be obtained again.
1351        fileLock = writeOnlyFileChannel.tryLock(0, 10, false);
1352        assertTrue(fileLock.isValid());
1353    }
1354
1355    /**
1356     * @tests java.nio.channels.FileChannel#read(ByteBuffer)
1357     */
1358    @TestTargetNew(
1359        level = TestLevel.PARTIAL_COMPLETE,
1360        notes = "Verifies NullPointerException.",
1361        method = "read",
1362        args = {java.nio.ByteBuffer.class}
1363    )
1364    public void test_readLByteBuffer_Null() throws Exception {
1365        ByteBuffer readBuffer = null;
1366
1367        try {
1368            readOnlyFileChannel.read(readBuffer);
1369            fail("should throw NullPointerException");
1370        } catch (NullPointerException e) {
1371            // expected
1372        }
1373
1374        try {
1375            readWriteFileChannel.read(readBuffer);
1376            fail("should throw NullPointerException");
1377        } catch (NullPointerException e) {
1378            // expected
1379        }
1380    }
1381
1382    /**
1383     * @tests java.nio.channels.FileChannel#read(ByteBuffer)
1384     */
1385    @TestTargetNew(
1386        level = TestLevel.PARTIAL_COMPLETE,
1387        notes = "Verifies ClosedChannelException.",
1388        method = "read",
1389        args = {java.nio.ByteBuffer.class}
1390    )
1391    public void test_readLByteBuffer_Closed() throws Exception {
1392        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1393
1394        readOnlyFileChannel.close();
1395        try {
1396            readOnlyFileChannel.read(readBuffer);
1397            fail("should throw ClosedChannelException");
1398        } catch (ClosedChannelException e) {
1399            // expected
1400        }
1401
1402        writeOnlyFileChannel.close();
1403        try {
1404            writeOnlyFileChannel.read(readBuffer);
1405            fail("should throw ClosedChannelException");
1406        } catch (ClosedChannelException e) {
1407            // expected
1408        }
1409
1410        readWriteFileChannel.close();
1411        try {
1412            readWriteFileChannel.read(readBuffer);
1413            fail("should throw ClosedChannelException");
1414        } catch (ClosedChannelException e) {
1415            // expected
1416        }
1417
1418        // should throw ClosedChannelException first
1419        readBuffer = null;
1420        try {
1421            readOnlyFileChannel.read(readBuffer);
1422            fail("should throw ClosedChannelException");
1423        } catch (ClosedChannelException e) {
1424            // expected
1425        }
1426
1427        try {
1428            writeOnlyFileChannel.read(readBuffer);
1429            fail("should throw ClosedChannelException");
1430        } catch (ClosedChannelException e) {
1431            // expected
1432        }
1433
1434        try {
1435            readWriteFileChannel.read(readBuffer);
1436            fail("should throw ClosedChannelException");
1437        } catch (ClosedChannelException e) {
1438            // expected
1439        }
1440    }
1441
1442    /**
1443     * @tests java.nio.channels.FileChannel#read(ByteBuffer)
1444     */
1445    @TestTargetNew(
1446        level = TestLevel.PARTIAL_COMPLETE,
1447        notes = "Verifies NonReadableChannelException.",
1448        method = "read",
1449        args = {java.nio.ByteBuffer.class}
1450    )
1451    public void test_readLByteBuffer_WriteOnly() throws Exception {
1452        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1453
1454        try {
1455            writeOnlyFileChannel.read(readBuffer);
1456            fail("should throw NonReadableChannelException");
1457        } catch (NonReadableChannelException e) {
1458            // expected
1459        }
1460
1461        // first throws NonReadableChannelException
1462        readBuffer = null;
1463        try {
1464            writeOnlyFileChannel.read(readBuffer);
1465            fail("should throw NonReadableChannelException");
1466        } catch (NonReadableChannelException e) {
1467            // expected
1468        }
1469    }
1470
1471    /**
1472     * @tests java.nio.channels.FileChannel#read(ByteBuffer)
1473     */
1474    @TestTargetNew(
1475        level = TestLevel.PARTIAL_COMPLETE,
1476        notes = "",
1477        method = "read",
1478        args = {java.nio.ByteBuffer.class}
1479    )
1480    public void test_readLByteBuffer_EmptyFile() throws Exception {
1481        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1482        int result = readOnlyFileChannel.read(readBuffer);
1483        assertEquals(-1, result);
1484        assertEquals(0, readBuffer.position());
1485    }
1486
1487    /**
1488     * @tests java.nio.channels.FileChannel#read(ByteBuffer)
1489     */
1490    @TestTargetNew(
1491        level = TestLevel.PARTIAL_COMPLETE,
1492        notes = "",
1493        method = "read",
1494        args = {java.nio.ByteBuffer.class}
1495    )
1496    public void test_readLByteBuffer_LimitedCapacity() throws Exception {
1497        writeDataToFile(fileOfReadOnlyFileChannel);
1498
1499        ByteBuffer readBuffer = ByteBuffer.allocate(LIMITED_CAPACITY);
1500        int result = readOnlyFileChannel.read(readBuffer);
1501        assertEquals(LIMITED_CAPACITY, result);
1502        assertEquals(LIMITED_CAPACITY, readBuffer.position());
1503        readBuffer.flip();
1504        for (int i = 0; i < LIMITED_CAPACITY; i++) {
1505            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
1506        }
1507    }
1508
1509    /**
1510     * @tests java.nio.channels.FileChannel#read(ByteBuffer)
1511     */
1512    @TestTargetNew(
1513        level = TestLevel.PARTIAL_COMPLETE,
1514        notes = "",
1515        method = "read",
1516        args = {java.nio.ByteBuffer.class}
1517    )
1518    public void test_readLByteBuffer() throws Exception {
1519        writeDataToFile(fileOfReadOnlyFileChannel);
1520
1521        ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_AS_BYTES_LENGTH);
1522        int result = readOnlyFileChannel.read(readBuffer);
1523        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
1524        assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffer.position());
1525        readBuffer.flip();
1526        for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
1527            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
1528        }
1529    }
1530
1531    /**
1532     * @tests java.nio.channels.FileChannel#read(ByteBuffer, long)
1533     */
1534    @TestTargetNew(
1535        level = TestLevel.PARTIAL_COMPLETE,
1536        notes = "Verifies NullPointerException.",
1537        method = "read",
1538        args = {java.nio.ByteBuffer.class, long.class}
1539    )
1540    public void test_readLByteBufferJ_Null() throws Exception {
1541        ByteBuffer readBuffer = null;
1542
1543        try {
1544            readOnlyFileChannel.read(readBuffer, 0);
1545            fail("should throw NullPointerException");
1546        } catch (NullPointerException e) {
1547            // expected
1548        }
1549
1550        // first throws NullPointerException
1551        try {
1552            readOnlyFileChannel.read(readBuffer, -1);
1553            fail("should throw NullPointerException");
1554        } catch (NullPointerException e) {
1555            // expected
1556        }
1557
1558        try {
1559            readWriteFileChannel.read(readBuffer, 0);
1560            fail("should throw NullPointerException");
1561        } catch (NullPointerException e) {
1562            // expected
1563        }
1564
1565        // first throws NullPointerException
1566        try {
1567            readWriteFileChannel.read(readBuffer, -1);
1568            fail("should throw NullPointerException");
1569        } catch (NullPointerException e) {
1570            // expected
1571        }
1572
1573        try {
1574            writeOnlyFileChannel.read(readBuffer, 0);
1575            fail("should throw NullPointerException");
1576        } catch (NullPointerException e) {
1577            // expected
1578        }
1579
1580        // first throws NullPointerException
1581        try {
1582            writeOnlyFileChannel.read(readBuffer, -1);
1583            fail("should throw NullPointerException");
1584        } catch (NullPointerException e) {
1585            // expected
1586        }
1587
1588        // first throws NullPointerException
1589        writeOnlyFileChannel.close();
1590        try {
1591            writeOnlyFileChannel.read(readBuffer, 0);
1592            fail("should throw NullPointerException");
1593        } catch (NullPointerException e) {
1594            // expected
1595        }
1596
1597        try {
1598            writeOnlyFileChannel.read(readBuffer, -1);
1599            fail("should throw NullPointerException");
1600        } catch (NullPointerException e) {
1601            // expected
1602        }
1603
1604        readWriteFileChannel.close();
1605        try {
1606            readWriteFileChannel.read(readBuffer, 0);
1607            fail("should throw NullPointerException");
1608        } catch (NullPointerException e) {
1609            // expected
1610        }
1611
1612        try {
1613            readWriteFileChannel.read(readBuffer, -1);
1614            fail("should throw NullPointerException");
1615        } catch (NullPointerException e) {
1616            // expected
1617        }
1618
1619        readOnlyFileChannel.close();
1620        try {
1621            readOnlyFileChannel.read(readBuffer, 0);
1622            fail("should throw NullPointerException");
1623        } catch (NullPointerException e) {
1624            // expected
1625        }
1626
1627        try {
1628            readOnlyFileChannel.read(readBuffer, -1);
1629            fail("should throw NullPointerException");
1630        } catch (NullPointerException e) {
1631            // expected
1632        }
1633    }
1634
1635    /**
1636     * @tests java.nio.channels.FileChannel#read(ByteBuffer, long)
1637     */
1638    @TestTargetNew(
1639        level = TestLevel.PARTIAL_COMPLETE,
1640        notes = "Verifies ClosedChannelException.",
1641        method = "read",
1642        args = {java.nio.ByteBuffer.class, long.class}
1643    )
1644    public void test_readLByteBufferJ_Closed() throws Exception {
1645        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1646
1647        readOnlyFileChannel.close();
1648        try {
1649            readOnlyFileChannel.read(readBuffer, 0);
1650            fail("should throw ClosedChannelException");
1651        } catch (ClosedChannelException e) {
1652            // expected
1653        }
1654
1655        readWriteFileChannel.close();
1656        try {
1657            readWriteFileChannel.read(readBuffer, 0);
1658            fail("should throw ClosedChannelException");
1659        } catch (ClosedChannelException e) {
1660            // expected
1661        }
1662    }
1663
1664    /**
1665     * @tests java.nio.channels.FileChannel#read(ByteBuffer, long)
1666     */
1667    @TestTargetNew(
1668        level = TestLevel.PARTIAL_COMPLETE,
1669        notes = "Verifies IllegalArgumentException.",
1670        method = "read",
1671        args = {java.nio.ByteBuffer.class, long.class}
1672    )
1673    public void test_readLByteBufferJ_IllegalArgument() throws Exception {
1674        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1675
1676        try {
1677            readOnlyFileChannel.read(readBuffer, -1);
1678            fail("should throw IllegalArgumentException");
1679        } catch (IllegalArgumentException e) {
1680            // expected
1681        }
1682
1683        try {
1684            writeOnlyFileChannel.read(readBuffer, -1);
1685            fail("should throw IllegalArgumentException");
1686        } catch (IllegalArgumentException e) {
1687            // expected
1688        }
1689
1690        try {
1691            readWriteFileChannel.read(readBuffer, -1);
1692            fail("should throw IllegalArgumentException");
1693        } catch (IllegalArgumentException e) {
1694            // expected
1695        }
1696
1697        // throws IllegalArgumentException first.
1698        readOnlyFileChannel.close();
1699        try {
1700            readOnlyFileChannel.read(readBuffer, -1);
1701            fail("should throw IllegalArgumentException");
1702        } catch (IllegalArgumentException e) {
1703            // expected
1704        }
1705
1706        writeOnlyFileChannel.close();
1707        try {
1708            writeOnlyFileChannel.read(readBuffer, -1);
1709            fail("should throw IllegalArgumentException");
1710        } catch (IllegalArgumentException e) {
1711            // expected
1712        }
1713
1714        readWriteFileChannel.close();
1715        try {
1716            readWriteFileChannel.read(readBuffer, -1);
1717            fail("should throw IllegalArgumentException");
1718        } catch (IllegalArgumentException e) {
1719            // expected
1720        }
1721    }
1722
1723    /**
1724     * @tests java.nio.channels.FileChannel#read(ByteBuffer, long)
1725     */
1726    @TestTargetNew(
1727        level = TestLevel.PARTIAL_COMPLETE,
1728        notes = "Verifies NonReadableChannelException.",
1729        method = "read",
1730        args = {java.nio.ByteBuffer.class, long.class}
1731    )
1732    public void test_readLByteBufferJ_WriteOnly() throws Exception {
1733        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1734
1735        try {
1736            writeOnlyFileChannel.read(readBuffer, 0);
1737            fail("should throw NonReadableChannelException");
1738        } catch (NonReadableChannelException e) {
1739            // expected
1740        }
1741
1742        // throws NonReadableChannelException first.
1743        writeOnlyFileChannel.close();
1744        try {
1745            writeOnlyFileChannel.read(readBuffer, 0);
1746            fail("should throw NonReadableChannelException");
1747        } catch (NonReadableChannelException e) {
1748            // expected
1749        }
1750    }
1751
1752    /**
1753     * @tests java.nio.channels.FileChannel#read(ByteBuffer,long)
1754     */
1755    @TestTargetNew(
1756        level = TestLevel.PARTIAL_COMPLETE,
1757        notes = "",
1758        method = "read",
1759        args = {java.nio.ByteBuffer.class, long.class}
1760    )
1761    public void test_readLByteBufferJ_Emptyfile() throws Exception {
1762        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1763        int result = readOnlyFileChannel.read(readBuffer, 0);
1764        assertEquals(-1, result);
1765        assertEquals(0, readBuffer.position());
1766    }
1767
1768    /**
1769     * @tests java.nio.channels.FileChannel#read(ByteBuffer,long)
1770     */
1771    @TestTargetNew(
1772        level = TestLevel.PARTIAL_COMPLETE,
1773        notes = "",
1774        method = "read",
1775        args = {java.nio.ByteBuffer.class, long.class}
1776    )
1777    public void test_readLByteBufferJ_Postion_BeyondFileLimit()
1778            throws Exception {
1779
1780        writeDataToFile(fileOfReadOnlyFileChannel);
1781
1782        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1783        int result = readOnlyFileChannel.read(readBuffer,
1784                CONTENT_AS_BYTES.length);
1785        assertEquals(-1, result);
1786        assertEquals(0, readBuffer.position());
1787    }
1788
1789    /**
1790     * @tests java.nio.channels.FileChannel#read(ByteBuffer,long)
1791     */
1792    @TestTargetNew(
1793        level = TestLevel.PARTIAL_COMPLETE,
1794        notes = "Verifies IOException.",
1795        method = "read",
1796        args = {java.nio.ByteBuffer.class, long.class}
1797    )
1798    public void test_readLByteBufferJ_Postion_As_Long() throws Exception {
1799        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1800        try {
1801            readOnlyFileChannel.read(readBuffer, Long.MAX_VALUE);
1802        } catch (IOException e) {
1803            // expected
1804        }
1805    }
1806
1807    /**
1808     * @tests java.nio.channels.FileChannel#read(ByteBuffer,long)
1809     */
1810    @TestTargetNew(
1811        level = TestLevel.PARTIAL_COMPLETE,
1812        notes = "",
1813        method = "read",
1814        args = {java.nio.ByteBuffer.class, long.class}
1815    )
1816    public void test_readLByteBufferJ() throws Exception {
1817        writeDataToFile(fileOfReadOnlyFileChannel);
1818        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
1819
1820        final int BUFFER_POSITION = 1;
1821        readBuffer.position(BUFFER_POSITION);
1822
1823        final int POSITION = 2;
1824        int result = readOnlyFileChannel.read(readBuffer, POSITION);
1825        assertEquals(CONTENT_AS_BYTES_LENGTH - POSITION, result);
1826        assertEquals(BUFFER_POSITION + result, readBuffer.position());
1827
1828        readBuffer.flip();
1829        readBuffer.position(BUFFER_POSITION);
1830        for (int i = POSITION; i < CONTENT_AS_BYTES_LENGTH; i++) {
1831            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
1832        }
1833    }
1834
1835    /**
1836     * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
1837     */
1838    @TestTargetNew(
1839        level = TestLevel.PARTIAL_COMPLETE,
1840        notes = "",
1841        method = "read",
1842        args = {java.nio.ByteBuffer[].class}
1843    )
1844    public void test_read$LByteBuffer_Regression() throws Exception {
1845        // regression test for Harmony-849
1846        writeDataToFile(fileOfReadOnlyFileChannel);
1847        ByteBuffer[] readBuffers = new ByteBuffer[2];
1848        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
1849        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
1850
1851        long readCount = readOnlyFileChannel.read(readBuffers);
1852        assertEquals(CONTENT_AS_BYTES_LENGTH, readCount);
1853        assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[0].position());
1854        assertEquals(0, readBuffers[1].position());
1855        readBuffers[0].flip();
1856        for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
1857            assertEquals(CONTENT_AS_BYTES[i], readBuffers[0].get());
1858        }
1859    }
1860
1861    /**
1862     * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
1863     */
1864    @TestTargetNew(
1865        level = TestLevel.PARTIAL_COMPLETE,
1866        notes = "",
1867        method = "read",
1868        args = {java.nio.ByteBuffer[].class}
1869    )
1870    public void test_read$LByteBuffer() throws Exception {
1871        FileChannel mockChannel = new MockFileChannel();
1872        ByteBuffer[] buffers = new ByteBuffer[2];
1873        mockChannel.read(buffers);
1874        // Verify that calling read(ByteBuffer[] dsts) leads to the method
1875        // read(dsts, 0, dsts.length)
1876        assertTrue(((MockFileChannel)mockChannel).isReadCalled);
1877    }
1878
1879    /**
1880     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
1881     */
1882    @TestTargetNew(
1883        level = TestLevel.PARTIAL_COMPLETE,
1884        notes = "",
1885        method = "read",
1886        args = {java.nio.ByteBuffer[].class, int.class, int.class}
1887    )
1888    public void test_read$LByteBufferII_Null() throws Exception {
1889
1890        try {
1891            readOnlyFileChannel.read(null, 0, 1);
1892            fail("should throw NullPointerException");
1893        } catch (NullPointerException e) {
1894            // expected
1895        }
1896        try {
1897            readOnlyFileChannel.read(null, 0, 3);
1898            fail("should throw NullPointerException");
1899        } catch (NullPointerException e) {
1900            // expected
1901        }
1902        try {
1903            readOnlyFileChannel.read(null, 1, 2);
1904            fail("should throw NullPointerException");
1905        } catch (NullPointerException e) {
1906            // expected
1907        }
1908        try {
1909            readOnlyFileChannel.read(null, 2, 1);
1910            fail("should throw NullPointerException");
1911        } catch (NullPointerException e) {
1912            // expected
1913        }
1914        try {
1915            readOnlyFileChannel.read(null, 3, 0);
1916            fail("should throw NullPointerException");
1917        } catch (NullPointerException e) {
1918            // expected
1919        }
1920
1921        try {
1922            writeOnlyFileChannel.read(null, 0, 1);
1923            fail("should throw NullPointerException");
1924        } catch (NullPointerException e) {
1925            // expected
1926        }
1927        try {
1928            writeOnlyFileChannel.read(null, 0, 3);
1929            fail("should throw NullPointerException");
1930        } catch (NullPointerException e) {
1931            // expected
1932        }
1933        try {
1934            writeOnlyFileChannel.read(null, 1, 2);
1935            fail("should throw NullPointerException");
1936        } catch (NullPointerException e) {
1937            // expected
1938        }
1939        try {
1940            writeOnlyFileChannel.read(null, 2, 1);
1941            fail("should throw NullPointerException");
1942        } catch (NullPointerException e) {
1943            // expected
1944        }
1945        try {
1946            writeOnlyFileChannel.read(null, 3, 0);
1947            fail("should throw NullPointerException");
1948        } catch (NullPointerException e) {
1949            // expected
1950        }
1951
1952        try {
1953            readWriteFileChannel.read(null, 0, 1);
1954            fail("should throw NullPointerException");
1955        } catch (NullPointerException e) {
1956            // expected
1957        }
1958        try {
1959            readWriteFileChannel.read(null, 0, 3);
1960            fail("should throw NullPointerException");
1961        } catch (NullPointerException e) {
1962            // expected
1963        }
1964        try {
1965            readWriteFileChannel.read(null, 1, 2);
1966            fail("should throw NullPointerException");
1967        } catch (NullPointerException e) {
1968            // expected
1969        }
1970        try {
1971            readWriteFileChannel.read(null, 2, 1);
1972            fail("should throw NullPointerException");
1973        } catch (NullPointerException e) {
1974            // expected
1975        }
1976        try {
1977            readWriteFileChannel.read(null, 3, 0);
1978            fail("should throw NullPointerException");
1979        } catch (NullPointerException e) {
1980            // expected
1981        }
1982
1983        // first throws NullPointerException
1984        readOnlyFileChannel.close();
1985        try {
1986            readOnlyFileChannel.read(null, 0, 1);
1987            fail("should throw NullPointerException");
1988        } catch (NullPointerException e) {
1989            // expected
1990        }
1991        try {
1992            readOnlyFileChannel.read(null, 0, 3);
1993            fail("should throw NullPointerException");
1994        } catch (NullPointerException e) {
1995            // expected
1996        }
1997        try {
1998            readOnlyFileChannel.read(null, 1, 2);
1999            fail("should throw NullPointerException");
2000        } catch (NullPointerException e) {
2001            // expected
2002        }
2003        try {
2004            readOnlyFileChannel.read(null, 2, 1);
2005            fail("should throw NullPointerException");
2006        } catch (NullPointerException e) {
2007            // expected
2008        }
2009        try {
2010            readOnlyFileChannel.read(null, 3, 0);
2011            fail("should throw NullPointerException");
2012        } catch (NullPointerException e) {
2013            // expected
2014        }
2015
2016        readWriteFileChannel.close();
2017        try {
2018            readWriteFileChannel.read(null, 0, 1);
2019            fail("should throw NullPointerException");
2020        } catch (NullPointerException e) {
2021            // expected
2022        }
2023        try {
2024            readWriteFileChannel.read(null, 0, 3);
2025            fail("should throw NullPointerException");
2026        } catch (NullPointerException e) {
2027            // expected
2028        }
2029        try {
2030            readWriteFileChannel.read(null, 1, 2);
2031            fail("should throw NullPointerException");
2032        } catch (NullPointerException e) {
2033            // expected
2034        }
2035        try {
2036            readWriteFileChannel.read(null, 2, 1);
2037            fail("should throw NullPointerException");
2038        } catch (NullPointerException e) {
2039            // expected
2040        }
2041        try {
2042            readWriteFileChannel.read(null, 3, 0);
2043            fail("should throw NullPointerException");
2044        } catch (NullPointerException e) {
2045            // expected
2046        }
2047
2048        writeOnlyFileChannel.close();
2049        try {
2050            writeOnlyFileChannel.read(null, 0, 1);
2051            fail("should throw NullPointerException");
2052        } catch (NullPointerException e) {
2053            // expected
2054        }
2055        try {
2056            writeOnlyFileChannel.read(null, 0, 3);
2057            fail("should throw NullPointerException");
2058        } catch (NullPointerException e) {
2059            // expected
2060        }
2061        try {
2062            writeOnlyFileChannel.read(null, 1, 2);
2063            fail("should throw NullPointerException");
2064        } catch (NullPointerException e) {
2065            // expected
2066        }
2067        try {
2068            writeOnlyFileChannel.read(null, 2, 1);
2069            fail("should throw NullPointerException");
2070        } catch (NullPointerException e) {
2071            // expected
2072        }
2073        try {
2074            writeOnlyFileChannel.read(null, 3, 0);
2075            fail("should throw NullPointerException");
2076        } catch (NullPointerException e) {
2077            // expected
2078        }
2079    }
2080
2081    /**
2082     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2083     */
2084    @TestTargetNew(
2085        level = TestLevel.PARTIAL_COMPLETE,
2086        notes = "Verifies ClosedChannelException.",
2087        method = "read",
2088        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2089    )
2090    public void test_read$LByteBufferII_Closed() throws Exception {
2091        ByteBuffer[] readBuffers = new ByteBuffer[2];
2092        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2093
2094        readOnlyFileChannel.close();
2095        try {
2096            readOnlyFileChannel.read(readBuffers, 0, 1);
2097            fail("should throw ClosedChannelException");
2098        } catch (ClosedChannelException e) {
2099            // expected
2100        }
2101
2102        writeOnlyFileChannel.close();
2103        try {
2104            writeOnlyFileChannel.read(readBuffers, 0, 1);
2105            fail("should throw ClosedChannelException");
2106        } catch (ClosedChannelException e) {
2107            // expected
2108        }
2109
2110        readWriteFileChannel.close();
2111        try {
2112            readWriteFileChannel.read(readBuffers, 0, 1);
2113            fail("should throw ClosedChannelException");
2114        } catch (ClosedChannelException e) {
2115            // expected
2116        }
2117
2118        // regression test for Harmony-902
2119        readBuffers[0] = null;
2120        try {
2121            readOnlyFileChannel.read(readBuffers, 0, 1);
2122            fail("should throw ClosedChannelException");
2123        } catch (ClosedChannelException e) {
2124            // expected
2125        }
2126        try {
2127            writeOnlyFileChannel.read(readBuffers, 0, 1);
2128            fail("should throw ClosedChannelException");
2129        } catch (ClosedChannelException e) {
2130            // expected
2131        }
2132        try {
2133            readWriteFileChannel.read(readBuffers, 0, 1);
2134            fail("should throw ClosedChannelException");
2135        } catch (ClosedChannelException e) {
2136            // expected
2137        }
2138    }
2139
2140    /**
2141     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2142     */
2143    @TestTargetNew(
2144        level = TestLevel.PARTIAL_COMPLETE,
2145        notes = "",
2146        method = "read",
2147        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2148    )
2149    public void test_read$LByteBufferII_WriteOnly() throws Exception {
2150        ByteBuffer[] readBuffers = new ByteBuffer[2];
2151        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2152
2153        try {
2154            writeOnlyFileChannel.read(readBuffers, 0, 1);
2155            fail("should throw NonReadableChannelException");
2156        } catch (NonReadableChannelException e) {
2157            // expected
2158        }
2159
2160        // first throws NonReadableChannelException.
2161        readBuffers[0] = null;
2162        try {
2163            writeOnlyFileChannel.read(readBuffers, 0, 1);
2164            fail("should throw NonReadableChannelException");
2165        } catch (NonReadableChannelException e) {
2166            // expected
2167        }
2168    }
2169
2170    private void doTestForIOOBException(FileChannel channel,
2171            ByteBuffer[] buffer) throws IOException{
2172        try {
2173            channel.read(buffer, -1, 0);
2174            fail("should throw IndexOutOfBoundException");
2175        } catch (IndexOutOfBoundsException e) {
2176            // expected
2177        }
2178        try {
2179            channel.read(buffer, 0, -1);
2180            fail("should throw IndexOutOfBoundException");
2181        } catch (IndexOutOfBoundsException e) {
2182            // expected
2183        }
2184        try {
2185            channel.read(buffer, 0, 3);
2186            fail("should throw IndexOutOfBoundException");
2187        } catch (IndexOutOfBoundsException e) {
2188            // expected
2189        }
2190        try {
2191            channel.read(buffer, 1, 2);
2192            fail("should throw IndexOutOfBoundException");
2193        } catch (IndexOutOfBoundsException e) {
2194            // expected
2195        }
2196        try {
2197            channel.read(buffer, 2, 1);
2198            fail("should throw IndexOutOfBoundException");
2199        } catch (IndexOutOfBoundsException e) {
2200            // expected
2201        }
2202        try {
2203            channel.read(buffer, 3, 0);
2204            fail("should throw IndexOutOfBoundException");
2205        } catch (IndexOutOfBoundsException e) {
2206            // expected
2207        }
2208    }
2209    /**
2210     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2211     */
2212    @TestTargetNew(
2213        level = TestLevel.PARTIAL_COMPLETE,
2214        notes = "Verifies IndexOutOfBoundsException.",
2215        method = "read",
2216        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2217    )
2218    public void test_read$LByteBufferII_IndexOutOfBound() throws Exception {
2219        ByteBuffer[] readBuffers = new ByteBuffer[2];
2220        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2221        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
2222        ByteBuffer[] readBuffersNull = new ByteBuffer[2];
2223
2224        doTestForIOOBException(readOnlyFileChannel, readBuffers);
2225        doTestForIOOBException(readWriteFileChannel, readBuffers);
2226        doTestForIOOBException(writeOnlyFileChannel, readBuffers);
2227
2228        doTestForIOOBException(readOnlyFileChannel, readBuffersNull);
2229        doTestForIOOBException(readWriteFileChannel, readBuffersNull);
2230        doTestForIOOBException(writeOnlyFileChannel, readBuffersNull);
2231        try {
2232            readOnlyFileChannel.read(null, -1, 0);
2233            fail("should throw IndexOutOfBoundException");
2234        } catch (IndexOutOfBoundsException e) {
2235            // expected
2236        }
2237        try {
2238            readOnlyFileChannel.read(null, 0, -1);
2239            fail("should throw IndexOutOfBoundException");
2240        } catch (IndexOutOfBoundsException e) {
2241            // expected
2242        }
2243
2244        try {
2245            readWriteFileChannel.read(null, -1, 0);
2246            fail("should throw IndexOutOfBoundException");
2247        } catch (IndexOutOfBoundsException e) {
2248            // expected
2249        }
2250        try {
2251            readWriteFileChannel.read(null, 0, -1);
2252            fail("should throw IndexOutOfBoundException");
2253        } catch (IndexOutOfBoundsException e) {
2254            // expected
2255        }
2256
2257        try {
2258            writeOnlyFileChannel.read(null, -1, 0);
2259            fail("should throw IndexOutOfBoundException");
2260        } catch (IndexOutOfBoundsException e) {
2261            // expected
2262        }
2263        try {
2264            writeOnlyFileChannel.read(null, 0, -1);
2265            fail("should throw IndexOutOfBoundException");
2266        } catch (IndexOutOfBoundsException e) {
2267            // expected
2268        }
2269
2270        readOnlyFileChannel.close();
2271        doTestForIOOBException(readOnlyFileChannel, readBuffers);
2272        doTestForIOOBException(readOnlyFileChannel, readBuffersNull);
2273
2274        readWriteFileChannel.close();
2275        doTestForIOOBException(readWriteFileChannel, readBuffers);
2276        doTestForIOOBException(readWriteFileChannel, readBuffersNull);
2277
2278        writeOnlyFileChannel.close();
2279        doTestForIOOBException(writeOnlyFileChannel, readBuffers);
2280        doTestForIOOBException(writeOnlyFileChannel, readBuffersNull);
2281    }
2282
2283    /**
2284     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2285     */
2286    @TestTargetNew(
2287        level = TestLevel.PARTIAL_COMPLETE,
2288        notes = "",
2289        method = "read",
2290        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2291    )
2292    public void test_read$LByteBufferII_EmptyFile() throws Exception {
2293        ByteBuffer[] readBuffers = new ByteBuffer[2];
2294        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2295        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
2296        long result = readOnlyFileChannel.read(readBuffers, 0, 2);
2297        assertEquals(-1, result);
2298        assertEquals(0, readBuffers[0].position());
2299        assertEquals(0, readBuffers[1].position());
2300    }
2301
2302    /**
2303     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2304     */
2305    @TestTargetNew(
2306        level = TestLevel.PARTIAL_COMPLETE,
2307        notes = "Verifies NullPointerException.",
2308        method = "read",
2309        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2310    )
2311    public void test_read$LByteBufferII_EmptyBuffers() throws Exception {
2312        ByteBuffer[] readBuffers = new ByteBuffer[2];
2313        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2314
2315        try {
2316            readOnlyFileChannel.read(readBuffers, 0, 2);
2317        } catch (NullPointerException e) {
2318            // expected
2319        }
2320
2321        writeDataToFile(fileOfReadOnlyFileChannel);
2322        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2323        try {
2324            readOnlyFileChannel.read(readBuffers, 0, 2);
2325        } catch (NullPointerException e) {
2326            // expected
2327        }
2328
2329        long result = readOnlyFileChannel.read(readBuffers, 0, 1);
2330        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
2331    }
2332
2333    /**
2334     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2335     */
2336    @TestTargetNew(
2337        level = TestLevel.PARTIAL_COMPLETE,
2338        notes = "",
2339        method = "read",
2340        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2341    )
2342    public void test_read$LByteBufferII_EmptyFile_EmptyBuffers()
2343            throws Exception {
2344        ByteBuffer[] readBuffers = new ByteBuffer[2];
2345        // will not throw NullPointerException
2346        long result = readOnlyFileChannel.read(readBuffers, 0, 0);
2347        assertEquals(0, result);
2348    }
2349
2350    /**
2351     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2352     */
2353    @TestTargetNew(
2354        level = TestLevel.PARTIAL_COMPLETE,
2355        notes = "",
2356        method = "read",
2357        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2358    )
2359    public void test_read$LByteBufferII_Length_Zero() throws Exception {
2360        writeDataToFile(fileOfReadOnlyFileChannel);
2361        ByteBuffer[] readBuffers = new ByteBuffer[2];
2362        readBuffers[0] = ByteBuffer.allocate(LIMITED_CAPACITY);
2363        readBuffers[1] = ByteBuffer.allocate(LIMITED_CAPACITY);
2364        long result = readOnlyFileChannel.read(readBuffers, 1, 0);
2365        assertEquals(0, result);
2366    }
2367
2368    /**
2369     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2370     */
2371    @TestTargetNew(
2372        level = TestLevel.PARTIAL_COMPLETE,
2373        notes = "",
2374        method = "read",
2375        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2376    )
2377    public void test_read$LByteBufferII_LimitedCapacity() throws Exception {
2378        writeDataToFile(fileOfReadOnlyFileChannel);
2379        ByteBuffer[] readBuffers = new ByteBuffer[2];
2380        readBuffers[0] = ByteBuffer.allocate(LIMITED_CAPACITY);
2381        readBuffers[1] = ByteBuffer.allocate(LIMITED_CAPACITY);
2382
2383        // reads to the second buffer
2384        long result = readOnlyFileChannel.read(readBuffers, 1, 1);
2385        assertEquals(LIMITED_CAPACITY, result);
2386        assertEquals(0, readBuffers[0].position());
2387        assertEquals(LIMITED_CAPACITY, readBuffers[1].position());
2388
2389        readBuffers[1].flip();
2390        for (int i = 0; i < LIMITED_CAPACITY; i++) {
2391            assertEquals(CONTENT_AS_BYTES[i], readBuffers[1].get());
2392        }
2393    }
2394
2395    /**
2396     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
2397     */
2398    @TestTargetNew(
2399        level = TestLevel.PARTIAL_COMPLETE,
2400        notes = "",
2401        method = "read",
2402        args = {java.nio.ByteBuffer[].class, int.class, int.class}
2403    )
2404    public void test_read$LByteBufferII() throws Exception {
2405        writeDataToFile(fileOfReadOnlyFileChannel);
2406        ByteBuffer[] readBuffers = new ByteBuffer[2];
2407        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
2408        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
2409
2410        // writes to the second buffer
2411        assertEquals(CONTENT_AS_BYTES_LENGTH, readOnlyFileChannel.read(
2412                readBuffers, 1, 1));
2413        assertEquals(0, readBuffers[0].position());
2414        assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[1].position());
2415
2416        readBuffers[1].flip();
2417        for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
2418            assertEquals(CONTENT_AS_BYTES[i], readBuffers[1].get());
2419        }
2420    }
2421
2422    /**
2423     * @tests java.nio.channels.FileChannel#isOpen()
2424     * @tests java.nio.channels.FileChannel#close()
2425     */
2426    @TestTargets({
2427        @TestTargetNew(
2428            level = TestLevel.PARTIAL_COMPLETE,
2429            notes = "",
2430            method = "isOpen",
2431            args = {}
2432        ),
2433        @TestTargetNew(
2434            level = TestLevel.PARTIAL_COMPLETE,
2435            notes = "",
2436            method = "close",
2437            args = {}
2438        )
2439    })
2440    public void test_isOpen() throws Exception {
2441        // Regression for HARMONY-40
2442        File logFile = File.createTempFile("out", "tmp");
2443        logFile.deleteOnExit();
2444        FileOutputStream out = new FileOutputStream(logFile, true);
2445        FileChannel channel = out.getChannel();
2446        out.write(1);
2447        assertTrue("Assert 0: Channel is not open", channel.isOpen());
2448        out.close();
2449        assertFalse("Assert 0: Channel is still open", channel.isOpen());
2450    }
2451
2452    /**
2453     * @tests java.nio.channels.FileChannel#position()
2454     */
2455    @TestTargetNew(
2456        level = TestLevel.PARTIAL_COMPLETE,
2457        notes = "",
2458        method = "position",
2459        args = {}
2460    )
2461    @AndroidOnly("Fails on RI. See comment below")
2462    public void test_position_append() throws Exception {
2463        // Regression test for Harmony-508
2464        File tmpfile = File.createTempFile("FileOutputStream", "tmp");
2465        tmpfile.deleteOnExit();
2466        FileOutputStream fos = new FileOutputStream(tmpfile);
2467        byte[] b = new byte[10];
2468        for (int i = 0; i < b.length; i++) {
2469            b[i] = (byte) i;
2470        }
2471        fos.write(b);
2472        fos.flush();
2473        fos.close();
2474        FileOutputStream f = new FileOutputStream(tmpfile, true);
2475        // Below assertion fails on RI. RI behaviour is counter to spec.
2476        assertEquals(10, f.getChannel().position());
2477    }
2478
2479
2480    /**
2481     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2482     */
2483    @TestTargetNew(
2484        level = TestLevel.PARTIAL_COMPLETE,
2485        notes = "Verifies NonReadableChannelException, NonWritableChannelException , ClosedChannelException, IllegalArgumentException, IOException. ",
2486        method = "map",
2487        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2488    )
2489    public void test_map_AbnormalMode() throws IOException {
2490        try {
2491            writeOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
2492            fail("should throw NonReadableChannelException.");
2493        } catch (NonReadableChannelException ex) {
2494            // expected;
2495        }
2496        try {
2497            writeOnlyFileChannel.map(MapMode.READ_WRITE, 0, CONTENT_LENGTH);
2498            fail("should throw NonReadableChannelException.");
2499        } catch (NonReadableChannelException ex) {
2500            // expected;
2501        }
2502        try {
2503            writeOnlyFileChannel.map(MapMode.PRIVATE, 0, CONTENT_LENGTH);
2504            fail("should throw NonReadableChannelException.");
2505        } catch (NonReadableChannelException ex) {
2506            // expected;
2507        }
2508        writeOnlyFileChannel.close();
2509        try {
2510            writeOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1);
2511            fail("should throw ClosedChannelException.");
2512        } catch (ClosedChannelException ex) {
2513            // expected;
2514        }
2515
2516        try {
2517            readOnlyFileChannel.map(MapMode.READ_WRITE, 0, CONTENT_LENGTH);
2518            fail("should throw NonWritableChannelException .");
2519        } catch (NonWritableChannelException ex) {
2520            // expected;
2521        }
2522        try {
2523            readOnlyFileChannel.map(MapMode.PRIVATE, 0, CONTENT_LENGTH);
2524            fail("should throw NonWritableChannelException .");
2525        } catch (NonWritableChannelException ex) {
2526            // expected;
2527        }
2528        try {
2529            readOnlyFileChannel.map(MapMode.READ_WRITE, -1, CONTENT_LENGTH);
2530            fail("should throw IAE.");
2531        } catch (IllegalArgumentException ex) {
2532            // expected;
2533        }
2534        try {
2535            readOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1);
2536            fail("should throw IAE.");
2537        } catch (IllegalArgumentException ex) {
2538            // expected;
2539        }
2540
2541        try {
2542            readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH + 1);
2543            fail("should throw IOException.");
2544        } catch (IOException ex) {
2545            // expected;
2546        }
2547        try {
2548            readOnlyFileChannel.map(MapMode.READ_ONLY, 2, CONTENT_LENGTH - 1);
2549            fail("should throw IOException.");
2550        } catch (IOException ex) {
2551            // expected;
2552        }
2553
2554        readOnlyFileChannel.close();
2555        try {
2556            readOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1);
2557            fail("should throw ClosedChannelException.");
2558        } catch (ClosedChannelException ex) {
2559            // expected;
2560        }
2561        try {
2562            readOnlyFileChannel.map(MapMode.READ_ONLY, 2, CONTENT_LENGTH - 1);
2563            fail("should throw IOException.");
2564        } catch (IOException ex) {
2565            // expected;
2566        }
2567
2568        readWriteFileChannel.close();
2569        try {
2570            readWriteFileChannel.map(MapMode.READ_WRITE, 0, -1);
2571            fail("should throw ClosedChannelException.");
2572        } catch (ClosedChannelException ex) {
2573            // expected;
2574        }
2575    }
2576
2577    /**
2578     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2579     */
2580    @TestTargetNew(
2581        level = TestLevel.PARTIAL_COMPLETE,
2582        notes = "",
2583        method = "map",
2584        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2585    )
2586    public void test_map_ReadOnly_CloseChannel() throws IOException {
2587        // close channel has no effect on map if mapped
2588        assertEquals(0, readWriteFileChannel.size());
2589        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_ONLY,
2590                0, CONTENT_LENGTH);
2591        assertEquals(CONTENT_LENGTH, readWriteFileChannel.size());
2592        readOnlyFileChannel.close();
2593        assertEquals(CONTENT_LENGTH, mapped.limit());
2594    }
2595
2596    /**
2597     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2598     */
2599    @TestTargetNew(
2600        level = TestLevel.PARTIAL_COMPLETE,
2601        notes = "",
2602        method = "map",
2603        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2604    )
2605    public void test_map_Private_CloseChannel() throws IOException {
2606        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
2607                CONTENT_LENGTH);
2608        readWriteFileChannel.close();
2609        mapped.put(TEST_BYTES);
2610        assertEquals(CONTENT_LENGTH, mapped.limit());
2611        assertEquals("test".length(), mapped.position());
2612    }
2613
2614    /**
2615     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2616     */
2617    @TestTargetNew(
2618        level = TestLevel.PARTIAL_COMPLETE,
2619        notes = "",
2620        method = "map",
2621        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2622    )
2623    public void test_map_ReadOnly() throws IOException {
2624        MappedByteBuffer mapped = null;
2625        // try put something to readonly map
2626        writeDataToFile(fileOfReadOnlyFileChannel);
2627        mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
2628        try {
2629            mapped.put(TEST_BYTES);
2630            fail("should throw ReadOnlyBufferException.");
2631        } catch (ReadOnlyBufferException ex) {
2632            // expected;
2633        }
2634        assertEquals(CONTENT_LENGTH, mapped.limit());
2635        assertEquals(CONTENT_LENGTH, mapped.capacity());
2636        assertEquals(0, mapped.position());
2637
2638        // try to get a readonly map from read/write channel
2639        writeDataToFile(fileOfReadWriteFileChannel);
2640        mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 0, CONTENT
2641                .length());
2642        assertEquals(CONTENT_LENGTH, mapped.limit());
2643        assertEquals(CONTENT_LENGTH, mapped.capacity());
2644        assertEquals(0, mapped.position());
2645
2646        // map not change channel's position
2647        assertEquals(0, readOnlyFileChannel.position());
2648        assertEquals(0, readWriteFileChannel.position());
2649    }
2650
2651    /**
2652     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2653     */
2654    @TestTargetNew(
2655        level = TestLevel.PARTIAL_COMPLETE,
2656        notes = "",
2657        method = "map",
2658        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2659    )
2660    public void test_map_ReadOnly_NonZeroPosition() throws IOException {
2661        this.writeDataToFile(fileOfReadOnlyFileChannel);
2662        MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
2663                10, CONTENT_LENGTH - 10);
2664        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
2665        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
2666        assertEquals(0, mapped.position());
2667    }
2668
2669    /**
2670     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2671     */
2672    @TestTargetNew(
2673        level = TestLevel.PARTIAL_COMPLETE,
2674        notes = "",
2675        method = "map",
2676        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2677    )
2678    public void test_map_Private() throws IOException {
2679        this.writeDataToFile(fileOfReadWriteFileChannel);
2680        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
2681                CONTENT_LENGTH);
2682        assertEquals(CONTENT_LENGTH, mapped.limit());
2683        // test copy on write if private
2684        ByteBuffer returnByPut = mapped.put(TEST_BYTES);
2685        assertSame(returnByPut, mapped);
2686        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
2687        mapped.force();
2688        readWriteFileChannel.read(checkBuffer);
2689        assertEquals(CONTENT, new String(checkBuffer.array(), "iso8859-1"));
2690
2691        // test overflow
2692        try {
2693            mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
2694            fail("should throw BufferOverflowException.");
2695        } catch (BufferOverflowException ex) {
2696            // expected;
2697        }
2698    }
2699
2700    /**
2701     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2702     */
2703    @TestTargetNew(
2704        level = TestLevel.PARTIAL_COMPLETE,
2705        notes = "",
2706        method = "map",
2707        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2708    )
2709    public void test_map_Private_NonZeroPosition() throws IOException {
2710        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 10,
2711                CONTENT_LENGTH - 10);
2712        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
2713        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
2714        assertEquals(0, mapped.position());
2715    }
2716
2717    /**
2718     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2719     */
2720    @TestTargetNew(
2721        level = TestLevel.PARTIAL_COMPLETE,
2722        notes = "",
2723        method = "map",
2724        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2725    )
2726    public void test_map_ReadWrite() throws IOException {
2727        MappedByteBuffer mapped = null;
2728        writeDataToFile(fileOfReadWriteFileChannel);
2729        mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT
2730                .length());
2731
2732        // put something will change its channel
2733        ByteBuffer returnByPut = mapped.put(TEST_BYTES);
2734        assertSame(returnByPut, mapped);
2735        String checkString = "test" + CONTENT.substring(4);
2736        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
2737        mapped.force();
2738        readWriteFileChannel.position(0);
2739        readWriteFileChannel.read(checkBuffer);
2740        assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1"));
2741
2742        try {
2743            mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
2744            fail("should throw BufferOverflowException.");
2745        } catch (BufferOverflowException ex) {
2746            // expected;
2747        }
2748    }
2749
2750    /**
2751     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2752     */
2753    @TestTargetNew(
2754        level = TestLevel.PARTIAL_COMPLETE,
2755        notes = "",
2756        method = "map",
2757        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2758    )
2759    public void test_map_ReadWrite_NonZeroPosition() throws IOException {
2760        // test position non-zero
2761        writeDataToFile(fileOfReadWriteFileChannel);
2762        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_WRITE,
2763                10, CONTENT_LENGTH - 10);
2764        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
2765        assertEquals(CONTENT.length() - 10, mapped.capacity());
2766        assertEquals(0, mapped.position());
2767        mapped.put(TEST_BYTES);
2768        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
2769        readWriteFileChannel.read(checkBuffer);
2770        String expected = CONTENT.substring(0, 10) + "test"
2771                + CONTENT.substring(10 + "test".length());
2772        assertEquals(expected, new String(checkBuffer.array(), "iso8859-1"));
2773    }
2774
2775    /**
2776     * Tests map() method for the value of positions exceeding memory
2777     * page size and allocation granularity size.
2778     *
2779     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
2780     */
2781    @TestTargetNew(
2782        level = TestLevel.PARTIAL_COMPLETE,
2783        notes = "",
2784        method = "map",
2785        args = {java.nio.channels.FileChannel.MapMode.class, long.class, long.class}
2786    )
2787    @AndroidOnly("Platform.class is harmony specific")
2788    public void test_map_LargePosition() throws IOException {
2789        // Regression test for HARMONY-3085
2790        int[] sizes = {
2791            4096, // 4K size (normal page size for Linux & Windows)
2792            65536, // 64K size (alocation granularity size for Windows)
2793            Platform.getFileSystem().getAllocGranularity() // alloc granularity
2794        };
2795        final int CONTENT_LEN = 10;
2796
2797        for (int i = 0; i < sizes.length; ++i) {
2798            // reset the file and the channel for the iterations
2799            // (for the first iteration it was done by setUp()
2800            if (i > 0 ) {
2801                fileOfReadOnlyFileChannel = File.createTempFile(
2802                        "File_of_readOnlyFileChannel", "tmp");
2803                fileOfReadOnlyFileChannel.deleteOnExit();
2804                readOnlyFileChannel = new FileInputStream(
2805                        fileOfReadOnlyFileChannel).getChannel();
2806            }
2807
2808            writeLargeDataToFile(fileOfReadOnlyFileChannel, sizes[i] +
2809                    2 * CONTENT_LEN);
2810            MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
2811                    sizes[i], CONTENT_LEN);
2812            assertEquals("Incorrectly mapped file channel for " + sizes[i]
2813                    + " position (capacity)", CONTENT_LEN, mapped.capacity());
2814            assertEquals("Incorrectly mapped file channel for " + sizes[i]
2815                    + " position (limit)", CONTENT_LEN, mapped.limit());
2816            assertEquals("Incorrectly mapped file channel for " + sizes[i]
2817                    + " position (position)", 0, mapped.position());
2818
2819            // map not change channel's position
2820            assertEquals(0, readOnlyFileChannel.position());
2821
2822            // Close the file and the channel before the next iteration
2823            readOnlyFileChannel.close();
2824            fileOfReadOnlyFileChannel.delete();
2825        }
2826    }
2827
2828    /**
2829     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
2830     */
2831    @TestTargetNew(
2832        level = TestLevel.PARTIAL_COMPLETE,
2833        notes = "Verifies NullPointerException.",
2834        method = "write",
2835        args = {java.nio.ByteBuffer.class}
2836    )
2837    public void test_writeLByteBuffer_Null() throws Exception {
2838        ByteBuffer writeBuffer = null;
2839
2840        try {
2841            writeOnlyFileChannel.write(writeBuffer);
2842            fail("should throw NullPointerException");
2843        } catch (NullPointerException e) {
2844            // expected
2845        }
2846
2847        try {
2848            readWriteFileChannel.write(writeBuffer);
2849            fail("should throw NullPointerException");
2850        } catch (NullPointerException e) {
2851            // expected
2852        }
2853    }
2854
2855    /**
2856     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
2857     */
2858    @TestTargetNew(
2859        level = TestLevel.PARTIAL_COMPLETE,
2860        notes = "Verifies ClosedChannelException.",
2861        method = "write",
2862        args = {java.nio.ByteBuffer.class}
2863    )
2864    public void test_writeLByteBuffer_Closed() throws Exception {
2865        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
2866
2867        readOnlyFileChannel.close();
2868        try {
2869            readOnlyFileChannel.write(writeBuffer);
2870            fail("should throw ClosedChannelException");
2871        } catch (ClosedChannelException e) {
2872            // expected
2873        }
2874
2875        writeOnlyFileChannel.close();
2876        try {
2877            writeOnlyFileChannel.write(writeBuffer);
2878            fail("should throw ClosedChannelException");
2879        } catch (ClosedChannelException e) {
2880            // expected
2881        }
2882
2883        readWriteFileChannel.close();
2884        try {
2885            readWriteFileChannel.write(writeBuffer);
2886            fail("should throw ClosedChannelException");
2887        } catch (ClosedChannelException e) {
2888            // expected
2889        }
2890
2891        // should throw ClosedChannelException first
2892        writeBuffer = null;
2893        try {
2894            readWriteFileChannel.read(writeBuffer);
2895            fail("should throw ClosedChannelException");
2896        } catch (ClosedChannelException e) {
2897            // expected
2898        }
2899
2900        try {
2901            readOnlyFileChannel.write(writeBuffer);
2902            fail("should throw ClosedChannelException");
2903        } catch (ClosedChannelException e) {
2904            // expected
2905        }
2906
2907        writeOnlyFileChannel.close();
2908        try {
2909            writeOnlyFileChannel.write(writeBuffer);
2910            fail("should throw ClosedChannelException");
2911        } catch (ClosedChannelException e) {
2912            // expected
2913        }
2914    }
2915
2916    /**
2917     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
2918     */
2919    @TestTargetNew(
2920        level = TestLevel.PARTIAL_COMPLETE,
2921        notes = "Verifies NonWritableChannelException.",
2922        method = "write",
2923        args = {java.nio.ByteBuffer.class}
2924    )
2925    public void test_writeLByteBuffer_ReadOnly() throws Exception {
2926        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
2927
2928        try {
2929            readOnlyFileChannel.write(writeBuffer);
2930            fail("should throw NonWritableChannelException");
2931        } catch (NonWritableChannelException e) {
2932            // expected
2933        }
2934
2935        // first throws NonWriteableChannelException
2936        writeBuffer = null;
2937        try {
2938            readOnlyFileChannel.write(writeBuffer);
2939            fail("should throw NonWritableChannelException");
2940        } catch (NonWritableChannelException e) {
2941            // expected
2942        }
2943    }
2944
2945    /**
2946     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
2947     */
2948    @TestTargetNew(
2949        level = TestLevel.PARTIAL_COMPLETE,
2950        notes = "",
2951        method = "write",
2952        args = {java.nio.ByteBuffer.class}
2953    )
2954    public void test_writeLByteBuffer() throws Exception {
2955        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
2956
2957        int result = writeOnlyFileChannel.write(writeBuffer);
2958        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
2959        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position());
2960        writeOnlyFileChannel.close();
2961
2962        assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel
2963                .length());
2964
2965        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
2966        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
2967        fis.read(inputBuffer);
2968        assertTrue(Arrays.equals(CONTENT_AS_BYTES, inputBuffer));
2969    }
2970
2971    /**
2972     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
2973     */
2974    @TestTargetNew(
2975        level = TestLevel.PARTIAL_COMPLETE,
2976        notes = "",
2977        method = "write",
2978        args = {java.nio.ByteBuffer.class}
2979    )
2980    public void test_writeLByteBuffer_NonZeroPosition() throws Exception {
2981        final int pos = 5;
2982        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
2983        writeBuffer.position(pos);
2984        int result = writeOnlyFileChannel.write(writeBuffer);
2985        assertEquals(CONTENT_AS_BYTES_LENGTH - pos, result);
2986        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position());
2987        writeOnlyFileChannel.close();
2988
2989        assertEquals(CONTENT_AS_BYTES_LENGTH - pos, fileOfWriteOnlyFileChannel
2990                .length());
2991
2992        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
2993        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH - pos];
2994        fis.read(inputBuffer);
2995        String test = CONTENT.substring(pos);
2996        assertTrue(Arrays.equals(test.getBytes(), inputBuffer));
2997    }
2998
2999    /**
3000     * @tests java.nio.channels.FileChannel#write(ByteBuffer, long)
3001     */
3002    @TestTargetNew(
3003        level = TestLevel.PARTIAL_COMPLETE,
3004        notes = "Verifies NullPointerException.",
3005        method = "write",
3006        args = {java.nio.ByteBuffer.class, long.class}
3007    )
3008    public void test_writeLByteBufferJ_Null() throws Exception {
3009        ByteBuffer writeBuffer = null;
3010
3011        try {
3012            readOnlyFileChannel.write(writeBuffer, 0);
3013            fail("should throw NullPointerException");
3014        } catch (NullPointerException e) {
3015            // expected
3016        }
3017
3018        // first throws NullPointerException
3019        try {
3020            readOnlyFileChannel.write(writeBuffer, -1);
3021            fail("should throw NullPointerException");
3022        } catch (NullPointerException e) {
3023            // expected
3024        }
3025
3026        try {
3027            writeOnlyFileChannel.write(writeBuffer, 0);
3028            fail("should throw NullPointerException");
3029        } catch (NullPointerException e) {
3030            // expected
3031        }
3032
3033        // first throws NullPointerException
3034        try {
3035            writeOnlyFileChannel.write(writeBuffer, -1);
3036            fail("should throw NullPointerException");
3037        } catch (NullPointerException e) {
3038            // expected
3039        }
3040
3041        try {
3042            readWriteFileChannel.write(writeBuffer, 0);
3043            fail("should throw NullPointerException");
3044        } catch (NullPointerException e) {
3045            // expected
3046        }
3047
3048        // first throws NullPointerException
3049        try {
3050            readWriteFileChannel.write(writeBuffer, -1);
3051            fail("should throw NullPointerException");
3052        } catch (NullPointerException e) {
3053            // expected
3054        }
3055
3056        // first throws NullPointerException
3057        readWriteFileChannel.close();
3058        try {
3059            readWriteFileChannel.write(writeBuffer, 0);
3060            fail("should throw NullPointerException");
3061        } catch (NullPointerException e) {
3062            // expected
3063        }
3064
3065        try {
3066            readWriteFileChannel.write(writeBuffer, -1);
3067            fail("should throw NullPointerException");
3068        } catch (NullPointerException e) {
3069            // expected
3070        }
3071
3072        writeOnlyFileChannel.close();
3073        try {
3074            writeOnlyFileChannel.write(writeBuffer, 0);
3075            fail("should throw NullPointerException");
3076        } catch (NullPointerException e) {
3077            // expected
3078        }
3079
3080        try {
3081            writeOnlyFileChannel.write(writeBuffer, -1);
3082            fail("should throw NullPointerException");
3083        } catch (NullPointerException e) {
3084            // expected
3085        }
3086
3087        readOnlyFileChannel.close();
3088        try {
3089            readOnlyFileChannel.write(writeBuffer, 0);
3090            fail("should throw NullPointerException");
3091        } catch (NullPointerException e) {
3092            // expected
3093        }
3094
3095        try {
3096            readOnlyFileChannel.write(writeBuffer, -1);
3097            fail("should throw NullPointerException");
3098        } catch (NullPointerException e) {
3099            // expected
3100        }
3101    }
3102
3103    /**
3104     * @tests java.nio.channels.FileChannel#write(ByteBuffer, long)
3105     */
3106    @TestTargetNew(
3107        level = TestLevel.PARTIAL_COMPLETE,
3108        notes = "Verifies ClosedChannelException.",
3109        method = "write",
3110        args = {java.nio.ByteBuffer.class, long.class}
3111    )
3112    public void test_writeLByteBufferJ_Closed() throws Exception {
3113        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
3114
3115        writeOnlyFileChannel.close();
3116        try {
3117            writeOnlyFileChannel.write(writeBuffer, 0);
3118            fail("should throw ClosedChannelException");
3119        } catch (ClosedChannelException e) {
3120            // expected
3121        }
3122
3123        readWriteFileChannel.close();
3124        try {
3125            readWriteFileChannel.write(writeBuffer, 0);
3126            fail("should throw ClosedChannelException");
3127        } catch (ClosedChannelException e) {
3128            // expected
3129        }
3130    }
3131
3132    /**
3133     * @tests java.nio.channels.FileChannel#write(ByteBuffer, long)
3134     */
3135    @TestTargetNew(
3136        level = TestLevel.PARTIAL_COMPLETE,
3137        notes = "Verifies NonWritableChannelException.",
3138        method = "write",
3139        args = {java.nio.ByteBuffer.class, long.class}
3140    )
3141    public void test_writeLByteBufferJ_ReadOnly() throws Exception {
3142        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
3143
3144        try {
3145            readOnlyFileChannel.write(writeBuffer, 10);
3146            fail("should throw NonWritableChannelException");
3147        } catch (NonWritableChannelException e) {
3148            // expected
3149        }
3150
3151        // regression test for Harmony-903
3152
3153        // read-only file channel never throws ClosedChannelException even if
3154        // the channel is closed.
3155        readOnlyFileChannel.close();
3156        try {
3157            readOnlyFileChannel.write(writeBuffer, 10);
3158            fail("should throw NonWritableChannelException");
3159        } catch (NonWritableChannelException e) {
3160            // expected
3161        }
3162
3163        try {
3164            readOnlyFileChannel.write(writeBuffer, -1);
3165            fail("should throw IllegalArgumentException");
3166        } catch (IllegalArgumentException e) {
3167            // expected
3168        }
3169
3170        writeBuffer = null;
3171        try {
3172            readOnlyFileChannel.write(writeBuffer, -1);
3173            fail("should throw NullPointerException");
3174        } catch (NullPointerException e) {
3175            // expected
3176        }
3177    }
3178
3179    /**
3180     * @tests java.nio.channels.FileChannel#read(ByteBuffer,long)
3181     */
3182    @TestTargetNew(
3183        level = TestLevel.PARTIAL_COMPLETE,
3184        notes = "Verifies IOException.",
3185        method = "read",
3186        args = {java.nio.ByteBuffer.class, long.class}
3187    )
3188    public void test_writeLByteBufferJ_Postion_As_Long() throws Exception {
3189        ByteBuffer writeBuffer = ByteBuffer.wrap(TEST_BYTES);
3190        try {
3191            writeOnlyFileChannel.write(writeBuffer, Long.MAX_VALUE);
3192        } catch (IOException e) {
3193            // expected
3194        }
3195    }
3196
3197    /**
3198     * @tests java.nio.channels.FileChannel#write(ByteBuffer, long)
3199     */
3200    @TestTargetNew(
3201        level = TestLevel.PARTIAL_COMPLETE,
3202        notes = "Verifies IllegalArgumentException.",
3203        method = "write",
3204        args = {java.nio.ByteBuffer.class, long.class}
3205    )
3206    public void test_writeLByteBufferJ_IllegalArgument() throws Exception {
3207        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
3208
3209        try {
3210            readOnlyFileChannel.write(writeBuffer, -1);
3211            fail("should throw IllegalArgumentException");
3212        } catch (IllegalArgumentException e) {
3213            // expected
3214        }
3215
3216        try {
3217            writeOnlyFileChannel.write(writeBuffer, -1);
3218            fail("should throw IllegalArgumentException");
3219        } catch (IllegalArgumentException e) {
3220            // expected
3221        }
3222
3223        try {
3224            readWriteFileChannel.write(writeBuffer, -1);
3225            fail("should throw IllegalArgumentException");
3226        } catch (IllegalArgumentException e) {
3227            // expected
3228        }
3229
3230        // throws IllegalArgumentException first.
3231        readOnlyFileChannel.close();
3232        try {
3233            readOnlyFileChannel.write(writeBuffer, -1);
3234            fail("should throw IllegalArgumentException");
3235        } catch (IllegalArgumentException e) {
3236            // expected
3237        }
3238
3239        writeOnlyFileChannel.close();
3240        try {
3241            writeOnlyFileChannel.write(writeBuffer, -1);
3242            fail("should throw IllegalArgumentException");
3243        } catch (IllegalArgumentException e) {
3244            // expected
3245        }
3246
3247        readWriteFileChannel.close();
3248        try {
3249            readWriteFileChannel.write(writeBuffer, -1);
3250            fail("should throw IllegalArgumentException");
3251        } catch (IllegalArgumentException e) {
3252            // expected
3253        }
3254    }
3255
3256    /**
3257     * @tests java.nio.channels.FileChannel#write(ByteBuffer,long)
3258     */
3259    @TestTargetNew(
3260        level = TestLevel.PARTIAL_COMPLETE,
3261        notes = "",
3262        method = "write",
3263        args = {java.nio.ByteBuffer.class, long.class}
3264    )
3265    public void test_writeLByteBufferJ() throws Exception {
3266        writeDataToFile(fileOfWriteOnlyFileChannel);
3267
3268        final int POSITION = 4;
3269        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
3270        int result = writeOnlyFileChannel.write(writeBuffer, POSITION);
3271        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
3272        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position());
3273        writeOnlyFileChannel.close();
3274
3275        assertEquals(POSITION + CONTENT_AS_BYTES_LENGTH,
3276                fileOfWriteOnlyFileChannel.length());
3277
3278        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
3279        byte[] inputBuffer = new byte[POSITION + CONTENT_AS_BYTES_LENGTH];
3280        fis.read(inputBuffer);
3281        byte[] expectedResult = new byte[POSITION + CONTENT_AS_BYTES_LENGTH];
3282        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, POSITION);
3283        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, POSITION,
3284                CONTENT_AS_BYTES_LENGTH);
3285        assertTrue(Arrays.equals(expectedResult, inputBuffer));
3286    }
3287
3288    /**
3289     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
3290     */
3291    @TestTargetNew(
3292        level = TestLevel.PARTIAL_COMPLETE,
3293        notes = "",
3294        method = "write",
3295        args = {java.nio.ByteBuffer.class, long.class}
3296    )
3297    public void test_writeLByteBufferJ_NonZeroPosition() throws Exception {
3298        final int pos = 5;
3299        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
3300        writeBuffer.position(pos);
3301        int result = writeOnlyFileChannel.write(writeBuffer, pos);
3302        assertEquals(CONTENT_AS_BYTES_LENGTH - pos, result);
3303        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position());
3304        writeOnlyFileChannel.close();
3305
3306        assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel
3307                .length());
3308
3309        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
3310        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH - pos];
3311        fis.skip(pos);
3312        fis.read(inputBuffer);
3313        String test = CONTENT.substring(pos);
3314        assertTrue(Arrays.equals(test.getBytes(), inputBuffer));
3315    }
3316
3317    /**
3318     * @tests java.nio.channels.FileChannel#write(ByteBuffer[])
3319     */
3320    @TestTargetNew(
3321        level = TestLevel.PARTIAL_COMPLETE,
3322        notes = "Verifies ClosedChannelException.",
3323        method = "write",
3324        args = {java.nio.ByteBuffer[].class}
3325    )
3326    public void test_write$LByteBuffer_Closed() throws Exception {
3327        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3328        writeBuffers[0] = ByteBuffer.allocate(CAPACITY);
3329        writeBuffers[1] = ByteBuffer.allocate(CAPACITY);
3330
3331        readOnlyFileChannel.close();
3332        try {
3333            readOnlyFileChannel.write(writeBuffers);
3334            fail("should throw ClosedChannelException");
3335        } catch (ClosedChannelException e) {
3336            // expected
3337        }
3338
3339        writeOnlyFileChannel.close();
3340        try {
3341            writeOnlyFileChannel.write(writeBuffers);
3342            fail("should throw ClosedChannelException");
3343        } catch (ClosedChannelException e) {
3344            // expected
3345        }
3346
3347        readWriteFileChannel.close();
3348        try {
3349            readWriteFileChannel.write(writeBuffers);
3350            fail("should throw ClosedChannelException");
3351        } catch (ClosedChannelException e) {
3352            // expected
3353        }
3354    }
3355
3356    /**
3357     * @tests java.nio.channels.FileChannel#write(ByteBuffer[])
3358     */
3359    @TestTargetNew(
3360        level = TestLevel.PARTIAL_COMPLETE,
3361        notes = "Verifies NonWritableChannelException",
3362        method = "write",
3363        args = {java.nio.ByteBuffer[].class}
3364    )
3365    public void test_write$LByteBuffer_ReadOnly() throws Exception {
3366        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3367        writeBuffers[0] = ByteBuffer.allocate(CAPACITY);
3368        writeBuffers[1] = ByteBuffer.allocate(CAPACITY);
3369
3370        try {
3371            readOnlyFileChannel.write(writeBuffers);
3372            fail("should throw NonWritableChannelException");
3373        } catch (NonWritableChannelException e) {
3374            // expected
3375        }
3376    }
3377
3378    /**
3379     * @tests java.nio.channels.FileChannel#write(ByteBuffer[])
3380     */
3381    @TestTargetNew(
3382        level = TestLevel.PARTIAL_COMPLETE,
3383        notes = "Verifies NullPointerException.",
3384        method = "write",
3385        args = {java.nio.ByteBuffer[].class}
3386    )
3387    public void test_write$LByteBuffer_EmptyBuffers() throws Exception {
3388        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3389        writeBuffers[0] = ByteBuffer.allocate(this.CONTENT_LENGTH);
3390        try {
3391            writeOnlyFileChannel.write(writeBuffers);
3392            fail("should throw NullPointerException");
3393        } catch (NullPointerException e) {
3394            // expected
3395        }
3396
3397        try {
3398            readWriteFileChannel.write(writeBuffers);
3399            fail("should throw NullPointerException");
3400        } catch (NullPointerException e) {
3401            // expected
3402        }
3403    }
3404
3405    /**
3406     * @tests java.nio.channels.FileChannel#write(ByteBuffer[])
3407     */
3408    @TestTargetNew(
3409        level = TestLevel.PARTIAL_COMPLETE,
3410        notes = "",
3411        method = "write",
3412        args = {java.nio.ByteBuffer[].class}
3413    )
3414    public void test_write$LByteBuffer() throws Exception {
3415        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3416        writeBuffers[0] = ByteBuffer.wrap(CONTENT_AS_BYTES);
3417        writeBuffers[1] = ByteBuffer.wrap(CONTENT_AS_BYTES);
3418
3419        long result = writeOnlyFileChannel.write(writeBuffers);
3420        assertEquals(CONTENT_AS_BYTES_LENGTH * 2, result);
3421        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[0].position());
3422        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[1].position());
3423        writeOnlyFileChannel.close();
3424
3425        assertEquals(CONTENT_AS_BYTES_LENGTH * 2, fileOfWriteOnlyFileChannel
3426                .length());
3427
3428        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
3429        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
3430        fis.read(inputBuffer);
3431        byte[] expectedResult = new byte[CONTENT_AS_BYTES_LENGTH * 2];
3432        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0,
3433                CONTENT_AS_BYTES_LENGTH);
3434        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult,
3435                CONTENT_AS_BYTES_LENGTH, CONTENT_AS_BYTES_LENGTH);
3436        assertTrue(Arrays.equals(CONTENT_AS_BYTES, inputBuffer));
3437    }
3438
3439    /**
3440     * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int)
3441     */
3442    @TestTargetNew(
3443        level = TestLevel.PARTIAL_COMPLETE,
3444        notes = "Verifies NullPointerException.",
3445        method = "write",
3446        args = {java.nio.ByteBuffer[].class, int.class, int.class}
3447    )
3448    public void test_write$LByteBufferII_Null() throws Exception {
3449        ByteBuffer[] writeBuffers = null;
3450
3451        try {
3452            readOnlyFileChannel.write(writeBuffers, 1, 2);
3453            fail("should throw NullPointerException");
3454        } catch (NullPointerException e) {
3455            // expected
3456        }
3457
3458        try {
3459            writeOnlyFileChannel.write(writeBuffers, 1, 2);
3460            fail("should throw NullPointerException");
3461        } catch (NullPointerException e) {
3462            // expected
3463        }
3464
3465        try {
3466            readWriteFileChannel.write(writeBuffers, 1, 2);
3467            fail("should throw NullPointerException");
3468        } catch (NullPointerException e) {
3469            // expected
3470        }
3471
3472        // first throws NullPointerException
3473        readOnlyFileChannel.close();
3474        try {
3475            readOnlyFileChannel.write(writeBuffers, 1, 2);
3476            fail("should throw NullPointerException");
3477        } catch (NullPointerException e) {
3478            // expected
3479        }
3480
3481        writeOnlyFileChannel.close();
3482        try {
3483            writeOnlyFileChannel.write(writeBuffers, 1, 2);
3484            fail("should throw NullPointerException");
3485        } catch (NullPointerException e) {
3486            // expected
3487        }
3488
3489        readWriteFileChannel.close();
3490        try {
3491            readWriteFileChannel.write(writeBuffers, 1, 2);
3492            fail("should throw NullPointerException");
3493        } catch (NullPointerException e) {
3494            // expected
3495        }
3496    }
3497
3498    /**
3499     * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int)
3500     */
3501    @TestTargetNew(
3502        level = TestLevel.PARTIAL_COMPLETE,
3503        notes = "Verifies ClosedChannelException.",
3504        method = "write",
3505        args = {java.nio.ByteBuffer[].class, int.class, int.class}
3506    )
3507    public void test_write$LByteBufferII_Closed() throws Exception {
3508        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3509        writeBuffers[0] = ByteBuffer.allocate(CAPACITY);
3510        writeBuffers[1] = ByteBuffer.allocate(CAPACITY);
3511
3512        readOnlyFileChannel.close();
3513        try {
3514            readOnlyFileChannel.write(writeBuffers, 0, 2);
3515            fail("should throw ClosedChannelException");
3516        } catch (ClosedChannelException e) {
3517            // expected
3518        }
3519
3520        writeOnlyFileChannel.close();
3521        try {
3522            writeOnlyFileChannel.write(writeBuffers, 0, 2);
3523            fail("should throw ClosedChannelException");
3524        } catch (ClosedChannelException e) {
3525            // expected
3526        }
3527
3528        readWriteFileChannel.close();
3529        try {
3530            readWriteFileChannel.write(writeBuffers, 0, 2);
3531            fail("should throw ClosedChannelException");
3532        } catch (ClosedChannelException e) {
3533            // expected
3534        }
3535    }
3536
3537    /**
3538     * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int)
3539     */
3540    @TestTargetNew(
3541        level = TestLevel.PARTIAL_COMPLETE,
3542        notes = "Verifies NonWritableChannelException, IndexOutOfBoundsException, NullPointerException.",
3543        method = "write",
3544        args = {java.nio.ByteBuffer[].class, int.class, int.class}
3545    )
3546    public void test_write$LByteBufferII_ReadOnly() throws Exception {
3547        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3548        writeBuffers[0] = ByteBuffer.allocate(CAPACITY);
3549        writeBuffers[1] = ByteBuffer.allocate(CAPACITY);
3550
3551        try {
3552            readOnlyFileChannel.write(writeBuffers, 0, 2);
3553            fail("should throw NonWritableChannelException");
3554        } catch (NonWritableChannelException e) {
3555            // expected
3556        }
3557
3558        // throw NonWritableChannelException first although buffer array is
3559        // empty.
3560        writeBuffers = new ByteBuffer[2];
3561        try {
3562            readOnlyFileChannel.write(writeBuffers, 0, 2);
3563            fail("should throw NonWritableChannelException");
3564        } catch (NonWritableChannelException e) {
3565            // expected
3566        }
3567
3568        readOnlyFileChannel.close();
3569        writeBuffers = null;
3570        try {
3571            readOnlyFileChannel.write(writeBuffers, 0, -1);
3572            fail("should throw IndexOutOfBoundsException");
3573        } catch (IndexOutOfBoundsException e) {
3574            // expected
3575        }
3576
3577        try {
3578            readOnlyFileChannel.write(writeBuffers, 0, 1);
3579            fail("should throw NullPointerException");
3580        } catch (NullPointerException e) {
3581            // expected
3582        }
3583    }
3584
3585    /**
3586     * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int)
3587     */
3588    @TestTargetNew(
3589        level = TestLevel.PARTIAL_COMPLETE,
3590        notes = "Verifies IndexOutOfBoundsException.",
3591        method = "write",
3592        args = {java.nio.ByteBuffer[].class, int.class, int.class}
3593    )
3594    public void test_write$LByteBufferII_IndexOutOfBound() throws Exception {
3595        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3596        writeBuffers[0] = ByteBuffer.allocate(this.CONTENT_LENGTH);
3597        writeBuffers[1] = ByteBuffer.allocate(this.CONTENT_LENGTH);
3598
3599        try {
3600            writeOnlyFileChannel.write(writeBuffers, -1, 0);
3601            fail("should throw IndexOutOfBoundsException");
3602        } catch (IndexOutOfBoundsException e) {
3603            // expected
3604        }
3605        try {
3606            writeOnlyFileChannel.write(writeBuffers, 0, -1);
3607            fail("should throw IndexOutOfBoundsException");
3608        } catch (IndexOutOfBoundsException e) {
3609            // expected
3610        }
3611        try {
3612            writeOnlyFileChannel.write(writeBuffers, 0, 3);
3613            fail("should throw IndexOutOfBoundsException");
3614        } catch (IndexOutOfBoundsException e) {
3615            // expected
3616        }
3617        try {
3618            writeOnlyFileChannel.write(writeBuffers, 1, 2);
3619            fail("should throw IndexOutOfBoundsException");
3620        } catch (IndexOutOfBoundsException e) {
3621            // expected
3622        }
3623        try {
3624            writeOnlyFileChannel.write(writeBuffers, 2, 1);
3625            fail("should throw IndexOutOfBoundsException");
3626        } catch (IndexOutOfBoundsException e) {
3627            // expected
3628        }
3629        try {
3630            writeOnlyFileChannel.write(writeBuffers, 3, 0);
3631            fail("should throw IndexOutOfBoundsException");
3632        } catch (IndexOutOfBoundsException e) {
3633            // expected
3634        }
3635
3636        try {
3637            readWriteFileChannel.write(writeBuffers, -1, 0);
3638            fail("should throw IndexOutOfBoundsException");
3639        } catch (IndexOutOfBoundsException e) {
3640            // expected
3641        }
3642        try {
3643            readWriteFileChannel.write(writeBuffers, 0, -1);
3644            fail("should throw IndexOutOfBoundsException");
3645        } catch (IndexOutOfBoundsException e) {
3646            // expected
3647        }
3648        try {
3649            readWriteFileChannel.write(writeBuffers, 0, 3);
3650            fail("should throw IndexOutOfBoundsException");
3651        } catch (IndexOutOfBoundsException e) {
3652            // expected
3653        }
3654        try {
3655            readWriteFileChannel.write(writeBuffers, 1, 2);
3656            fail("should throw IndexOutOfBoundsException");
3657        } catch (IndexOutOfBoundsException e) {
3658            // expected
3659        }
3660        try {
3661            readWriteFileChannel.write(writeBuffers, 2, 1);
3662            fail("should throw IndexOutOfBoundsException");
3663        } catch (IndexOutOfBoundsException e) {
3664            // expected
3665        }
3666        try {
3667            readWriteFileChannel.write(writeBuffers, 3, 0);
3668            fail("should throw IndexOutOfBoundsException");
3669        } catch (IndexOutOfBoundsException e) {
3670            // expected
3671        }
3672
3673        try {
3674            readOnlyFileChannel.write(writeBuffers, -1, 0);
3675            fail("should throw IndexOutOfBoundsException");
3676        } catch (IndexOutOfBoundsException e) {
3677            // expected
3678        }
3679        try {
3680            readOnlyFileChannel.write(writeBuffers, 0, -1);
3681            fail("should throw IndexOutOfBoundsException");
3682        } catch (IndexOutOfBoundsException e) {
3683            // expected
3684        }
3685        try {
3686            readOnlyFileChannel.write(writeBuffers, 0, 3);
3687            fail("should throw IndexOutOfBoundsException");
3688        } catch (IndexOutOfBoundsException e) {
3689            // expected
3690        }
3691        try {
3692            readOnlyFileChannel.write(writeBuffers, 1, 2);
3693            fail("should throw IndexOutOfBoundsException");
3694        } catch (IndexOutOfBoundsException e) {
3695            // expected
3696        }
3697        try {
3698            readOnlyFileChannel.write(writeBuffers, 2, 1);
3699            fail("should throw IndexOutOfBoundsException");
3700        } catch (IndexOutOfBoundsException e) {
3701            // expected
3702        }
3703        try {
3704            readOnlyFileChannel.write(writeBuffers, 3, 0);
3705            fail("should throw IndexOutOfBoundsException");
3706        } catch (IndexOutOfBoundsException e) {
3707            // expected
3708        }
3709    }
3710
3711    /**
3712     * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int)
3713     */
3714    @TestTargetNew(
3715        level = TestLevel.PARTIAL_COMPLETE,
3716        notes = "Verifies NullPointerException.",
3717        method = "write",
3718        args = {java.nio.ByteBuffer[].class, int.class, int.class}
3719    )
3720    public void test_write$LByteBufferII_EmptyBuffers() throws Exception {
3721        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3722        writeBuffers[0] = ByteBuffer.allocate(this.CONTENT_LENGTH);
3723        try {
3724            writeOnlyFileChannel.write(writeBuffers, 0, 2);
3725            fail("should throw NullPointerException");
3726        } catch (NullPointerException e) {
3727            // expected
3728        }
3729
3730        try {
3731            readWriteFileChannel.write(writeBuffers, 0, 2);
3732            fail("should throw NullPointerException");
3733        } catch (NullPointerException e) {
3734            // expected
3735        }
3736    }
3737
3738    /**
3739     * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int)
3740     */
3741    @TestTargetNew(
3742        level = TestLevel.PARTIAL_COMPLETE,
3743        notes = "",
3744        method = "write",
3745        args = {java.nio.ByteBuffer[].class, int.class, int.class}
3746    )
3747    public void test_write$LByteBufferII() throws Exception {
3748        ByteBuffer[] writeBuffers = new ByteBuffer[2];
3749        writeBuffers[0] = ByteBuffer.wrap(CONTENT_AS_BYTES);
3750        writeBuffers[1] = ByteBuffer.wrap(CONTENT_AS_BYTES);
3751
3752        long result = writeOnlyFileChannel.write(writeBuffers, 0, 2);
3753        assertEquals(CONTENT_AS_BYTES_LENGTH * 2, result);
3754        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[0].position());
3755        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[1].position());
3756        writeOnlyFileChannel.close();
3757
3758        assertEquals(CONTENT_AS_BYTES_LENGTH * 2, fileOfWriteOnlyFileChannel
3759                .length());
3760
3761        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
3762        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
3763        fis.read(inputBuffer);
3764        byte[] expectedResult = new byte[CONTENT_AS_BYTES_LENGTH * 2];
3765        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0,
3766                CONTENT_AS_BYTES_LENGTH);
3767        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult,
3768                CONTENT_AS_BYTES_LENGTH, CONTENT_AS_BYTES_LENGTH);
3769        assertTrue(Arrays.equals(CONTENT_AS_BYTES, inputBuffer));
3770    }
3771
3772    /**
3773     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3774     */
3775    @TestTargetNew(
3776        level = TestLevel.PARTIAL_COMPLETE,
3777        notes = "Verifies ClosedChannelException.",
3778        method = "transferFrom",
3779        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3780    )
3781    public void test_transferFromLReadableByteChannelJJ_Closed()
3782            throws Exception {
3783        readByteChannel = DatagramChannel.open();
3784        readOnlyFileChannel.close();
3785        try {
3786            readOnlyFileChannel.transferFrom(readByteChannel, 0, 0);
3787            fail("should throw ClosedChannelException.");
3788        } catch (ClosedChannelException e) {
3789            // expected
3790        }
3791
3792        writeOnlyFileChannel.close();
3793        try {
3794            writeOnlyFileChannel.transferFrom(readByteChannel, 0, 10);
3795            fail("should throw ClosedChannelException.");
3796        } catch (ClosedChannelException e) {
3797            // expected
3798        }
3799
3800        readWriteFileChannel.close();
3801        try {
3802            readWriteFileChannel.transferFrom(readByteChannel, 0, 0);
3803            fail("should throw ClosedChannelException.");
3804        } catch (ClosedChannelException e) {
3805            // expected
3806        }
3807
3808        // should throw ClosedChannelException first.
3809        try {
3810            readWriteFileChannel.transferFrom(readByteChannel, 0, -1);
3811            fail("should throw ClosedChannelException.");
3812        } catch (ClosedChannelException e) {
3813            // expected
3814        }
3815    }
3816
3817    /**
3818     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3819     */
3820    @TestTargetNew(
3821        level = TestLevel.PARTIAL_COMPLETE,
3822        notes = "",
3823        method = "transferFrom",
3824        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3825    )
3826    public void test_transferFromLReadableByteChannelJJ_SourceClosed()
3827            throws Exception {
3828        readByteChannel = DatagramChannel.open();
3829        readByteChannel.close();
3830
3831        try {
3832            readOnlyFileChannel.transferFrom(readByteChannel, 0, 10);
3833            fail("should throw ClosedChannelException.");
3834        } catch (ClosedChannelException e) {
3835            // expected
3836        }
3837
3838        try {
3839            writeOnlyFileChannel.transferFrom(readByteChannel, 0, 10);
3840            fail("should throw ClosedChannelException.");
3841        } catch (ClosedChannelException e) {
3842            // expected
3843        }
3844
3845        try {
3846            readWriteFileChannel.transferFrom(readByteChannel, 0, 10);
3847            fail("should throw ClosedChannelException.");
3848        } catch (ClosedChannelException e) {
3849            // expected
3850        }
3851
3852        // should throw ClosedChannelException first.
3853        try {
3854            readWriteFileChannel.transferFrom(readByteChannel, 0, -1);
3855            fail("should throw ClosedChannelException.");
3856        } catch (ClosedChannelException e) {
3857            // expected
3858        }
3859    }
3860
3861    /**
3862     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3863     */
3864    @TestTargetNew(
3865        level = TestLevel.PARTIAL_COMPLETE,
3866        notes = "Verifies IllegalArgumentException.",
3867        method = "transferFrom",
3868        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3869    )
3870    public void test_transferFromLReadableByteChannelJJ_IllegalArgument()
3871            throws Exception {
3872        readByteChannel = DatagramChannel.open();
3873        try {
3874            writeOnlyFileChannel.transferFrom(readByteChannel, 10, -1);
3875            fail("should throw IllegalArgumentException.");
3876        } catch (IllegalArgumentException e) {
3877            // expected
3878        }
3879
3880        try {
3881            readWriteFileChannel.transferFrom(readByteChannel, -1, 10);
3882            fail("should throw IllegalArgumentException.");
3883        } catch (IllegalArgumentException e) {
3884            // expected
3885        }
3886    }
3887
3888    /**
3889     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3890     */
3891    @TestTargetNew(
3892        level = TestLevel.PARTIAL_COMPLETE,
3893        notes = "",
3894        method = "transferFrom",
3895        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3896    )
3897    public void test_transferFromLReadableByteChannelJJ_NonWritable()
3898            throws Exception {
3899        readByteChannel = DatagramChannel.open();
3900        try {
3901            readOnlyFileChannel.transferFrom(readByteChannel, 0, 0);
3902            fail("should throw NonWritableChannelException.");
3903        } catch (NonWritableChannelException e) {
3904            // expected
3905        }
3906    }
3907
3908    /**
3909     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3910     */
3911    @TestTargetNew(
3912        level = TestLevel.PARTIAL_COMPLETE,
3913        notes = "",
3914        method = "transferFrom",
3915        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3916    )
3917    public void test_transferFromLReadableByteChannelJJ_SourceNonReadable()
3918            throws Exception {
3919        try {
3920            readWriteFileChannel.transferFrom(writeOnlyFileChannel, 0, 0);
3921            fail("should throw NonReadableChannelException.");
3922        } catch (NonReadableChannelException e) {
3923            // expected
3924        }
3925
3926        // not throws NonReadableChannelException first if position beyond file
3927        // size.
3928        readWriteFileChannel.transferFrom(writeOnlyFileChannel, 10, 10);
3929    }
3930
3931    /**
3932     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3933     */
3934    @TestTargetNew(
3935        level = TestLevel.PARTIAL_COMPLETE,
3936        notes = "",
3937        method = "transferFrom",
3938        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3939    )
3940    public void test_transferFromLReadableByteChannelJJ_PositionBeyondSize()
3941            throws Exception {
3942        // init data to file.
3943        writeDataToFile(fileOfReadOnlyFileChannel);
3944        writeDataToFile(fileOfWriteOnlyFileChannel);
3945
3946        final int READONLYFILECHANNELPOSITION = 2;
3947        readOnlyFileChannel.position(READONLYFILECHANNELPOSITION);
3948
3949        final int POSITION = CONTENT_AS_BYTES_LENGTH * 2;
3950        final int LENGTH = 5;
3951        long result = writeOnlyFileChannel.transferFrom(readOnlyFileChannel,
3952                POSITION, LENGTH);
3953        assertEquals(0, result);
3954        assertEquals(0, writeOnlyFileChannel.position());
3955        assertEquals(READONLYFILECHANNELPOSITION, readOnlyFileChannel
3956                .position());
3957    }
3958
3959    /**
3960     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
3961     */
3962    @TestTargetNew(
3963        level = TestLevel.PARTIAL_COMPLETE,
3964        notes = "",
3965        method = "transferFrom",
3966        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
3967    )
3968    public void test_transferFromLReadableByteChannelJJ_FileChannel()
3969            throws Exception {
3970        // init data to file.
3971        writeDataToFile(fileOfReadOnlyFileChannel);
3972        writeDataToFile(fileOfWriteOnlyFileChannel);
3973
3974        final int READONLYFILECHANNELPOSITION = 2;
3975        final int WRITEONLYFILECHANNELPOSITION = 4;
3976        readOnlyFileChannel.position(READONLYFILECHANNELPOSITION);
3977        writeOnlyFileChannel.position(WRITEONLYFILECHANNELPOSITION);
3978
3979        final int POSITION = 3;
3980        final int LENGTH = 5;
3981        long result = writeOnlyFileChannel.transferFrom(readOnlyFileChannel,
3982                POSITION, LENGTH);
3983        assertEquals(LENGTH, result);
3984        assertEquals(WRITEONLYFILECHANNELPOSITION, writeOnlyFileChannel
3985                .position());
3986        assertEquals(READONLYFILECHANNELPOSITION + LENGTH, readOnlyFileChannel
3987                .position());
3988        writeOnlyFileChannel.close();
3989
3990        final int EXPECTED_LENGTH = POSITION + LENGTH;
3991        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
3992        byte[] resultContent = new byte[EXPECTED_LENGTH];
3993        fis.read(resultContent);
3994
3995        byte[] expectedContent = new byte[EXPECTED_LENGTH];
3996        System.arraycopy(CONTENT_AS_BYTES, 0, expectedContent, 0, POSITION);
3997        System.arraycopy(CONTENT_AS_BYTES, READONLYFILECHANNELPOSITION,
3998                expectedContent, POSITION, LENGTH);
3999        assertTrue(Arrays.equals(expectedContent, resultContent));
4000    }
4001
4002    /**
4003     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
4004     */
4005    @TestTargetNew(
4006        level = TestLevel.PARTIAL_COMPLETE,
4007        notes = "",
4008        method = "transferFrom",
4009        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
4010    )
4011    public void test_transferFromLReadableByteChannelJJ_DatagramChannel()
4012            throws Exception {
4013        // connects two datagramChannels.
4014        datagramChannelReceiver = DatagramChannel.open();
4015        datagramChannelReceiver.socket().bind(
4016                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4017        datagramChannelSender = DatagramChannel.open();
4018        datagramChannelSender.socket().bind(
4019                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4020        datagramChannelReceiver.socket().setSoTimeout(TIME_OUT);
4021        datagramChannelReceiver.connect(datagramChannelSender.socket()
4022                .getLocalSocketAddress());
4023        datagramChannelSender.socket().setSoTimeout(TIME_OUT);
4024        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
4025        datagramChannelSender.socket().setSoTimeout(TIME_OUT);
4026        // sends data from datagramChannelSender to datagramChannelReceiver.
4027        datagramChannelSender.send(writeBuffer, datagramChannelReceiver
4028                .socket().getLocalSocketAddress());
4029        datagramChannelReceiver.socket().setSoTimeout(TIME_OUT);
4030
4031        // transfers data from datagramChannelReceiver to fileChannel.
4032        long result = writeOnlyFileChannel.transferFrom(
4033                datagramChannelReceiver, 0, CONTENT_AS_BYTES_LENGTH);
4034        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
4035        assertEquals(0, writeOnlyFileChannel.position());
4036        writeOnlyFileChannel.close();
4037
4038        // gets content from file.
4039        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
4040        assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel
4041                .length());
4042        byte[] resultContent = new byte[CONTENT_AS_BYTES_LENGTH];
4043        fis.read(resultContent);
4044
4045        // compares contents.
4046        assertTrue(Arrays.equals(CONTENT_AS_BYTES, resultContent));
4047    }
4048
4049    /**
4050     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
4051     */
4052    @TestTargetNew(
4053        level = TestLevel.PARTIAL_COMPLETE,
4054        notes = "",
4055        method = "transferFrom",
4056        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
4057    )
4058    public void test_transferFromLReadableByteChannelJJ_SocketChannel()
4059            throws Exception {
4060        // connects two socketChannels.
4061        socketChannelReceiver = SocketChannel.open();
4062        serverSocketChannel = ServerSocketChannel.open();
4063        serverSocketChannel.socket().bind(
4064                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4065        socketChannelReceiver.socket().setSoTimeout(TIME_OUT);
4066        socketChannelReceiver.connect(serverSocketChannel.socket()
4067                .getLocalSocketAddress());
4068        serverSocketChannel.socket().setSoTimeout(TIME_OUT);
4069        socketChannelSender = serverSocketChannel.accept();
4070        socketChannelSender.socket().setSoTimeout(TIME_OUT);
4071
4072        // sends data from socketChannelSender to socketChannelReceiver.
4073        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
4074        socketChannelSender.write(writeBuffer);
4075
4076        // transfers data from socketChannelReceiver to fileChannel.
4077        long result = readWriteFileChannel.transferFrom(socketChannelReceiver,
4078                0, CONTENT_AS_BYTES_LENGTH);
4079        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
4080        assertEquals(0, readWriteFileChannel.position());
4081        readWriteFileChannel.close();
4082
4083        // gets content from file.
4084        fis = new FileInputStream(fileOfReadWriteFileChannel);
4085        assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfReadWriteFileChannel
4086                .length());
4087        byte[] resultContent = new byte[CONTENT_AS_BYTES_LENGTH];
4088        fis.read(resultContent);
4089
4090        // compares content.
4091        assertTrue(Arrays.equals(CONTENT_AS_BYTES, resultContent));
4092    }
4093
4094    /**
4095     * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long)
4096     */
4097    @TestTargetNew(
4098        level = TestLevel.PARTIAL_COMPLETE,
4099        notes = "",
4100        method = "transferFrom",
4101        args = {java.nio.channels.ReadableByteChannel.class, long.class, long.class}
4102    )
4103    public void test_transferFromLReadableByteChannelJJ_Pipe() throws Exception {
4104        // inits data in file.
4105        writeDataToFile(fileOfWriteOnlyFileChannel);
4106
4107        // inits pipe.
4108        pipe = Pipe.open();
4109
4110        // writes content to pipe.
4111        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
4112        pipe.sink().write(writeBuffer);
4113
4114        // transfers data from pipe to fileChannel.
4115        final int OFFSET = 2;
4116        final int LENGTH = 4;
4117        long result = writeOnlyFileChannel.transferFrom(pipe.source(), OFFSET,
4118                LENGTH);
4119        assertEquals(LENGTH, result);
4120        writeOnlyFileChannel.close();
4121
4122        // gets content from file.
4123        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
4124        byte[] resultBytes = new byte[OFFSET + LENGTH];
4125        fis.read(resultBytes);
4126
4127        // compares content.
4128        byte[] expectedBytes = new byte[OFFSET + LENGTH];
4129        System.arraycopy(CONTENT_AS_BYTES, 0, expectedBytes, 0, OFFSET);
4130        System.arraycopy(CONTENT_AS_BYTES, 0, expectedBytes, OFFSET, LENGTH);
4131
4132        assertTrue(Arrays.equals(expectedBytes, resultBytes));
4133    }
4134
4135    /**
4136     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4137     */
4138    @TestTargetNew(
4139        level = TestLevel.PARTIAL_COMPLETE,
4140        notes = "",
4141        method = "transferTo",
4142        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4143    )
4144    public void test_transferToJJLWritableByteChannel_Null() throws Exception {
4145        writableByteChannel = null;
4146        try {
4147            readOnlyFileChannel.transferTo(0, 10, writableByteChannel);
4148            fail("should throw NullPointerException.");
4149        } catch (NullPointerException e) {
4150            // expected
4151        }
4152
4153        try {
4154            writeOnlyFileChannel.transferTo(0, 10, writableByteChannel);
4155            fail("should throw NullPointerException.");
4156        } catch (NullPointerException e) {
4157            // expected
4158        }
4159
4160        try {
4161            readWriteFileChannel.transferTo(0, 10, writableByteChannel);
4162            fail("should throw NullPointerException.");
4163        } catch (NullPointerException e) {
4164            // expected
4165        }
4166
4167        // should throw NullPointerException first.
4168        readOnlyFileChannel.close();
4169        try {
4170            writeOnlyFileChannel.transferTo(-1, 0, writableByteChannel);
4171            fail("should throw NullPointerException.");
4172        } catch (NullPointerException e) {
4173            // expected
4174        }
4175    }
4176
4177    /**
4178     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4179     */
4180    @TestTargetNew(
4181        level = TestLevel.PARTIAL_COMPLETE,
4182        notes = "",
4183        method = "transferTo",
4184        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4185    )
4186    public void test_transferToJJLWritableByteChannel_Closed() throws Exception {
4187        writableByteChannel = DatagramChannel.open();
4188        readOnlyFileChannel.close();
4189        try {
4190            readOnlyFileChannel.transferTo(0, 10, writableByteChannel);
4191            fail("should throw ClosedChannelException.");
4192        } catch (ClosedChannelException e) {
4193            // expected
4194        }
4195
4196        writeOnlyFileChannel.close();
4197        try {
4198            writeOnlyFileChannel.transferTo(0, 10, writableByteChannel);
4199            fail("should throw ClosedChannelException.");
4200        } catch (ClosedChannelException e) {
4201            // expected
4202        }
4203
4204        readWriteFileChannel.close();
4205        try {
4206            readWriteFileChannel.transferTo(0, 10, writableByteChannel);
4207            fail("should throw ClosedChannelException.");
4208        } catch (ClosedChannelException e) {
4209            // expected
4210        }
4211
4212        // should throw ClosedChannelException first.
4213        try {
4214            readWriteFileChannel.transferTo(0, -1, writableByteChannel);
4215            fail("should throw ClosedChannelException.");
4216        } catch (ClosedChannelException e) {
4217            // expected
4218        }
4219    }
4220
4221    /**
4222     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4223     */
4224    @TestTargetNew(
4225        level = TestLevel.PARTIAL_COMPLETE,
4226        notes = "",
4227        method = "transferTo",
4228        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4229    )
4230    public void test_transferToJJLWritableByteChannel_SourceClosed()
4231            throws Exception {
4232        writableByteChannel = DatagramChannel.open();
4233        writableByteChannel.close();
4234
4235        try {
4236            readOnlyFileChannel.transferTo(0, 10, writableByteChannel);
4237            fail("should throw ClosedChannelException.");
4238        } catch (ClosedChannelException e) {
4239            // expected
4240        }
4241
4242        try {
4243            writeOnlyFileChannel.transferTo(0, 10, writableByteChannel);
4244            fail("should throw ClosedChannelException.");
4245        } catch (ClosedChannelException e) {
4246            // expected
4247        }
4248
4249        try {
4250            readWriteFileChannel.transferTo(0, 10, writableByteChannel);
4251            fail("should throw ClosedChannelException.");
4252        } catch (ClosedChannelException e) {
4253            // expected
4254        }
4255
4256        // should throw ClosedChannelException first.
4257        try {
4258            readWriteFileChannel.transferTo(0, -1, writableByteChannel);
4259            fail("should throw ClosedChannelException.");
4260        } catch (ClosedChannelException e) {
4261            // expected
4262        }
4263    }
4264
4265    /**
4266     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4267     */
4268    @TestTargetNew(
4269        level = TestLevel.PARTIAL_COMPLETE,
4270        notes = "",
4271        method = "transferTo",
4272        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4273    )
4274    public void test_transferToJJLWritableByteChannel_IllegalArgument()
4275            throws Exception {
4276        writableByteChannel = DatagramChannel.open();
4277        try {
4278            readOnlyFileChannel.transferTo(10, -1, writableByteChannel);
4279            fail("should throw IllegalArgumentException.");
4280        } catch (IllegalArgumentException e) {
4281            // expected
4282        }
4283
4284        try {
4285            readWriteFileChannel.transferTo(-1, 10, writableByteChannel);
4286            fail("should throw IllegalArgumentException.");
4287        } catch (IllegalArgumentException e) {
4288            // expected
4289        }
4290    }
4291
4292    /**
4293     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4294     */
4295    @TestTargetNew(
4296        level = TestLevel.PARTIAL_COMPLETE,
4297        notes = "",
4298        method = "transferTo",
4299        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4300    )
4301    public void test_transferToJJLWritableByteChannel_NonReadable()
4302            throws Exception {
4303        writableByteChannel = DatagramChannel.open();
4304        try {
4305            writeOnlyFileChannel.transferTo(-1, 10, writableByteChannel);
4306            fail("should throw NonReadableChannelException.");
4307        } catch (NonReadableChannelException e) {
4308            // expected
4309        }
4310    }
4311
4312    /**
4313     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4314     */
4315    @TestTargetNew(
4316        level = TestLevel.PARTIAL_COMPLETE,
4317        notes = "",
4318        method = "transferTo",
4319        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4320    )
4321    public void test_transferToJJLWritableByteChannel_TargetNonWritable()
4322            throws Exception {
4323        try {
4324            readWriteFileChannel.transferTo(0, 0, readOnlyFileChannel);
4325            fail("should throw NonWritableChannelException.");
4326        } catch (NonWritableChannelException e) {
4327            // expected
4328        }
4329
4330        // first throws NonWritableChannelException even position out of file
4331        // size.
4332        try {
4333            readWriteFileChannel.transferTo(10, 10, readOnlyFileChannel);
4334            fail("should throw NonWritableChannelException.");
4335        } catch (NonWritableChannelException e) {
4336            // expected
4337        }
4338
4339        // regression test for Harmony-941
4340        // first throws NonWritableChannelException even arguments are illegal.
4341        try {
4342            readWriteFileChannel.transferTo(-1, 10, readOnlyFileChannel);
4343            fail("should throw NonWritableChannelException.");
4344        } catch (NonWritableChannelException e) {
4345            // expected
4346        }
4347
4348        try {
4349            readWriteFileChannel.transferTo(0, -1, readOnlyFileChannel);
4350            fail("should throw NonWritableChannelException.");
4351        } catch (NonWritableChannelException e) {
4352            // expected
4353        }
4354    }
4355
4356    /**
4357     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4358     */
4359    @TestTargetNew(
4360        level = TestLevel.PARTIAL_COMPLETE,
4361        notes = "",
4362        method = "transferTo",
4363        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4364    )
4365    public void test_transferToJJLWritableByteChannel_PositionBeyondSize()
4366            throws Exception {
4367        // init data to file.
4368        writeDataToFile(fileOfReadOnlyFileChannel);
4369        writeDataToFile(fileOfWriteOnlyFileChannel);
4370
4371        final int WRITEONLYFILECHANNELPOSITION = 2;
4372        writeOnlyFileChannel.position(WRITEONLYFILECHANNELPOSITION);
4373
4374        final int POSITION = CONTENT_AS_BYTES_LENGTH * 2;
4375        final int LENGTH = 5;
4376        long result = readOnlyFileChannel.transferTo(POSITION, LENGTH,
4377                writeOnlyFileChannel);
4378        assertEquals(0, result);
4379        assertEquals(0, readOnlyFileChannel.position());
4380        assertEquals(WRITEONLYFILECHANNELPOSITION, writeOnlyFileChannel
4381                .position());
4382    }
4383
4384    /**
4385     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4386     */
4387    @TestTargetNew(
4388        level = TestLevel.PARTIAL_COMPLETE,
4389        notes = "",
4390        method = "transferTo",
4391        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4392    )
4393    public void test_transferToJJLWritableByteChannel_FileChannel()
4394            throws Exception {
4395        // init data to file.
4396        writeDataToFile(fileOfReadOnlyFileChannel);
4397        writeDataToFile(fileOfWriteOnlyFileChannel);
4398
4399        final int READONLYFILECHANNELPOSITION = 2;
4400        final int WRITEONLYFILECHANNELPOSITION = 4;
4401        readOnlyFileChannel.position(READONLYFILECHANNELPOSITION);
4402        writeOnlyFileChannel.position(WRITEONLYFILECHANNELPOSITION);
4403
4404        final int POSITION = 3;
4405        final int LENGTH = 5;
4406        long result = readOnlyFileChannel.transferTo(POSITION, LENGTH,
4407                writeOnlyFileChannel);
4408        assertEquals(LENGTH, result);
4409        assertEquals(READONLYFILECHANNELPOSITION, readOnlyFileChannel
4410                .position());
4411        assertEquals(WRITEONLYFILECHANNELPOSITION + LENGTH,
4412                writeOnlyFileChannel.position());
4413        writeOnlyFileChannel.close();
4414
4415        final int EXPECTED_LENGTH = WRITEONLYFILECHANNELPOSITION + LENGTH;
4416        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
4417        byte[] resultContent = new byte[EXPECTED_LENGTH];
4418        fis.read(resultContent);
4419
4420        byte[] expectedContent = new byte[EXPECTED_LENGTH];
4421        System.arraycopy(CONTENT_AS_BYTES, 0, expectedContent, 0,
4422                WRITEONLYFILECHANNELPOSITION);
4423        System.arraycopy(CONTENT_AS_BYTES, POSITION, expectedContent,
4424                WRITEONLYFILECHANNELPOSITION, LENGTH);
4425        assertTrue(Arrays.equals(expectedContent, resultContent));
4426    }
4427
4428    /**
4429     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4430     */
4431    @TestTargetNew(
4432        level = TestLevel.PARTIAL_COMPLETE,
4433        notes = "",
4434        method = "transferTo",
4435        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4436    )
4437    public void test_transferToJJLWritableByteChannel_SocketChannel()
4438            throws Exception {
4439        // inits data into file.
4440        writeDataToFile(fileOfReadOnlyFileChannel);
4441
4442        // connects two socketChannels.
4443        socketChannelReceiver = SocketChannel.open();
4444        socketChannelReceiver.socket().bind(
4445                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4446        serverSocketChannel = ServerSocketChannel.open();
4447        serverSocketChannel.socket().bind(
4448                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4449        socketChannelReceiver.socket().setSoTimeout(TIME_OUT);
4450        socketChannelReceiver.connect(serverSocketChannel.socket()
4451                .getLocalSocketAddress());
4452        serverSocketChannel.socket().setSoTimeout(TIME_OUT);
4453        socketChannelSender = serverSocketChannel.accept();
4454        socketChannelSender.socket().setSoTimeout(TIME_OUT);
4455
4456        // position here should have no effect on transferTo since it uses
4457        // offset from file_begin
4458        final int POSITION = 10;
4459        readOnlyFileChannel.position(POSITION);
4460
4461        // transfers data from file to socketChannelSender.
4462        final int OFFSET = 2;
4463        long result = readOnlyFileChannel.transferTo(OFFSET,
4464                CONTENT_AS_BYTES_LENGTH * 2, socketChannelSender);
4465        final int LENGTH = CONTENT_AS_BYTES_LENGTH - OFFSET;
4466        assertEquals(LENGTH, result);
4467        assertEquals(POSITION, readOnlyFileChannel.position());
4468        readOnlyFileChannel.close();
4469        socketChannelSender.close();
4470
4471        // gets contents from socketChannelReceiver.
4472        ByteBuffer readBuffer = ByteBuffer.allocate(LENGTH + 1);
4473        int totalRead = 0;
4474        int countRead = 0;
4475        long beginTime = System.currentTimeMillis();
4476        while ((countRead = socketChannelReceiver.read(readBuffer)) != -1) {
4477            totalRead += countRead;
4478            // TIMEOUT
4479            if (System.currentTimeMillis() - beginTime > TIME_OUT) {
4480                break;
4481            }
4482        }
4483        assertEquals(LENGTH, totalRead);
4484
4485        // compares contents.
4486        readBuffer.flip();
4487        for (int i = OFFSET; i < CONTENT_AS_BYTES_LENGTH; i++) {
4488            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
4489        }
4490    }
4491
4492    /**
4493     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4494     */
4495    @TestTargetNew(
4496        level = TestLevel.PARTIAL_COMPLETE,
4497        notes = "",
4498        method = "transferTo",
4499        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4500    )
4501    public void test_transferToJJLWritableByteChannel_DatagramChannel()
4502            throws Exception {
4503        // inits data to file.
4504        writeDataToFile(fileOfReadOnlyFileChannel);
4505
4506        // connects two datagramChannel
4507        datagramChannelReceiver = DatagramChannel.open();
4508        datagramChannelReceiver.socket().bind(
4509                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4510        datagramChannelSender = DatagramChannel.open();
4511        datagramChannelSender.socket().bind(
4512                new InetSocketAddress(InetAddress.getLocalHost(), 0));
4513        datagramChannelSender.socket().setSoTimeout(TIME_OUT);
4514        datagramChannelSender.connect(datagramChannelReceiver.socket()
4515                .getLocalSocketAddress());
4516        datagramChannelReceiver.socket().setSoTimeout(TIME_OUT);
4517        datagramChannelReceiver.connect(datagramChannelSender.socket()
4518                .getLocalSocketAddress());
4519
4520        // transfers data from fileChannel to datagramChannelSender
4521        long result = readOnlyFileChannel.transferTo(0,
4522                CONTENT_AS_BYTES_LENGTH, datagramChannelSender);
4523        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
4524        assertEquals(0, readOnlyFileChannel.position());
4525        readOnlyFileChannel.close();
4526        datagramChannelSender.close();
4527
4528        // gets contents from datagramChannelReceiver
4529        ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_AS_BYTES_LENGTH);
4530        long beginTime = System.currentTimeMillis();
4531        int totalRead = 0;
4532        while (totalRead < CONTENT_AS_BYTES_LENGTH) {
4533            totalRead += datagramChannelReceiver.read(readBuffer);
4534            if (System.currentTimeMillis() - beginTime > TIME_OUT) {
4535                break;
4536            }
4537        }
4538        assertEquals(CONTENT_AS_BYTES_LENGTH, totalRead);
4539
4540        // compares contents.
4541        readBuffer.flip();
4542        for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
4543            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
4544        }
4545    }
4546
4547    /**
4548     * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel)
4549     */
4550    @TestTargetNew(
4551        level = TestLevel.PARTIAL_COMPLETE,
4552        notes = "",
4553        method = "transferTo",
4554        args = {long.class, long.class, java.nio.channels.WritableByteChannel.class}
4555    )
4556    public void test_transferToJJLWritableByteChannel_Pipe() throws Exception {
4557        // inits data in file.
4558        writeDataToFile(fileOfReadOnlyFileChannel);
4559
4560        // inits pipe.
4561        pipe = Pipe.open();
4562
4563        // transfers data from fileChannel to pipe.
4564        final int OFFSET = 2;
4565        final int LENGTH = 4;
4566        long result = readOnlyFileChannel.transferTo(OFFSET, LENGTH, pipe
4567                .sink());
4568        assertEquals(LENGTH, result);
4569        assertEquals(0, readOnlyFileChannel.position());
4570        readOnlyFileChannel.close();
4571
4572        // gets content from pipe.
4573        ByteBuffer readBuffer = ByteBuffer.allocate(LENGTH);
4574        result = pipe.source().read(readBuffer);
4575        assertEquals(LENGTH, result);
4576
4577        // compares content.
4578        readBuffer.flip();
4579        for (int i = OFFSET; i < OFFSET + LENGTH; i++) {
4580            assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
4581        }
4582    }
4583
4584    /**
4585     * Regression test for Harmony-3324
4586     * Make sure we could delete the file after we called transferTo() method.
4587     */
4588    public void test_transferTo_couldDelete() throws Exception {
4589        // init data in files
4590        writeDataToFile(fileOfReadOnlyFileChannel);
4591        writeDataToFile(fileOfWriteOnlyFileChannel);
4592
4593        // call transferTo() method
4594        readOnlyFileChannel.transferTo(0 , 2, writeOnlyFileChannel);
4595
4596        // delete both files
4597        readOnlyFileChannel.close();
4598        writeOnlyFileChannel.close();
4599        boolean rDel = fileOfReadOnlyFileChannel.delete();
4600        boolean wDel = fileOfWriteOnlyFileChannel.delete();
4601
4602        // make sure both files were deleted
4603        assertTrue("File " + readOnlyFileChannel + " exists", rDel);
4604        assertTrue("File " + writeOnlyFileChannel + " exists", wDel);
4605    }
4606
4607    /**
4608     * Regression test for Harmony-3324
4609     * Make sure we could delete the file after we called transferFrom() method.
4610     */
4611    public void test_transferFrom_couldDelete() throws Exception {
4612        // init data in files
4613        writeDataToFile(fileOfReadOnlyFileChannel);
4614        writeDataToFile(fileOfWriteOnlyFileChannel);
4615
4616        // call transferTo() method
4617        writeOnlyFileChannel.transferFrom(readOnlyFileChannel, 0 , 2);
4618
4619        // delete both files
4620        readOnlyFileChannel.close();
4621        writeOnlyFileChannel.close();
4622        boolean rDel = fileOfReadOnlyFileChannel.delete();
4623        boolean wDel = fileOfWriteOnlyFileChannel.delete();
4624
4625        // make sure both files were deleted
4626        assertTrue("File " + readOnlyFileChannel + " exists", rDel);
4627        assertTrue("File " + writeOnlyFileChannel + " exists", wDel);
4628    }
4629
4630    private class MockFileChannel extends FileChannel {
4631
4632        private boolean isLockCalled = false;
4633
4634        private boolean isTryLockCalled = false;
4635
4636        private boolean isReadCalled = false;
4637
4638        private boolean isWriteCalled = false;
4639
4640        public void force(boolean arg0) throws IOException {
4641            // do nothing
4642        }
4643
4644        public FileLock lock(long position, long size, boolean shared)
4645                throws IOException {
4646            // verify that calling lock() leads to the method
4647            // lock(0, Long.MAX_VALUE, false).
4648            if (0 == position && Long.MAX_VALUE == size && false == shared) {
4649                isLockCalled = true;
4650            }
4651            return null;
4652        }
4653
4654        public MappedByteBuffer map(MapMode arg0, long arg1, long arg2)
4655                throws IOException {
4656            return null;
4657        }
4658
4659        public long position() throws IOException {
4660            return 0;
4661        }
4662
4663        public FileChannel position(long arg0) throws IOException {
4664            return null;
4665        }
4666
4667        public int read(ByteBuffer arg0) throws IOException {
4668            return 0;
4669        }
4670
4671        public int read(ByteBuffer arg0, long arg1) throws IOException {
4672            return 0;
4673        }
4674
4675        public long read(ByteBuffer[] srcs, int offset, int length)
4676                throws IOException {
4677            // verify that calling read(ByteBuffer[] srcs) leads to the method
4678            // read(srcs, 0, srcs.length)
4679            if (0 == offset && length == srcs.length) {
4680                isReadCalled = true;
4681            }
4682            return 0;
4683        }
4684
4685        public long size() throws IOException {
4686            return 0;
4687        }
4688
4689        public long transferFrom(ReadableByteChannel arg0, long arg1, long arg2)
4690                throws IOException {
4691            return 0;
4692        }
4693
4694        public long transferTo(long arg0, long arg1, WritableByteChannel arg2)
4695                throws IOException {
4696            return 0;
4697        }
4698
4699        public FileChannel truncate(long arg0) throws IOException {
4700            return null;
4701        }
4702
4703        public FileLock tryLock(long position, long size, boolean shared)
4704                throws IOException {
4705            // verify that calling tryLock() leads to the method
4706            // tryLock(0, Long.MAX_VALUE, false).
4707            if (0 == position && Long.MAX_VALUE == size && false == shared) {
4708                isTryLockCalled = true;
4709            }
4710            return null;
4711        }
4712
4713        public int write(ByteBuffer arg0) throws IOException {
4714            return 0;
4715        }
4716
4717        public int write(ByteBuffer arg0, long arg1) throws IOException {
4718            return 0;
4719        }
4720
4721        public long write(ByteBuffer[] srcs, int offset, int length)
4722                throws IOException {
4723            // verify that calling write(ByteBuffer[] srcs) leads to the method
4724            // write(srcs, 0, srcs.length)
4725            if(0 == offset && length == srcs.length){
4726                isWriteCalled = true;
4727            }
4728            return 0;
4729        }
4730
4731        protected void implCloseChannel() throws IOException {
4732
4733        }
4734    }
4735}
4736