Skip to content

Commit ecf2f25

Browse files
committed
---
yaml --- r: 1085 b: refs/heads/master c: 36af448 h: refs/heads/master i: 1083: d8112ed v: v3
1 parent 5cdeb85 commit ecf2f25

7 files changed

Lines changed: 156 additions & 10 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
refs/heads/master: d4530f604125c52522b729cd6bd1142f5eacd54a
2+
refs/heads/master: 36af4485261d1abae1060ac2936e78f43192614c
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BatchResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public boolean equals(Object obj) {
133133
BatchResponse other = (BatchResponse) obj;
134134
return Objects.equals(deleteResult, other.deleteResult)
135135
&& Objects.equals(updateResult, other.updateResult)
136-
&& Objects.equals(updateResult, other.updateResult);
136+
&& Objects.equals(getResult, other.getResult);
137137
}
138138

139139
/**

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import com.google.common.base.MoreObjects;
2223
import com.google.gcloud.storage.Storage.BlobSourceOption;
2324
import com.google.gcloud.storage.Storage.BlobTargetOption;
25+
import com.google.gcloud.storage.Storage.BlobWriteOption;
2426
import com.google.gcloud.storage.Storage.BucketSourceOption;
2527
import com.google.gcloud.storage.Storage.BucketTargetOption;
28+
import java.io.InputStream;
2629

2730
import java.util.ArrayList;
2831
import java.util.Collections;
@@ -172,17 +175,43 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
172175
}
173176

174177
/**
175-
* Creates a new blob in this bucket.
178+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
179+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
180+
* is recommended as it uses resumable upload. MD5 and CRC32C hashes of {@code content} are
181+
* computed and used for validating transferred data.
176182
*
177183
* @param blob a blob name
178184
* @param content the blob content
185+
* @param contentType the blob content type. If {@code null} then
186+
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
179187
* @param options options for blob creation
180188
* @return a complete blob information.
181189
* @throws StorageException upon failure
182190
*/
183-
Blob create(String blob, byte[] content, BlobTargetOption... options) {
184-
BlobId blobId = BlobId.of(info.name(), blob);
185-
return new Blob(storage, storage.create(BlobInfo.builder(blobId).build(), content, options));
191+
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
192+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
193+
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
194+
return new Blob(storage, storage.create(blobInfo, content, options));
195+
}
196+
197+
/**
198+
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}.
199+
* For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)}
200+
* is recommended as it uses resumable upload.
201+
*
202+
* @param blob a blob name
203+
* @param content the blob content as a stream
204+
* @param contentType the blob content type. If {@code null} then
205+
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
206+
* @param options options for blob creation
207+
* @return a complete blob information.
208+
* @throws StorageException upon failure
209+
*/
210+
public Blob create(String blob, InputStream content, String contentType,
211+
BlobWriteOption... options) {
212+
BlobInfo blobInfo = BlobInfo.builder(BlobId.of(info.name(), blob))
213+
.contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build();
214+
return new Blob(storage, storage.create(blobInfo, content, options));
186215
}
187216

