Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make the StorageOption returned by LocalStorageHelper.getOptions() serializable #606

Merged
merged 3 commits into from Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
}