Skip to content

Commit 0d2446e

Browse files
committed
Add serialVersionUID and readResolve method to service factories. Updated tests.
1 parent 09e03e0 commit 0d2446e

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreFactory.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@
1717
package com.google.gcloud.datastore;
1818

1919
import com.google.gcloud.ServiceFactory;
20+
import java.io.ObjectStreamException;
2021

2122
/**
2223
* A base class for Datastore factories.
2324
*/
2425
public abstract class DatastoreFactory implements ServiceFactory<Datastore, DatastoreOptions> {
2526

27+
private static final long serialVersionUID = 5037190305022535983L;
28+
2629
private static final DatastoreFactory INSTANCE = new DatastoreFactory() {
27-
@Override
28-
public Datastore get(DatastoreOptions options) {
29-
return new DatastoreImpl(options, this);
30-
}
31-
};
30+
31+
private static final long serialVersionUID = 5893914895344559491L;
32+
33+
@Override
34+
public Datastore get(DatastoreOptions options) {
35+
return new DatastoreImpl(options, this);
36+
}
37+
38+
private Object readResolve() throws ObjectStreamException {
39+
return INSTANCE;
40+
}
41+
};
3242

3343
/**
3444
* Returns the default factory instance.

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public class SerializationTest {
135135
@Test
136136
public void testStorageFactory() throws Exception {
137137
DatastoreFactory serializedCopy = serializeAndDeserialize(DATASTORE_FACTORY);
138-
assertNotSame(DATASTORE_FACTORY, serializedCopy);
138+
assertEquals(DATASTORE_FACTORY, serializedCopy);
139+
assertEquals(DATASTORE_FACTORY.hashCode(), serializedCopy.hashCode());
139140
}
140141

141142
@Test

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,27 @@
1717
package com.google.gcloud.storage;
1818

1919
import com.google.gcloud.ServiceFactory;
20+
import java.io.ObjectStreamException;
2021

2122
/**
2223
* A base class for Storage factories.
2324
*/
2425
public abstract class StorageFactory implements ServiceFactory<Storage, StorageOptions> {
2526

27+
private static final long serialVersionUID = 1866883249985063753L;
28+
2629
private static final StorageFactory INSTANCE = new StorageFactory() {
30+
31+
private static final long serialVersionUID = -7985210081064222485L;
32+
2733
@Override
2834
public Storage get(StorageOptions options) {
2935
return new StorageImpl(options, this);
3036
}
37+
38+
private Object readResolve() throws ObjectStreamException {
39+
return INSTANCE;
40+
}
3141
};
3242

3343
/**

gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public class SerializationTest {
6969
@Test
7070
public void testStorageFactory() throws Exception {
7171
StorageFactory serializedCopy = serializeAndDeserialize(STORAGE_FACTORY);
72-
assertNotSame(STORAGE_FACTORY, serializedCopy);
72+
assertEquals(STORAGE_FACTORY, serializedCopy);
73+
assertEquals(STORAGE_FACTORY.hashCode(), serializedCopy.hashCode());
7374
}
7475

7576
@Test

0 commit comments

Comments
 (0)