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

Create base class for serialization tests #754

Merged
merged 7 commits into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gcloud-java-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,25 @@
package com.google.gcloud.bigquery;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.BaseSerializationTest;
import com.google.gcloud.RestorableState;
import com.google.gcloud.RetryParams;
import com.google.gcloud.WriteChannel;
import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class SerializationTest {
public class SerializationTest extends BaseSerializationTest {

private static final Acl DOMAIN_ACCESS =
Acl.of(new Acl.Domain("domain"), Acl.Role.WRITER);
Expand Down Expand Up @@ -231,27 +227,18 @@ public class SerializationTest {
private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO));
private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO));

@Test
public void testServiceOptions() throws Exception {
@Override
public Serializable[] serializableObjects() {
BigQueryOptions options = BigQueryOptions.builder()
.projectId("p1")
.authCredentials(AuthCredentials.createForAppEngine())
.build();
BigQueryOptions serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);

options = options.toBuilder()
BigQueryOptions otherOptions = options.toBuilder()
.projectId("p2")
.retryParams(RetryParams.defaultInstance())
.authCredentials(null)
.build();
serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);
}

@Test
public void testModelAndRequests() throws Exception {
Serializable[] objects = {DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID,
return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID,
DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION,
EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO,
EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION, JOB_STATISTICS, EXTRACT_STATISTICS,
Expand All @@ -262,14 +249,7 @@ public void testModelAndRequests() throws Exception {
BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(),
BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(),
BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(),
BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB};
for (Serializable obj : objects) {
Object copy = serializeAndDeserialize(obj);
assertEquals(obj, obj);
assertEquals(obj, copy);
assertNotSame(obj, copy);
assertEquals(copy, copy);
}
BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions};
}

@Test
Expand All @@ -288,17 +268,4 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException {
assertEquals(state.hashCode(), deserializedState.hashCode());
assertEquals(state.toString(), deserializedState.toString());
}

@SuppressWarnings("unchecked")
private <T> T serializeAndDeserialize(T obj)
throws IOException, ClassNotFoundException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (ObjectOutputStream output = new ObjectOutputStream(bytes)) {
output.writeObject(obj);
}
try (ObjectInputStream input =
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
return (T) input.readObject();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.gcloud;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
* Base class for serialization tests. To use this class in your tests override the
* {@code serializableObjects()} method to return all objects that must be serializable.
*/
public abstract class BaseSerializationTest {

public abstract Serializable[] serializableObjects();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


@Test
public void testSerializableObjects() throws Exception {
for (Serializable obj : serializableObjects()) {
Object copy = serializeAndDeserialize(obj);
assertEquals(obj, obj);
assertEquals(obj, copy);
assertEquals(obj.hashCode(), copy.hashCode());
assertEquals(obj.toString(), copy.toString());
assertNotSame(obj, copy);
assertEquals(copy, copy);
}
}

@SuppressWarnings("unchecked")
public <T> T serializeAndDeserialize(T obj)
throws IOException, ClassNotFoundException {

This comment was marked as spam.

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (ObjectOutputStream output = new ObjectOutputStream(bytes)) {
output.writeObject(obj);
}
try (ObjectInputStream input =
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
return (T) input.readObject();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.gcloud;

import com.google.common.collect.ImmutableList;

import java.io.Serializable;

public class SerializationTest extends BaseSerializationTest {

private static final PageImpl<String> PAGE =
new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2"));
private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


@Override
public Serializable[] serializableObjects() {
return new Serializable[]{PAGE, RETRY_PARAMS};
}
}
7 changes: 7 additions & 0 deletions gcloud-java-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,17 @@
package com.google.gcloud.datastore;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.BaseSerializationTest;
import com.google.gcloud.RetryParams;
import com.google.gcloud.datastore.StructuredQuery.CompositeFilter;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
import com.google.gcloud.datastore.StructuredQuery.Projection;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationTest {
public class SerializationTest extends BaseSerializationTest {

private static final IncompleteKey INCOMPLETE_KEY1 =
IncompleteKey.builder("ds", "k").ancestors(PathElement.of("p", 1)).build();
Expand Down Expand Up @@ -114,82 +103,23 @@ public class SerializationTest {
.build();
private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb());

@SuppressWarnings("rawtypes")
private static final Multimap<ValueType, Value> TYPE_TO_VALUES =
ImmutableMultimap.<ValueType, Value>builder()
.put(ValueType.NULL, NULL_VALUE)
.put(ValueType.KEY, KEY_VALUE)
.put(ValueType.STRING, STRING_VALUE)
.putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2,
EMBEDDED_ENTITY_VALUE3)
.put(ValueType.LIST, LIST_VALUE)
.put(ValueType.LONG, LONG_VALUE)
.put(ValueType.DOUBLE, DOUBLE_VALUE)
.put(ValueType.BOOLEAN, BOOLEAN_VALUE)
.put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE)
.put(ValueType.BLOB, BLOB_VALUE)
.put(ValueType.RAW_VALUE, RAW_VALUE)
.build();

@Test
public void testServiceOptions() throws Exception {
@Override
public java.io.Serializable[] serializableObjects() {
DatastoreOptions options = DatastoreOptions.builder()
.authCredentials(AuthCredentials.createForAppEngine())
.normalizeDataset(false)
.projectId("ds1")
.build();
DatastoreOptions serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);

options = options.toBuilder()
DatastoreOptions otherOptions = options.toBuilder()
.namespace("ns1")
.retryParams(RetryParams.defaultInstance())
.authCredentials(null)
.force(true)
.build();
serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);
}

@Test
public void testValues() throws Exception {
for (ValueType valueType : ValueType.values()) {
for (Value<?> value : TYPE_TO_VALUES.get(valueType)) {
Value<?> copy = serializeAndDeserialize(value);
assertEquals(value, value);
assertEquals(value, copy);
assertNotSame(value, copy);
assertEquals(copy, copy);
assertEquals(value.get(), copy.get());
}
}
}

@Test
public void testTypes() throws Exception {
Serializable<?>[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2,
ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2,
QUERY1, QUERY2, QUERY3};
for (Serializable<?> obj : types) {
Object copy = serializeAndDeserialize(obj);
assertEquals(obj, obj);
assertEquals(obj, copy);
assertNotSame(obj, copy);
assertEquals(copy, copy);
}
}

private <T extends java.io.Serializable> T serializeAndDeserialize(T obj)
throws IOException, ClassNotFoundException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (ObjectOutputStream output = new ObjectOutputStream(bytes)) {
output.writeObject(obj);
}
try (ObjectInputStream input =
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
@SuppressWarnings("unchecked")
T result = (T) input.readObject();
return result;
}
return new java.io.Serializable[]{KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1,
ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1,
GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1,
EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE,
BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions};
}
}
7 changes: 7 additions & 0 deletions gcloud-java-resourcemanager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Loading