Skip to content

Commit 3a6af05

Browse files
committed
---
yaml --- r: 6147 b: refs/heads/tswast-patch-1 c: a10b74b h: refs/heads/master i: 6145: ca55908 6143: b81efd6
1 parent 43069dc commit 3a6af05

3 files changed

Lines changed: 40 additions & 30 deletions

File tree

  • branches/tswast-patch-1/gcloud-java-storage/src

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 8591284926cec97e1d58b2d6e24b07d098fda171
60+
refs/heads/tswast-patch-1: a10b74b73c5f5d5a2ed337503ddd4351f95d2a64
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.gcloud.storage.Blob.BlobSourceOption.convert;
2222

23-
import com.google.common.collect.ImmutableList;
24-
import com.google.common.collect.Iterables;
2523
import com.google.gcloud.spi.StorageRpc;
2624
import com.google.gcloud.storage.Storage.BlobTargetOption;
2725
import com.google.gcloud.storage.Storage.CopyRequest;
@@ -166,32 +164,31 @@ public boolean delete(BlobSourceOption... options) {
166164
}
167165

168166
/**
169-
* Copy this blob.
167+
* Copy this blob to the target bucket, preserving its name. Possibly update metadata.
170168
*
171-
* @param target target blob
169+
* @param targetBucket target bucket's name
172170
* @param options source blob options
173171
* @return the copied blob
174172
* @throws StorageException upon failure
175173
*/
176-
public Blob copyTo(BlobInfo target, BlobSourceOption... options) {
177-
return copyTo(target, ImmutableList.copyOf(options), ImmutableList.<BlobTargetOption>of());
174+
public Blob copyTo(String targetBucket, BlobSourceOption... options) {
175+
return copyTo(targetBucket, info.name(), options);
178176
}
179177

180178
/**
181-
* Copy this blob.
179+
* Copy this blob to the target bucket with a new name. Possibly update metadata.
182180
*
183-
* @param target target blob
184-
* @param sourceOptions source blob options
185-
* @param targetOptions target blob options
181+
* @param targetBucket target bucket's name
182+
* @param targetBlob target blob's name
183+
* @param options source blob options
186184
* @return the copied blob
187185
* @throws StorageException upon failure
188186
*/
189-
public Blob copyTo(BlobInfo target, Iterable<BlobSourceOption> sourceOptions,
190-
Iterable<BlobTargetOption> targetOptions) {
187+
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
188+
BlobInfo updatedInfo = info.toBuilder().bucket(targetBucket).name(targetBlob).build();
191189
CopyRequest copyRequest =
192190
CopyRequest.builder().source(info.bucket(), info.name())
193-
.sourceOptions(convert(info, Iterables.toArray(sourceOptions, BlobSourceOption.class)))
194-
.target(target).targetOptions(targetOptions).build();
191+
.sourceOptions(convert(info, options)).target(updatedInfo).build();
195192
return new Blob(storage, storage.copy(copyRequest));
196193
}
197194

branches/tswast-patch-1/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,23 @@
2929
import static org.junit.Assert.assertTrue;
3030

3131
import com.google.gcloud.storage.Storage.CopyRequest;
32-
3332
import org.easymock.Capture;
3433
import org.junit.After;
3534
import org.junit.Before;
3635
import org.junit.Test;
37-
3836
import java.net.URL;
3937

4038
public 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

Comments
 (0)