Skip to content

Commit dea8eff

Browse files
committed
Add SerializationTest class
1 parent 3fc189b commit dea8eff

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.bigquery;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotSame;
21+
22+
import com.google.common.collect.ImmutableList;
23+
import com.google.gcloud.AuthCredentials;
24+
import com.google.gcloud.RetryParams;
25+
26+
import org.junit.Test;
27+
28+
import java.io.ByteArrayInputStream;
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
31+
import java.io.ObjectInputStream;
32+
import java.io.ObjectOutputStream;
33+
import java.io.Serializable;
34+
import java.util.List;
35+
36+
public class SerializationTest {
37+
38+
private static final Acl DOMAIN_ACCESS =
39+
new Acl(new Acl.Domain("domain"), Acl.Role.WRITER);
40+
private static final Acl GROUP_ACCESS =
41+
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER);
42+
private static final Acl USER_ACCESS = new Acl(new Acl.User("user"), Acl.Role.OWNER);
43+
private static final Acl VIEW_ACCESS =
44+
new Acl(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER);
45+
private static final List<Acl> ACCESS_RULES = ImmutableList.of(DOMAIN_ACCESS, GROUP_ACCESS,
46+
VIEW_ACCESS, USER_ACCESS);
47+
private static final Long CREATION_TIME = System.currentTimeMillis() - 10;
48+
private static final Long DEFAULT_TABLE_EXPIRATION = 100L;
49+
private static final String DESCRIPTION = "Description";
50+
private static final String ETAG = "0xFF00";
51+
private static final String FRIENDLY_NAME = "friendlyDataset";
52+
private static final String ID = "P/D:1";
53+
private static final Long LAST_MODIFIED = CREATION_TIME + 50;
54+
private static final String LOCATION = "";
55+
private static final String SELF_LINK = "http://bigquery/p/d";
56+
private static final DatasetId DATASET_ID = DatasetId.of("project", "dataset");
57+
private static final DatasetInfo DATASET_INFO = DatasetInfo.builder(DATASET_ID)
58+
.acl(ACCESS_RULES)
59+
.creationTime(CREATION_TIME)
60+
.defaultTableLifetime(DEFAULT_TABLE_EXPIRATION)
61+
.description(DESCRIPTION)
62+
.etag(ETAG)
63+
.friendlyName(FRIENDLY_NAME)
64+
.id(ID)
65+
.lastModified(LAST_MODIFIED)
66+
.location(LOCATION)
67+
.selfLink(SELF_LINK)
68+
.build();
69+
private static final TableId TABLE_ID = TableId.of("project", "dataset", "table");
70+
71+
@Test
72+
public void testServiceOptions() throws Exception {
73+
BigQueryOptions options = BigQueryOptions.builder()
74+
.projectId("p1")
75+
.authCredentials(AuthCredentials.createForAppEngine())
76+
.build();
77+
BigQueryOptions serializedCopy = serializeAndDeserialize(options);
78+
assertEquals(options, serializedCopy);
79+
80+
options = options.toBuilder()
81+
.projectId("p2")
82+
.retryParams(RetryParams.getDefaultInstance())
83+
.authCredentials(AuthCredentials.noCredentials())
84+
.build();
85+
serializedCopy = serializeAndDeserialize(options);
86+
assertEquals(options, serializedCopy);
87+
}
88+
89+
@Test
90+
public void testModelAndRequests() throws Exception {
91+
Serializable[] objects = {DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID,
92+
DATASET_INFO, TABLE_ID};
93+
for (Serializable obj : objects) {
94+
Object copy = serializeAndDeserialize(obj);
95+
assertEquals(obj, obj);
96+
assertEquals(obj, copy);
97+
assertNotSame(obj, copy);
98+
assertEquals(copy, copy);
99+
}
100+
}
101+
102+
@SuppressWarnings("unchecked")
103+
private <T extends java.io.Serializable> T serializeAndDeserialize(T obj)
104+
throws IOException, ClassNotFoundException {
105+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
106+
try (ObjectOutputStream output = new ObjectOutputStream(bytes)) {
107+
output.writeObject(obj);
108+
}
109+
try (ObjectInputStream input =
110+
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
111+
return (T) input.readObject();
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)