### Question:
GlusterFileChannel extends FileChannel { long readHelper(ByteBuffer[] byteBuffers, int offset, int length) throws IOException { long totalRead = 0L; boolean endOfStream = false; for (int i = offset; i < length + offset && !endOfStream; i++) { byte[] bytes = byteBuffers[i].array(); int remaining; while ((remaining = byteBuffers[i].remaining()) > 0) { long read = GLFS.glfs_read(fileptr, bytes, remaining, 0); if (read < 0) { throw new IOException(UtilJNI.strerror()); } totalRead += read; byteBuffers[i].position((int) read); if (0 == read) { endOfStream = true; break; } } } if (endOfStream && totalRead == 0) { return -1; } return totalRead; }  @Override int read(ByteBuffer byteBuffer); @Override long read(ByteBuffer[] byteBuffers, int offset, int length); @Override int write(ByteBuffer byteBuffer); @Override long write(ByteBuffer[] byteBuffers, int offset, int length); @Override long position(); @Override FileChannel position(long offset); @Override long size(); @Override FileChannel truncate(long l); @Override void force(boolean b); @Override long transferTo(long l, long l2, WritableByteChannel writableByteChannel); @Override long transferFrom(ReadableByteChannel readableByteChannel, long l, long l2); @Override int read(ByteBuffer byteBuffer, long position); @Override int write(ByteBuffer byteBuffer, long position); @Override MappedByteBuffer map(MapMode mapMode, long l, long l2); @Override FileLock lock(long l, long l2, boolean b); @Override FileLock tryLock(long l, long l2, boolean b); static final Map<StandardOpenOption, Integer> optionMap; static final Map<PosixFilePermission, Integer> perms; }### Answer:
@Test public void testReadHelper() throws IOException { long fileptr = 1234L; channel.setFileptr(fileptr); ByteBuffer[] buffers = new ByteBuffer[2]; buffers[0] = mockBuffer; buffers[1] = mockBuffer; int offset = 0; int length = 2; byte[] bytes = new byte[]{'h', 'e', 'l', 'l', 'o'}; doReturn(bytes).when(mockBuffer).array(); when(mockBuffer.remaining()).thenReturn(5, 0, 5, 0); mockStatic(GLFS.class); when(GLFS.glfs_read(fileptr, bytes, 5, 0)).thenReturn(5L); doReturn(mockBuffer).when(mockBuffer).position(5); channel.readHelper(buffers, offset, length); verify(mockBuffer, times(2)).position(5); verifyStatic(times(2)); GLFS.glfs_read(fileptr, bytes, 5, 0); verify(mockBuffer, times(4)).remaining(); verify(mockBuffer, times(2)).array(); }
@Test(expected = IOException.class) public void testReadHelper_whenReadFails() throws IOException { long fileptr = 1234L; channel.setFileptr(fileptr); ByteBuffer[] buffers = new ByteBuffer[2]; buffers[0] = mockBuffer; buffers[1] = mockBuffer; int offset = 0; int length = 2; byte[] bytes = new byte[]{'h', 'e', 'l', 'l', 'o'}; doReturn(bytes).when(mockBuffer).array(); when(mockBuffer.remaining()).thenReturn(5, 0, 5, 0); mockStatic(GLFS.class); when(GLFS.glfs_read(fileptr, bytes, 5, 0)).thenReturn(-1L); channel.readHelper(buffers, offset, length); }