Skip to content

Commit 23215a1

Browse files
committed
Better javadoc, check contentType and metadata in integrationt tests
1 parent 352e097 commit 23215a1

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
import java.util.concurrent.Callable;
3333

3434
/**
35-
* Google Storage blob copy writer. This class holds the result of a copy request.
36-
* If source and destination blobs do not share the same location or storage class more than one
37-
* RPC request is needed to copy the blob otherwise one or more {@link #copyChunk()} calls are
38-
* necessary to complete the copy.
35+
* Google Storage blob copy writer. This class holds the result of a copy request. If source and
36+
* destination blobs share the same location and storage class the copy is completed in one RPC call
37+
* otherwise one or more {@link #copyChunk} calls are necessary to complete the copy. In addition,
38+
* {@link CopyWriter#result()} can be used to automatically complete the copy and return information
39+
* on the newly created blob.
3940
*
4041
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite">Rewrite</a>
4142
*/
@@ -52,20 +53,26 @@ public class CopyWriter implements Restorable<CopyWriter> {
5253
}
5354

5455
/**
55-
* Returns the updated information for the just written blob. This method might block and issue
56-
* several RPC requests to complete blob copy.
56+
* Returns the updated information for the written blob. Calling this method when {@code isDone()}
57+
* is {@code false} will block until all pending chunks are copied.
58+
* <p>
59+
* This method has the same effect of doing:
60+
* <pre> {@code while (!copyWriter.isDone()) {
61+
* copyWriter.copyChunk();
62+
* }}
63+
* </pre>
5764
*
5865
* @throws StorageException upon failure
5966
*/
6067
public BlobInfo result() {
61-
while(!isDone()) {
68+
while (!isDone()) {
6269
copyChunk();
6370
}
6471
return BlobInfo.fromPb(rewriteResponse.result);
6572
}
6673

6774
/**
68-
* Size of the blob being copied.
75+
* Returns the size of the blob being copied.
6976
*/
7077
public long blobSize() {
7178
return rewriteResponse.blobSize;

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,14 @@ public static Builder builder() {
839839
* Example usage of copy:
840840
* <pre> {@code BlobInfo blob = service.copy(copyRequest).result();}
841841
* </pre>
842+
* To explicitly issue chunk copy requests use {@link CopyWriter#copyChunk()} instead:
843+
* <pre> {@code CopyWriter copyWriter = service.copy(copyRequest);
844+
* while (!copyWriter.isDone()) {
845+
* copyWriter.copyChunk();
846+
* }
847+
* BlobInfo blob = copyWriter.result();
848+
* }
849+
* </pre>
842850
*
843851
* @return a {@link CopyWriter} object that can be used to get information on the newly created
844852
* blob or to complete the copy if more than one RPC request is needed

gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,20 @@ public void testComposeBlobFail() {
318318
public void testCopyBlob() {
319319
String sourceBlobName = "test-copy-blob-source";
320320
BlobId source = BlobId.of(BUCKET, sourceBlobName);
321-
assertNotNull(storage.create(BlobInfo.builder(source).build(), BLOB_BYTE_CONTENT));
321+
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
322+
BlobInfo blob = BlobInfo.builder(source)
323+
.contentType(CONTENT_TYPE)
324+
.metadata(metadata)
325+
.build();
326+
assertNotNull(storage.create(blob, BLOB_BYTE_CONTENT));
322327
String targetBlobName = "test-copy-blob-target";
323-
BlobInfo target = BlobInfo.builder(BUCKET, targetBlobName).contentType(CONTENT_TYPE).build();
328+
BlobInfo target = BlobInfo.builder(BUCKET, targetBlobName).build();
324329
Storage.CopyRequest req = Storage.CopyRequest.of(source, target);
325330
CopyWriter copyWriter = storage.copy(req);
326-
assertEquals(copyWriter.result(), storage.get(BUCKET, targetBlobName));
331+
assertEquals(BUCKET, copyWriter.result().bucket());
332+
assertEquals(targetBlobName, copyWriter.result().name());
333+
assertEquals(CONTENT_TYPE, copyWriter.result().contentType());
334+
assertEquals(metadata, copyWriter.result().metadata());
327335
assertTrue(copyWriter.isDone());
328336
assertTrue(storage.delete(BUCKET, sourceBlobName));
329337
assertTrue(storage.delete(BUCKET, targetBlobName));
@@ -334,14 +342,18 @@ public void testCopyBlobUpdateMetadata() {
334342
String sourceBlobName = "test-copy-blob-update-metadata-source";
335343
BlobId source = BlobId.of(BUCKET, sourceBlobName);
336344
assertNotNull(storage.create(BlobInfo.builder(source).build(), BLOB_BYTE_CONTENT));
337-
String targetBlobName = "test-copy-blob-target";
345+
String targetBlobName = "test-copy-blob-update-metadata-target";
346+
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
338347
BlobInfo target = BlobInfo.builder(BUCKET, targetBlobName)
339348
.contentType(CONTENT_TYPE)
340-
.metadata(ImmutableMap.of("k", "v"))
349+
.metadata(metadata)
341350
.build();
342351
Storage.CopyRequest req = Storage.CopyRequest.of(source, target);
343352
CopyWriter copyWriter = storage.copy(req);
344-
assertEquals(copyWriter.result(), storage.get(BUCKET, targetBlobName));
353+
assertEquals(BUCKET, copyWriter.result().bucket());
354+
assertEquals(targetBlobName, copyWriter.result().name());
355+
assertEquals(CONTENT_TYPE, copyWriter.result().contentType());
356+
assertEquals(metadata, copyWriter.result().metadata());
345357
assertTrue(copyWriter.isDone());
346358
assertTrue(storage.delete(BUCKET, sourceBlobName));
347359
assertTrue(storage.delete(BUCKET, targetBlobName));

0 commit comments

Comments
 (0)