8
8
import com .google .gcloud .storage .BucketInfo ;
9
9
import com .google .gcloud .storage .Storage ;
10
10
import com .google .gcloud .storage .StorageOptions ;
11
- import com .google .gcloud .storage .contrib .nio .CloudStorageConfiguration ;
12
11
import com .google .gcloud .storage .contrib .nio .CloudStorageFileSystem ;
13
12
import com .google .gcloud .storage .testing .RemoteGcsHelper ;
14
13
24
23
import java .io .IOException ;
25
24
import java .io .OutputStreamWriter ;
26
25
import java .io .PrintWriter ;
26
+ import java .net .URI ;
27
27
import java .nio .ByteBuffer ;
28
28
import java .nio .channels .ReadableByteChannel ;
29
29
import java .nio .channels .SeekableByteChannel ;
30
30
import java .nio .file .Files ;
31
31
import java .nio .file .NoSuchFileException ;
32
32
import java .nio .file .Path ;
33
+ import java .nio .file .Paths ;
33
34
import java .nio .file .StandardOpenOption ;
34
35
import java .util .Arrays ;
35
36
import java .util .List ;
55
56
* to your browsers is your "Service Account JSON Key".
56
57
*/
57
58
@ RunWith (JUnit4 .class )
59
+ @ SuppressWarnings ("resource" )
58
60
public class ITGcsNio {
59
61
60
62
private static final List <String > FILE_CONTENTS =
@@ -63,38 +65,38 @@ public class ITGcsNio {
63
65
"Ils sont doués de raison et de conscience et doivent agir " ,
64
66
"les uns envers les autres dans un esprit de fraternité." );
65
67
66
- private static final Logger log = Logger .getLogger (ITGcsNio .class .getName ());
67
- private static final String BUCKET = RemoteGcsHelper .generateBucketName ();
68
68
private static final String SML_FILE = "tmp-test-small-file.txt" ;
69
69
private static final int SML_SIZE = 100 ;
70
- // it's big, relatively speaking.
71
- private static final String BIG_FILE = "tmp-test-big-file.txt" ;
72
- // arbitrary size that's not too round.
73
- private static final int BIG_SIZE = 2 * 1024 * 1024 - 50 ;
70
+ private static final String BIG_FILE = "tmp-test-big-file.txt" ; // it's big, relatively speaking.
71
+ private static final int BIG_SIZE = 2 * 1024 * 1024 - 50 ; // arbitrary size that's not too round.
74
72
private static final String PREFIX = "tmp-test-file" ;
73
+
74
+ private static final Logger logger = Logger .getLogger (ITGcsNio .class .getName ());
75
+ private static final Random random = new Random ();
76
+
77
+ private static String bucket ;
75
78
private static Storage storage ;
76
79
private static StorageOptions storageOptions ;
77
80
78
- private final Random rnd = new Random ();
79
-
80
81
@ BeforeClass
81
- public static void beforeClass () throws IOException {
82
+ public static void beforeClass () {
83
+ bucket = RemoteGcsHelper .generateBucketName ();
82
84
// loads the credentials from local disk as par README
83
85
RemoteGcsHelper gcsHelper = RemoteGcsHelper .create ();
84
86
storageOptions = gcsHelper .options ();
85
87
storage = storageOptions .service ();
86
88
// create and populate test bucket
87
- storage .create (BucketInfo .of (BUCKET ));
89
+ storage .create (BucketInfo .of (bucket ));
88
90
fillFile (storage , SML_FILE , SML_SIZE );
89
91
fillFile (storage , BIG_FILE , BIG_SIZE );
90
92
}
91
93
92
94
@ AfterClass
93
95
public static void afterClass () throws ExecutionException , InterruptedException {
94
96
if (storage != null
95
- && !RemoteGcsHelper .forceDelete (storage , BUCKET , 5 , TimeUnit .SECONDS )
96
- && log .isLoggable (Level .WARNING )) {
97
- log .log (Level .WARNING , "Deletion of bucket {0} timed out, bucket is not empty" , BUCKET );
97
+ && !RemoteGcsHelper .forceDelete (storage , bucket , 5 , TimeUnit .SECONDS )
98
+ && logger .isLoggable (Level .WARNING )) {
99
+ logger .log (Level .WARNING , "Deletion of bucket {0} timed out, bucket is not empty" , bucket );
98
100
}
99
101
}
100
102
@@ -104,27 +106,33 @@ private static byte[] randomContents(int size) {
104
106
return bytes ;
105
107
}
106
108
107
- private static void fillFile (Storage storage , String fname , int size ) throws IOException {
108
- storage .create (BlobInfo .builder (BUCKET , fname ).build (), randomContents (size ));
109
+ private static void fillFile (Storage storage , String fname , int size ) {
110
+ storage .create (BlobInfo .builder (bucket , fname ).build (), randomContents (size ));
109
111
}
110
112
111
113
@ Test
112
- public void testFileExists () throws IOException {
113
- CloudStorageFileSystem testBucket = getTestBucket ( );
114
+ public void testFileExists () {
115
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
114
116
Path path = testBucket .getPath (SML_FILE );
115
117
assertThat (Files .exists (path )).isTrue ();
116
118
}
117
119
120
+ @ Test
121
+ public void testFileExistsUsingSpi () {
122
+ Path path = Paths .get (URI .create (String .format ("gs://%s/%s" , bucket , SML_FILE )));
123
+ assertThat (Files .exists (path )).isTrue ();
124
+ }
125
+
118
126
@ Test
119
127
public void testFileSize () throws IOException {
120
- CloudStorageFileSystem testBucket = getTestBucket ( );
128
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
121
129
Path path = testBucket .getPath (SML_FILE );
122
130
assertThat (Files .size (path )).isEqualTo (SML_SIZE );
123
131
}
124
132
125
133
@ Test (timeout = 60_000 )
126
134
public void testReadByteChannel () throws IOException {
127
- CloudStorageFileSystem testBucket = getTestBucket ( );
135
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
128
136
Path path = testBucket .getPath (SML_FILE );
129
137
long size = Files .size (path );
130
138
SeekableByteChannel chan = Files .newByteChannel (path , StandardOpenOption .READ );
@@ -150,7 +158,7 @@ public void testReadByteChannel() throws IOException {
150
158
151
159
@ Test
152
160
public void testSeek () throws IOException {
153
- CloudStorageFileSystem testBucket = getTestBucket ( );
161
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
154
162
Path path = testBucket .getPath (BIG_FILE );
155
163
int size = BIG_SIZE ;
156
164
byte [] contents = randomContents (size );
@@ -179,7 +187,7 @@ public void testSeek() throws IOException {
179
187
180
188
@ Test
181
189
public void testCreate () throws IOException {
182
- CloudStorageFileSystem testBucket = getTestBucket ( );
190
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
183
191
Path path = testBucket .getPath (PREFIX + randomSuffix ());
184
192
// file shouldn't exist initially. If it does it's either because it's a leftover
185
193
// from a previous run (so we should delete the file)
@@ -201,7 +209,7 @@ public void testCreate() throws IOException {
201
209
202
210
@ Test
203
211
public void testWrite () throws IOException {
204
- CloudStorageFileSystem testBucket = getTestBucket ( );
212
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
205
213
Path path = testBucket .getPath (PREFIX + randomSuffix ());
206
214
// file shouldn't exist initially. If it does it's either because it's a leftover
207
215
// from a previous run (so we should delete the file)
@@ -233,7 +241,7 @@ public void testWrite() throws IOException {
233
241
234
242
@ Test
235
243
public void testCreateAndWrite () throws IOException {
236
- CloudStorageFileSystem testBucket = getTestBucket ( );
244
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
237
245
Path path = testBucket .getPath (PREFIX + randomSuffix ());
238
246
// file shouldn't exist initially (see above).
239
247
assertThat (Files .exists (path )).isFalse ();
@@ -262,7 +270,7 @@ public void testCreateAndWrite() throws IOException {
262
270
263
271
@ Test
264
272
public void testWriteOnClose () throws Exception {
265
- CloudStorageFileSystem testBucket = getTestBucket ( );
273
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
266
274
Path path = testBucket .getPath (PREFIX + randomSuffix ());
267
275
// file shouldn't exist initially (see above)
268
276
assertThat (Files .exists (path )).isFalse ();
@@ -296,7 +304,7 @@ public void testWriteOnClose() throws Exception {
296
304
297
305
@ Test
298
306
public void testCopy () throws IOException {
299
- CloudStorageFileSystem testBucket = getTestBucket ( );
307
+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
300
308
Path src = testBucket .getPath (SML_FILE );
301
309
Path dst = testBucket .getPath (PREFIX + randomSuffix ());
302
310
// file shouldn't exist initially (see above).
@@ -330,19 +338,6 @@ private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOExcep
330
338
}
331
339
332
340
private String randomSuffix () {
333
- return "-" + rnd .nextInt (99999 );
334
- }
335
-
336
- private CloudStorageFileSystem getTestBucket () throws IOException {
337
- // in typical usage we use the single-argument version of forBucket
338
- // and rely on the user being logged into their project with the
339
- // gcloud tool, and then everything authenticates automagically
340
- // (or we just use paths that start with "gs://" and rely on NIO's magic).
341
- //
342
- // However for the tests we want to be able to run in automated environments
343
- // where we can set environment variables but not necessarily install gcloud
344
- // or run it. That's why we're setting the credentials programmatically.
345
- return CloudStorageFileSystem .forBucket (
346
- BUCKET , CloudStorageConfiguration .DEFAULT , storageOptions );
341
+ return "-" + random .nextInt (99999 );
347
342
}
348
343
}
0 commit comments