This repository was archived by the owner on Feb 26, 2025. It is now read-only.
forked from martint/s3fs
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathToUriTest.java
More file actions
157 lines (117 loc) · 4.84 KB
/
ToUriTest.java
File metadata and controls
157 lines (117 loc) · 4.84 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.upplication.s3fs.Path;
import static com.upplication.s3fs.AmazonS3Factory.ACCESS_KEY;
import static com.upplication.s3fs.AmazonS3Factory.SECRET_KEY;
import static com.upplication.s3fs.util.S3EndpointConstant.S3_GLOBAL_URI_TEST;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import com.google.common.collect.ImmutableMap;
import com.upplication.s3fs.S3FileSystem;
import com.upplication.s3fs.S3FileSystemProvider;
import com.upplication.s3fs.S3Path;
import com.upplication.s3fs.S3UnitTestBase;
import com.upplication.s3fs.util.S3EndpointConstant;
public class ToUriTest extends S3UnitTestBase {
private S3FileSystemProvider s3fsProvider;
@Before
public void setup() {
s3fsProvider = getS3fsProvider();
s3fsProvider.newFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST, null);
}
@Test
public void toUri() {
Path path = getPath("/bucket/path/to/file");
URI uri = path.toUri();
// the scheme is s3
assertEquals("s3", uri.getScheme());
// could get the correct fileSystem
S3FileSystem fs = s3fsProvider.getFileSystem(uri);
// the host is the endpoint specified in fileSystem
assertEquals(fs.getEndpoint(), uri.getHost());
// bucket name as first path
Path pathActual = fs.provider().getPath(uri);
assertEquals(path, pathActual);
}
@Test
public void toUriSpecialChars() {
Path path = getPath("/bucket/([fol! @#$%der])");
URI uri = path.toUri();
// the scheme is s3
assertEquals("s3", uri.getScheme());
// could get the correct fileSystem
S3FileSystem fs = s3fsProvider.getFileSystem(uri);
// the host is the endpoint specified in fileSystem
assertEquals(fs.getEndpoint(), uri.getHost());
// bucket name as first path
Path pathActual = fs.provider().getPath(uri);
assertEquals(path, pathActual);
}
@Test
public void toUriWithEndSlash() {
S3Path s3Path = getPath("/bucket/folder/");
assertEquals(S3_GLOBAL_URI_TEST + "bucket/folder/", s3Path.toUri().toString());
}
@Test
public void toUriWithSpaces() {
S3Path s3Path = getPath("/bucket/with spaces");
assertEquals(S3_GLOBAL_URI_TEST.resolve("bucket/with%20spaces") , s3Path.toUri());
}
@Test
public void toUriWithNotEndSlash() {
S3Path s3Path = getPath("/bucket/file");
assertEquals(S3_GLOBAL_URI_TEST + "bucket/file", s3Path.toUri().toString());
}
@Test
public void toUriRelative() {
S3FileSystem fileSystem = s3fsProvider.getFileSystem(S3_GLOBAL_URI_TEST);
S3Path path = new S3Path(fileSystem, "bla");
assertEquals(URI.create("bla"), path.toUri());
}
@Test
public void toUriRelativeWithSpaces() {
S3FileSystem fileSystem = s3fsProvider.getFileSystem(S3_GLOBAL_URI_TEST);
S3Path path = new S3Path(fileSystem, "with space");
assertEquals(URI.create("with%20space"), path.toUri());
}
@Test
public void toUriBucketWithoutEndSlash() {
S3Path s3Path = getPath("/bucket");
assertEquals(S3_GLOBAL_URI_TEST.resolve("/bucket/"), s3Path.toUri());
}
@Test
public void toUriWithCredentials() {
Map<String, String> envs = ImmutableMap.<String, String>builder().put(ACCESS_KEY, "access").put(SECRET_KEY, "secret").build();
FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI_TEST, envs);
Path path = fileSystem.getPath("/bla/file");
assertEquals(URI.create("s3://access@s3.test.amazonaws.com/bla/file"), path.toUri());
}
@Test
public void toUriWithCredentialBySystemProperty() {
System.setProperty(ACCESS_KEY, "accessKeywii");
System.setProperty(SECRET_KEY, "secretKey");
FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI_TEST, null);
Path path = fileSystem.getPath("/bla/file");
assertEquals(URI.create("s3://accessKeywii@s3.test.amazonaws.com/bla/file"), path.toUri());
System.clearProperty(ACCESS_KEY);
System.clearProperty(SECRET_KEY);
}
@Test
public void toUriWithEndpoint() throws IOException {
try (FileSystem fs = s3fsProvider.newFileSystem(URI.create("s3://endpoint/"), null)) {
Path path = fs.getPath("/bucket/path/to/file");
URI uri = path.toUri();
// the scheme is s3
assertEquals("s3", uri.getScheme());
assertEquals("endpoint", uri.getHost());
assertEquals("/bucket/path/to/file", uri.getPath());
}
}
private S3Path getPath(String path) {
return s3fsProvider.getFileSystem(S3_GLOBAL_URI_TEST).getPath(path);
}
}