forked from awslabs/aws-java-nio-spi-for-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesNewByteChannelTest.java
76 lines (59 loc) · 2.42 KB
/
FilesNewByteChannelTest.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
73
74
75
76
/*
* 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.file.Files;
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("Files$newByteChannel* should read and write on S3")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class FilesNewByteChannelTest {
String bucketName;
@BeforeEach
public void createBucket() {
bucketName = "byte-channel-bucket" + System.currentTimeMillis();
Containers.createBucket(bucketName);
}
@Test
@DisplayName("newByteChannel with CREATE and WRITE is supported")
public void newByteChannel_CREATE_WRITE() throws IOException {
var path = Paths.get(URI.create(localStackConnectionEndpoint() + "/" + bucketName + "/bc-create-write-test.txt"));
String text = "we test Files#newByteChannel";
try (var channel = Files.newByteChannel(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
channel.write(ByteBuffer.wrap(text.getBytes()));
}
assertThat(path).hasContent(text);
}
@Test
@DisplayName("newByteChannel with READ and WRITE is supported")
public void newByteChannel_READ_WRITE() throws IOException {
var path = putObject(bucketName, "bc-read-write-test.txt", "xyz");
String text = "abcdefhij";
try (var channel = Files.newByteChannel(path, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
// write
channel.position(3);
channel.write(ByteBuffer.wrap("def".getBytes()));
channel.position(0);
channel.write(ByteBuffer.wrap("abc".getBytes()));
channel.position(6);
channel.write(ByteBuffer.wrap("hij".getBytes()));
// read
var dst = ByteBuffer.allocate(text.getBytes().length);
channel.position(0);
channel.read(dst);
// verify
assertThat(dst.array()).isEqualTo(text.getBytes());
}
assertThat(path).hasContent(text);
}
}