3434import java .nio .ByteBuffer ;
3535import java .util .Arrays ;
3636import java .util .Calendar ;
37+ import java .util .Iterator ;
38+ import java .util .concurrent .ExecutionException ;
39+ import java .util .concurrent .TimeUnit ;
40+ import java .util .concurrent .TimeoutException ;
3741
3842import org .junit .AfterClass ;
3943import org .junit .Before ;
4044import org .junit .BeforeClass ;
41- import org .junit .Rule ;
4245import org .junit .Test ;
43- import org .junit .rules .ExpectedException ;
4446
4547public class ITStorageTest {
4648
47- private static StorageOptions options ;
4849 private static Storage storage ;
4950 private static RemoteGcsHelper gcsHelper ;
50- private static String bucket ;
5151
52+ private static final String bucket = RemoteGcsHelper .generateBucketName ();
5253 private static final String CONTENT_TYPE = "text/plain" ;
5354 private static final byte [] BLOB_BYTE_CONTENT = {0xD , 0xE , 0xA , 0xD };
5455 private static final String BLOB_STRING_CONTENT = "Hello Google Cloud Storage!" ;
5556
56- @ Rule
57- public ExpectedException thrown = ExpectedException .none ();
58-
5957 @ BeforeClass
6058 public static void beforeClass () {
61- gcsHelper = RemoteGcsHelper .create ();
62- if (gcsHelper != null ) {
63- options = gcsHelper .options ();
64- storage = StorageFactory .instance ().get (options );
65- bucket = gcsHelper .bucket ();
59+ try {
60+ gcsHelper = RemoteGcsHelper .create ();
61+ storage = StorageFactory .instance ().get (gcsHelper .options ());
6662 storage .create (BucketInfo .of (bucket ));
63+ } catch (RemoteGcsHelper .GcsHelperException e ) {
64+ // ignore
6765 }
6866 }
6967
7068 @ AfterClass
71- public static void afterClass () {
69+ public static void afterClass ()
70+ throws ExecutionException , TimeoutException , InterruptedException {
7271 if (storage != null ) {
73- for ( BlobInfo info : storage . list ( bucket )) {
74- storage . delete ( bucket , info . name () );
72+ if (! RemoteGcsHelper . deleteBucketRecursively ( storage , bucket , 5 , TimeUnit . SECONDS )) {
73+ throw new RuntimeException ( "Bucket deletion timed out. Could not delete non-empty bucket" );
7574 }
76- storage .delete (bucket );
7775 }
7876 }
7977
@@ -82,11 +80,16 @@ public void beforeMethod() {
8280 org .junit .Assume .assumeNotNull (storage );
8381 }
8482
85- @ Test
86- public void testListBuckets () {
87- ListResult <BucketInfo > bucketList = storage .list (Storage .BucketListOption .prefix (bucket ));
88- for (BucketInfo bucketInfo : bucketList ) {
89- assertTrue (bucketInfo .name ().startsWith (bucket ));
83+ @ Test (timeout = 5000 )
84+ public void testListBuckets () throws InterruptedException {
85+ Iterator <BucketInfo > bucketIterator =
86+ storage .list (Storage .BucketListOption .prefix (bucket )).iterator ();
87+ while (!bucketIterator .hasNext ()) {
88+ Thread .sleep (500 );
89+ bucketIterator = storage .list (Storage .BucketListOption .prefix (bucket )).iterator ();
90+ }
91+ while (bucketIterator .hasNext ()) {
92+ assertTrue (bucketIterator .next ().name ().startsWith (bucket ));
9093 }
9194 }
9295
@@ -137,7 +140,7 @@ public void testCreateBlobFail() {
137140 BlobInfo blob = BlobInfo .of (bucket , blobName );
138141 assertNotNull (storage .create (blob ));
139142 try {
140- storage .create (blob .toBuilder ().generation (42L ).build (), BLOB_BYTE_CONTENT ,
143+ storage .create (blob .toBuilder ().generation (- 1L ).build (), BLOB_BYTE_CONTENT ,
141144 Storage .BlobTargetOption .generationMatch ());
142145 fail ("StorageException was expected" );
143146 } catch (StorageException ex ) {
@@ -165,7 +168,7 @@ public void testUpdateBlobFail() {
165168 BlobInfo blob = BlobInfo .of (bucket , blobName );
166169 assertNotNull (storage .create (blob ));
167170 try {
168- storage .update (blob .toBuilder ().contentType (CONTENT_TYPE ).generation (42L ).build (),
171+ storage .update (blob .toBuilder ().contentType (CONTENT_TYPE ).generation (- 1L ).build (),
169172 Storage .BlobTargetOption .generationMatch ());
170173 fail ("StorageException was expected" );
171174 } catch (StorageException ex ) {
@@ -187,7 +190,7 @@ public void testDeleteBlobFail() {
187190 BlobInfo blob = BlobInfo .of (bucket , blobName );
188191 assertNotNull (storage .create (blob ));
189192 try {
190- storage .delete (bucket , blob .name (), Storage .BlobSourceOption .generationMatch (42L ));
193+ storage .delete (bucket , blob .name (), Storage .BlobSourceOption .generationMatch (- 1L ));
191194 fail ("StorageException was expected" );
192195 } catch (StorageException ex ) {
193196 // expected
@@ -232,8 +235,8 @@ public void testComposeBlobFail() {
232235 String targetBlobName = "test-compose-blob-fail-target" ;
233236 BlobInfo targetBlob = BlobInfo .of (bucket , targetBlobName );
234237 Storage .ComposeRequest req = Storage .ComposeRequest .builder ()
235- .addSource (sourceBlobName1 , 42L )
236- .addSource (sourceBlobName2 , 42L )
238+ .addSource (sourceBlobName1 , - 1L )
239+ .addSource (sourceBlobName2 , - 1L )
237240 .target (targetBlob )
238241 .build ();
239242 try {
@@ -290,7 +293,7 @@ public void testCopyBlobFail() {
290293 Storage .CopyRequest req = new Storage .CopyRequest .Builder ()
291294 .source (bucket , sourceBlobName )
292295 .target (BlobInfo .builder (bucket , targetBlobName ).build ())
293- .sourceOptions (Storage .BlobSourceOption .metagenerationMatch (42L ))
296+ .sourceOptions (Storage .BlobSourceOption .metagenerationMatch (- 1L ))
294297 .build ();
295298 try {
296299 storage .copy (req );
@@ -362,11 +365,11 @@ public void testBatchRequestFail() {
362365 String blobName = "test-batch-request-blob-fail" ;
363366 BlobInfo blob = BlobInfo .of (bucket , blobName );
364367 assertNotNull (storage .create (blob ));
365- BlobInfo updatedBlob = blob .toBuilder ().generation (42L ).build ();
368+ BlobInfo updatedBlob = blob .toBuilder ().generation (- 1L ).build ();
366369 BatchRequest batchRequest = BatchRequest .builder ()
367370 .update (updatedBlob , Storage .BlobTargetOption .generationMatch ())
368- .delete (bucket , blobName , Storage .BlobSourceOption .generationMatch (42L ))
369- .get (bucket , blobName , Storage .BlobSourceOption .generationMatch (42L ))
371+ .delete (bucket , blobName , Storage .BlobSourceOption .generationMatch (- 1L ))
372+ .get (bucket , blobName , Storage .BlobSourceOption .generationMatch (- 1L ))
370373 .build ();
371374 BatchResponse updateResponse = storage .apply (batchRequest );
372375 assertEquals (1 , updateResponse .updates ().size ());
@@ -407,7 +410,7 @@ public void testReadChannelFail() throws UnsupportedEncodingException, IOExcepti
407410 BlobInfo blob = BlobInfo .of (bucket , blobName );
408411 assertNotNull (storage .create (blob ));
409412 try (BlobReadChannel reader =
410- storage .reader (bucket , blobName , Storage .BlobSourceOption .metagenerationMatch (42L ))) {
413+ storage .reader (bucket , blobName , Storage .BlobSourceOption .metagenerationMatch (- 1L ))) {
411414 reader .read (ByteBuffer .allocate (42 ));
412415 fail ("StorageException was expected" );
413416 } catch (StorageException ex ) {
@@ -419,7 +422,7 @@ public void testReadChannelFail() throws UnsupportedEncodingException, IOExcepti
419422 @ Test
420423 public void testWriteChannelFail () throws UnsupportedEncodingException , IOException {
421424 String blobName = "test-write-channel-blob-fail" ;
422- BlobInfo blob = BlobInfo .builder (bucket , blobName ).generation (42L ).build ();
425+ BlobInfo blob = BlobInfo .builder (bucket , blobName ).generation (- 1L ).build ();
423426 try {
424427 try (BlobWriteChannel writer =
425428 storage .writer (blob , Storage .BlobTargetOption .generationMatch ())) {
0 commit comments