Repository navigation
Expand file tree
/
Copy pathObjectMapTest.java
More file actions
319 lines (246 loc) · 7.81 KB
/
Copy pathObjectMapTest.java
File metadata and controls
319 lines (246 loc) · 7.81 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2017 Google Inc.
*
* 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.firebase.database;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.firebase.database.snapshot.EmptyNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.Test;
public class ObjectMapTest {
private MutableData emptyData() {
return new MutableData(EmptyNode.Empty());
}
@Test
public void basicObjectMapping() throws IOException {
MutableData data = emptyData();
Author author = new Author("Greg", 3);
Message message = new Message("hello world", author);
data.setValue(message);
Message result = data.getValue(Message.class);
assertEquals(message, result);
try {
data.getValue(Author.class);
fail("Should throw");
} catch (DatabaseException e) {
// No-op, expected to throw
}
}
@Test
public void nativeToObject() {
MutableData data = emptyData();
Map<String, Object> toSet =
new MapBuilder()
.put("text", "hello world")
.put("author", new MapBuilder().put("name", "Greg").put("id", 3).build())
.build();
data.setValue(toSet);
Message m = data.getValue(Message.class);
assertEquals("hello world", m.getText());
Author a = m.getAuthor();
assertEquals("Greg", a.getName());
assertEquals(3, a.getId());
// An extra field should cause the deserializer to throw
data.child("foo").setValue("bar");
try {
m = data.getValue(Message.class);
fail("Should throw");
} catch (DatabaseException e) {
// No-op, expected to throw
}
}
@Test
public void objectToNative() {
MutableData data = emptyData();
Author a = new Author("Greg", 3);
Message m = new Message("hello world", a);
data.setValue(m);
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) data.getValue();
Map<String, Object> expected =
new MapBuilder()
.put("text", "hello world")
.put("author", new MapBuilder().put("name", "Greg").put("id", 3L).build())
.build();
TestHelpers.assertDeepEquals(expected, result);
}
@Test
public void nullsWork() {
MutableData data = emptyData();
Message m = new Message("hello world", null);
data.setValue(m);
// Author is gone, equivalent to setting child("author") to null
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) data.getValue();
Map<String, Object> expected = new MapBuilder().put("text", "hello world").build();
TestHelpers.assertDeepEquals(expected, result);
Message resultMessage = data.getValue(Message.class);
assertEquals(m, resultMessage);
}
@Test
public void numbersCanBeConverted() {
MutableData data = emptyData();
data.setValue(3);
Object result = data.getValue();
assertTrue(result instanceof Long);
result = data.getValue(Integer.class);
assertTrue(result != null);
}
@Test
public void basicGenerics() {
List<Author> authors = new ArrayList<>();
authors.addAll(
Arrays.asList(new Author("Greg", 3), new Author("Vikrum", 4), new Author("Michael", 5)));
MutableData data = emptyData();
data.setValue(authors);
GenericTypeIndicator<List<Author>> t = new GenericTypeIndicator<List<Author>>() {};
List<Author> result = data.getValue(t);
TestHelpers.assertDeepEquals(authors, result);
}
@Test
public void nestedGenerics() throws IOException {
MutableData data = emptyData();
Author author = new Author("Greg", 3);
List<Message> messages = new ArrayList<>();
messages.add(new Message("hello world", author));
messages.add(new Message("foo bar", author));
AuthorMessages am = new AuthorMessages(author, messages);
data.setValue(am);
AuthorMessages result = data.getValue(AuthorMessages.class);
assertEquals(am.getAuthor(), result.getAuthor());
TestHelpers.assertDeepEquals(am.getMessages(), result.getMessages());
}
@Test
public void incompleteObject() {
MutableData data = emptyData();
Incomplete i = new Incomplete(1, 2);
data.setValue(i);
i = data.getValue(Incomplete.class);
assertEquals(1, i.getFoo());
// bar doesn't get serialized, its getter isn't public
assertEquals(0, i.getBar());
}
@Test
public void intsAndLongs() {
MutableData data = emptyData();
// Verify that jackson does the right thing when it encounters a number too large for an int
data.setValue(new MapBuilder().put("foo", (long) Integer.MAX_VALUE + 1).build());
Map<String, Object> output = data.getValue(new GenericTypeIndicator<Map<String, Object>>() {});
Object value = output.get("foo");
assertEquals(Long.class, value.getClass());
// Verify that our default is Long
data.setValue(3);
value = data.getValue();
assertEquals(Long.class, value.getClass());
}
@ThrowOnExtraProperties
private static class Author {
private String name;
private int id;
private Author() {}
private Author(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Name: " + name + " id: " + id;
}
@Override
public boolean equals(Object o) {
return (o instanceof Author) && ((Author) o).name.equals(name) && ((Author) o).id == id;
}
@Override
public int hashCode() {
return id << 2 | name.hashCode();
}
}
@ThrowOnExtraProperties
private static class Message {
private String text;
private Author author;
private Message() {}
private Message(String text, Author author) {
this.text = text;
this.author = author;
}
public String getText() {
return text;
}
public Author getAuthor() {
return author;
}
@Override
public String toString() {
return "Message: " + text + " Author: " + author.toString();
}
@Override
public boolean equals(Object o) {
return (o instanceof Message)
&& ((Message) o).text.equals(text)
&& (((((Message) o).author == null) && (author == null))
|| ((Message) o).author.equals(author));
}
@Override
public int hashCode() {
return text.hashCode() << 2 | author.hashCode();
}
}
private static class AuthorMessages {
private Author author;
private List<Message> messages;
private AuthorMessages() {}
private AuthorMessages(Author author, List<Message> messages) {
this.author = author;
this.messages = messages;
}
public Author getAuthor() {
return author;
}
public List<Message> getMessages() {
return messages;
}
@Override
public String toString() {
return "Author: " + author + " messages: " + messages;
}
}
private static class Incomplete {
private int foo;
private int bar;
private Incomplete() {}
private Incomplete(int foo, int bar) {
this.foo = foo;
this.bar = bar;
}
public int getFoo() {
return foo;
}
int getBar() {
return bar;
}
}
}