|
14 | 14 | import org.junit.jupiter.params.provider.MethodSource;
|
15 | 15 |
|
16 | 16 | import java.io.IOException;
|
| 17 | +import java.nio.ByteBuffer; |
17 | 18 | import java.nio.file.FileAlreadyExistsException;
|
18 | 19 | import java.nio.file.Files;
|
19 | 20 | import java.nio.file.NoSuchFileException;
|
|
25 | 26 |
|
26 | 27 | import static java.nio.file.StandardOpenOption.CREATE;
|
27 | 28 | import static java.nio.file.StandardOpenOption.CREATE_NEW;
|
| 29 | +import static java.nio.file.StandardOpenOption.READ; |
| 30 | +import static java.nio.file.StandardOpenOption.WRITE; |
28 | 31 | import static java.util.Collections.emptySet;
|
29 | 32 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 33 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
@@ -66,6 +69,31 @@ void whenFileDoesNotExistsAndNoCreateNewShouldThrowNoSuchFileException() throws
|
66 | 69 | .isInstanceOf(NoSuchFileException.class);
|
67 | 70 | }
|
68 | 71 |
|
| 72 | + @Test |
| 73 | + @DisplayName("S3WritableByteChannel is a SeekableByteChannel") |
| 74 | + void shouldBeSeekable() throws InterruptedException, TimeoutException, IOException { |
| 75 | + S3FileSystemProvider provider = mock(); |
| 76 | + when(provider.exists(any(S3AsyncClient.class), any())).thenReturn(true); |
| 77 | + |
| 78 | + S3FileSystem fs = mock(); |
| 79 | + when(fs.provider()).thenReturn(provider); |
| 80 | + |
| 81 | + var file = S3Path.getPath(fs, "somefile"); |
| 82 | + var channel = new S3WritableByteChannel(file, mock(), mock(), Set.of(READ, WRITE)); |
| 83 | + assertThat(channel.size()).isZero(); |
| 84 | + assertThat(channel.position()).isZero(); |
| 85 | + channel.write(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4 })); |
| 86 | + assertThat(channel.position()).isEqualTo(4); |
| 87 | + assertThat(channel.position(2)).isSameAs(channel); |
| 88 | + channel.write(ByteBuffer.wrap(new byte[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); |
| 89 | + assertThat(channel.size()).isEqualTo(12); |
| 90 | + assertThat(channel.position(3)).isSameAs(channel); |
| 91 | + ByteBuffer buffer = ByteBuffer.allocate(6); |
| 92 | + channel.read(buffer); |
| 93 | + assertThat(buffer.array()).contains(4, 5, 6, 7, 8, 9); |
| 94 | + assertThatThrownBy(() -> channel.truncate(6)).isInstanceOf(UnsupportedOperationException.class); |
| 95 | + } |
| 96 | + |
69 | 97 | @ParameterizedTest(name = "can be instantiated when file exists ({0}) and open options are {1}")
|
70 | 98 | @MethodSource("acceptedFileExistsAndOpenOptions")
|
71 | 99 | @DisplayName("S3WritableByteChannel")
|
|
0 commit comments