google / google/jimfs

Use mocks to provide MappedByteBuffers.

Open
#54 2 comments 0 reactions 0 assignees View on GitHub
P4 type=enhancement
Dominant language
Java
Stars
2.6k
Forks
296
Avg merge
9m
Merged PRs (30d)
7

Description

When called, `FileChannel.map` throws an `UnsupportedOperationException`, but the [Javadoc doesn't mention](http://docs.oracle.com/javase/8/docs/api/java/nio/channels/FileChannel.html#map-java.nio.channels.FileChannel.MapMode-long-long-) any such Exception:

Example code to reproduce the error:

FileSystem fs = Jimfs.newFileSystem();
Path file = fs.getPath("MyFile.data");
FileChannel channel = FileChannel.open(file, READ, WRITE, CREATE, DSYNC);
MappedByteBuffer buffer = channel.map(READ_WRITE, 0L, 1024L);

Okay, I read the code of the method, which is relatively simple:

@Override
public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException {
// would like this to pretend to work, but can't create an implementation of MappedByteBuffer
// well, a direct buffer could be cast to MappedByteBuffer, but it couldn't work in general
throw new UnsupportedOperationException();
}

However, I have to say that I successfully mocked `MappedByteBuffer` using Mockito (latest version). Wouldn't it be possible to create such a mock (using either Mockito or your own bytecode manipulator) so that we can properly test our usage of FileChannel.map?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.