Skip to content

Commit

Permalink
fix: Make the StorageOption returned by LocalStorageHelper.getOptions…
Browse files Browse the repository at this point in the history
…() serializable (#606)

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 ☕️
  • Loading branch information
jianglai committed Jul 1, 2021
1 parent 60782fa commit 12e872b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -71,13 +71,7 @@ public static StorageOptions getOptions() {
instance.reset();
return StorageOptions.newBuilder()
.setProjectId("fake-project-for-testing")
.setServiceRpcFactory(
new ServiceRpcFactory<StorageOptions>() {
@Override
public StorageRpc create(StorageOptions options) {
return instance;
}
})
.setServiceRpcFactory(new FakeStorageRpcFactory())
.build();
}

Expand All @@ -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<StorageOptions>() {
@Override
public StorageRpc create(StorageOptions options) {
return new FakeStorageRpc(throwIfOptions);
}
})
.setServiceRpcFactory(new FakeStorageRpcFactory(new FakeStorageRpc(throwIfOptions)))
.build();
}

public static class FakeStorageRpcFactory implements ServiceRpcFactory<StorageOptions> {

private final FakeStorageRpc instance;

public FakeStorageRpcFactory() {
this(LocalStorageHelper.instance);
}

public FakeStorageRpcFactory(FakeStorageRpc instance) {
this.instance = instance;
}

@Override
public ServiceRpc create(StorageOptions storageOptions) {
return instance;
}
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 12e872b

Please sign in to comment.