| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d75f1f4 commit cbe2634
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -156,16 +156,18 @@ public Blob(Storage storage, BlobInfo info) { | |||
| 156 | 156 | ||
| 157 | 157 | /** | |
| 158 | 158 | * Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to | |
| 159 | - * get the latest blob information. | ||
| 159 | + * get the latest blob information. Returns {@code null} if the blob does not exist. | ||
| 160 | 160 | * | |
| 161 | 161 | * @param storage the storage service used for issuing requests | |
| 162 | 162 | * @param bucket bucket's name | |
| 163 | + * @param options blob get options | ||
| 163 | 164 | * @param blob blob's name | |
| 164 | - * @return the {@code Blob} object or {@code null} if not found. | ||
| 165 | + * @return the {@code Blob} object or {@code null} if not found | ||
| 165 | 166 | * @throws StorageException upon failure | |
| 166 | 167 | */ | |
| 167 | - public static Blob load(Storage storage, String bucket, String blob) { | ||
| 168 | - return load(storage, BlobId.of(bucket, blob)); | ||
| 168 | + public static Blob load(Storage storage, String bucket, String blob, | ||
| 169 | + Storage.BlobGetOption... options) { | ||
| 170 | + return load(storage, BlobId.of(bucket, blob), options); | ||
| 169 | 171 | } | |
| 170 | 172 | ||
| 171 | 173 | /** | |
@@ -174,11 +176,12 @@ public static Blob load(Storage storage, String bucket, String blob) { | |||
| 174 | 176 | * | |
| 175 | 177 | * @param storage the storage service used for issuing requests | |
| 176 | 178 | * @param blobId blob's identifier | |
| 177 | - * @return the {@code Blob} object or {@code null} if not found. | ||
| 179 | + * @param options blob get options | ||
| 180 | + * @return the {@code Blob} object or {@code null} if not found | ||
| 178 | 181 | * @throws StorageException upon failure | |
| 179 | 182 | */ | |
| 180 | - public static Blob load(Storage storage, BlobId blobId) { | ||
| 181 | - BlobInfo info = storage.get(blobId); | ||
| 183 | + public static Blob load(Storage storage, BlobId blobId, Storage.BlobGetOption... options) { | ||
| 184 | + BlobInfo info = storage.get(blobId, options); | ||
| 182 | 185 | return info != null ? new Blob(storage, info) : null; | |
| 183 | 186 | } | |
| 184 | 187 | ||
@@ -221,14 +224,14 @@ public byte[] content(Storage.BlobSourceOption... options) { | |||
| 221 | 224 | } | |
| 222 | 225 | ||
| 223 | 226 | /** | |
| 224 | - * Fetches current blob's latest information. | ||
| 227 | + * Fetches current blob's latest information. Returns {@code null} if the blob does not exist. | ||
| 225 | 228 | * | |
| 226 | 229 | * @param options blob read options | |
| 227 | - * @return a {@code Blob} object with latest information | ||
| 230 | + * @return a {@code Blob} object with latest information or {@code null} if not found | ||
| 228 | 231 | * @throws StorageException upon failure | |
| 229 | 232 | */ | |
| 230 | 233 | public Blob reload(BlobSourceOption... options) { | |
| 231 | - return new Blob(storage, storage.get(info.blobId(), toGetOptions(info, options))); | ||
| 234 | + return Blob.load(storage, info.blobId(), toGetOptions(info, options)); | ||
| 232 | 235 | } | |
| 233 | 236 | ||
| 234 | 237 | /** | |
@@ -368,7 +371,7 @@ public Storage storage() { | |||
| 368 | 371 | * @param storage the storage service used to issue the request | |
| 369 | 372 | * @param blobs the blobs to get | |
| 370 | 373 | * @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has | |
| 371 | - * been denied the corresponding item in the list is {@code null}. | ||
| 374 | + * been denied the corresponding item in the list is {@code null} | ||
| 372 | 375 | * @throws StorageException upon failure | |
| 373 | 376 | */ | |
| 374 | 377 | public static List<Blob> get(final Storage storage, BlobId... blobs) { | |
@@ -397,7 +400,7 @@ public Blob apply(BlobInfo blobInfo) { | |||
| 397 | 400 | * @param storage the storage service used to issue the request | |
| 398 | 401 | * @param infos the blobs to update | |
| 399 | 402 | * @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has | |
| 400 | - * been denied the corresponding item in the list is {@code null}. | ||
| 403 | + * been denied the corresponding item in the list is {@code null} | ||
| 401 | 404 | * @throws StorageException upon failure | |
| 402 | 405 | */ | |
| 403 | 406 | public static List<Blob> update(final Storage storage, BlobInfo... infos) { | |
@@ -422,7 +425,7 @@ public Blob apply(BlobInfo blobInfo) { | |||
| 422 | 425 | * @param blobs the blobs to delete | |
| 423 | 426 | * @return an immutable list of booleans. If a blob has been deleted the corresponding item in the | |
| 424 | 427 | * list is {@code true}. If a blob was not found, deletion failed or access to the resource | |
| 425 | - * was denied the corresponding item is {@code false}. | ||
| 428 | + * was denied the corresponding item is {@code false} | ||
| 426 | 429 | * @throws StorageException upon failure | |
| 427 | 430 | */ | |
| 428 | 431 | public static List<Boolean> delete(Storage storage, BlobId... blobs) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -210,11 +210,12 @@ public Bucket(Storage storage, BucketInfo info) { | |||
| 210 | 210 | * | |
| 211 | 211 | * @param storage the storage service used for issuing requests | |
| 212 | 212 | * @param bucket bucket's name | |
| 213 | - * @return the {@code Bucket} object or {@code null} if not found. | ||
| 213 | + * @param options blob get options | ||
| 214 | + * @return the {@code Bucket} object or {@code null} if not found | ||
| 214 | 215 | * @throws StorageException upon failure | |
| 215 | 216 | */ | |
| 216 | - public static Bucket load(Storage storage, String bucket) { | ||
| 217 | - BucketInfo info = storage.get(bucket); | ||
| 217 | + public static Bucket load(Storage storage, String bucket, Storage.BucketGetOption... options) { | ||
| 218 | + BucketInfo info = storage.get(bucket, options); | ||
| 218 | 219 | return info != null ? new Bucket(storage, info) : null; | |
| 219 | 220 | } | |
| 220 | 221 | ||
@@ -239,14 +240,14 @@ public boolean exists(BucketSourceOption... options) { | |||
| 239 | 240 | } | |
| 240 | 241 | ||
| 241 | 242 | /** | |
| 242 | - * Fetches current bucket's latest information. | ||
| 243 | + * Fetches current bucket's latest information. Returns {@code null} if the bucket does not exist. | ||
| 243 | 244 | * | |
| 244 | 245 | * @param options bucket read options | |
| 245 | - * @return a {@code Bucket} object with latest information | ||
| 246 | + * @return a {@code Bucket} object with latest information or {@code null} if not found | ||
| 246 | 247 | * @throws StorageException upon failure | |
| 247 | 248 | */ | |
| 248 | 249 | public Bucket reload(BucketSourceOption... options) { | |
| 249 | - return new Bucket(storage, storage.get(info.name(), toGetOptions(info, options))); | ||
| 250 | + return Bucket.load(storage, info.name(), toGetOptions(info, options)); | ||
| 250 | 251 | } | |
| 251 | 252 | ||
| 252 | 253 | /** | |
@@ -307,7 +308,7 @@ public Blob get(String blob, BlobGetOption... options) { | |||
| 307 | 308 | * @param blobName1 first blob to get | |
| 308 | 309 | * @param blobName2 second blob to get | |
| 309 | 310 | * @param blobNames other blobs to get | |
| 310 | - * @return an immutable list of {@code Blob} objects. | ||
| 311 | + * @return an immutable list of {@code Blob} objects | ||
| 311 | 312 | * @throws StorageException upon failure | |
| 312 | 313 | */ | |
| 313 | 314 | public List<Blob> get(String blobName1, String blobName2, String... blobNames) { | |
@@ -337,7 +338,7 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) { | |||
| 337 | 338 | * @param contentType the blob content type. If {@code null} then | |
| 338 | 339 | * {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used. | |
| 339 | 340 | * @param options options for blob creation | |
| 340 | - * @return a complete blob information. | ||
| 341 | + * @return a complete blob information | ||
| 341 | 342 | * @throws StorageException upon failure | |
| 342 | 343 | */ | |
| 343 | 344 | public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) { | |
@@ -356,7 +357,7 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp | |||
| 356 | 357 | * @param contentType the blob content type. If {@code null} then | |
| 357 | 358 | * {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used. | |
| 358 | 359 | * @param options options for blob creation | |
| 359 | - * @return a complete blob information. | ||
| 360 | + * @return a complete blob information | ||
| 360 | 361 | * @throws StorageException upon failure | |
| 361 | 362 | */ | |
| 362 | 363 | public Blob create(String blob, InputStream content, String contentType, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,7 +45,7 @@ | |||
| 45 | 45 | ||
| 46 | 46 | public class BlobTest { | |
| 47 | 47 | ||
| 48 | - private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").build(); | ||
| 48 | + private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").metageneration(42L).build(); | ||
| 49 | 49 | private static final BlobId[] BLOB_ID_ARRAY = {BlobId.of("b1", "n1"), | |
| 50 | 50 | BlobId.of("b2", "n2"), BlobId.of("b3", "n3")}; | |
| 51 | 51 | private static final BlobInfo[] BLOB_INFO_ARRAY = {BlobInfo.builder("b1", "n1").build(), | |
@@ -105,6 +105,24 @@ public void testReload() throws Exception { | |||
| 105 | 105 | assertEquals(updatedInfo, updatedBlob.info()); | |
| 106 | 106 | } | |
| 107 | 107 | ||
| 108 | + @Test | ||
| 109 | + public void testReloadNull() throws Exception { | ||
| 110 | + expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(null); | ||
| 111 | + replay(storage); | ||
| 112 | + assertNull(blob.reload()); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + @Test | ||
| 116 | + public void testReloadWithOptions() throws Exception { | ||
| 117 | + BlobInfo updatedInfo = BLOB_INFO.toBuilder().cacheControl("c").build(); | ||
| 118 | + Storage.BlobGetOption[] options = {Storage.BlobGetOption.metagenerationMatch(42L)}; | ||
| 119 | + expect(storage.get(BLOB_INFO.blobId(), options)).andReturn(updatedInfo); | ||
| 120 | + replay(storage); | ||
| 121 | + Blob updatedBlob = blob.reload(Blob.BlobSourceOption.metagenerationMatch()); | ||
| 122 | + assertSame(storage, updatedBlob.storage()); | ||
| 123 | + assertEquals(updatedInfo, updatedBlob.info()); | ||
| 124 | + } | ||
| 125 | + | ||
| 108 | 126 | @Test | |
| 109 | 127 | public void testUpdate() throws Exception { | |
| 110 | 128 | BlobInfo updatedInfo = BLOB_INFO.toBuilder().cacheControl("c").build(); | |
@@ -285,18 +303,53 @@ public void testDeleteSome() throws Exception { | |||
| 285 | 303 | ||
| 286 | 304 | @Test | |
| 287 | 305 | public void testLoadFromString() throws Exception { | |
| 288 | - expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO); | ||
| 306 | + expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(BLOB_INFO); | ||
| 289 | 307 | replay(storage); | |
| 290 | 308 | Blob loadedBlob = Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name()); | |
| 291 | 309 | assertEquals(BLOB_INFO, loadedBlob.info()); | |
| 292 | 310 | } | |
| 293 | 311 | ||
| 294 | 312 | @Test | |
| 295 | 313 | public void testLoadFromId() throws Exception { | |
| 296 | - expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO); | ||
| 314 | + expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(BLOB_INFO); | ||
| 297 | 315 | replay(storage); | |
| 298 | 316 | Blob loadedBlob = Blob.load(storage, BLOB_INFO.blobId()); | |
| 299 | 317 | assertNotNull(loadedBlob); | |
| 300 | 318 | assertEquals(BLOB_INFO, loadedBlob.info()); | |
| 301 | 319 | } | |
| 320 | + | ||
| 321 | + @Test | ||
| 322 | + public void testLoadFromStringNull() throws Exception { | ||
| 323 | + expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(null); | ||
| 324 | + replay(storage); | ||
| 325 | + assertNull(Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name())); | ||
| 326 | + } | ||
| 327 | + | ||
| 328 | + @Test | ||
| 329 | + public void testLoadFromIdNull() throws Exception { | ||
| 330 | + expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(null); | ||
| 331 | + replay(storage); | ||
| 332 | + assertNull(Blob.load(storage, BLOB_INFO.blobId())); | ||
| 333 | + } | ||
| 334 | + | ||
| 335 | + @Test | ||
| 336 | + public void testLoadFromStringWithOptions() throws Exception { | ||
| 337 | + expect(storage.get(BLOB_INFO.blobId(), Storage.BlobGetOption.generationMatch(42L))) | ||
| 338 | + .andReturn(BLOB_INFO); | ||
| 339 | + replay(storage); | ||
| 340 | + Blob loadedBlob = Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name(), | ||
| 341 | + Storage.BlobGetOption.generationMatch(42L)); | ||
| 342 | + assertEquals(BLOB_INFO, loadedBlob.info()); | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + @Test | ||
| 346 | + public void testLoadFromIdWithOptions() throws Exception { | ||
| 347 | + expect(storage.get(BLOB_INFO.blobId(), Storage.BlobGetOption.generationMatch(42L))) | ||
| 348 | + .andReturn(BLOB_INFO); | ||
| 349 | + replay(storage); | ||
| 350 | + Blob loadedBlob = | ||
| 351 | + Blob.load(storage, BLOB_INFO.blobId(), Storage.BlobGetOption.generationMatch(42L)); | ||
| 352 | + assertNotNull(loadedBlob); | ||
| 353 | + assertEquals(BLOB_INFO, loadedBlob.info()); | ||
| 354 | + } | ||
| 302 | 355 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ | |||
| 24 | 24 | import static org.junit.Assert.assertEquals; | |
| 25 | 25 | import static org.junit.Assert.assertFalse; | |
| 26 | 26 | import static org.junit.Assert.assertNotNull; | |
| 27 | + import static org.junit.Assert.assertNull; | ||
| 27 | 28 | import static org.junit.Assert.assertSame; | |
| 28 | 29 | import static org.junit.Assert.assertTrue; | |
| 29 | 30 | ||
@@ -47,7 +48,7 @@ | |||
| 47 | 48 | ||
| 48 | 49 | public class BucketTest { | |
| 49 | 50 | ||
| 50 | - private static final BucketInfo BUCKET_INFO = BucketInfo.of("b"); | ||
| 51 | + private static final BucketInfo BUCKET_INFO = BucketInfo.builder("b").metageneration(42L).build(); | ||
| 51 | 52 | private static final Iterable<BlobInfo> BLOB_INFO_RESULTS = ImmutableList.of( | |
| 52 | 53 | BlobInfo.builder("b", "n1").build(), | |
| 53 | 54 | BlobInfo.builder("b", "n2").build(), | |
@@ -100,6 +101,24 @@ public void testReload() throws Exception { | |||
| 100 | 101 | assertEquals(updatedInfo, updatedBucket.info()); | |
| 101 | 102 | } | |
| 102 | 103 | ||
| 104 | + @Test | ||
| 105 | + public void testReloadNull() throws Exception { | ||
| 106 | + expect(storage.get(BUCKET_INFO.name())).andReturn(null); | ||
| 107 | + replay(storage); | ||
| 108 | + assertNull(bucket.reload()); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Test | ||
| 112 | + public void testReloadWithOptions() throws Exception { | ||
| 113 | + BucketInfo updatedInfo = BUCKET_INFO.toBuilder().notFoundPage("p").build(); | ||
| 114 | + expect(storage.get(updatedInfo.name(), Storage.BucketGetOption.metagenerationMatch(42L))) | ||
| 115 | + .andReturn(updatedInfo); | ||
| 116 | + replay(storage); | ||
| 117 | + Bucket updatedBucket = bucket.reload(Bucket.BucketSourceOption.metagenerationMatch()); | ||
| 118 | + assertSame(storage, updatedBucket.storage()); | ||
| 119 | + assertEquals(updatedInfo, updatedBucket.info()); | ||
| 120 | + } | ||
| 121 | + | ||
| 103 | 122 | @Test | |
| 104 | 123 | public void testUpdate() throws Exception { | |
| 105 | 124 | BucketInfo updatedInfo = BUCKET_INFO.toBuilder().notFoundPage("p").build(); | |
@@ -223,4 +242,22 @@ public void testLoad() throws Exception { | |||
| 223 | 242 | assertNotNull(loadedBucket); | |
| 224 | 243 | assertEquals(BUCKET_INFO, loadedBucket.info()); | |
| 225 | 244 | } | |
| 245 | + | ||
| 246 | + @Test | ||
| 247 | + public void testLoadNull() throws Exception { | ||
| 248 | + expect(storage.get(BUCKET_INFO.name())).andReturn(null); | ||
| 249 | + replay(storage); | ||
| 250 | + assertNull(Bucket.load(storage, BUCKET_INFO.name())); | ||
| 251 | + } | ||
| 252 | + | ||
| 253 | + @Test | ||
| 254 | + public void testLoadWithOptions() throws Exception { | ||
| 255 | + expect(storage.get(BUCKET_INFO.name(), Storage.BucketGetOption.fields())) | ||
| 256 | + .andReturn(BUCKET_INFO); | ||
| 257 | + replay(storage); | ||
| 258 | + Bucket loadedBucket = | ||
| 259 | + Bucket.load(storage, BUCKET_INFO.name(), Storage.BucketGetOption.fields()); | ||
| 260 | + assertNotNull(loadedBucket); | ||
| 261 | + assertEquals(BUCKET_INFO, loadedBucket.info()); | ||
| 262 | + } | ||
| 226 | 263 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments