32
32
import static org .junit .Assert .assertNull ;
33
33
import static org .junit .Assert .assertSame ;
34
34
import static org .junit .Assert .assertTrue ;
35
+ import static org .junit .Assert .fail ;
35
36
36
37
import com .google .api .core .ApiClock ;
37
38
import com .google .api .gax .retrying .RetrySettings ;
@@ -585,17 +586,22 @@ public void testBuilder() {
585
586
assertTrue (blob .isDirectory ());
586
587
}
587
588
588
- @ Test
589
- public void testDownload () throws Exception {
590
- final byte [] expected = {1 , 2 };
589
+ private StorageRpc prepareForDownload () {
591
590
StorageRpc mockStorageRpc = createNiceMock (StorageRpc .class );
592
- expect (storage .getOptions ()).andReturn (mockOptions ). times ( 1 ) ;
591
+ expect (storage .getOptions ()).andReturn (mockOptions );
593
592
replay (storage );
594
593
expect (mockOptions .getStorageRpcV1 ()).andReturn (mockStorageRpc );
595
594
expect (mockOptions .getRetrySettings ()).andReturn (RETRY_SETTINGS );
596
595
expect (mockOptions .getClock ()).andReturn (API_CLOCK );
597
596
replay (mockOptions );
598
597
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 ();
599
605
expect (
600
606
mockStorageRpc .read (
601
607
anyObject (StorageObject .class ),
@@ -618,16 +624,9 @@ public Long answer() throws Throwable {
618
624
}
619
625
620
626
@ Test
621
- public void testDownloadWithRetries () throws Exception {
627
+ public void testDownloadToWithRetries () throws Exception {
622
628
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 ();
631
630
expect (
632
631
mockStorageRpc .read (
633
632
anyObject (StorageObject .class ),
@@ -662,4 +661,25 @@ public Long answer() throws Throwable {
662
661
byte actual [] = Files .readAllBytes (file .toPath ());
663
662
assertArrayEquals (expected , actual );
664
663
}
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
+ }
665
685
}
0 commit comments