Skip to content

Commit 548ef8c

Browse files
authored
fix: Make the StorageOption returned by LocalStorageHelper.getOptions() 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 ☕️
1 parent 60bb3f6 commit 548ef8c

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.cloud.storage.contrib.nio.testing;
1818

19+
import com.google.cloud.ServiceRpc;
1920
import com.google.cloud.spi.ServiceRpcFactory;
2021
import com.google.cloud.storage.StorageOptions;
21-
import com.google.cloud.storage.spi.v1.StorageRpc;
2222

2323
/**
2424
* Utility to create an in-memory storage configuration for testing. Storage options can be obtained
@@ -71,13 +71,7 @@ public static StorageOptions getOptions() {
7171
instance.reset();
7272
return StorageOptions.newBuilder()
7373
.setProjectId("fake-project-for-testing")
74-
.setServiceRpcFactory(
75-
new ServiceRpcFactory<StorageOptions>() {
76-
@Override
77-
public StorageRpc create(StorageOptions options) {
78-
return instance;
79-
}
80-
})
74+
.setServiceRpcFactory(new FakeStorageRpcFactory())
8175
.build();
8276
}
8377

@@ -88,13 +82,25 @@ public StorageRpc create(StorageOptions options) {
8882
public static StorageOptions customOptions(final boolean throwIfOptions) {
8983
return StorageOptions.newBuilder()
9084
.setProjectId("fake-project-for-testing")
91-
.setServiceRpcFactory(
92-
new ServiceRpcFactory<StorageOptions>() {
93-
@Override
94-
public StorageRpc create(StorageOptions options) {
95-
return new FakeStorageRpc(throwIfOptions);
96-
}
97-
})
85+
.setServiceRpcFactory(new FakeStorageRpcFactory(new FakeStorageRpc(throwIfOptions)))
9886
.build();
9987
}
88+
89+
public static class FakeStorageRpcFactory implements ServiceRpcFactory<StorageOptions> {
90+
91+
private final FakeStorageRpc instance;
92+
93+
public FakeStorageRpcFactory() {
94+
this(LocalStorageHelper.instance);
95+
}
96+
97+
public FakeStorageRpcFactory(FakeStorageRpc instance) {
98+
this.instance = instance;
99+
}
100+
101+
@Override
102+
public ServiceRpc create(StorageOptions storageOptions) {
103+
return instance;
104+
}
105+
}
100106
}

java-storage-nio/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
import com.google.cloud.storage.BlobId;
2424
import com.google.cloud.storage.BlobInfo;
2525
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageOptions;
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
2629
import java.io.File;
2730
import java.io.IOException;
31+
import java.io.ObjectInputStream;
32+
import java.io.ObjectOutputStream;
2833
import java.nio.ByteBuffer;
2934
import java.nio.file.Files;
3035
import org.junit.After;
@@ -117,4 +122,38 @@ public void testCreateNewFileSetsUpdateTime() {
117122

118123
assertThat(obj.getUpdateTime()).isNotNull();
119124
}
125+
126+
@Test
127+
public void testStorageOptionIsSerializable() throws Exception {
128+
StorageOptions storageOptions = LocalStorageHelper.getOptions();
129+
byte[] bytes;
130+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
131+
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
132+
oos.writeObject(storageOptions);
133+
oos.flush();
134+
oos.close();
135+
bytes = baos.toByteArray();
136+
}
137+
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
138+
ObjectInputStream ois = new ObjectInputStream(bais)) {
139+
assertThat(ois.readObject()).isEqualTo(storageOptions);
140+
}
141+
}
142+
143+
@Test
144+
public void testStorageOptionIsSerializable_customOptions() throws Exception {
145+
StorageOptions storageOptions = LocalStorageHelper.customOptions(false);
146+
byte[] bytes;
147+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
148+
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
149+
oos.writeObject(storageOptions);
150+
oos.flush();
151+
oos.close();
152+
bytes = baos.toByteArray();
153+
}
154+
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
155+
ObjectInputStream ois = new ObjectInputStream(bais)) {
156+
assertThat(ois.readObject()).isEqualTo(storageOptions);
157+
}
158+
}
120159
}

0 commit comments

Comments
 (0)