2929import static org .junit .Assert .assertTrue ;
3030
3131import com .google .gcloud .storage .Storage .CopyRequest ;
32-
3332import org .easymock .Capture ;
3433import org .junit .After ;
3534import org .junit .Before ;
3635import org .junit .Test ;
37-
3836import java .net .URL ;
3937
4038public class BlobTest {
4139
40+ private static final BlobInfo BLOB_INFO = BlobInfo .of ("b" , "n" );
41+
4242 private Storage storage ;
4343 private Blob blob ;
44- private final BlobInfo blobInfo = BlobInfo .of ("b" , "n" );
4544
4645 @ Before
4746 public void setUp () throws Exception {
4847 storage = createStrictMock (Storage .class );
49- blob = new Blob (storage , blobInfo );
48+ blob = new Blob (storage , BLOB_INFO );
5049 }
5150
5251 @ After
@@ -56,35 +55,35 @@ public void tearDown() throws Exception {
5655
5756 @ Test
5857 public void testInfo () throws Exception {
59- assertEquals (blobInfo , blob .info ());
58+ assertEquals (BLOB_INFO , blob .info ());
6059 replay (storage );
6160 }
6261
6362 @ Test
6463 public void testExists_True () throws Exception {
65- expect (storage .get (blobInfo .bucket (), blobInfo .name ())).andReturn (blobInfo );
64+ expect (storage .get (BLOB_INFO .bucket (), BLOB_INFO .name ())).andReturn (BLOB_INFO );
6665 replay (storage );
6766 assertTrue (blob .exists ());
6867 }
6968
7069 @ Test
7170 public void testExists_False () throws Exception {
72- expect (storage .get (blobInfo .bucket (), blobInfo .name ())).andReturn (null );
71+ expect (storage .get (BLOB_INFO .bucket (), BLOB_INFO .name ())).andReturn (null );
7372 replay (storage );
7473 assertFalse (blob .exists ());
7574 }
7675
7776 @ Test
7877 public void testContent () throws Exception {
7978 byte [] content = {1 , 2 };
80- expect (storage .readAllBytes (blobInfo .bucket (), blobInfo .name ())).andReturn (content );
79+ expect (storage .readAllBytes (BLOB_INFO .bucket (), BLOB_INFO .name ())).andReturn (content );
8180 replay (storage );
8281 assertArrayEquals (content , blob .content ());
8382 }
8483
8584 @ Test
8685 public void testUpdate () throws Exception {
87- BlobInfo updatedInfo = blobInfo .toBuilder ().cacheControl ("c" ).build ();
86+ BlobInfo updatedInfo = BLOB_INFO .toBuilder ().cacheControl ("c" ).build ();
8887 expect (storage .update (updatedInfo )).andReturn (updatedInfo );
8988 replay (storage );
9089 blob .update (updatedInfo );
@@ -94,18 +93,32 @@ public void testUpdate() throws Exception {
9493
9594 @ Test
9695 public void testDelete () throws Exception {
97- expect (storage .delete (blobInfo .bucket (), blobInfo .name ())).andReturn (true );
96+ expect (storage .delete (BLOB_INFO .bucket (), BLOB_INFO .name ())).andReturn (true );
9897 replay (storage );
9998 assertTrue (blob .delete ());
10099 }
101100
101+ @ Test
102+ public void testCopyToBucket () throws Exception {
103+ BlobInfo target = BLOB_INFO .toBuilder ().bucket ("bt" ).build ();
104+ Capture <CopyRequest > capturedCopyRequest = Capture .newInstance ();
105+ expect (storage .copy (capture (capturedCopyRequest ))).andReturn (target );
106+ replay (storage );
107+ Blob targetBlob = blob .copyTo ("bt" );
108+ assertEquals (target , targetBlob .info ());
109+ assertEquals (capturedCopyRequest .getValue ().sourceBlob (), blob .info ().name ());
110+ assertEquals (capturedCopyRequest .getValue ().sourceBucket (), blob .info ().bucket ());
111+ assertEquals (capturedCopyRequest .getValue ().target (), target );
112+ assertSame (storage , targetBlob .storage ());
113+ }
114+
102115 @ Test
103116 public void testCopyTo () throws Exception {
104- BlobInfo target = BlobInfo . of ( "bt" , "nt" );
117+ BlobInfo target = BLOB_INFO . toBuilder (). bucket ( "bt" ). name ( "nt" ). build ( );
105118 Capture <CopyRequest > capturedCopyRequest = Capture .newInstance ();
106119 expect (storage .copy (capture (capturedCopyRequest ))).andReturn (target );
107120 replay (storage );
108- Blob targetBlob = blob .copyTo (target );
121+ Blob targetBlob = blob .copyTo ("bt" , "nt" );
109122 assertEquals (target , targetBlob .info ());
110123 assertEquals (capturedCopyRequest .getValue ().sourceBlob (), blob .info ().name ());
111124 assertEquals (capturedCopyRequest .getValue ().sourceBucket (), blob .info ().bucket ());
@@ -116,23 +129,23 @@ public void testCopyTo() throws Exception {
116129 @ Test
117130 public void testReader () throws Exception {
118131 BlobReadChannel channel = createMock (BlobReadChannel .class );
119- expect (storage .reader (blobInfo .bucket (), blobInfo .name ())).andReturn (channel );
132+ expect (storage .reader (BLOB_INFO .bucket (), BLOB_INFO .name ())).andReturn (channel );
120133 replay (storage );
121134 assertSame (channel , blob .reader ());
122135 }
123136
124137 @ Test
125138 public void testWriter () throws Exception {
126139 BlobWriteChannel channel = createMock (BlobWriteChannel .class );
127- expect (storage .writer (blobInfo )).andReturn (channel );
140+ expect (storage .writer (BLOB_INFO )).andReturn (channel );
128141 replay (storage );
129142 assertSame (channel , blob .writer ());
130143 }
131144
132145 @ Test
133146 public void testSignUrl () throws Exception {
134147 URL url = new URL ("http://localhost:123/bla" );
135- expect (storage .signUrl (blobInfo , 100 )).andReturn (url );
148+ expect (storage .signUrl (BLOB_INFO , 100 )).andReturn (url );
136149 replay (storage );
137150 assertEquals (url , blob .signUrl (100 ));
138151 }
0 commit comments