Skip to content

Commit

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

Fixes #605
  • Loading branch information
jianglai committed Jul 1, 2021
1 parent 60782fa commit 58cc461
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Expand Up @@ -16,6 +16,7 @@

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;
Expand Down Expand Up @@ -71,13 +72,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 @@ -97,4 +92,11 @@ public StorageRpc create(StorageOptions options) {
})
.build();
}

public static class FakeStorageRpcFactory implements ServiceRpcFactory<StorageOptions> {
@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,21 @@ 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);
}
}
}

0 comments on commit 58cc461

Please sign in to comment.