-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPojoCodecCyclicalLookupTest.java
More file actions
178 lines (150 loc) · 8.21 KB
/
PojoCodecCyclicalLookupTest.java
File metadata and controls
178 lines (150 loc) · 8.21 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
/*
* Copyright 2008-present MongoDB, 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 org.bson.codecs.pojo;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.Codec;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.entities.ConventionModel;
import org.bson.codecs.pojo.entities.GenericHolderModel;
import org.bson.codecs.pojo.entities.GenericTreeModel;
import org.bson.codecs.pojo.entities.ListListGenericExtendedModel;
import org.bson.codecs.pojo.entities.NestedGenericHolderFieldWithMultipleTypeParamsModel;
import org.bson.codecs.pojo.entities.NestedGenericTreeModel;
import org.bson.codecs.pojo.entities.PropertyWithMultipleTypeParamsModel;
import org.bson.codecs.pojo.entities.SimpleGenericsModel;
import org.bson.codecs.pojo.entities.SimpleModel;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PojoCodecCyclicalLookupTest extends PojoTestCase {
@Test
void testSimpleModel() {
SimpleModel model = getSimpleModel();
LookupCountingCodecRegistry registry = createRegistry(SimpleModel.class);
roundTrip(registry, model, SIMPLE_MODEL_JSON);
assertEquals(2, registry.counters.get(SimpleModel.class).get()); // Looked up in encodesTo & decodesTo
assertEquals(1, registry.counters.get(String.class).get()); // Lookup on encode then cached (PropertyCodecRegistry)
assertEquals(1, registry.counters.get(Integer.class).get()); // Lookup on encode then cached (PropertyCodecRegistry)
}
@Test
void testConventionModel() {
ConventionModel model = getConventionModel();
String json = "{'_id': 'id', '_cls': 'AnnotatedConventionModel', 'myFinalField': 10, 'myIntField': 10,"
+ "'child': {'_id': 'child', 'myFinalField': 10, 'myIntField': 10,"
+ "'model': {'integerField': 42, 'stringField': 'myString'}}}";
LookupCountingCodecRegistry registry = createRegistry(ConventionModel.class, SimpleModel.class);
roundTrip(registry, model, json);
assertEquals(2, registry.counters.get(ConventionModel.class).get()); // Looked up in encodesTo & decodesTo
assertEquals(1, registry.counters.get(SimpleModel.class).get()); // Lookup on encode then cached (PropertyCodecRegistry)
assertEquals(2, registry.counters.get(String.class).get()); // Once for ConventionModel & once for SimpleModel
assertEquals(2, registry.counters.get(Integer.class).get()); // Once for ConventionModel & once for SimpleModel
}
@Test
void testNestedGenericTreeModel() {
NestedGenericTreeModel model = new NestedGenericTreeModel(42, getGenericTreeModel());
String json = "{'intField': 42, 'nested': {'field1': 'top', 'field2': 1, "
+ "'left': {'field1': 'left', 'field2': 2, 'left': {'field1': 'left', 'field2': 3}}, "
+ "'right': {'field1': 'right', 'field2': 4, 'left': {'field1': 'left', 'field2': 5}}}}";
LookupCountingCodecRegistry registry = createRegistry(NestedGenericTreeModel.class, GenericTreeModel.class);
roundTrip(registry, model, json);
assertEquals(2, registry.counters.get(NestedGenericTreeModel.class).get());
assertEquals(1, registry.counters.get(GenericTreeModel.class).get());
assertEquals(1, registry.counters.get(String.class).get());
assertEquals(1, registry.counters.get(Integer.class).get());
}
@Test
void testNestedGenericHolderFieldWithMultipleTypeParamsModel() {
NestedGenericHolderFieldWithMultipleTypeParamsModel model = getNestedGenericHolderFieldWithMultipleTypeParamsModel();
LookupCountingCodecRegistry registry = createRegistry(NestedGenericHolderFieldWithMultipleTypeParamsModel.class,
PropertyWithMultipleTypeParamsModel.class, SimpleGenericsModel.class, GenericHolderModel.class);
String json = "{'nested': {'myGenericField': {_t: 'PropertyWithMultipleTypeParamsModel', "
+ "'simpleGenericsModel': {_t: 'org.bson.codecs.pojo.entities.SimpleGenericsModel', 'myIntegerField': 42, "
+ "'myGenericField': {'$numberLong': '101'}, 'myListField': ['B', 'C'], 'myMapField': {'D': 2, 'E': 3, 'F': 4 }}},"
+ "'myLongField': {'$numberLong': '42'}}}";
roundTrip(registry, model, json);
assertEquals(2, registry.counters.get(NestedGenericHolderFieldWithMultipleTypeParamsModel.class).get());
assertEquals(1, registry.counters.get(PropertyWithMultipleTypeParamsModel.class).get());
assertEquals(1, registry.counters.get(SimpleGenericsModel.class).get());
assertEquals(1, registry.counters.get(GenericHolderModel.class).get());
assertEquals(1, registry.counters.get(Long.class).get());
assertEquals(1, registry.counters.get(String.class).get());
assertEquals(1, registry.counters.get(Integer.class).get());
}
@Test
void testListListGenericExtendedModel() {
ListListGenericExtendedModel model = new ListListGenericExtendedModel(asList(asList(1, 2, 3), asList(4, 5, 6)));
LookupCountingCodecRegistry registry = createRegistry(ListListGenericExtendedModel .class);
String json = "{values: [[1, 2, 3], [4, 5, 6]]}";
roundTrip(registry, model, json);
assertEquals(2, registry.counters.get(ListListGenericExtendedModel.class).get());
assertEquals(1, registry.counters.get(Integer.class).get());
}
LookupCountingCodecRegistry createRegistry(final Class<?>... classes) {
return new LookupCountingCodecRegistry(
new BsonValueCodecProvider(),
new ValueCodecProvider(),
getPojoCodecProviderBuilder(classes).build()
);
}
static class LookupCountingCodecRegistry implements CodecRegistry {
private final ConcurrentHashMap<Class<?>, AtomicInteger> counters;
private final List<CodecProvider> codecProviders;
LookupCountingCodecRegistry(final CodecProvider... providers) {
this.codecProviders = asList(providers);
this.counters = new ConcurrentHashMap<>();
}
@Override
public <T> Codec<T> get(final Class<T> clazz) {
return get(clazz, this);
}
@Override
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments) {
return get(clazz, typeArguments, this);
}
@Override
public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
incrementCount(clazz);
for (CodecProvider provider : codecProviders) {
Codec<T> codec = provider.get(clazz, registry);
if (codec != null) {
return codec;
}
}
return null;
}
@Override
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments, final CodecRegistry registry) {
incrementCount(clazz);
for (CodecProvider provider : codecProviders) {
Codec<T> codec = provider.get(clazz, typeArguments, registry);
if (codec != null) {
return codec;
}
}
return null;
}
private synchronized <T> void incrementCount(final Class<T> clazz) {
AtomicInteger atomicInteger = counters.computeIfAbsent(clazz, k -> new AtomicInteger());
atomicInteger.incrementAndGet();
}
}
}