@@ -393,36 +393,41 @@ public void testBatchBlobDelete() throws Exception {
393393 // setup
394394
395395 CloudBlobContainer container = BlobTestHelper .getRandomContainerReference ();
396- container .createIfNotExists ();
396+ try {
397+ container .createIfNotExists ();
397398
398- List <CloudBlob > blobs = new ArrayList <>();
399- BlobDeleteBatchOperation batchDeleteOp = new BlobDeleteBatchOperation ();
399+ List <CloudBlob > blobs = new ArrayList <>();
400+ BlobDeleteBatchOperation batchDeleteOp = new BlobDeleteBatchOperation ();
400401
401- for (int i = 0 ; i < 3 ; i ++) {
402- CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
403- blob .uploadText ("content" );
402+ for (int i = 0 ; i < 3 ; i ++) {
403+ CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
404+ blob .uploadText ("content" );
404405
405- blobs .add (blob );
406- batchDeleteOp .addSubOperation (blob );
407- }
406+ blobs .add (blob );
407+ batchDeleteOp .addSubOperation (blob );
408+ }
408409
409- // execute batch
410+ // execute batch
410411
411- Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchDeleteOp );
412+ Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchDeleteOp );
412413
413- // validate
414+ // validate
414415
415- for (CloudBlob blob : blobs ) {
416- assertFalse (blob .exists ());
417- }
416+ for (CloudBlob blob : blobs ) {
417+ assertFalse (blob .exists ());
418+ }
418419
419- int numResponses = 0 ;
420- for (Map .Entry <CloudBlob , Void > response : responses .entrySet ()) {
421- assertTrue (blobs .contains (response .getKey ()));
422- assertNull (response .getValue ()); // CloudBlob::delete() returns Void (null), so all batch responses should be null
423- numResponses ++;
420+ int numResponses = 0 ;
421+ for (Map .Entry <CloudBlob , Void > response : responses .entrySet ()) {
422+ assertTrue (blobs .contains (response .getKey ()));
423+ assertNull (response .getValue ()); // CloudBlob::delete() returns Void (null), so all batch responses should be null
424+ numResponses ++;
425+ }
426+ assertEquals (blobs .size (), numResponses );
427+ }
428+ finally {
429+ container .deleteIfExists ();
424430 }
425- assertEquals (blobs .size (), numResponses );
426431 }
427432
428433 @ Test (expected = StorageException .class )
@@ -432,13 +437,18 @@ public void testBatchBlobDeleteNoRequests() throws Exception {
432437 // setup
433438
434439 CloudBlobContainer container = BlobTestHelper .getRandomContainerReference ();
435- container .createIfNotExists ();
440+ try {
441+ container .createIfNotExists ();
436442
437- BlobDeleteBatchOperation batchDeleteOp = new BlobDeleteBatchOperation ();
443+ BlobDeleteBatchOperation batchDeleteOp = new BlobDeleteBatchOperation ();
438444
439- // execute batch
445+ // execute batch
440446
441- Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchDeleteOp ); //throws
447+ Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchDeleteOp ); //throws
448+ }
449+ finally {
450+ container .deleteIfExists ();
451+ }
442452 }
443453
444454 @ Test
@@ -450,42 +460,46 @@ public void testBatchBlobDeleteMixedRequests() throws Exception {
450460 int BAD_REQUESTS = 2 ; // blobs that don't exist to delete
451461
452462 CloudBlobContainer container = BlobTestHelper .getRandomContainerReference ();
453- container .createIfNotExists ();
463+ try {
464+ container .createIfNotExists ();
454465
455- List <CloudBlob > blobs = new ArrayList <>();
456- BlobDeleteBatchOperation batchDeleteOp = new BlobDeleteBatchOperation ();
466+ List <CloudBlob > blobs = new ArrayList <>();
467+ BlobDeleteBatchOperation batchDeleteOp = new BlobDeleteBatchOperation ();
457468
458- for (int i = 0 ; i < BLOBS ; i ++) {
459- CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
469+ for (int i = 0 ; i < BLOBS ; i ++) {
470+ CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
460471
461- if (i >= BAD_REQUESTS ) {
462- blob .uploadText ("content" );
463- }
472+ if (i >= BAD_REQUESTS ) {
473+ blob .uploadText ("content" );
474+ }
464475
465- blobs .add (blob );
466- batchDeleteOp .addSubOperation (blob );
467- }
476+ blobs .add (blob );
477+ batchDeleteOp .addSubOperation (blob );
478+ }
468479
469- // execute batch
480+ // execute batch
470481
471- Map <CloudBlob , Void > responses ;
472- boolean threw = true ;
473- try {
474- responses = container .getServiceClient ().executeBatch (batchDeleteOp );
475- threw = false ;
476- }
482+ Map <CloudBlob , Void > responses ;
483+ boolean threw = true ;
484+ try {
485+ responses = container .getServiceClient ().executeBatch (batchDeleteOp );
486+ threw = false ;
487+ }
477488
478- // validate
489+ // validate
479490
480- catch (BatchException e ) {
481- // good deletes are successful
482- for (CloudBlob blob : blobs ) {
483- assertFalse (blob .exists ());
484- assertTrue (e .getSuccessfulResponses ().keySet ().contains (blob ) || e .getExceptions ().keySet ().contains (blob ));
491+ catch (BatchException e ) {
492+ // good deletes are successful
493+ for (CloudBlob blob : blobs ) {
494+ assertFalse (blob .exists ());
495+ assertTrue (e .getSuccessfulResponses ().keySet ().contains (blob ) || e .getExceptions ().keySet ().contains (blob ));
496+ }
497+ } finally {
498+ assertTrue (threw );
485499 }
486500 }
487501 finally {
488- assertTrue ( threw );
502+ container . deleteIfExists ( );
489503 }
490504 }
491505
@@ -496,37 +510,42 @@ public void testBatchBlobSetTier() throws Exception {
496510 // setup
497511
498512 CloudBlobContainer container = BlobTestHelper .getRandomContainerReference ();
499- container .createIfNotExists ();
513+ try {
514+ container .createIfNotExists ();
500515
501- List <CloudBlob > blobs = new ArrayList <>();
502- BlobSetTierBatchOperation batchTierOp = new BlobSetTierBatchOperation ();
516+ List <CloudBlob > blobs = new ArrayList <>();
517+ BlobSetTierBatchOperation batchTierOp = new BlobSetTierBatchOperation ();
503518
504- for (int i = 0 ; i < 3 ; i ++) {
505- CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
506- blob .uploadText ("content" );
519+ for (int i = 0 ; i < 3 ; i ++) {
520+ CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
521+ blob .uploadText ("content" );
507522
508- blobs .add (blob );
509- batchTierOp .addSubOperation (blob , StandardBlobTier .HOT );
510- }
523+ blobs .add (blob );
524+ batchTierOp .addSubOperation (blob , StandardBlobTier .HOT );
525+ }
511526
512- // execute batch
527+ // execute batch
513528
514- Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchTierOp );
529+ Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchTierOp );
515530
516- // validate
531+ // validate
517532
518- for (CloudBlob blob : blobs ) {
519- blob .downloadAttributes ();
520- assertEquals (StandardBlobTier .HOT , blob .getProperties ().getStandardBlobTier ());
521- }
533+ for (CloudBlob blob : blobs ) {
534+ blob .downloadAttributes ();
535+ assertEquals (StandardBlobTier .HOT , blob .getProperties ().getStandardBlobTier ());
536+ }
522537
523- int numResponses = 0 ;
524- for (Map .Entry <CloudBlob , Void > response : responses .entrySet ()) {
525- assertTrue (blobs .contains (response .getKey ()));
526- assertNull (response .getValue ()); // CloudBlob::setTier() returns Void (null), so all batch responses should be null
527- numResponses ++;
538+ int numResponses = 0 ;
539+ for (Map .Entry <CloudBlob , Void > response : responses .entrySet ()) {
540+ assertTrue (blobs .contains (response .getKey ()));
541+ assertNull (response .getValue ()); // CloudBlob::setTier() returns Void (null), so all batch responses should be null
542+ numResponses ++;
543+ }
544+ assertEquals (blobs .size (), numResponses );
545+ }
546+ finally {
547+ container .deleteIfExists ();
528548 }
529- assertEquals (blobs .size (), numResponses );
530549 }
531550
532551 @ Test (expected = StorageException .class )
@@ -536,13 +555,18 @@ public void testBatchBlobSetTierNoRequests() throws Exception {
536555 // setup
537556
538557 CloudBlobContainer container = BlobTestHelper .getRandomContainerReference ();
539- container .createIfNotExists ();
558+ try {
559+ container .createIfNotExists ();
540560
541- BlobSetTierBatchOperation batchDeleteOp = new BlobSetTierBatchOperation ();
561+ BlobSetTierBatchOperation batchDeleteOp = new BlobSetTierBatchOperation ();
542562
543- // execute batch
563+ // execute batch
544564
545- Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchDeleteOp ); //throws
565+ Map <CloudBlob , Void > responses = container .getServiceClient ().executeBatch (batchDeleteOp ); //throws
566+ }
567+ finally {
568+ container .deleteIfExists ();
569+ }
546570 }
547571
548572 @ Test
@@ -554,46 +578,50 @@ public void testBatchBlobSetTierMixedRequests() throws Exception {
554578 int BAD_REQUESTS = 2 ; // blobs that don't exist to delete
555579
556580 CloudBlobContainer container = BlobTestHelper .getRandomContainerReference ();
557- container .createIfNotExists ();
581+ try {
582+ container .createIfNotExists ();
558583
559- List <CloudBlob > blobs = new ArrayList <>();
560- BlobSetTierBatchOperation batchSetTierOp = new BlobSetTierBatchOperation ();
584+ List <CloudBlob > blobs = new ArrayList <>();
585+ BlobSetTierBatchOperation batchSetTierOp = new BlobSetTierBatchOperation ();
561586
562- for (int i = 0 ; i < BLOBS ; i ++) {
563- CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
587+ for (int i = 0 ; i < BLOBS ; i ++) {
588+ CloudBlockBlob blob = container .getBlockBlobReference ("testblob_" + UUID .randomUUID ());
564589
565- // bad requests first; displays that good responses come first regardless of order of request
566- if (i >= BAD_REQUESTS ) {
567- blob .uploadText ("content" );
568- }
590+ // bad requests first; displays that good responses come first regardless of order of request
591+ if (i >= BAD_REQUESTS ) {
592+ blob .uploadText ("content" );
593+ }
569594
570- blobs .add (blob );
571- batchSetTierOp .addSubOperation (blob , StandardBlobTier .HOT );
572- }
595+ blobs .add (blob );
596+ batchSetTierOp .addSubOperation (blob , StandardBlobTier .HOT );
597+ }
573598
574- // execute batch
599+ // execute batch
575600
576- Map <CloudBlob , Void > responses ;
577- boolean threw = true ;
578- try {
579- responses = container .getServiceClient ().executeBatch (batchSetTierOp );
580- threw = false ;
581- }
601+ Map <CloudBlob , Void > responses ;
602+ boolean threw = true ;
603+ try {
604+ responses = container .getServiceClient ().executeBatch (batchSetTierOp );
605+ threw = false ;
606+ }
582607
583- // validate
608+ // validate
584609
585- catch (BatchException e ) {
586- // good deletes are successful
587- for (CloudBlob blob : blobs ) {
588- if (blob .exists ()) {
589- blob .downloadAttributes ();
590- assertEquals (StandardBlobTier .HOT , blob .getProperties ().getStandardBlobTier ());
610+ catch (BatchException e ) {
611+ // good deletes are successful
612+ for (CloudBlob blob : blobs ) {
613+ if (blob .exists ()) {
614+ blob .downloadAttributes ();
615+ assertEquals (StandardBlobTier .HOT , blob .getProperties ().getStandardBlobTier ());
616+ }
617+ assertTrue (e .getSuccessfulResponses ().keySet ().contains (blob ) || e .getExceptions ().keySet ().contains (blob ));
591618 }
592- assertTrue (e .getSuccessfulResponses ().keySet ().contains (blob ) || e .getExceptions ().keySet ().contains (blob ));
619+ } finally {
620+ assertTrue (threw );
593621 }
594622 }
595623 finally {
596- assertTrue ( threw );
624+ container . deleteIfExists ( );
597625 }
598626 }
599627}
0 commit comments