3232import static org .junit .Assert .assertNull ;
3333import static org .junit .Assert .assertSame ;
3434import static org .junit .Assert .assertTrue ;
35+ import static org .junit .Assert .fail ;
3536
3637import com .google .api .core .ApiClock ;
3738import com .google .api .gax .retrying .RetrySettings ;
@@ -585,17 +586,22 @@ public void testBuilder() {
585586 assertTrue (blob .isDirectory ());
586587 }
587588
588- @ Test
589- public void testDownload () throws Exception {
590- final byte [] expected = {1 , 2 };
589+ private StorageRpc prepareForDownload () {
591590 StorageRpc mockStorageRpc = createNiceMock (StorageRpc .class );
592- expect (storage .getOptions ()).andReturn (mockOptions ). times ( 1 ) ;
591+ expect (storage .getOptions ()).andReturn (mockOptions );
593592 replay (storage );
594593 expect (mockOptions .getStorageRpcV1 ()).andReturn (mockStorageRpc );
595594 expect (mockOptions .getRetrySettings ()).andReturn (RETRY_SETTINGS );
596595 expect (mockOptions .getClock ()).andReturn (API_CLOCK );
597596 replay (mockOptions );
598597 blob = new Blob (storage , new BlobInfo .BuilderImpl (BLOB_INFO ));
598+ return mockStorageRpc ;
599+ }
600+
601+ @ Test
602+ public void testDownloadTo () throws Exception {
603+ final byte [] expected = {1 , 2 };
604+ StorageRpc mockStorageRpc = prepareForDownload ();
599605 expect (
600606 mockStorageRpc .read (
601607 anyObject (StorageObject .class ),
@@ -618,16 +624,9 @@ public Long answer() throws Throwable {
618624 }
619625
620626 @ Test
621- public void testDownloadWithRetries () throws Exception {
627+ public void testDownloadToWithRetries () throws Exception {
622628 final byte [] expected = {1 , 2 };
623- StorageRpc mockStorageRpc = createNiceMock (StorageRpc .class );
624- expect (storage .getOptions ()).andReturn (mockOptions );
625- replay (storage );
626- expect (mockOptions .getStorageRpcV1 ()).andReturn (mockStorageRpc );
627- expect (mockOptions .getRetrySettings ()).andReturn (RETRY_SETTINGS );
628- expect (mockOptions .getClock ()).andReturn (API_CLOCK );
629- replay (mockOptions );
630- blob = new Blob (storage , new BlobInfo .BuilderImpl (BLOB_INFO ));
629+ StorageRpc mockStorageRpc = prepareForDownload ();
631630 expect (
632631 mockStorageRpc .read (
633632 anyObject (StorageObject .class ),
@@ -662,4 +661,25 @@ public Long answer() throws Throwable {
662661 byte actual [] = Files .readAllBytes (file .toPath ());
663662 assertArrayEquals (expected , actual );
664663 }
664+
665+ @ Test
666+ public void testDownloadToWithException () throws Exception {
667+ StorageRpc mockStorageRpc = prepareForDownload ();
668+ Exception exception = new IllegalStateException ("test" );
669+ expect (
670+ mockStorageRpc .read (
671+ anyObject (StorageObject .class ),
672+ anyObject (Map .class ),
673+ eq (0l ),
674+ anyObject (OutputStream .class )))
675+ .andThrow (exception );
676+ replay (mockStorageRpc );
677+ File file = File .createTempFile ("blob" , ".tmp" );
678+ try {
679+ blob .downloadTo (file .toPath ());
680+ fail ();
681+ } catch (StorageException e ) {
682+ assertSame (exception , e .getCause ());
683+ }
684+ }
665685}
0 commit comments