188217
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
*/
4747
public interface Storage extends Service<StorageOptions> {
4848

49+
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
50+
4951
enum PredefinedAcl {
5052
AUTHENTICATED_READ("authenticatedRead"),
5153
ALL_AUTHENTICATED_USERS("allAuthenticatedUsers"),

trunk/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BatchRequestTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.gcloud.storage.Storage.PredefinedAcl.PUBLIC_READ;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNotEquals;
2223
import static org.junit.Assert.assertTrue;
2324

2425
import com.google.common.collect.Iterables;
@@ -82,4 +83,56 @@ public void testBatchRequest() {
8283
assertTrue(Iterables.isEmpty(get.getValue()));
8384
assertFalse(gets.hasNext());
8485
}
86+
87+
@Test
88+
public void testEquals() {
89+
BatchRequest request = BatchRequest.builder()
90+
.delete("b1", "o1")
91+
.delete("b1", "o2")
92+
.update(BlobInfo.builder("b2", "o1").build())
93+
.update(BlobInfo.builder("b2", "o2").build())
94+
.get("b3", "o1")
95+
.get("b3", "o2")
96+
.build();
97+
BatchRequest requestEquals = BatchRequest.builder()
98+
.delete("b1", "o1")
99+
.delete("b1", "o2")
100+
.update(BlobInfo.builder("b2", "o1").build())
101+
.update(BlobInfo.builder("b2", "o2").build())
102+
.get("b3", "o1")
103+
.get("b3", "o2")
104+
.build();
105+
BatchRequest requestNotEquals1 = BatchRequest.builder()
106+
.delete("b1", "o1")
107+
.delete("b1", "o3")
108+
.update(BlobInfo.builder("b2", "o1").build())
109+
.update(BlobInfo.builder("b2", "o2").build())
110+
.get("b3", "o1")
111+
.get("b3", "o2")
112+
.build();
113+
BatchRequest requestNotEquals2 = BatchRequest.builder()
114+
.delete("b1", "o1")
115+
.delete("b1", "o2")
116+
.update(BlobInfo.builder("b2", "o1").build())
117+
.update(BlobInfo.builder("b2", "o3").build())
118+
.get("b3", "o1")
119+
.get("b3", "o2")
120+
.build();
121+
BatchRequest requestNotEquals3 = BatchRequest.builder()
122+
.delete("b1", "o1")
123+
.delete("b1", "o2")
124+
.update(BlobInfo.builder("b2", "o1").build())
125+
.update(BlobInfo.builder("b2", "o2").build())
126+
.get("b3", "o1")
127+
.get("b3", "o3")
128+
.build();
129+
assertEquals(request, requestEquals);
130+
assertEquals(request.hashCode(), requestEquals.hashCode());
131+
assertNotEquals(request, requestNotEquals1);
132+
assertNotEquals(request.hashCode(), requestNotEquals1.hashCode());
133+
assertNotEquals(request, requestNotEquals2);
134+
assertNotEquals(request.hashCode(), requestNotEquals2.hashCode());
135+
assertNotEquals(request, requestNotEquals3);
136+
assertNotEquals(request.hashCode(), requestNotEquals3.hashCode());
137+
}
85138
}

trunk/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BatchResponseTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.gcloud.storage;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
2021

2122
import com.google.common.collect.ImmutableList;
2223
import com.google.gcloud.storage.BatchResponse.Result;
@@ -34,12 +35,38 @@ public class BatchResponseTest {
3435
@Test
3536
public void testBatchResponse() {
3637
List<Result<Boolean>> deletes = ImmutableList.of(Result.of(true), Result.of(false));
37-
List<Result<BlobInfo>> updates = ImmutableList.of(Result.of(BLOB_INFO_1), Result.of(BLOB_INFO_2));
38+
List<Result<BlobInfo>> updates =
39+
ImmutableList.of(Result.of(BLOB_INFO_1), Result.of(BLOB_INFO_2));
3840
List<Result<BlobInfo>> gets = ImmutableList.of(Result.of(BLOB_INFO_2), Result.of(BLOB_INFO_3));
3941
BatchResponse response = new BatchResponse(deletes, updates, gets);
40-
4142
assertEquals(deletes, response.deletes());
4243
assertEquals(updates, response.updates());
4344
assertEquals(gets, response.gets());
4445
}
46+
47+
@Test
48+
public void testEquals() {
49+
List<Result<Boolean>> deletes = ImmutableList.of(Result.of(true), Result.of(false));
50+
List<Result<BlobInfo>> updates =
51+
ImmutableList.of(Result.of(BLOB_INFO_1), Result.of(BLOB_INFO_2));
52+
List<Result<BlobInfo>> gets = ImmutableList.of(Result.of(BLOB_INFO_2), Result.of(BLOB_INFO_3));
53+
List<Result<Boolean>> otherDeletes = ImmutableList.of(Result.of(false), Result.of(true));
54+
List<Result<BlobInfo>> otherUpdates =
55+
ImmutableList.of(Result.of(BLOB_INFO_2), Result.of(BLOB_INFO_3));
56+
List<Result<BlobInfo>> otherGets =
57+
ImmutableList.of(Result.of(BLOB_INFO_1), Result.of(BLOB_INFO_2));
58+
BatchResponse response = new BatchResponse(deletes, updates, gets);
59+
BatchResponse responseEquals = new BatchResponse(deletes, updates, gets);
60+
BatchResponse responseNotEquals1 = new BatchResponse(otherDeletes, updates, gets);
61+
BatchResponse responseNotEquals2 = new BatchResponse(deletes, otherUpdates, gets);
62+
BatchResponse responseNotEquals3 = new BatchResponse(deletes, updates, otherGets);
63+
assertEquals(response, responseEquals);
64+
assertEquals(response.hashCode(), responseEquals.hashCode());
65+
assertNotEquals(response, responseNotEquals1);
66+
assertNotEquals(response.hashCode(), responseNotEquals1.hashCode());
67+
assertNotEquals(response, responseNotEquals2);
68+
assertNotEquals(response.hashCode(), responseNotEquals2.hashCode());
69+
assertNotEquals(response, responseNotEquals3);
70+
assertNotEquals(response.hashCode(), responseNotEquals3.hashCode());
71+
}
4572
}

trunk/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import com.google.common.collect.ImmutableList;
3030
import com.google.gcloud.storage.BatchResponse.Result;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.InputStream;
3133
import java.util.Collections;
3234
import java.util.Iterator;
3335
import java.util.LinkedList;
@@ -45,6 +47,7 @@ public class BucketTest {
4547
BlobInfo.builder("b", "n1").build(),
4648
BlobInfo.builder("b", "n2").build(),
4749
BlobInfo.builder("b", "n3").build());
50+
private static final String CONTENT_TYPE = "text/plain";
4851

4952
private Storage storage;
5053
private Bucket bucket;
@@ -161,11 +164,43 @@ public void testGetAll() throws Exception {
161164

162165
@Test
163166
public void testCreate() throws Exception {
164-
BlobInfo info = BlobInfo.builder("b", "n").build();
167+
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
168+
byte[] content = {0xD, 0xE, 0xA, 0xD};
169+
expect(storage.create(info, content)).andReturn(info);
170+
replay(storage);
171+
Blob blob = bucket.create("n", content, CONTENT_TYPE);
172+
assertEquals(info, blob.info());
173+
}
174+
175+
@Test
176+
public void testCreateNullContentType() throws Exception {
177+
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
165178
byte[] content = {0xD, 0xE, 0xA, 0xD};
166179
expect(storage.create(info, content)).andReturn(info);
167180
replay(storage);
168-
Blob blob = bucket.create("n", content);
181+
Blob blob = bucket.create("n", content, null);
182+
assertEquals(info, blob.info());
183+
}
184+
185+
@Test
186+
public void testCreateFromStream() throws Exception {
187+
BlobInfo info = BlobInfo.builder("b", "n").contentType(CONTENT_TYPE).build();
188+
byte[] content = {0xD, 0xE, 0xA, 0xD};
189+
InputStream streamContent = new ByteArrayInputStream(content);
190+
expect(storage.create(info, streamContent)).andReturn(info);
191+
replay(storage);
192+
Blob blob = bucket.create("n", streamContent, CONTENT_TYPE);
193+
assertEquals(info, blob.info());
194+
}
195+
196+
@Test
197+
public void testCreateFromStreamNullContentType() throws Exception {
198+
BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build();
199+
byte[] content = {0xD, 0xE, 0xA, 0xD};
200+
InputStream streamContent = new ByteArrayInputStream(content);
201+
expect(storage.create(info, streamContent)).andReturn(info);
202+
replay(storage);
203+
Blob blob = bucket.create("n", streamContent, null);
169204
assertEquals(info, blob.info());
170205
}
171206

0 commit comments

Comments
 (0)