OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 12 matching lines...) Expand all Loading... |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 | |
34 import com.google.protobuf.Descriptors.Descriptor; | 33 import com.google.protobuf.Descriptors.Descriptor; |
35 import com.google.protobuf.Descriptors.EnumDescriptor; | 34 import com.google.protobuf.Descriptors.EnumDescriptor; |
36 import com.google.protobuf.Descriptors.EnumValueDescriptor; | 35 import com.google.protobuf.Descriptors.EnumValueDescriptor; |
37 import com.google.protobuf.Descriptors.FieldDescriptor; | 36 import com.google.protobuf.Descriptors.FieldDescriptor; |
38 import map_test.MapTestProto.BizarroTestMap; | |
39 import map_test.MapTestProto.ReservedAsMapField; | |
40 import map_test.MapTestProto.ReservedAsMapFieldWithEnumValue; | |
41 import map_test.MapTestProto.TestMap; | 37 import map_test.MapTestProto.TestMap; |
42 import map_test.MapTestProto.TestMap.MessageValue; | 38 import map_test.MapTestProto.TestMap.MessageValue; |
43 import map_test.MapTestProto.TestMapOrBuilder; | |
44 import map_test.MapTestProto.TestOnChangeEventPropagation; | 39 import map_test.MapTestProto.TestOnChangeEventPropagation; |
45 import java.io.ByteArrayOutputStream; | 40 import junit.framework.TestCase; |
46 import java.io.IOException; | 41 |
47 import java.util.ArrayList; | 42 import java.util.ArrayList; |
48 import java.util.Arrays; | 43 import java.util.Arrays; |
49 import java.util.HashMap; | 44 import java.util.HashMap; |
50 import java.util.List; | 45 import java.util.List; |
51 import java.util.Map; | 46 import java.util.Map; |
52 import junit.framework.TestCase; | |
53 | 47 |
54 /** | 48 /** |
55 * Unit tests for map fields. | 49 * Unit tests for map fields. |
56 */ | 50 */ |
57 public class MapTest extends TestCase { | 51 public class MapTest extends TestCase { |
58 | 52 private void setMapValues(TestMap.Builder builder) { |
59 private void setMapValuesUsingMutableMap(TestMap.Builder builder) { | |
60 builder.getMutableInt32ToInt32Field().put(1, 11); | 53 builder.getMutableInt32ToInt32Field().put(1, 11); |
61 builder.getMutableInt32ToInt32Field().put(2, 22); | 54 builder.getMutableInt32ToInt32Field().put(2, 22); |
62 builder.getMutableInt32ToInt32Field().put(3, 33); | 55 builder.getMutableInt32ToInt32Field().put(3, 33); |
63 // | 56 |
64 builder.getMutableInt32ToStringField().put(1, "11"); | 57 builder.getMutableInt32ToStringField().put(1, "11"); |
65 builder.getMutableInt32ToStringField().put(2, "22"); | 58 builder.getMutableInt32ToStringField().put(2, "22"); |
66 builder.getMutableInt32ToStringField().put(3, "33"); | 59 builder.getMutableInt32ToStringField().put(3, "33"); |
67 // | 60 |
68 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); | 61 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); |
69 builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); | 62 builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); |
70 builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); | 63 builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); |
71 // | 64 |
72 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); | 65 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); |
73 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); | 66 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); |
74 builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); | 67 builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); |
75 // | 68 |
76 builder.getMutableInt32ToMessageField().put( | 69 builder.getMutableInt32ToMessageField().put( |
77 1, MessageValue.newBuilder().setValue(11).build()); | 70 1, MessageValue.newBuilder().setValue(11).build()); |
78 builder.getMutableInt32ToMessageField().put( | 71 builder.getMutableInt32ToMessageField().put( |
79 2, MessageValue.newBuilder().setValue(22).build()); | 72 2, MessageValue.newBuilder().setValue(22).build()); |
80 builder.getMutableInt32ToMessageField().put( | 73 builder.getMutableInt32ToMessageField().put( |
81 3, MessageValue.newBuilder().setValue(33).build()); | 74 3, MessageValue.newBuilder().setValue(33).build()); |
82 // | 75 |
83 builder.getMutableStringToInt32Field().put("1", 11); | 76 builder.getMutableStringToInt32Field().put("1", 11); |
84 builder.getMutableStringToInt32Field().put("2", 22); | 77 builder.getMutableStringToInt32Field().put("2", 22); |
85 builder.getMutableStringToInt32Field().put("3", 33); | 78 builder.getMutableStringToInt32Field().put("3", 33); |
86 } | 79 } |
87 | 80 |
88 private void setMapValuesUsingAccessors(TestMap.Builder builder) { | |
89 builder | |
90 .putInt32ToInt32Field(1, 11) | |
91 .putInt32ToInt32Field(2, 22) | |
92 .putInt32ToInt32Field(3, 33) | |
93 | |
94 .putInt32ToStringField(1, "11") | |
95 .putInt32ToStringField(2, "22") | |
96 .putInt32ToStringField(3, "33") | |
97 | |
98 .putInt32ToBytesField(1, TestUtil.toBytes("11")) | |
99 .putInt32ToBytesField(2, TestUtil.toBytes("22")) | |
100 .putInt32ToBytesField(3, TestUtil.toBytes("33")) | |
101 | |
102 .putInt32ToEnumField(1, TestMap.EnumValue.FOO) | |
103 .putInt32ToEnumField(2, TestMap.EnumValue.BAR) | |
104 .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) | |
105 | |
106 .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build(
)) | |
107 .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build(
)) | |
108 .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build(
)) | |
109 | |
110 .putStringToInt32Field("1", 11) | |
111 .putStringToInt32Field("2", 22) | |
112 .putStringToInt32Field("3", 33); | |
113 } | |
114 | |
115 public void testSetMapValues() { | |
116 TestMap.Builder usingMutableMapBuilder = TestMap.newBuilder(); | |
117 setMapValuesUsingMutableMap(usingMutableMapBuilder); | |
118 TestMap usingMutableMap = usingMutableMapBuilder.build(); | |
119 assertMapValuesSet(usingMutableMap); | |
120 | |
121 TestMap.Builder usingAccessorsBuilder = TestMap.newBuilder(); | |
122 setMapValuesUsingAccessors(usingAccessorsBuilder); | |
123 TestMap usingAccessors = usingAccessorsBuilder.build(); | |
124 assertMapValuesSet(usingAccessors); | |
125 | |
126 assertEquals(usingAccessors, usingMutableMap); | |
127 } | |
128 | |
129 private void copyMapValues(TestMap source, TestMap.Builder destination) { | 81 private void copyMapValues(TestMap source, TestMap.Builder destination) { |
130 destination | 82 destination |
131 .putAllInt32ToInt32Field(source.getInt32ToInt32Field()) | 83 .putAllInt32ToInt32Field(source.getInt32ToInt32Field()) |
132 .putAllInt32ToStringField(source.getInt32ToStringField()) | 84 .putAllInt32ToStringField(source.getInt32ToStringField()) |
133 .putAllInt32ToBytesField(source.getInt32ToBytesField()) | 85 .putAllInt32ToBytesField(source.getInt32ToBytesField()) |
134 .putAllInt32ToEnumField(source.getInt32ToEnumField()) | 86 .putAllInt32ToEnumField(source.getInt32ToEnumField()) |
135 .putAllInt32ToMessageField(source.getInt32ToMessageField()) | 87 .putAllInt32ToMessageField(source.getInt32ToMessageField()) |
136 .putAllStringToInt32Field(source.getStringToInt32Field()); | 88 .putAllStringToInt32Field(source.getStringToInt32Field()); |
137 } | 89 } |
138 | 90 |
(...skipping 22 matching lines...) Expand all Loading... |
161 assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); | 113 assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); |
162 assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); | 114 assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); |
163 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); | 115 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); |
164 | 116 |
165 assertEquals(3, message.getStringToInt32Field().size()); | 117 assertEquals(3, message.getStringToInt32Field().size()); |
166 assertEquals(11, message.getStringToInt32Field().get("1").intValue()); | 118 assertEquals(11, message.getStringToInt32Field().get("1").intValue()); |
167 assertEquals(22, message.getStringToInt32Field().get("2").intValue()); | 119 assertEquals(22, message.getStringToInt32Field().get("2").intValue()); |
168 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); | 120 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); |
169 } | 121 } |
170 | 122 |
171 private void updateMapValuesUsingMutableMap(TestMap.Builder builder) { | 123 private void updateMapValues(TestMap.Builder builder) { |
172 builder.getMutableInt32ToInt32Field().put(1, 111); | 124 builder.getMutableInt32ToInt32Field().put(1, 111); |
173 builder.getMutableInt32ToInt32Field().remove(2); | 125 builder.getMutableInt32ToInt32Field().remove(2); |
174 builder.getMutableInt32ToInt32Field().put(4, 44); | 126 builder.getMutableInt32ToInt32Field().put(4, 44); |
175 // | 127 |
176 builder.getMutableInt32ToStringField().put(1, "111"); | 128 builder.getMutableInt32ToStringField().put(1, "111"); |
177 builder.getMutableInt32ToStringField().remove(2); | 129 builder.getMutableInt32ToStringField().remove(2); |
178 builder.getMutableInt32ToStringField().put(4, "44"); | 130 builder.getMutableInt32ToStringField().put(4, "44"); |
179 // | 131 |
180 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); | 132 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); |
181 builder.getMutableInt32ToBytesField().remove(2); | 133 builder.getMutableInt32ToBytesField().remove(2); |
182 builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); | 134 builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); |
183 // | 135 |
184 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); | 136 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); |
185 builder.getMutableInt32ToEnumField().remove(2); | 137 builder.getMutableInt32ToEnumField().remove(2); |
186 builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); | 138 builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); |
187 // | 139 |
188 builder.getMutableInt32ToMessageField().put( | 140 builder.getMutableInt32ToMessageField().put( |
189 1, MessageValue.newBuilder().setValue(111).build()); | 141 1, MessageValue.newBuilder().setValue(111).build()); |
190 builder.getMutableInt32ToMessageField().remove(2); | 142 builder.getMutableInt32ToMessageField().remove(2); |
191 builder.getMutableInt32ToMessageField().put( | 143 builder.getMutableInt32ToMessageField().put( |
192 4, MessageValue.newBuilder().setValue(44).build()); | 144 4, MessageValue.newBuilder().setValue(44).build()); |
193 // | 145 |
194 builder.getMutableStringToInt32Field().put("1", 111); | 146 builder.getMutableStringToInt32Field().put("1", 111); |
195 builder.getMutableStringToInt32Field().remove("2"); | 147 builder.getMutableStringToInt32Field().remove("2"); |
196 builder.getMutableStringToInt32Field().put("4", 44); | 148 builder.getMutableStringToInt32Field().put("4", 44); |
197 } | 149 } |
198 | 150 |
199 private void updateMapValuesUsingAccessors(TestMap.Builder builder) { | |
200 builder | |
201 .putInt32ToInt32Field(1, 111) | |
202 .removeInt32ToInt32Field(2) | |
203 .putInt32ToInt32Field(4, 44) | |
204 | |
205 .putInt32ToStringField(1, "111") | |
206 .removeInt32ToStringField(2) | |
207 .putInt32ToStringField(4, "44") | |
208 | |
209 .putInt32ToBytesField(1, TestUtil.toBytes("111")) | |
210 .removeInt32ToBytesField(2) | |
211 .putInt32ToBytesField(4, TestUtil.toBytes("44")) | |
212 | |
213 .putInt32ToEnumField(1, TestMap.EnumValue.BAR) | |
214 .removeInt32ToEnumField(2) | |
215 .putInt32ToEnumField(4, TestMap.EnumValue.QUX) | |
216 | |
217 .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build
()) | |
218 .removeInt32ToMessageField(2) | |
219 .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build(
)) | |
220 | |
221 .putStringToInt32Field("1", 111) | |
222 .removeStringToInt32Field("2") | |
223 .putStringToInt32Field("4", 44); | |
224 } | |
225 | |
226 public void testUpdateMapValues() { | |
227 TestMap.Builder usingMutableMapBuilder = TestMap.newBuilder(); | |
228 setMapValuesUsingMutableMap(usingMutableMapBuilder); | |
229 TestMap usingMutableMap = usingMutableMapBuilder.build(); | |
230 assertMapValuesSet(usingMutableMap); | |
231 | |
232 TestMap.Builder usingAccessorsBuilder = TestMap.newBuilder(); | |
233 setMapValuesUsingAccessors(usingAccessorsBuilder); | |
234 TestMap usingAccessors = usingAccessorsBuilder.build(); | |
235 assertMapValuesSet(usingAccessors); | |
236 | |
237 assertEquals(usingAccessors, usingMutableMap); | |
238 // | |
239 usingMutableMapBuilder = usingMutableMap.toBuilder(); | |
240 updateMapValuesUsingMutableMap(usingMutableMapBuilder); | |
241 usingMutableMap = usingMutableMapBuilder.build(); | |
242 assertMapValuesUpdated(usingMutableMap); | |
243 | |
244 usingAccessorsBuilder = usingAccessors.toBuilder(); | |
245 updateMapValuesUsingAccessors(usingAccessorsBuilder); | |
246 usingAccessors = usingAccessorsBuilder.build(); | |
247 assertMapValuesUpdated(usingAccessors); | |
248 | |
249 assertEquals(usingAccessors, usingMutableMap); | |
250 } | |
251 | |
252 private void assertMapValuesUpdated(TestMap message) { | 151 private void assertMapValuesUpdated(TestMap message) { |
253 assertEquals(3, message.getInt32ToInt32Field().size()); | 152 assertEquals(3, message.getInt32ToInt32Field().size()); |
254 assertEquals(111, message.getInt32ToInt32Field().get(1).intValue()); | 153 assertEquals(111, message.getInt32ToInt32Field().get(1).intValue()); |
255 assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); | 154 assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); |
256 assertEquals(44, message.getInt32ToInt32Field().get(4).intValue()); | 155 assertEquals(44, message.getInt32ToInt32Field().get(4).intValue()); |
257 | 156 |
258 assertEquals(3, message.getInt32ToStringField().size()); | 157 assertEquals(3, message.getInt32ToStringField().size()); |
259 assertEquals("111", message.getInt32ToStringField().get(1)); | 158 assertEquals("111", message.getInt32ToStringField().get(1)); |
260 assertEquals("33", message.getInt32ToStringField().get(3)); | 159 assertEquals("33", message.getInt32ToStringField().get(3)); |
261 assertEquals("44", message.getInt32ToStringField().get(4)); | 160 assertEquals("44", message.getInt32ToStringField().get(4)); |
(...skipping 12 matching lines...) Expand all Loading... |
274 assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); | 173 assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); |
275 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); | 174 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); |
276 assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); | 175 assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); |
277 | 176 |
278 assertEquals(3, message.getStringToInt32Field().size()); | 177 assertEquals(3, message.getStringToInt32Field().size()); |
279 assertEquals(111, message.getStringToInt32Field().get("1").intValue()); | 178 assertEquals(111, message.getStringToInt32Field().get("1").intValue()); |
280 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); | 179 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); |
281 assertEquals(44, message.getStringToInt32Field().get("4").intValue()); | 180 assertEquals(44, message.getStringToInt32Field().get("4").intValue()); |
282 } | 181 } |
283 | 182 |
284 private void assertMapValuesCleared(TestMapOrBuilder testMapOrBuilder) { | 183 private void assertMapValuesCleared(TestMap message) { |
285 assertEquals(0, testMapOrBuilder.getInt32ToInt32Field().size()); | 184 assertEquals(0, message.getInt32ToInt32Field().size()); |
286 assertEquals(0, testMapOrBuilder.getInt32ToInt32FieldCount()); | 185 assertEquals(0, message.getInt32ToStringField().size()); |
287 assertEquals(0, testMapOrBuilder.getInt32ToStringField().size()); | 186 assertEquals(0, message.getInt32ToBytesField().size()); |
288 assertEquals(0, testMapOrBuilder.getInt32ToStringFieldCount()); | 187 assertEquals(0, message.getInt32ToEnumField().size()); |
289 assertEquals(0, testMapOrBuilder.getInt32ToBytesField().size()); | 188 assertEquals(0, message.getInt32ToMessageField().size()); |
290 assertEquals(0, testMapOrBuilder.getInt32ToBytesFieldCount()); | 189 assertEquals(0, message.getStringToInt32Field().size()); |
291 assertEquals(0, testMapOrBuilder.getInt32ToEnumField().size()); | |
292 assertEquals(0, testMapOrBuilder.getInt32ToEnumFieldCount()); | |
293 assertEquals(0, testMapOrBuilder.getInt32ToMessageField().size()); | |
294 assertEquals(0, testMapOrBuilder.getInt32ToMessageFieldCount()); | |
295 assertEquals(0, testMapOrBuilder.getStringToInt32Field().size()); | |
296 assertEquals(0, testMapOrBuilder.getStringToInt32FieldCount()); | |
297 } | 190 } |
298 | 191 |
299 public void testGetMapIsImmutable() { | |
300 TestMap.Builder builder = TestMap.newBuilder(); | |
301 assertMapsAreImmutable(builder); | |
302 assertMapsAreImmutable(builder.build()); | |
303 | |
304 setMapValuesUsingAccessors(builder); | |
305 assertMapsAreImmutable(builder); | |
306 assertMapsAreImmutable(builder.build()); | |
307 } | |
308 | |
309 private void assertMapsAreImmutable(TestMapOrBuilder testMapOrBuilder) { | |
310 assertImmutable(testMapOrBuilder.getInt32ToInt32Field(), 1, 2); | |
311 assertImmutable(testMapOrBuilder.getInt32ToStringField(), 1, "2"); | |
312 assertImmutable(testMapOrBuilder.getInt32ToBytesField(), 1, TestUtil.toBytes
("2")); | |
313 assertImmutable(testMapOrBuilder.getInt32ToEnumField(), 1, TestMap.EnumValue
.FOO); | |
314 assertImmutable( | |
315 testMapOrBuilder.getInt32ToMessageField(), 1, MessageValue.getDefaultIns
tance()); | |
316 assertImmutable(testMapOrBuilder.getStringToInt32Field(), "1", 2); | |
317 } | |
318 | |
319 private <K, V> void assertImmutable(Map<K, V> map, K key, V value) { | |
320 try { | |
321 map.put(key, value); | |
322 fail(); | |
323 } catch (UnsupportedOperationException e) { | |
324 // expected | |
325 } | |
326 } | |
327 | |
328 public void testMutableMapLifecycle() { | 192 public void testMutableMapLifecycle() { |
329 TestMap.Builder builder = TestMap.newBuilder(); | 193 TestMap.Builder builder = TestMap.newBuilder(); |
330 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); | 194 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); |
331 intMap.put(1, 2); | 195 intMap.put(1, 2); |
332 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); | 196 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); |
333 try { | 197 try { |
334 intMap.put(2, 3); | 198 intMap.put(2, 3); |
335 fail(); | 199 fail(); |
336 } catch (UnsupportedOperationException e) { | 200 } catch (UnsupportedOperationException e) { |
337 // expected | 201 // expected |
338 } | 202 } |
339 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); | 203 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); |
340 builder.getMutableInt32ToInt32Field().put(2, 3); | 204 builder.getMutableInt32ToInt32Field().put(2, 3); |
341 assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); | 205 assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); |
342 // | 206 |
343 Map<Integer, TestMap.EnumValue> enumMap = builder.getMutableInt32ToEnumField
(); | 207 Map<Integer, TestMap.EnumValue> enumMap = builder.getMutableInt32ToEnumField
(); |
344 enumMap.put(1, TestMap.EnumValue.BAR); | 208 enumMap.put(1, TestMap.EnumValue.BAR); |
345 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnu
mField()); | 209 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnu
mField()); |
346 try { | 210 try { |
347 enumMap.put(2, TestMap.EnumValue.FOO); | 211 enumMap.put(2, TestMap.EnumValue.FOO); |
348 fail(); | 212 fail(); |
349 } catch (UnsupportedOperationException e) { | 213 } catch (UnsupportedOperationException e) { |
350 // expected | 214 // expected |
351 } | 215 } |
352 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()
); | 216 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()
); |
353 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.FOO); | 217 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.FOO); |
354 assertEquals( | 218 assertEquals( |
355 newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), | 219 newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), |
356 builder.getInt32ToEnumField()); | 220 builder.getInt32ToEnumField()); |
357 // | 221 |
358 Map<Integer, String> stringMap = builder.getMutableInt32ToStringField(); | 222 Map<Integer, String> stringMap = builder.getMutableInt32ToStringField(); |
359 stringMap.put(1, "1"); | 223 stringMap.put(1, "1"); |
360 assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); | 224 assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); |
361 try { | 225 try { |
362 stringMap.put(2, "2"); | 226 stringMap.put(2, "2"); |
363 fail(); | 227 fail(); |
364 } catch (UnsupportedOperationException e) { | 228 } catch (UnsupportedOperationException e) { |
365 // expected | 229 // expected |
366 } | 230 } |
367 assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); | 231 assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); |
368 builder.putInt32ToStringField(2, "2"); | 232 builder.getMutableInt32ToStringField().put(2, "2"); |
369 assertEquals( | 233 assertEquals( |
370 newMap(1, "1", 2, "2"), | 234 newMap(1, "1", 2, "2"), |
371 builder.getInt32ToStringField()); | 235 builder.getInt32ToStringField()); |
372 // | 236 |
373 Map<Integer, TestMap.MessageValue> messageMap = builder.getMutableInt32ToMes
sageField(); | 237 Map<Integer, TestMap.MessageValue> messageMap = builder.getMutableInt32ToMes
sageField(); |
374 messageMap.put(1, TestMap.MessageValue.getDefaultInstance()); | 238 messageMap.put(1, TestMap.MessageValue.getDefaultInstance()); |
375 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), | 239 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), |
376 builder.build().getInt32ToMessageField()); | 240 builder.build().getInt32ToMessageField()); |
377 try { | 241 try { |
378 messageMap.put(2, TestMap.MessageValue.getDefaultInstance()); | 242 messageMap.put(2, TestMap.MessageValue.getDefaultInstance()); |
379 fail(); | 243 fail(); |
380 } catch (UnsupportedOperationException e) { | 244 } catch (UnsupportedOperationException e) { |
381 // expected | 245 // expected |
382 } | 246 } |
383 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), | 247 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), |
384 builder.getInt32ToMessageField()); | 248 builder.getInt32ToMessageField()); |
385 builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance())
; | 249 builder.getMutableInt32ToMessageField().put(2, TestMap.MessageValue.getDefau
ltInstance()); |
386 assertEquals( | 250 assertEquals( |
387 newMap(1, TestMap.MessageValue.getDefaultInstance(), | 251 newMap(1, TestMap.MessageValue.getDefaultInstance(), |
388 2, TestMap.MessageValue.getDefaultInstance()), | 252 2, TestMap.MessageValue.getDefaultInstance()), |
389 builder.getInt32ToMessageField()); | 253 builder.getInt32ToMessageField()); |
390 } | 254 } |
391 // | 255 |
392 public void testMutableMapLifecycle_collections() { | 256 public void testMutableMapLifecycle_collections() { |
393 TestMap.Builder builder = TestMap.newBuilder(); | 257 TestMap.Builder builder = TestMap.newBuilder(); |
394 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); | 258 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); |
395 intMap.put(1, 2); | 259 intMap.put(1, 2); |
396 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); | 260 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); |
397 try { | 261 try { |
398 intMap.remove(2); | 262 intMap.remove(2); |
399 fail(); | 263 fail(); |
400 } catch (UnsupportedOperationException e) { | 264 } catch (UnsupportedOperationException e) { |
401 // expected | 265 // expected |
(...skipping 25 matching lines...) Expand all Loading... |
427 try { | 291 try { |
428 intMap.values().iterator().remove(); | 292 intMap.values().iterator().remove(); |
429 fail(); | 293 fail(); |
430 } catch (UnsupportedOperationException e) { | 294 } catch (UnsupportedOperationException e) { |
431 // expected | 295 // expected |
432 } | 296 } |
433 assertEquals(newMap(1, 2), intMap); | 297 assertEquals(newMap(1, 2), intMap); |
434 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); | 298 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); |
435 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); | 299 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); |
436 } | 300 } |
437 | 301 |
438 | |
439 public void testGettersAndSetters() throws Exception { | 302 public void testGettersAndSetters() throws Exception { |
440 TestMap.Builder builder = TestMap.newBuilder(); | 303 TestMap.Builder builder = TestMap.newBuilder(); |
441 TestMap message = builder.build(); | 304 TestMap message = builder.build(); |
442 assertMapValuesCleared(message); | 305 assertMapValuesCleared(message); |
443 | 306 |
444 builder = message.toBuilder(); | 307 builder = message.toBuilder(); |
445 setMapValuesUsingAccessors(builder); | 308 setMapValues(builder); |
446 message = builder.build(); | 309 message = builder.build(); |
447 assertMapValuesSet(message); | 310 assertMapValuesSet(message); |
448 | 311 |
449 builder = message.toBuilder(); | 312 builder = message.toBuilder(); |
450 updateMapValuesUsingAccessors(builder); | 313 updateMapValues(builder); |
451 message = builder.build(); | 314 message = builder.build(); |
452 assertMapValuesUpdated(message); | 315 assertMapValuesUpdated(message); |
453 | 316 |
454 builder = message.toBuilder(); | 317 builder = message.toBuilder(); |
455 builder.clear(); | 318 builder.clear(); |
456 assertMapValuesCleared(builder); | |
457 message = builder.build(); | 319 message = builder.build(); |
458 assertMapValuesCleared(message); | 320 assertMapValuesCleared(message); |
459 } | 321 } |
460 | 322 |
461 public void testPutAll() throws Exception { | 323 public void testPutAll() throws Exception { |
462 TestMap.Builder sourceBuilder = TestMap.newBuilder(); | 324 TestMap.Builder sourceBuilder = TestMap.newBuilder(); |
463 setMapValuesUsingAccessors(sourceBuilder); | 325 setMapValues(sourceBuilder); |
464 TestMap source = sourceBuilder.build(); | 326 TestMap source = sourceBuilder.build(); |
465 assertMapValuesSet(source); | |
466 | 327 |
467 TestMap.Builder destination = TestMap.newBuilder(); | 328 TestMap.Builder destination = TestMap.newBuilder(); |
468 copyMapValues(source, destination); | 329 copyMapValues(source, destination); |
469 assertMapValuesSet(destination.build()); | 330 assertMapValuesSet(destination.build()); |
470 } | 331 } |
471 | 332 |
472 public void testPutAllForUnknownEnumValues() throws Exception { | 333 public void testPutAllForUnknownEnumValues() throws Exception { |
473 TestMap source = TestMap.newBuilder() | 334 TestMap.Builder sourceBuilder = TestMap.newBuilder(); |
474 .putAllInt32ToEnumFieldValue(newMap( | 335 sourceBuilder.getMutableInt32ToEnumFieldValue().put(0, 0); |
475 0, 0, | 336 sourceBuilder.getMutableInt32ToEnumFieldValue().put(1, 1); |
476 1, 1, | 337 sourceBuilder.getMutableInt32ToEnumFieldValue().put(2, 1000); // unknown va
lue. |
477 2, 1000)) // unknown value. | 338 TestMap source = sourceBuilder.build(); |
478 .build(); | |
479 | 339 |
480 TestMap destination = TestMap.newBuilder() | 340 TestMap.Builder destinationBuilder = TestMap.newBuilder(); |
481 .putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()) | 341 destinationBuilder.putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldVal
ue()); |
482 .build(); | 342 TestMap destination = destinationBuilder.build(); |
483 | 343 |
484 assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); | 344 assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); |
485 assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); | 345 assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); |
486 assertEquals(1000, destination.getInt32ToEnumFieldValue().get(2).intValue())
; | 346 assertEquals(1000, destination.getInt32ToEnumFieldValue().get(2).intValue())
; |
487 assertEquals(3, destination.getInt32ToEnumFieldCount()); | |
488 } | |
489 | |
490 public void testPutForUnknownEnumValues() throws Exception { | |
491 TestMap.Builder builder = TestMap.newBuilder() | |
492 .putInt32ToEnumFieldValue(0, 0) | |
493 .putInt32ToEnumFieldValue(1, 1); | |
494 | |
495 try { | |
496 builder.putInt32ToEnumFieldValue(2, 1000); // unknown value. | |
497 fail(); | |
498 } catch (IllegalArgumentException e) { | |
499 // expected | |
500 } | |
501 | |
502 TestMap message = builder.build(); | |
503 assertEquals(0, message.getInt32ToEnumFieldValueOrThrow(0)); | |
504 assertEquals(1, message.getInt32ToEnumFieldValueOrThrow(1)); | |
505 assertEquals(2, message.getInt32ToEnumFieldCount()); | |
506 } | |
507 | |
508 public void testPutChecksNullKeysAndValues() throws Exception { | |
509 TestMap.Builder builder = TestMap.newBuilder(); | |
510 | |
511 try { | |
512 builder.putInt32ToStringField(1, null); | |
513 fail(); | |
514 } catch (NullPointerException e) { | |
515 // expected. | |
516 } | |
517 | |
518 try { | |
519 builder.putInt32ToBytesField(1, null); | |
520 fail(); | |
521 } catch (NullPointerException e) { | |
522 // expected. | |
523 } | |
524 | |
525 try { | |
526 builder.putInt32ToEnumField(1, null); | |
527 fail(); | |
528 } catch (NullPointerException e) { | |
529 // expected. | |
530 } | |
531 | |
532 try { | |
533 builder.putInt32ToMessageField(1, null); | |
534 fail(); | |
535 } catch (NullPointerException e) { | |
536 // expected. | |
537 } | |
538 | |
539 try { | |
540 builder.putStringToInt32Field(null, 1); | |
541 fail(); | |
542 } catch (NullPointerException e) { | |
543 // expected. | |
544 } | |
545 } | 347 } |
546 | 348 |
547 public void testSerializeAndParse() throws Exception { | 349 public void testSerializeAndParse() throws Exception { |
548 TestMap.Builder builder = TestMap.newBuilder(); | 350 TestMap.Builder builder = TestMap.newBuilder(); |
549 setMapValuesUsingAccessors(builder); | 351 setMapValues(builder); |
550 TestMap message = builder.build(); | 352 TestMap message = builder.build(); |
551 assertEquals(message.getSerializedSize(), message.toByteString().size()); | 353 assertEquals(message.getSerializedSize(), message.toByteString().size()); |
552 message = TestMap.parser().parseFrom(message.toByteString()); | 354 message = TestMap.parser().parseFrom(message.toByteString()); |
553 assertMapValuesSet(message); | 355 assertMapValuesSet(message); |
554 | 356 |
555 builder = message.toBuilder(); | 357 builder = message.toBuilder(); |
556 updateMapValuesUsingAccessors(builder); | 358 updateMapValues(builder); |
557 message = builder.build(); | 359 message = builder.build(); |
558 assertEquals(message.getSerializedSize(), message.toByteString().size()); | 360 assertEquals(message.getSerializedSize(), message.toByteString().size()); |
559 message = TestMap.parser().parseFrom(message.toByteString()); | 361 message = TestMap.parser().parseFrom(message.toByteString()); |
560 assertMapValuesUpdated(message); | 362 assertMapValuesUpdated(message); |
561 | 363 |
562 builder = message.toBuilder(); | 364 builder = message.toBuilder(); |
563 builder.clear(); | 365 builder.clear(); |
564 message = builder.build(); | 366 message = builder.build(); |
565 assertEquals(message.getSerializedSize(), message.toByteString().size()); | 367 assertEquals(message.getSerializedSize(), message.toByteString().size()); |
566 message = TestMap.parser().parseFrom(message.toByteString()); | 368 message = TestMap.parser().parseFrom(message.toByteString()); |
567 assertMapValuesCleared(message); | 369 assertMapValuesCleared(message); |
568 } | 370 } |
569 | 371 |
570 private TestMap tryParseTestMap(BizarroTestMap bizarroMap) throws IOException
{ | |
571 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
572 CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStre
am); | |
573 bizarroMap.writeTo(output); | |
574 output.flush(); | |
575 return TestMap.parser().parseFrom(ByteString.copyFrom(byteArrayOutputStream.
toByteArray())); | |
576 } | |
577 | |
578 public void testParseError() throws Exception { | |
579 ByteString bytes = TestUtil.toBytes("SOME BYTES"); | |
580 String stringKey = "a string key"; | |
581 | |
582 TestMap map = tryParseTestMap(BizarroTestMap.newBuilder() | |
583 .putInt32ToInt32Field(5, bytes) | |
584 .build()); | |
585 assertEquals(map.getInt32ToInt32FieldOrDefault(5, -1), 0); | |
586 | |
587 map = tryParseTestMap(BizarroTestMap.newBuilder() | |
588 .putInt32ToStringField(stringKey, 5) | |
589 .build()); | |
590 assertEquals(map.getInt32ToStringFieldOrDefault(0, null), ""); | |
591 | |
592 map = tryParseTestMap(BizarroTestMap.newBuilder() | |
593 .putInt32ToBytesField(stringKey, 5) | |
594 .build()); | |
595 assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); | |
596 | |
597 map = tryParseTestMap(BizarroTestMap.newBuilder() | |
598 .putInt32ToEnumField(stringKey, bytes) | |
599 .build()); | |
600 assertEquals(map.getInt32ToEnumFieldOrDefault(0, null), TestMap.EnumValue.FO
O); | |
601 | |
602 try { | |
603 tryParseTestMap(BizarroTestMap.newBuilder() | |
604 .putInt32ToMessageField(stringKey, bytes) | |
605 .build()); | |
606 fail(); | |
607 } catch (InvalidProtocolBufferException expected) { | |
608 assertTrue(expected.getUnfinishedMessage() instanceof TestMap); | |
609 map = (TestMap) expected.getUnfinishedMessage(); | |
610 assertTrue(map.getInt32ToMessageField().isEmpty()); | |
611 } | |
612 | |
613 map = tryParseTestMap(BizarroTestMap.newBuilder() | |
614 .putStringToInt32Field(stringKey, bytes) | |
615 .build()); | |
616 assertEquals(map.getStringToInt32FieldOrDefault(stringKey, -1), 0); | |
617 } | |
618 | |
619 public void testMergeFrom() throws Exception { | 372 public void testMergeFrom() throws Exception { |
620 TestMap.Builder builder = TestMap.newBuilder(); | 373 TestMap.Builder builder = TestMap.newBuilder(); |
621 setMapValuesUsingAccessors(builder); | 374 setMapValues(builder); |
622 TestMap message = builder.build(); | 375 TestMap message = builder.build(); |
623 | 376 |
624 TestMap.Builder other = TestMap.newBuilder(); | 377 TestMap.Builder other = TestMap.newBuilder(); |
625 other.mergeFrom(message); | 378 other.mergeFrom(message); |
626 assertMapValuesSet(other.build()); | 379 assertMapValuesSet(other.build()); |
627 } | 380 } |
628 | 381 |
629 public void testEqualsAndHashCode() throws Exception { | 382 public void testEqualsAndHashCode() throws Exception { |
630 // Test that generated equals() and hashCode() will disregard the order | 383 // Test that generated equals() and hashCode() will disregard the order |
631 // of map entries when comparing/hashing map fields. | 384 // of map entries when comparing/hashing map fields. |
632 | 385 |
633 // We can't control the order of elements in a HashMap. The best we can do | 386 // We can't control the order of elements in a HashMap. The best we can do |
634 // here is to add elements in different order. | 387 // here is to add elements in different order. |
635 TestMap.Builder b1 = TestMap.newBuilder() | 388 TestMap.Builder b1 = TestMap.newBuilder(); |
636 .putInt32ToInt32Field(1, 2) | 389 b1.getMutableInt32ToInt32Field().put(1, 2); |
637 .putInt32ToInt32Field(3, 4) | 390 b1.getMutableInt32ToInt32Field().put(3, 4); |
638 .putInt32ToInt32Field(5, 6); | 391 b1.getMutableInt32ToInt32Field().put(5, 6); |
639 TestMap m1 = b1.build(); | 392 TestMap m1 = b1.build(); |
640 | 393 |
641 TestMap.Builder b2 = TestMap.newBuilder() | 394 TestMap.Builder b2 = TestMap.newBuilder(); |
642 .putInt32ToInt32Field(5, 6) | 395 b2.getMutableInt32ToInt32Field().put(5, 6); |
643 .putInt32ToInt32Field(1, 2) | 396 b2.getMutableInt32ToInt32Field().put(1, 2); |
644 .putInt32ToInt32Field(3, 4); | 397 b2.getMutableInt32ToInt32Field().put(3, 4); |
645 TestMap m2 = b2.build(); | 398 TestMap m2 = b2.build(); |
646 | 399 |
647 assertEquals(m1, m2); | 400 assertEquals(m1, m2); |
648 assertEquals(m1.hashCode(), m2.hashCode()); | 401 assertEquals(m1.hashCode(), m2.hashCode()); |
649 | 402 |
650 // Make sure we did compare map fields. | 403 // Make sure we did compare map fields. |
651 b2.putInt32ToInt32Field(1, 0); | 404 b2.getMutableInt32ToInt32Field().put(1, 0); |
652 m2 = b2.build(); | 405 m2 = b2.build(); |
653 assertFalse(m1.equals(m2)); | 406 assertFalse(m1.equals(m2)); |
654 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed | 407 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed |
655 // to be different. | 408 // to be different. |
656 | 409 |
657 // Regression test for b/18549190: if a map is a subset of the other map, | 410 // Regression test for b/18549190: if a map is a subset of the other map, |
658 // equals() should return false. | 411 // equals() should return false. |
659 b2.removeInt32ToInt32Field(1); | 412 b2.getMutableInt32ToInt32Field().remove(1); |
660 m2 = b2.build(); | 413 m2 = b2.build(); |
661 assertFalse(m1.equals(m2)); | 414 assertFalse(m1.equals(m2)); |
662 assertFalse(m2.equals(m1)); | 415 assertFalse(m2.equals(m1)); |
663 } | 416 } |
664 | 417 |
665 public void testNestedBuilderOnChangeEventPropagation() { | 418 public void testNestedBuilderOnChangeEventPropagation() { |
666 TestOnChangeEventPropagation.Builder parent = | 419 TestOnChangeEventPropagation.Builder parent = |
667 TestOnChangeEventPropagation.newBuilder(); | 420 TestOnChangeEventPropagation.newBuilder(); |
668 parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 2); | 421 parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2); |
669 TestOnChangeEventPropagation message = parent.build(); | 422 TestOnChangeEventPropagation message = parent.build(); |
670 assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); | 423 assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); |
671 | 424 |
672 // Make a change using nested builder. | 425 // Make a change using nested builder. |
673 parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 3); | 426 parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3); |
674 | 427 |
675 // Should be able to observe the change. | 428 // Should be able to observe the change. |
676 message = parent.build(); | 429 message = parent.build(); |
677 assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); | 430 assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); |
678 | 431 |
679 // Make another change using mergeFrom() | 432 // Make another change using mergeFrom() |
680 TestMap.Builder other = TestMap.newBuilder().putInt32ToInt32Field(1, 4); | 433 TestMap.Builder other = TestMap.newBuilder(); |
| 434 other.getMutableInt32ToInt32Field().put(1, 4); |
681 parent.getOptionalMessageBuilder().mergeFrom(other.build()); | 435 parent.getOptionalMessageBuilder().mergeFrom(other.build()); |
682 | 436 |
683 // Should be able to observe the change. | 437 // Should be able to observe the change. |
684 message = parent.build(); | 438 message = parent.build(); |
685 assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); | 439 assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); |
686 | 440 |
687 // Make yet another change by clearing the nested builder. | 441 // Make yet another change by clearing the nested builder. |
688 parent.getOptionalMessageBuilder().clear(); | 442 parent.getOptionalMessageBuilder().clear(); |
689 | 443 |
690 // Should be able to observe the change. | 444 // Should be able to observe the change. |
691 message = parent.build(); | 445 message = parent.build(); |
692 assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size()); | 446 assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size()); |
693 } | 447 } |
694 | 448 |
695 public void testNestedBuilderOnChangeEventPropagationReflection() { | 449 public void testNestedBuilderOnChangeEventPropagationReflection() { |
696 FieldDescriptor intMapField = f("int32_to_int32_field"); | 450 FieldDescriptor intMapField = f("int32_to_int32_field"); |
697 // Create an outer message builder with nested builder. | 451 // Create an outer message builder with nested builder. |
698 TestOnChangeEventPropagation.Builder parentBuilder = | 452 TestOnChangeEventPropagation.Builder parentBuilder = |
699 TestOnChangeEventPropagation.newBuilder(); | 453 TestOnChangeEventPropagation.newBuilder(); |
700 TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder(); | 454 TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder(); |
701 | 455 |
702 // Create a map entry message. | 456 // Create a map entry message. |
703 TestMap.Builder entryBuilder = TestMap.newBuilder().putInt32ToInt32Field(1,
1); | 457 TestMap.Builder entryBuilder = TestMap.newBuilder(); |
| 458 entryBuilder.getMutableInt32ToInt32Field().put(1, 1); |
704 | 459 |
705 // Put the entry into the nested builder. | 460 // Put the entry into the nested builder. |
706 testMapBuilder.addRepeatedField( | 461 testMapBuilder.addRepeatedField( |
707 intMapField, entryBuilder.getRepeatedField(intMapField, 0)); | 462 intMapField, entryBuilder.getRepeatedField(intMapField, 0)); |
708 | 463 |
709 // Should be able to observe the change. | 464 // Should be able to observe the change. |
710 TestOnChangeEventPropagation message = parentBuilder.build(); | 465 TestOnChangeEventPropagation message = parentBuilder.build(); |
711 assertEquals(1, message.getOptionalMessage().getInt32ToInt32Field().size()); | 466 assertEquals(1, message.getOptionalMessage().getInt32ToInt32Field().size()); |
712 | 467 |
713 // Change the entry value. | 468 // Change the entry value. |
714 entryBuilder.putInt32ToInt32Field(1, 4); | 469 entryBuilder.getMutableInt32ToInt32Field().put(1, 4); |
715 testMapBuilder = parentBuilder.getOptionalMessageBuilder(); | 470 testMapBuilder = parentBuilder.getOptionalMessageBuilder(); |
716 testMapBuilder.setRepeatedField( | 471 testMapBuilder.setRepeatedField( |
717 intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); | 472 intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); |
718 | 473 |
719 // Should be able to observe the change. | 474 // Should be able to observe the change. |
720 message = parentBuilder.build(); | 475 message = parentBuilder.build(); |
721 assertEquals(4, | 476 assertEquals(4, |
722 message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); | 477 message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); |
723 | 478 |
724 // Clear the nested builder. | 479 // Clear the nested builder. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 Map<KeyType, ValueType> mapForValues( | 546 Map<KeyType, ValueType> mapForValues( |
792 KeyType key1, ValueType value1, KeyType key2, ValueType value2) { | 547 KeyType key1, ValueType value1, KeyType key2, ValueType value2) { |
793 Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>(); | 548 Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>(); |
794 map.put(key1, value1); | 549 map.put(key1, value1); |
795 map.put(key2, value2); | 550 map.put(key2, value2); |
796 return map; | 551 return map; |
797 } | 552 } |
798 | 553 |
799 public void testReflectionApi() throws Exception { | 554 public void testReflectionApi() throws Exception { |
800 // In reflection API, map fields are just repeated message fields. | 555 // In reflection API, map fields are just repeated message fields. |
801 TestMap.Builder builder = TestMap.newBuilder() | 556 TestMap.Builder builder = TestMap.newBuilder(); |
802 .putInt32ToInt32Field(1, 2) | 557 builder.getMutableInt32ToInt32Field().put(1, 2); |
803 .putInt32ToInt32Field(3, 4) | 558 builder.getMutableInt32ToInt32Field().put(3, 4); |
804 .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build
()) | 559 builder.getMutableInt32ToMessageField().put( |
805 .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build
()); | 560 11, MessageValue.newBuilder().setValue(22).build()); |
| 561 builder.getMutableInt32ToMessageField().put( |
| 562 33, MessageValue.newBuilder().setValue(44).build()); |
806 TestMap message = builder.build(); | 563 TestMap message = builder.build(); |
807 | 564 |
808 // Test getField(), getRepeatedFieldCount(), getRepeatedField(). | 565 // Test getField(), getRepeatedFieldCount(), getRepeatedField(). |
809 assertHasMapValues(message, "int32_to_int32_field", | 566 assertHasMapValues(message, "int32_to_int32_field", |
810 mapForValues(1, 2, 3, 4)); | 567 mapForValues(1, 2, 3, 4)); |
811 assertHasMapValues(message, "int32_to_message_field", | 568 assertHasMapValues(message, "int32_to_message_field", |
812 mapForValues( | 569 mapForValues( |
813 11, MessageValue.newBuilder().setValue(22).build(), | 570 11, MessageValue.newBuilder().setValue(22).build(), |
814 33, MessageValue.newBuilder().setValue(44).build())); | 571 33, MessageValue.newBuilder().setValue(44).build())); |
815 | 572 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 builder.setRepeatedField(f("int32_to_int32_field"), i, mapEntryBuilder.bui
ld()); | 622 builder.setRepeatedField(f("int32_to_int32_field"), i, mapEntryBuilder.bui
ld()); |
866 } | 623 } |
867 message = builder.build(); | 624 message = builder.build(); |
868 assertEquals(11, message.getInt32ToInt32Field().get(22).intValue()); | 625 assertEquals(11, message.getInt32ToInt32Field().get(22).intValue()); |
869 assertEquals(33, message.getInt32ToInt32Field().get(44).intValue()); | 626 assertEquals(33, message.getInt32ToInt32Field().get(44).intValue()); |
870 assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); | 627 assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); |
871 } | 628 } |
872 | 629 |
873 public void testTextFormat() throws Exception { | 630 public void testTextFormat() throws Exception { |
874 TestMap.Builder builder = TestMap.newBuilder(); | 631 TestMap.Builder builder = TestMap.newBuilder(); |
875 setMapValuesUsingAccessors(builder); | 632 setMapValues(builder); |
876 TestMap message = builder.build(); | 633 TestMap message = builder.build(); |
877 | 634 |
878 String textData = TextFormat.printToString(message); | 635 String textData = TextFormat.printToString(message); |
879 | 636 |
880 builder = TestMap.newBuilder(); | 637 builder = TestMap.newBuilder(); |
881 TextFormat.merge(textData, builder); | 638 TextFormat.merge(textData, builder); |
882 message = builder.build(); | 639 message = builder.build(); |
883 | 640 |
884 assertMapValuesSet(message); | 641 assertMapValuesSet(message); |
885 } | 642 } |
886 | 643 |
887 public void testDynamicMessage() throws Exception { | 644 public void testDynamicMessage() throws Exception { |
888 TestMap.Builder builder = TestMap.newBuilder(); | 645 TestMap.Builder builder = TestMap.newBuilder(); |
889 setMapValuesUsingAccessors(builder); | 646 setMapValues(builder); |
890 TestMap message = builder.build(); | 647 TestMap message = builder.build(); |
891 | 648 |
892 Message dynamicDefaultInstance = | 649 Message dynamicDefaultInstance = |
893 DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); | 650 DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); |
894 Message dynamicMessage = dynamicDefaultInstance | 651 Message dynamicMessage = dynamicDefaultInstance |
895 .newBuilderForType().mergeFrom(message.toByteString()).build(); | 652 .newBuilderForType().mergeFrom(message.toByteString()).build(); |
896 | 653 |
897 assertEquals(message, dynamicMessage); | 654 assertEquals(message, dynamicMessage); |
898 assertEquals(message.hashCode(), dynamicMessage.hashCode()); | 655 assertEquals(message.hashCode(), dynamicMessage.hashCode()); |
899 } | 656 } |
(...skipping 24 matching lines...) Expand all Loading... |
924 | 681 |
925 // Make sure we did compare map fields. | 682 // Make sure we did compare map fields. |
926 b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0))
; | 683 b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0))
; |
927 m2 = b2.build(); | 684 m2 = b2.build(); |
928 assertFalse(m1.equals(m2)); | 685 assertFalse(m1.equals(m2)); |
929 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed | 686 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed |
930 // to be different. | 687 // to be different. |
931 } | 688 } |
932 | 689 |
933 public void testUnknownEnumValues() throws Exception { | 690 public void testUnknownEnumValues() throws Exception { |
934 TestMap.Builder builder = TestMap.newBuilder() | 691 TestMap.Builder builder = TestMap.newBuilder(); |
935 .putAllInt32ToEnumFieldValue(newMap( | 692 builder.getMutableInt32ToEnumFieldValue().put(0, 0); |
936 0, 0, | 693 builder.getMutableInt32ToEnumFieldValue().put(1, 1); |
937 1, 1, | 694 builder.getMutableInt32ToEnumFieldValue().put(2, 1000); // unknown value. |
938 2, 1000)); // unknown value. | |
939 TestMap message = builder.build(); | 695 TestMap message = builder.build(); |
940 | 696 |
941 assertEquals(TestMap.EnumValue.FOO, | 697 assertEquals(TestMap.EnumValue.FOO, |
942 message.getInt32ToEnumField().get(0)); | 698 message.getInt32ToEnumField().get(0)); |
943 assertEquals(TestMap.EnumValue.BAR, | 699 assertEquals(TestMap.EnumValue.BAR, |
944 message.getInt32ToEnumField().get(1)); | 700 message.getInt32ToEnumField().get(1)); |
945 assertEquals(TestMap.EnumValue.UNRECOGNIZED, | 701 assertEquals(TestMap.EnumValue.UNRECOGNIZED, |
946 message.getInt32ToEnumField().get(2)); | 702 message.getInt32ToEnumField().get(2)); |
947 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); | 703 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); |
948 | 704 |
949 // Unknown enum values should be preserved after: | 705 // Unknown enum values should be preserved after: |
950 // 1. Serialization and parsing. | 706 // 1. Serialization and parsing. |
951 // 2. toBuild(). | 707 // 2. toBuild(). |
952 // 3. mergeFrom(). | 708 // 3. mergeFrom(). |
953 message = TestMap.parseFrom(message.toByteString()); | 709 message = TestMap.parseFrom(message.toByteString()); |
954 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); | 710 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); |
955 builder = message.toBuilder(); | 711 builder = message.toBuilder(); |
956 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); | 712 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); |
957 builder = TestMap.newBuilder().mergeFrom(message); | 713 builder = TestMap.newBuilder().mergeFrom(message); |
958 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); | 714 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); |
959 | 715 |
960 // hashCode()/equals() should take unknown enum values into account. | 716 // hashCode()/equals() should take unknown enum values into account. |
961 builder.putAllInt32ToEnumFieldValue(newMap(2, 1001)); | 717 builder.getMutableInt32ToEnumFieldValue().put(2, 1001); |
962 TestMap message2 = builder.build(); | 718 TestMap message2 = builder.build(); |
963 assertFalse(message.hashCode() == message2.hashCode()); | 719 assertFalse(message.hashCode() == message2.hashCode()); |
964 assertFalse(message.equals(message2)); | 720 assertFalse(message.equals(message2)); |
965 // Unknown values will be converted to UNRECOGNIZED so the resulted enum map | 721 // Unknown values will be converted to UNRECOGNIZED so the resulted enum map |
966 // should be the same. | 722 // should be the same. |
967 assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField
())); | 723 assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField
())); |
968 } | 724 } |
969 | 725 |
970 public void testUnknownEnumValuesInReflectionApi() throws Exception { | 726 public void testUnknownEnumValuesInReflectionApi() throws Exception { |
971 Descriptor descriptor = TestMap.getDescriptor(); | 727 Descriptor descriptor = TestMap.getDescriptor(); |
972 EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); | 728 EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); |
973 FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); | 729 FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); |
974 | 730 |
975 Map<Integer, Integer> data = newMap( | 731 Map<Integer, Integer> data = new HashMap<Integer, Integer>(); |
976 0, 0, | 732 data.put(0, 0); |
977 1, 1, | 733 data.put(1, 1); |
978 2, 1000); // unknown value | 734 data.put(2, 1000); // unknown value. |
979 | 735 |
980 TestMap.Builder builder = TestMap.newBuilder() | 736 TestMap.Builder builder = TestMap.newBuilder(); |
981 .putAllInt32ToEnumFieldValue(data); | 737 for (Map.Entry<Integer, Integer> entry : data.entrySet()) { |
| 738 builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getVal
ue()); |
| 739 } |
982 | 740 |
983 // Try to read unknown enum values using reflection API. | 741 // Try to read unknown enum values using reflection API. |
984 for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) { | 742 for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) { |
985 Message mapEntry = (Message) builder.getRepeatedField(field, i); | 743 Message mapEntry = (Message) builder.getRepeatedField(field, i); |
986 int key = ((Integer) getFieldValue(mapEntry, "key")).intValue(); | 744 int key = ((Integer) getFieldValue(mapEntry, "key")).intValue(); |
987 int value = ((EnumValueDescriptor) getFieldValue(mapEntry, "value")).getNu
mber(); | 745 int value = ((EnumValueDescriptor) getFieldValue(mapEntry, "value")).getNu
mber(); |
988 assertEquals(data.get(key).intValue(), value); | 746 assertEquals(data.get(key).intValue(), value); |
989 Message.Builder mapEntryBuilder = mapEntry.toBuilder(); | 747 Message.Builder mapEntryBuilder = mapEntry.toBuilder(); |
990 // Increase the value by 1. | 748 // Increase the value by 1. |
991 setFieldValue(mapEntryBuilder, "value", | 749 setFieldValue(mapEntryBuilder, "value", |
992 enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1)); | 750 enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1)); |
993 builder.setRepeatedField(field, i, mapEntryBuilder.build()); | 751 builder.setRepeatedField(field, i, mapEntryBuilder.build()); |
994 } | 752 } |
995 | 753 |
996 // Verify that enum values have been successfully updated. | 754 // Verify that enum values have been successfully updated. |
997 TestMap message = builder.build(); | 755 TestMap message = builder.build(); |
998 for (Map.Entry<Integer, Integer> entry : message.getInt32ToEnumFieldValue().
entrySet()) { | 756 for (Map.Entry<Integer, Integer> entry : message.getInt32ToEnumFieldValue().
entrySet()) { |
999 assertEquals(data.get(entry.getKey()) + 1, entry.getValue().intValue()); | 757 assertEquals(data.get(entry.getKey()) + 1, entry.getValue().intValue()); |
1000 } | 758 } |
1001 } | 759 } |
1002 | 760 |
1003 public void testIterationOrder() throws Exception { | 761 public void testIterationOrder() throws Exception { |
1004 TestMap.Builder builder = TestMap.newBuilder(); | 762 TestMap.Builder builder = TestMap.newBuilder(); |
1005 setMapValuesUsingAccessors(builder); | 763 setMapValues(builder); |
1006 TestMap message = builder.build(); | 764 TestMap message = builder.build(); |
1007 | 765 |
1008 assertEquals(Arrays.asList("1", "2", "3"), | 766 assertEquals(Arrays.asList("1", "2", "3"), |
1009 new ArrayList<String>(message.getStringToInt32Field().keySet())); | 767 new ArrayList<String>(message.getStringToInt32Field().keySet())); |
1010 } | 768 } |
1011 | 769 |
1012 public void testGetMap() { | |
1013 TestMap.Builder builder = TestMap.newBuilder(); | |
1014 setMapValuesUsingAccessors(builder); | |
1015 TestMap message = builder.build(); | |
1016 assertEquals( | |
1017 message.getStringToInt32Field(), | |
1018 message.getStringToInt32FieldMap()); | |
1019 assertEquals( | |
1020 message.getInt32ToBytesField(), | |
1021 message.getInt32ToBytesFieldMap()); | |
1022 assertEquals( | |
1023 message.getInt32ToEnumField(), | |
1024 message.getInt32ToEnumFieldMap()); | |
1025 assertEquals( | |
1026 message.getInt32ToEnumFieldValue(), | |
1027 message.getInt32ToEnumFieldValueMap()); | |
1028 assertEquals( | |
1029 message.getInt32ToMessageField(), | |
1030 message.getInt32ToMessageFieldMap()); | |
1031 } | |
1032 | |
1033 public void testContains() { | |
1034 TestMap.Builder builder = TestMap.newBuilder(); | |
1035 setMapValuesUsingAccessors(builder); | |
1036 assertMapContainsSetValues(builder); | |
1037 assertMapContainsSetValues(builder.build()); | |
1038 } | |
1039 | |
1040 private void assertMapContainsSetValues(TestMapOrBuilder testMapOrBuilder) { | |
1041 assertTrue(testMapOrBuilder.containsInt32ToInt32Field(1)); | |
1042 assertTrue(testMapOrBuilder.containsInt32ToInt32Field(2)); | |
1043 assertTrue(testMapOrBuilder.containsInt32ToInt32Field(3)); | |
1044 assertFalse(testMapOrBuilder.containsInt32ToInt32Field(-1)); | |
1045 | |
1046 assertTrue(testMapOrBuilder.containsInt32ToStringField(1)); | |
1047 assertTrue(testMapOrBuilder.containsInt32ToStringField(2)); | |
1048 assertTrue(testMapOrBuilder.containsInt32ToStringField(3)); | |
1049 assertFalse(testMapOrBuilder.containsInt32ToStringField(-1)); | |
1050 | |
1051 assertTrue(testMapOrBuilder.containsInt32ToBytesField(1)); | |
1052 assertTrue(testMapOrBuilder.containsInt32ToBytesField(2)); | |
1053 assertTrue(testMapOrBuilder.containsInt32ToBytesField(3)); | |
1054 assertFalse(testMapOrBuilder.containsInt32ToBytesField(-1)); | |
1055 | |
1056 assertTrue(testMapOrBuilder.containsInt32ToEnumField(1)); | |
1057 assertTrue(testMapOrBuilder.containsInt32ToEnumField(2)); | |
1058 assertTrue(testMapOrBuilder.containsInt32ToEnumField(3)); | |
1059 assertFalse(testMapOrBuilder.containsInt32ToEnumField(-1)); | |
1060 | |
1061 assertTrue(testMapOrBuilder.containsInt32ToMessageField(1)); | |
1062 assertTrue(testMapOrBuilder.containsInt32ToMessageField(2)); | |
1063 assertTrue(testMapOrBuilder.containsInt32ToMessageField(3)); | |
1064 assertFalse(testMapOrBuilder.containsInt32ToMessageField(-1)); | |
1065 | |
1066 assertTrue(testMapOrBuilder.containsStringToInt32Field("1")); | |
1067 assertTrue(testMapOrBuilder.containsStringToInt32Field("2")); | |
1068 assertTrue(testMapOrBuilder.containsStringToInt32Field("3")); | |
1069 assertFalse(testMapOrBuilder.containsStringToInt32Field("-1")); | |
1070 } | |
1071 | |
1072 public void testCount() { | |
1073 TestMap.Builder builder = TestMap.newBuilder(); | |
1074 assertMapCounts(0, builder); | |
1075 | |
1076 setMapValuesUsingAccessors(builder); | |
1077 assertMapCounts(3, builder); | |
1078 | |
1079 TestMap message = builder.build(); | |
1080 assertMapCounts(3, message); | |
1081 | |
1082 builder = message.toBuilder().putInt32ToInt32Field(4, 44); | |
1083 assertEquals(4, builder.getInt32ToInt32FieldCount()); | |
1084 assertEquals(4, builder.build().getInt32ToInt32FieldCount()); | |
1085 | |
1086 // already present - should be unchanged | |
1087 builder.putInt32ToInt32Field(4, 44); | |
1088 assertEquals(4, builder.getInt32ToInt32FieldCount()); | |
1089 } | |
1090 | |
1091 private void assertMapCounts(int expectedCount, TestMapOrBuilder testMapOrBuil
der) { | |
1092 assertEquals(expectedCount, testMapOrBuilder.getInt32ToInt32FieldCount()); | |
1093 assertEquals(expectedCount, testMapOrBuilder.getInt32ToStringFieldCount()); | |
1094 assertEquals(expectedCount, testMapOrBuilder.getInt32ToBytesFieldCount()); | |
1095 assertEquals(expectedCount, testMapOrBuilder.getInt32ToEnumFieldCount()); | |
1096 assertEquals(expectedCount, testMapOrBuilder.getInt32ToMessageFieldCount()); | |
1097 assertEquals(expectedCount, testMapOrBuilder.getStringToInt32FieldCount()); | |
1098 } | |
1099 | |
1100 public void testGetOrDefault() { | |
1101 TestMap.Builder builder = TestMap.newBuilder(); | |
1102 assertMapCounts(0, builder); | |
1103 setMapValuesUsingAccessors(builder); | |
1104 doTestGetOrDefault(builder); | |
1105 doTestGetOrDefault(builder.build()); | |
1106 } | |
1107 | |
1108 public void doTestGetOrDefault(TestMapOrBuilder testMapOrBuilder) { | |
1109 assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(1, -11)); | |
1110 assertEquals(-11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(-1, -11)); | |
1111 | |
1112 assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrDefault(1, "-11")
); | |
1113 assertNull("-11", testMapOrBuilder.getInt32ToStringFieldOrDefault(-1, null))
; | |
1114 | |
1115 assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOr
Default(1, null)); | |
1116 assertNull(testMapOrBuilder.getInt32ToBytesFieldOrDefault(-1, null)); | |
1117 | |
1118 assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDe
fault(1, null)); | |
1119 assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null)); | |
1120 | |
1121 assertEquals( | |
1122 TestMap.EnumValue.BAR.getNumber(), | |
1123 (int) testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(2, -1)); | |
1124 assertEquals(-1, testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(-1000, -
1)); | |
1125 | |
1126 assertEquals(MessageValue.newBuilder().setValue(11).build(), | |
1127 testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); | |
1128 assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); | |
1129 | |
1130 assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrDefault("1", -11)); | |
1131 assertEquals(-11, testMapOrBuilder.getStringToInt32FieldOrDefault("-1", -11)
); | |
1132 | |
1133 try { | |
1134 testMapOrBuilder.getStringToInt32FieldOrDefault(null, -11); | |
1135 fail(); | |
1136 } catch (NullPointerException e) { | |
1137 // expected | |
1138 } | |
1139 } | |
1140 | |
1141 public void testGetOrThrow() { | |
1142 TestMap.Builder builder = TestMap.newBuilder(); | |
1143 assertMapCounts(0, builder); | |
1144 setMapValuesUsingAccessors(builder); | |
1145 doTestGetOrDefault(builder); | |
1146 doTestGetOrDefault(builder.build()); | |
1147 } | |
1148 | |
1149 public void doTestGetOrThrow(TestMapOrBuilder testMapOrBuilder) { | |
1150 assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrThrow(1)); | |
1151 try { | |
1152 testMapOrBuilder.getInt32ToInt32FieldOrThrow(-1); | |
1153 fail(); | |
1154 } catch (IllegalArgumentException e) { | |
1155 // expected | |
1156 } | |
1157 | |
1158 assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrThrow(1)); | |
1159 | |
1160 try { | |
1161 testMapOrBuilder.getInt32ToStringFieldOrThrow(-1); | |
1162 fail(); | |
1163 } catch (IllegalArgumentException e) { | |
1164 // expected | |
1165 } | |
1166 | |
1167 assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOr
Throw(1)); | |
1168 | |
1169 try { | |
1170 testMapOrBuilder.getInt32ToBytesFieldOrThrow(-1); | |
1171 fail(); | |
1172 } catch (IllegalArgumentException e) { | |
1173 // expected | |
1174 } | |
1175 | |
1176 assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrTh
row(1)); | |
1177 try { | |
1178 testMapOrBuilder.getInt32ToEnumFieldOrThrow(-1); | |
1179 fail(); | |
1180 } catch (IllegalArgumentException e) { | |
1181 // expected | |
1182 } | |
1183 | |
1184 assertEquals( | |
1185 TestMap.EnumValue.BAR.getNumber(), testMapOrBuilder.getInt32ToEnumFieldV
alueOrThrow(2)); | |
1186 try { | |
1187 testMapOrBuilder.getInt32ToEnumFieldValueOrThrow(-1); | |
1188 fail(); | |
1189 } catch (IllegalArgumentException e) { | |
1190 // expected | |
1191 } | |
1192 | |
1193 assertEquals(MessageValue.newBuilder().setValue(11).build(), | |
1194 testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); | |
1195 try { | |
1196 testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); | |
1197 fail(); | |
1198 } catch (IllegalArgumentException e) { | |
1199 // expected | |
1200 } | |
1201 | |
1202 assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrThrow("1")); | |
1203 try { | |
1204 testMapOrBuilder.getStringToInt32FieldOrThrow("-1"); | |
1205 fail(); | |
1206 } catch (IllegalArgumentException e) { | |
1207 // expected | |
1208 } | |
1209 | |
1210 try { | |
1211 testMapOrBuilder.getStringToInt32FieldOrThrow(null); | |
1212 fail(); | |
1213 } catch (NullPointerException e) { | |
1214 // expected | |
1215 } | |
1216 } | |
1217 | |
1218 public void testPut() { | |
1219 TestMap.Builder builder = TestMap.newBuilder(); | |
1220 builder.putInt32ToInt32Field(1, 11); | |
1221 assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); | |
1222 | |
1223 builder.putInt32ToStringField(1, "a"); | |
1224 assertEquals("a", builder.getInt32ToStringFieldOrThrow(1)); | |
1225 try { | |
1226 builder.putInt32ToStringField(1, null); | |
1227 fail(); | |
1228 } catch (NullPointerException e) { | |
1229 // expected | |
1230 } | |
1231 | |
1232 builder.putInt32ToBytesField(1, TestUtil.toBytes("11")); | |
1233 assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1))
; | |
1234 try { | |
1235 builder.putInt32ToBytesField(1, null); | |
1236 fail(); | |
1237 } catch (NullPointerException e) { | |
1238 // expected | |
1239 } | |
1240 | |
1241 builder.putInt32ToEnumField(1, TestMap.EnumValue.FOO); | |
1242 assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); | |
1243 try { | |
1244 builder.putInt32ToEnumField(1, null); | |
1245 fail(); | |
1246 } catch (NullPointerException e) { | |
1247 // expected | |
1248 } | |
1249 | |
1250 builder.putInt32ToEnumFieldValue(1, TestMap.EnumValue.BAR.getNumber()); | |
1251 assertEquals( | |
1252 TestMap.EnumValue.BAR.getNumber(), builder.getInt32ToEnumFieldValueOrThr
ow(1)); | |
1253 try { | |
1254 builder.putInt32ToEnumFieldValue(1, -1); | |
1255 fail(); | |
1256 } catch (IllegalArgumentException e) { | |
1257 // expected | |
1258 } | |
1259 | |
1260 builder.putStringToInt32Field("a", 1); | |
1261 assertEquals(1, builder.getStringToInt32FieldOrThrow("a")); | |
1262 try { | |
1263 builder.putStringToInt32Field(null, -1); | |
1264 } catch (NullPointerException e) { | |
1265 // expected | |
1266 } | |
1267 } | |
1268 | |
1269 public void testRemove() { | |
1270 TestMap.Builder builder = TestMap.newBuilder(); | |
1271 setMapValuesUsingAccessors(builder); | |
1272 assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); | |
1273 for (int times = 0; times < 2; times++) { | |
1274 builder.removeInt32ToInt32Field(1); | |
1275 assertEquals(-1, builder.getInt32ToInt32FieldOrDefault(1, -1)); | |
1276 } | |
1277 | |
1278 assertEquals("11", builder.getInt32ToStringFieldOrThrow(1)); | |
1279 for (int times = 0; times < 2; times++) { | |
1280 builder.removeInt32ToStringField(1); | |
1281 assertNull(builder.getInt32ToStringFieldOrDefault(1, null)); | |
1282 } | |
1283 | |
1284 assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1))
; | |
1285 for (int times = 0; times < 2; times++) { | |
1286 builder.removeInt32ToBytesField(1); | |
1287 assertNull(builder.getInt32ToBytesFieldOrDefault(1, null)); | |
1288 } | |
1289 | |
1290 assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); | |
1291 for (int times = 0; times < 2; times++) { | |
1292 builder.removeInt32ToEnumField(1); | |
1293 assertNull(builder.getInt32ToEnumFieldOrDefault(1, null)); | |
1294 } | |
1295 | |
1296 assertEquals(11, builder.getStringToInt32FieldOrThrow("1")); | |
1297 for (int times = 0; times < 2; times++) { | |
1298 builder.removeStringToInt32Field("1"); | |
1299 assertEquals(-1, builder.getStringToInt32FieldOrDefault("1", -1)); | |
1300 } | |
1301 | |
1302 try { | |
1303 builder.removeStringToInt32Field(null); | |
1304 fail(); | |
1305 } catch (NullPointerException e) { | |
1306 // expected | |
1307 } | |
1308 } | |
1309 | |
1310 public void testReservedWordsFieldNames() { | |
1311 ReservedAsMapField.newBuilder().build(); | |
1312 ReservedAsMapFieldWithEnumValue.newBuilder().build(); | |
1313 } | |
1314 | |
1315 public void testDeterministicSerialziation() throws Exception { | |
1316 TestMap.Builder builder = TestMap.newBuilder(); | |
1317 // int32->int32 | |
1318 builder.putInt32ToInt32Field(5, 1); | |
1319 builder.putInt32ToInt32Field(1, 1); | |
1320 builder.putInt32ToInt32Field(4, 1); | |
1321 builder.putInt32ToInt32Field(-2, 1); | |
1322 builder.putInt32ToInt32Field(0, 1); | |
1323 | |
1324 // uint32->int32 | |
1325 builder.putUint32ToInt32Field(5, 1); | |
1326 builder.putUint32ToInt32Field(1, 1); | |
1327 builder.putUint32ToInt32Field(4, 1); | |
1328 builder.putUint32ToInt32Field(-2, 1); | |
1329 builder.putUint32ToInt32Field(0, 1); | |
1330 | |
1331 // int64->int32 | |
1332 builder.putInt64ToInt32Field(5L, 1); | |
1333 builder.putInt64ToInt32Field(1L, 1); | |
1334 builder.putInt64ToInt32Field(4L, 1); | |
1335 builder.putInt64ToInt32Field(-2L, 1); | |
1336 builder.putInt64ToInt32Field(0L, 1); | |
1337 | |
1338 // string->int32 | |
1339 builder.putStringToInt32Field("baz", 1); | |
1340 builder.putStringToInt32Field("foo", 1); | |
1341 builder.putStringToInt32Field("bar", 1); | |
1342 builder.putStringToInt32Field("", 1); | |
1343 builder.putStringToInt32Field("hello", 1); | |
1344 builder.putStringToInt32Field("world", 1); | |
1345 | |
1346 TestMap message = builder.build(); | |
1347 byte[] serialized = new byte[message.getSerializedSize()]; | |
1348 CodedOutputStream output = CodedOutputStream.newInstance(serialized); | |
1349 output.useDeterministicSerialization(); | |
1350 message.writeTo(output); | |
1351 output.flush(); | |
1352 | |
1353 CodedInputStream input = CodedInputStream.newInstance(serialized); | |
1354 List<Integer> int32Keys = new ArrayList<Integer>(); | |
1355 List<Integer> uint32Keys = new ArrayList<Integer>(); | |
1356 List<Long> int64Keys = new ArrayList<Long>(); | |
1357 List<String> stringKeys = new ArrayList<String>(); | |
1358 int tag; | |
1359 while (true) { | |
1360 tag = input.readTag(); | |
1361 if (tag == 0) { | |
1362 break; | |
1363 } | |
1364 int length = input.readRawVarint32(); | |
1365 int oldLimit = input.pushLimit(length); | |
1366 switch (WireFormat.getTagFieldNumber(tag)) { | |
1367 case TestMap.STRING_TO_INT32_FIELD_FIELD_NUMBER: | |
1368 stringKeys.add(readMapStringKey(input)); | |
1369 break; | |
1370 case TestMap.INT32_TO_INT32_FIELD_FIELD_NUMBER: | |
1371 int32Keys.add(readMapIntegerKey(input)); | |
1372 break; | |
1373 case TestMap.UINT32_TO_INT32_FIELD_FIELD_NUMBER: | |
1374 uint32Keys.add(readMapIntegerKey(input)); | |
1375 break; | |
1376 case TestMap.INT64_TO_INT32_FIELD_FIELD_NUMBER: | |
1377 int64Keys.add(readMapLongKey(input)); | |
1378 break; | |
1379 default: | |
1380 fail("Unexpected fields."); | |
1381 } | |
1382 input.popLimit(oldLimit); | |
1383 } | |
1384 assertEquals( | |
1385 Arrays.asList(-2, 0, 1, 4, 5), | |
1386 int32Keys); | |
1387 assertEquals( | |
1388 Arrays.asList(-2, 0, 1, 4, 5), | |
1389 uint32Keys); | |
1390 assertEquals( | |
1391 Arrays.asList(-2L, 0L, 1L, 4L, 5L), | |
1392 int64Keys); | |
1393 assertEquals( | |
1394 Arrays.asList("", "bar", "baz", "foo", "hello", "world"), | |
1395 stringKeys); | |
1396 } | |
1397 | |
1398 public void testInitFromPartialDynamicMessage() { | |
1399 FieldDescriptor fieldDescriptor = | |
1400 TestMap.getDescriptor().findFieldByNumber(TestMap.INT32_TO_MESSAGE_FIELD
_FIELD_NUMBER); | |
1401 Descriptor mapEntryType = fieldDescriptor.getMessageType(); | |
1402 FieldDescriptor keyField = mapEntryType.findFieldByNumber(1); | |
1403 FieldDescriptor valueField = mapEntryType.findFieldByNumber(2); | |
1404 DynamicMessage dynamicMessage = | |
1405 DynamicMessage.newBuilder(TestMap.getDescriptor()) | |
1406 .addRepeatedField( | |
1407 fieldDescriptor, | |
1408 DynamicMessage.newBuilder(mapEntryType) | |
1409 .setField(keyField, 10) | |
1410 .setField(valueField, TestMap.MessageValue.newBuilder().setV
alue(10).build()) | |
1411 .build()) | |
1412 .build(); | |
1413 TestMap message = TestMap.newBuilder().mergeFrom(dynamicMessage).build(); | |
1414 assertEquals( | |
1415 TestMap.MessageValue.newBuilder().setValue(10).build(), | |
1416 message.getInt32ToMessageFieldMap().get(10)); | |
1417 } | |
1418 | |
1419 public void testInitFromFullyDynamicMessage() { | |
1420 FieldDescriptor fieldDescriptor = | |
1421 TestMap.getDescriptor().findFieldByNumber(TestMap.INT32_TO_MESSAGE_FIELD
_FIELD_NUMBER); | |
1422 Descriptor mapEntryType = fieldDescriptor.getMessageType(); | |
1423 FieldDescriptor keyField = mapEntryType.findFieldByNumber(1); | |
1424 FieldDescriptor valueField = mapEntryType.findFieldByNumber(2); | |
1425 DynamicMessage dynamicMessage = | |
1426 DynamicMessage.newBuilder(TestMap.getDescriptor()) | |
1427 .addRepeatedField( | |
1428 fieldDescriptor, | |
1429 DynamicMessage.newBuilder(mapEntryType) | |
1430 .setField(keyField, 10) | |
1431 .setField( | |
1432 valueField, | |
1433 DynamicMessage.newBuilder(TestMap.MessageValue.getDescri
ptor()) | |
1434 .setField( | |
1435 TestMap.MessageValue.getDescriptor().findFieldBy
Name("value"), 10) | |
1436 .build()) | |
1437 .build()) | |
1438 .build(); | |
1439 TestMap message = TestMap.newBuilder().mergeFrom(dynamicMessage).build(); | |
1440 assertEquals( | |
1441 TestMap.MessageValue.newBuilder().setValue(10).build(), | |
1442 message.getInt32ToMessageFieldMap().get(10)); | |
1443 } | |
1444 | |
1445 private int readMapIntegerKey(CodedInputStream input) throws IOException { | |
1446 int tag = input.readTag(); | |
1447 assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT), tag); | |
1448 int ret = input.readInt32(); | |
1449 // skip the value field. | |
1450 input.skipField(input.readTag()); | |
1451 assertTrue(input.isAtEnd()); | |
1452 return ret; | |
1453 } | |
1454 | |
1455 private long readMapLongKey(CodedInputStream input) throws IOException { | |
1456 int tag = input.readTag(); | |
1457 assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT), tag); | |
1458 long ret = input.readInt64(); | |
1459 // skip the value field. | |
1460 input.skipField(input.readTag()); | |
1461 assertTrue(input.isAtEnd()); | |
1462 return ret; | |
1463 } | |
1464 | |
1465 private String readMapStringKey(CodedInputStream input) throws IOException { | |
1466 int tag = input.readTag(); | |
1467 assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), ta
g); | |
1468 String ret = input.readString(); | |
1469 // skip the value field. | |
1470 input.skipField(input.readTag()); | |
1471 assertTrue(input.isAtEnd()); | |
1472 return ret; | |
1473 } | |
1474 | |
1475 private static <K, V> Map<K, V> newMap(K key1, V value1) { | 770 private static <K, V> Map<K, V> newMap(K key1, V value1) { |
1476 Map<K, V> map = new HashMap<K, V>(); | 771 Map<K, V> map = new HashMap<K, V>(); |
1477 map.put(key1, value1); | 772 map.put(key1, value1); |
1478 return map; | 773 return map; |
1479 } | 774 } |
1480 | 775 |
1481 private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2) { | 776 private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2) { |
1482 Map<K, V> map = new HashMap<K, V>(); | 777 Map<K, V> map = new HashMap<K, V>(); |
1483 map.put(key1, value1); | 778 map.put(key1, value1); |
1484 map.put(key2, value2); | 779 map.put(key2, value2); |
1485 return map; | 780 return map; |
1486 } | 781 } |
1487 | |
1488 private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2, K k
ey3, V value3) { | |
1489 Map<K, V> map = new HashMap<K, V>(); | |
1490 map.put(key1, value1); | |
1491 map.put(key2, value2); | |
1492 map.put(key3, value3); | |
1493 return map; | |
1494 } | |
1495 } | 782 } |
OLD | NEW |