forked from awslabs/aws-java-nio-spi-for-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileChannelOpenTest.java
72 lines (55 loc) · 2.27 KB
/
FileChannelOpenTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.nio.spi.s3;
import static org.assertj.core.api.Assertions.*;
import static software.amazon.nio.spi.s3.Containers.*;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
@DisplayName("FileChannel$open* should read and write on S3")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class FileChannelOpenTest {
String bucketName;
@BeforeEach
public void createBucket() {
bucketName = "file-channel-bucket" + System.currentTimeMillis();
Containers.createBucket(bucketName);
}
@Test
@DisplayName("open with CREATE and WRITE is supported")
public void open_CREATE_WRITE() throws IOException {
var path = Paths.get(URI.create(localStackConnectionEndpoint() + "/" + bucketName + "/fc-create-write-test.txt"));
String text = "we test FileChannel#open with CREATE and WRITE options";
try (var channel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
channel.write(ByteBuffer.wrap(text.getBytes()));
}
assertThat(path).hasContent(text);
}
@Test
@DisplayName("open with READ and WRITE is supported")
public void open_READ_WRITE() throws IOException {
var path = putObject(bucketName, "fc-read-write-test.txt");
String text = "abcdefhij";
try (var channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
// write
channel.write(ByteBuffer.wrap("def".getBytes()), 3);
channel.write(ByteBuffer.wrap("abc".getBytes()), 0);
channel.write(ByteBuffer.wrap("hij".getBytes()), 6);
// read
var dst = ByteBuffer.allocate(text.getBytes().length);
channel.read(dst, 0);
// verify
assertThat(dst.array()).isEqualTo(text.getBytes());
}
assertThat(path).hasContent(text);
}
}