forked from googleapis/java-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyTest.java
More file actions
271 lines (253 loc) · 10.1 KB
/
KeyTest.java
File metadata and controls
271 lines (253 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2017 Google LLC
*
* 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.cloud.spanner;
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.common.testing.EqualsTester;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import java.math.BigDecimal;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link com.google.cloud.spanner.Key}. */
@RunWith(JUnit4.class)
public class KeyTest {
@Test
public void of() {
Key k = Key.of();
assertThat(k.size()).isEqualTo(0);
assertThat(k.getParts()).isEmpty();
k = Key.of("a", "b", "c");
assertThat(k.size()).isEqualTo(3);
assertThat(k.getParts()).containsExactly("a", "b", "c").inOrder();
k = Key.of("a", null, "c");
assertThat(k.size()).isEqualTo(3);
assertThat(k.getParts()).containsExactly("a", null, "c").inOrder();
// All supported Java types: note coercion to canonical types.
String numeric = "3.141592";
String timestamp = "2015-09-15T00:00:00Z";
String date = "2015-09-15";
String uuid = UUID.randomUUID().toString();
String json = "{\"color\":\"red\",\"value\":\"#f00\"}";
k =
Key.of(
null,
true,
32,
64L,
2.0f,
4.0d,
new BigDecimal(numeric),
"x",
json,
ByteArray.copyFrom("y"),
Timestamp.parseTimestamp(timestamp),
Date.parseDate(date),
UUID.fromString(uuid));
assertThat(k.size()).isEqualTo(13);
assertThat(k.getParts())
.containsExactly(
null,
Boolean.TRUE,
32L,
64L,
2.0d,
4.0d,
BigDecimal.valueOf(3141592, 6),
"x",
json,
ByteArray.copyFrom("y"),
Timestamp.parseTimestamp(timestamp),
Date.parseDate(date),
UUID.fromString(uuid))
.inOrder();
// Singleton null key.
k = Key.of((Object) null);
assertThat(k.size()).isEqualTo(1);
assertThat(k.getParts()).contains(null);
}
@Test
public void builder() {
String numeric = "3.141592";
String timestamp = "2015-09-15T00:00:00Z";
String date = "2015-09-15";
String uuid = UUID.randomUUID().toString();
String json = "{\"color\":\"red\",\"value\":\"#f00\"}";
Key k =
Key.newBuilder()
.append((Boolean) null)
.append(true)
.append(32)
.append(64L)
.append(2.0f)
.append(4.0d)
.append(new BigDecimal(numeric))
.append("x")
.append(json)
.append(ByteArray.copyFrom("y"))
.append(Timestamp.parseTimestamp(timestamp))
.append(Date.parseDate(date))
.append(UUID.fromString(uuid))
.build();
assertThat(k.size()).isEqualTo(13);
assertThat(k.getParts())
.containsExactly(
null,
Boolean.TRUE,
32L,
64L,
2.0d,
4.0d,
BigDecimal.valueOf(3141592, 6),
"x",
json,
ByteArray.copyFrom("y"),
Timestamp.parseTimestamp(timestamp),
Date.parseDate(date),
UUID.fromString(uuid))
.inOrder();
}
@Test
public void toBuilder() {
Key k = Key.of(1, 2).toBuilder().append(3).build();
assertThat(k.size()).isEqualTo(3);
assertThat(k.getParts()).containsExactly(1L, 2L, 3L).inOrder();
}
@Test
public void testToString() {
assertThat(Key.of().toString()).isEqualTo("[]");
assertThat(Key.of(new Object[] {null}).toString()).isEqualTo("[<null>]");
assertThat(Key.of(true).toString()).isEqualTo("[true]");
assertThat(Key.of(32).toString()).isEqualTo("[32]");
assertThat(Key.of(2.0).toString()).isEqualTo("[2.0]");
assertThat(Key.of(new BigDecimal("3.14")).toString()).isEqualTo("[3.14]");
assertThat(Key.of("xyz").toString()).isEqualTo("[xyz]");
assertThat(Key.of("{\"color\":\"red\",\"value\":\"#f00\"}").toString())
.isEqualTo("[{\"color\":\"red\",\"value\":\"#f00\"}]");
ByteArray b = ByteArray.copyFrom("xyz");
assertThat(Key.of(b).toString()).isEqualTo("[" + b.toString() + "]");
String timestamp = "2015-09-15T00:00:00Z";
assertThat(Key.of(Timestamp.parseTimestamp(timestamp)).toString())
.isEqualTo("[" + timestamp + "]");
String date = "2015-09-15";
assertThat(Key.of(Date.parseDate(date)).toString()).isEqualTo("[" + date + "]");
String uuid = UUID.randomUUID().toString();
assertThat(Key.of(UUID.fromString(uuid)).toString()).isEqualTo("[" + uuid + "]");
assertThat(Key.of(1, 2, 3).toString()).isEqualTo("[1,2,3]");
}
@Test
public void equalsAndHashCode() {
EqualsTester tester = new EqualsTester();
tester.addEqualityGroup(Key.of(), Key.newBuilder().build());
// All types of null are considered equal.
tester.addEqualityGroup(
Key.of((Object) null),
Key.newBuilder().append((Boolean) null).build(),
Key.newBuilder().append((Long) null).build(),
Key.newBuilder().append((Double) null).build(),
Key.newBuilder().append((BigDecimal) null).build(),
Key.newBuilder().append((String) null).build(),
Key.newBuilder().append((ByteArray) null).build(),
Key.newBuilder().append((Timestamp) null).build(),
Key.newBuilder().append((Date) null).build(),
Key.newBuilder().append((UUID) null).build(),
Key.newBuilder().appendObject(null).build());
tester.addEqualityGroup(Key.of(true), Key.newBuilder().append(true).build());
tester.addEqualityGroup(Key.of(false), Key.newBuilder().append(false).build());
tester.addEqualityGroup(Key.of(1), Key.of(1L), Key.newBuilder().append(1).build());
tester.addEqualityGroup(Key.of(2), Key.of(2L), Key.newBuilder().append(2).build());
tester.addEqualityGroup(Key.of(1, 2));
tester.addEqualityGroup(Key.of(1.0f), Key.of(1.0d), Key.newBuilder().append(1.0).build());
tester.addEqualityGroup(Key.of(2.0f), Key.of(2.0d), Key.newBuilder().append(2.0).build());
tester.addEqualityGroup(
Key.of(new BigDecimal("3.141592")),
Key.of(BigDecimal.valueOf(3141592, 6)),
Key.newBuilder().append(new BigDecimal("3141592e-6")).build());
tester.addEqualityGroup(Key.of("a"), Key.newBuilder().append("a").build());
tester.addEqualityGroup(Key.of("a", "b", "c"));
tester.addEqualityGroup(
Key.of(ByteArray.copyFrom("a")), Key.newBuilder().append(ByteArray.copyFrom("a")).build());
tester.addEqualityGroup(
Key.of("{\"color\":\"red\",\"value\":\"#f00\"}"),
Key.newBuilder().append("{\"color\":\"red\",\"value\":\"#f00\"}").build());
Timestamp t = Timestamp.parseTimestamp("2015-09-15T00:00:00Z");
tester.addEqualityGroup(Key.of(t), Key.newBuilder().append(t).build());
Date d = Date.parseDate("2016-09-15");
tester.addEqualityGroup(Key.of(d), Key.newBuilder().append(d).build());
UUID uuid = UUID.randomUUID();
tester.addEqualityGroup(Key.of(uuid), Key.newBuilder().append(uuid).build());
tester.addEqualityGroup(Key.of("a", 2, null));
tester.testEquals();
}
@Test
public void serialization() {
reserializeAndAssert(Key.of());
reserializeAndAssert(Key.of(new Object[] {null}));
reserializeAndAssert(Key.of(true));
reserializeAndAssert(Key.of(32));
reserializeAndAssert(Key.of(2.0));
reserializeAndAssert(Key.of(new BigDecimal("3.141592")));
reserializeAndAssert(Key.of("xyz"));
reserializeAndAssert(Key.of("{\"color\":\"red\",\"value\":\"#f00\"}"));
reserializeAndAssert(Key.of(ByteArray.copyFrom("xyz")));
reserializeAndAssert(Key.of(Timestamp.parseTimestamp("2015-09-15T00:00:00Z")));
reserializeAndAssert(Key.of(Date.parseDate("2015-09-15")));
reserializeAndAssert(Key.of(UUID.randomUUID()));
reserializeAndAssert(Key.of(1, 2, 3));
}
@Test
public void toProto() {
String timestamp = "2015-09-15T00:00:00Z";
String date = "2015-09-15";
String uuid = UUID.randomUUID().toString();
Key k =
Key.newBuilder()
.append((Boolean) null)
.append(true)
.append(32)
.append(64L)
.append(2.0f)
.append(4.0d)
.append(new BigDecimal("6.62607004e-34"))
.append("x")
.append("{\"color\":\"red\",\"value\":\"#f00\"}")
.append(ByteArray.copyFrom("y"))
.append(Timestamp.parseTimestamp(timestamp))
.append(Date.parseDate(date))
.append(UUID.fromString(uuid))
.build();
ListValue.Builder builder = ListValue.newBuilder();
builder.addValuesBuilder().setNullValue(NullValue.NULL_VALUE);
builder.addValuesBuilder().setBoolValue(true);
builder.addValuesBuilder().setStringValue("32");
builder.addValuesBuilder().setStringValue("64");
builder.addValuesBuilder().setNumberValue(2.0f);
builder.addValuesBuilder().setNumberValue(4.0d);
builder.addValuesBuilder().setStringValue("6.62607004E-34");
builder.addValuesBuilder().setStringValue("x");
builder.addValuesBuilder().setStringValue("{\"color\":\"red\",\"value\":\"#f00\"}");
builder.addValuesBuilder().setStringValue("eQ==");
builder.addValuesBuilder().setStringValue(timestamp);
builder.addValuesBuilder().setStringValue(date);
builder.addValuesBuilder().setStringValue(uuid);
assertThat(k.toProto()).isEqualTo(builder.build());
}
}