Skip to content

Commit 0b0e151

Browse files
committed
Adds serialization test. Fixes googleapis#631.
1 parent 8d67442 commit 0b0e151

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java

+10
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,14 @@ public Builder toBuilder() {
9595
public static Builder builder() {
9696
return new Builder();
9797
}
98+
99+
@Override
100+
public boolean equals(Object obj) {
101+
return obj instanceof DnsOptions && baseEquals((DnsOptions) obj);
102+
}
103+
104+
@Override
105+
public int hashCode() {
106+
return baseHashCode();
107+
}
98108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2016 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.dns;
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.RetryParams;
24+
25+
import org.junit.Test;
26+
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.ObjectInputStream;
31+
import java.io.ObjectOutputStream;
32+
import java.io.Serializable;
33+
import java.math.BigInteger;
34+
35+
public class SerializationTest {
36+
37+
private static final ZoneInfo FULL_ZONE_INFO = Zone.builder("some zone name")
38+
.creationTimeMillis(132L)
39+
.description("some descriptions")
40+
.dnsName("www.example.com")
41+
.id("123333")
42+
.nameServers(ImmutableList.of("server 1", "server 2"))
43+
.nameServerSet("specificationstring")
44+
.build();
45+
private static final ZoneInfo PARTIAL_ZONE_INFO = Zone.builder("some zone name")
46+
.build();
47+
private static final ProjectInfo PARTIAL_PROJECT_INFO = ProjectInfo.builder().id("13").build();
48+
private static final ProjectInfo FULL_PROJECT_INFO = ProjectInfo.builder()
49+
.id("342")
50+
.number(new BigInteger("2343245"))
51+
.quota(new ProjectInfo.Quota(12, 13, 14, 15, 16, 17))
52+
.build();
53+
private static final Dns.ZoneListOption ZONE_LIST_OPTION =
54+
Dns.ZoneListOption.dnsName("www.example.com.");
55+
private static final Dns.DnsRecordListOption DNS_REOCRD_LIST_OPTION =
56+
Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL);
57+
private static final Dns.ChangeRequestListOption CHANGE_REQUEST_LIST_OPTION =
58+
Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS);
59+
private static final Dns.ZoneOption ZONE_OPTION =
60+
Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME);
61+
private static final Dns.ChangeRequestOption CHANGE_REQUEST_OPTION =
62+
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS);
63+
private static final Dns.ProjectOption PROJECT_OPTION =
64+
Dns.ProjectOption.fields(Dns.ProjectField.QUOTA);
65+
private static final DnsOptions OPTIONS = DnsOptions.builder()
66+
.projectId("some-unnecessary-project-ID")
67+
.retryParams(RetryParams.defaultInstance())
68+
.build();
69+
private static final Dns DNS = OPTIONS.service();
70+
private static final Zone FULL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(FULL_ZONE_INFO));
71+
private static final Zone PARTIAL_ZONE =
72+
new Zone(DNS, new ZoneInfo.BuilderImpl(PARTIAL_ZONE_INFO));
73+
74+
@Test
75+
public void testModelAndRequests() throws Exception {
76+
Serializable[] objects = {FULL_ZONE_INFO, PARTIAL_ZONE_INFO, ZONE_LIST_OPTION,
77+
DNS_REOCRD_LIST_OPTION, CHANGE_REQUEST_LIST_OPTION, ZONE_OPTION, CHANGE_REQUEST_OPTION,
78+
PROJECT_OPTION, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, OPTIONS, FULL_ZONE, PARTIAL_ZONE,
79+
OPTIONS};
80+
for (Serializable obj : objects) {
81+
Object copy = serializeAndDeserialize(obj);
82+
assertEquals(obj, obj);
83+
assertEquals(obj, copy);
84+
assertNotSame(obj, copy);
85+
assertEquals(copy, copy);
86+
}
87+
}
88+
89+
@SuppressWarnings("unchecked")
90+
private <T> T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException {
91+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
92+
try (ObjectOutputStream output = new ObjectOutputStream(bytes)) {
93+
output.writeObject(obj);
94+
}
95+
try (ObjectInputStream input =
96+
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
97+
return (T) input.readObject();
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)