88import com .google .gcloud .storage .BucketInfo ;
99import com .google .gcloud .storage .Storage ;
1010import com .google .gcloud .storage .StorageOptions ;
11- import com .google .gcloud .storage .contrib .nio .CloudStorageConfiguration ;
1211import com .google .gcloud .storage .contrib .nio .CloudStorageFileSystem ;
1312import com .google .gcloud .storage .testing .RemoteGcsHelper ;
1413
2423import java .io .IOException ;
2524import java .io .OutputStreamWriter ;
2625import java .io .PrintWriter ;
26+ import java .net .URI ;
2727import java .nio .ByteBuffer ;
2828import java .nio .channels .ReadableByteChannel ;
2929import java .nio .channels .SeekableByteChannel ;
3030import java .nio .file .Files ;
3131import java .nio .file .NoSuchFileException ;
3232import java .nio .file .Path ;
33+ import java .nio .file .Paths ;
3334import java .nio .file .StandardOpenOption ;
3435import java .util .Arrays ;
3536import java .util .List ;
5556 * to your browsers is your "Service Account JSON Key".
5657 */
5758@ RunWith (JUnit4 .class )
59+ @ SuppressWarnings ("resource" )
5860public class ITGcsNio {
5961
6062 private static final List <String > FILE_CONTENTS =
@@ -63,38 +65,38 @@ public class ITGcsNio {
6365 "Ils sont doués de raison et de conscience et doivent agir " ,
6466 "les uns envers les autres dans un esprit de fraternité." );
6567
66- private static final Logger log = Logger .getLogger (ITGcsNio .class .getName ());
67- private static final String BUCKET = RemoteGcsHelper .generateBucketName ();
6868 private static final String SML_FILE = "tmp-test-small-file.txt" ;
6969 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.
7472 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 ;
7578 private static Storage storage ;
7679 private static StorageOptions storageOptions ;
7780
78- private final Random rnd = new Random ();
79-
8081 @ BeforeClass
81- public static void beforeClass () throws IOException {
82+ public static void beforeClass () {
83+ bucket = RemoteGcsHelper .generateBucketName ();
8284 // loads the credentials from local disk as par README
8385 RemoteGcsHelper gcsHelper = RemoteGcsHelper .create ();
8486 storageOptions = gcsHelper .options ();
8587 storage = storageOptions .service ();
8688 // create and populate test bucket
87- storage .create (BucketInfo .of (BUCKET ));
89+ storage .create (BucketInfo .of (bucket ));
8890 fillFile (storage , SML_FILE , SML_SIZE );
8991 fillFile (storage , BIG_FILE , BIG_SIZE );
9092 }
9193
9294 @ AfterClass
9395 public static void afterClass () throws ExecutionException , InterruptedException {
9496 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 );
98100 }
99101 }
100102
@@ -104,27 +106,33 @@ private static byte[] randomContents(int size) {
104106 return bytes ;
105107 }
106108
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 ));
109111 }
110112
111113 @ Test
112- public void testFileExists () throws IOException {
113- CloudStorageFileSystem testBucket = getTestBucket ( );
114+ public void testFileExists () {
115+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
114116 Path path = testBucket .getPath (SML_FILE );
115117 assertThat (Files .exists (path )).isTrue ();
116118 }
117119
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+
118126 @ Test
119127 public void testFileSize () throws IOException {
120- CloudStorageFileSystem testBucket = getTestBucket ( );
128+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
121129 Path path = testBucket .getPath (SML_FILE );
122130 assertThat (Files .size (path )).isEqualTo (SML_SIZE );
123131 }
124132
125133 @ Test (timeout = 60_000 )
126134 public void testReadByteChannel () throws IOException {
127- CloudStorageFileSystem testBucket = getTestBucket ( );
135+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
128136 Path path = testBucket .getPath (SML_FILE );
129137 long size = Files .size (path );
130138 SeekableByteChannel chan = Files .newByteChannel (path , StandardOpenOption .READ );
@@ -150,7 +158,7 @@ public void testReadByteChannel() throws IOException {
150158
151159 @ Test
152160 public void testSeek () throws IOException {
153- CloudStorageFileSystem testBucket = getTestBucket ( );
161+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
154162 Path path = testBucket .getPath (BIG_FILE );
155163 int size = BIG_SIZE ;
156164 byte [] contents = randomContents (size );
@@ -179,7 +187,7 @@ public void testSeek() throws IOException {
179187
180188 @ Test
181189 public void testCreate () throws IOException {
182- CloudStorageFileSystem testBucket = getTestBucket ( );
190+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
183191 Path path = testBucket .getPath (PREFIX + randomSuffix ());
184192 // file shouldn't exist initially. If it does it's either because it's a leftover
185193 // from a previous run (so we should delete the file)
@@ -201,7 +209,7 @@ public void testCreate() throws IOException {
201209
202210 @ Test
203211 public void testWrite () throws IOException {
204- CloudStorageFileSystem testBucket = getTestBucket ( );
212+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
205213 Path path = testBucket .getPath (PREFIX + randomSuffix ());
206214 // file shouldn't exist initially. If it does it's either because it's a leftover
207215 // from a previous run (so we should delete the file)
@@ -233,7 +241,7 @@ public void testWrite() throws IOException {
233241
234242 @ Test
235243 public void testCreateAndWrite () throws IOException {
236- CloudStorageFileSystem testBucket = getTestBucket ( );
244+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
237245 Path path = testBucket .getPath (PREFIX + randomSuffix ());
238246 // file shouldn't exist initially (see above).
239247 assertThat (Files .exists (path )).isFalse ();
@@ -262,7 +270,7 @@ public void testCreateAndWrite() throws IOException {
262270
263271 @ Test
264272 public void testWriteOnClose () throws Exception {
265- CloudStorageFileSystem testBucket = getTestBucket ( );
273+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
266274 Path path = testBucket .getPath (PREFIX + randomSuffix ());
267275 // file shouldn't exist initially (see above)
268276 assertThat (Files .exists (path )).isFalse ();
@@ -296,7 +304,7 @@ public void testWriteOnClose() throws Exception {
296304
297305 @ Test
298306 public void testCopy () throws IOException {
299- CloudStorageFileSystem testBucket = getTestBucket ( );
307+ CloudStorageFileSystem testBucket = CloudStorageFileSystem . forBucket ( bucket );
300308 Path src = testBucket .getPath (SML_FILE );
301309 Path dst = testBucket .getPath (PREFIX + randomSuffix ());
302310 // file shouldn't exist initially (see above).
@@ -330,19 +338,6 @@ private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOExcep
330338 }
331339
332340 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 );
347342 }
348343}
0 commit comments