|
17 | 17 | package com.google.gcloud.datastore; |
18 | 18 |
|
19 | 19 | import static java.nio.charset.StandardCharsets.UTF_8; |
20 | | -import static org.junit.Assert.assertEquals; |
21 | | -import static org.junit.Assert.assertNotSame; |
22 | 20 |
|
23 | 21 | import com.google.api.services.datastore.DatastoreV1; |
24 | | -import com.google.common.collect.ImmutableMultimap; |
25 | | -import com.google.common.collect.Multimap; |
26 | 22 | import com.google.gcloud.AuthCredentials; |
| 23 | +import com.google.gcloud.BaseSerializationTest; |
27 | 24 | import com.google.gcloud.RetryParams; |
28 | 25 | import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; |
29 | 26 | import com.google.gcloud.datastore.StructuredQuery.OrderBy; |
30 | 27 | import com.google.gcloud.datastore.StructuredQuery.Projection; |
31 | 28 | import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; |
32 | 29 |
|
33 | | -import org.junit.Test; |
34 | | - |
35 | | -import java.io.ByteArrayInputStream; |
36 | | -import java.io.ByteArrayOutputStream; |
37 | | -import java.io.IOException; |
38 | | -import java.io.ObjectInputStream; |
39 | | -import java.io.ObjectOutputStream; |
40 | | - |
41 | | -public class SerializationTest { |
| 30 | +public class SerializationTest extends BaseSerializationTest { |
42 | 31 |
|
43 | 32 | private static final IncompleteKey INCOMPLETE_KEY1 = |
44 | 33 | IncompleteKey.builder("ds", "k").ancestors(PathElement.of("p", 1)).build(); |
@@ -114,82 +103,23 @@ public class SerializationTest { |
114 | 103 | .build(); |
115 | 104 | private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); |
116 | 105 |
|
117 | | - @SuppressWarnings("rawtypes") |
118 | | - private static final Multimap<ValueType, Value> TYPE_TO_VALUES = |
119 | | - ImmutableMultimap.<ValueType, Value>builder() |
120 | | - .put(ValueType.NULL, NULL_VALUE) |
121 | | - .put(ValueType.KEY, KEY_VALUE) |
122 | | - .put(ValueType.STRING, STRING_VALUE) |
123 | | - .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, |
124 | | - EMBEDDED_ENTITY_VALUE3) |
125 | | - .put(ValueType.LIST, LIST_VALUE) |
126 | | - .put(ValueType.LONG, LONG_VALUE) |
127 | | - .put(ValueType.DOUBLE, DOUBLE_VALUE) |
128 | | - .put(ValueType.BOOLEAN, BOOLEAN_VALUE) |
129 | | - .put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE) |
130 | | - .put(ValueType.BLOB, BLOB_VALUE) |
131 | | - .put(ValueType.RAW_VALUE, RAW_VALUE) |
132 | | - .build(); |
133 | | - |
134 | | - @Test |
135 | | - public void testServiceOptions() throws Exception { |
| 106 | + @Override |
| 107 | + public java.io.Serializable[] serializableObjects() { |
136 | 108 | DatastoreOptions options = DatastoreOptions.builder() |
137 | 109 | .authCredentials(AuthCredentials.createForAppEngine()) |
138 | 110 | .normalizeDataset(false) |
139 | 111 | .projectId("ds1") |
140 | 112 | .build(); |
141 | | - DatastoreOptions serializedCopy = serializeAndDeserialize(options); |
142 | | - assertEquals(options, serializedCopy); |
143 | | - |
144 | | - options = options.toBuilder() |
| 113 | + DatastoreOptions otherOptions = options.toBuilder() |
145 | 114 | .namespace("ns1") |
146 | 115 | .retryParams(RetryParams.defaultInstance()) |
147 | 116 | .authCredentials(null) |
148 | 117 | .force(true) |
149 | 118 | .build(); |
150 | | - serializedCopy = serializeAndDeserialize(options); |
151 | | - assertEquals(options, serializedCopy); |
152 | | - } |
153 | | - |
154 | | - @Test |
155 | | - public void testValues() throws Exception { |
156 | | - for (ValueType valueType : ValueType.values()) { |
157 | | - for (Value<?> value : TYPE_TO_VALUES.get(valueType)) { |
158 | | - Value<?> copy = serializeAndDeserialize(value); |
159 | | - assertEquals(value, value); |
160 | | - assertEquals(value, copy); |
161 | | - assertNotSame(value, copy); |
162 | | - assertEquals(copy, copy); |
163 | | - assertEquals(value.get(), copy.get()); |
164 | | - } |
165 | | - } |
166 | | - } |
167 | | - |
168 | | - @Test |
169 | | - public void testTypes() throws Exception { |
170 | | - Serializable<?>[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, |
171 | | - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, |
172 | | - QUERY1, QUERY2, QUERY3}; |
173 | | - for (Serializable<?> obj : types) { |
174 | | - Object copy = serializeAndDeserialize(obj); |
175 | | - assertEquals(obj, obj); |
176 | | - assertEquals(obj, copy); |
177 | | - assertNotSame(obj, copy); |
178 | | - assertEquals(copy, copy); |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - private <T extends java.io.Serializable> T serializeAndDeserialize(T obj) |
183 | | - throws IOException, ClassNotFoundException { |
184 | | - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
185 | | - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { |
186 | | - output.writeObject(obj); |
187 | | - } |
188 | | - try (ObjectInputStream input = |
189 | | - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { |
190 | | - @SuppressWarnings("unchecked") |
191 | | - T result = (T) input.readObject(); |
192 | | - return result; |
193 | | - } |
| 119 | + return new java.io.Serializable[]{KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, |
| 120 | + ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, |
| 121 | + GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1, |
| 122 | + EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, |
| 123 | + BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; |
194 | 124 | } |
195 | 125 | } |
0 commit comments