From 12e872b00cfefc0949e09918bcdbcd5517c750d9 Mon Sep 17 00:00:00 2001 From: Lai Jiang Date: Thu, 1 Jul 2021 12:10:19 -0400 Subject: [PATCH] fix: Make the StorageOption returned by LocalStorageHelper.getOptions() serializable (#606) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-storage-nio/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #605 ☕️ --- .../nio/testing/LocalStorageHelper.java | 36 ++++++++++------- .../nio/testing/LocalStorageHelperTest.java | 39 +++++++++++++++++++ 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java index d0ec92aa..436b9cf9 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java @@ -16,9 +16,9 @@ package com.google.cloud.storage.contrib.nio.testing; +import com.google.cloud.ServiceRpc; import com.google.cloud.spi.ServiceRpcFactory; import com.google.cloud.storage.StorageOptions; -import com.google.cloud.storage.spi.v1.StorageRpc; /** * Utility to create an in-memory storage configuration for testing. Storage options can be obtained @@ -71,13 +71,7 @@ public static StorageOptions getOptions() { instance.reset(); return StorageOptions.newBuilder() .setProjectId("fake-project-for-testing") - .setServiceRpcFactory( - new ServiceRpcFactory() { - @Override - public StorageRpc create(StorageOptions options) { - return instance; - } - }) + .setServiceRpcFactory(new FakeStorageRpcFactory()) .build(); } @@ -88,13 +82,25 @@ public StorageRpc create(StorageOptions options) { public static StorageOptions customOptions(final boolean throwIfOptions) { return StorageOptions.newBuilder() .setProjectId("fake-project-for-testing") - .setServiceRpcFactory( - new ServiceRpcFactory() { - @Override - public StorageRpc create(StorageOptions options) { - return new FakeStorageRpc(throwIfOptions); - } - }) + .setServiceRpcFactory(new FakeStorageRpcFactory(new FakeStorageRpc(throwIfOptions))) .build(); } + + public static class FakeStorageRpcFactory implements ServiceRpcFactory { + + private final FakeStorageRpc instance; + + public FakeStorageRpcFactory() { + this(LocalStorageHelper.instance); + } + + public FakeStorageRpcFactory(FakeStorageRpc instance) { + this.instance = instance; + } + + @Override + public ServiceRpc create(StorageOptions storageOptions) { + return instance; + } + } } diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java index 8c29ac40..0f186117 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java @@ -23,8 +23,13 @@ import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.nio.ByteBuffer; import java.nio.file.Files; import org.junit.After; @@ -117,4 +122,38 @@ public void testCreateNewFileSetsUpdateTime() { assertThat(obj.getUpdateTime()).isNotNull(); } + + @Test + public void testStorageOptionIsSerializable() throws Exception { + StorageOptions storageOptions = LocalStorageHelper.getOptions(); + byte[] bytes; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(storageOptions); + oos.flush(); + oos.close(); + bytes = baos.toByteArray(); + } + try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInputStream ois = new ObjectInputStream(bais)) { + assertThat(ois.readObject()).isEqualTo(storageOptions); + } + } + + @Test + public void testStorageOptionIsSerializable_customOptions() throws Exception { + StorageOptions storageOptions = LocalStorageHelper.customOptions(false); + byte[] bytes; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(storageOptions); + oos.flush(); + oos.close(); + bytes = baos.toByteArray(); + } + try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInputStream ois = new ObjectInputStream(bais)) { + assertThat(ois.readObject()).isEqualTo(storageOptions); + } + } }