MapForProto2LiteTest.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30 
31 package com.google.protobuf;
32 
33 import map_lite_test.MapForProto2TestProto.BizarroTestMap;
34 import map_lite_test.MapForProto2TestProto.TestMap;
35 import map_lite_test.MapForProto2TestProto.TestMap.MessageValue;
36 import map_lite_test.MapForProto2TestProto.TestMapOrBuilder;
37 import map_lite_test.MapForProto2TestProto.TestUnknownEnumValue;
38 import java.io.ByteArrayOutputStream;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.Map;
44 import junit.framework.TestCase;
45 
47 public final class MapForProto2LiteTest extends TestCase {
48 
49  private void setMapValues(TestMap.Builder builder) {
50  builder
51  .putInt32ToInt32Field(1, 11)
52  .putInt32ToInt32Field(2, 22)
53  .putInt32ToInt32Field(3, 33)
54  .putInt32ToStringField(1, "11")
55  .putInt32ToStringField(2, "22")
56  .putInt32ToStringField(3, "33")
57  .putInt32ToBytesField(1, TestUtil.toBytes("11"))
58  .putInt32ToBytesField(2, TestUtil.toBytes("22"))
59  .putInt32ToBytesField(3, TestUtil.toBytes("33"))
60  .putInt32ToEnumField(1, TestMap.EnumValue.FOO)
61  .putInt32ToEnumField(2, TestMap.EnumValue.BAR)
62  .putInt32ToEnumField(3, TestMap.EnumValue.BAZ)
63  .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build())
64  .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build())
65  .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build())
66  .putStringToInt32Field("1", 11)
67  .putStringToInt32Field("2", 22)
68  .putStringToInt32Field("3", 33);
69  }
70 
71  public void testSetMapValues() {
72  TestMap.Builder mapBuilder = TestMap.newBuilder();
73  setMapValues(mapBuilder);
74  TestMap map = mapBuilder.build();
76  }
77 
78  private void copyMapValues(TestMap source, TestMap.Builder destination) {
79  destination
80  .putAllInt32ToInt32Field(source.getInt32ToInt32Field())
81  .putAllInt32ToStringField(source.getInt32ToStringField())
82  .putAllInt32ToBytesField(source.getInt32ToBytesField())
83  .putAllInt32ToEnumField(source.getInt32ToEnumField())
84  .putAllInt32ToMessageField(source.getInt32ToMessageField())
85  .putAllStringToInt32Field(source.getStringToInt32Field());
86  }
87 
88  private void assertMapValuesSet(TestMap message) {
89  assertEquals(3, message.getInt32ToInt32Field().size());
90  assertEquals(11, message.getInt32ToInt32Field().get(1).intValue());
91  assertEquals(22, message.getInt32ToInt32Field().get(2).intValue());
92  assertEquals(33, message.getInt32ToInt32Field().get(3).intValue());
93 
94  assertEquals(3, message.getInt32ToStringField().size());
95  assertEquals("11", message.getInt32ToStringField().get(1));
96  assertEquals("22", message.getInt32ToStringField().get(2));
97  assertEquals("33", message.getInt32ToStringField().get(3));
98 
99  assertEquals(3, message.getInt32ToBytesField().size());
100  assertEquals(TestUtil.toBytes("11"), message.getInt32ToBytesField().get(1));
101  assertEquals(TestUtil.toBytes("22"), message.getInt32ToBytesField().get(2));
102  assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3));
103 
104  assertEquals(3, message.getInt32ToEnumField().size());
105  assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(1));
106  assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(2));
107  assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3));
108 
109  assertEquals(3, message.getInt32ToMessageField().size());
110  assertEquals(11, message.getInt32ToMessageField().get(1).getValue());
111  assertEquals(22, message.getInt32ToMessageField().get(2).getValue());
112  assertEquals(33, message.getInt32ToMessageField().get(3).getValue());
113 
114  assertEquals(3, message.getStringToInt32Field().size());
115  assertEquals(11, message.getStringToInt32Field().get("1").intValue());
116  assertEquals(22, message.getStringToInt32Field().get("2").intValue());
117  assertEquals(33, message.getStringToInt32Field().get("3").intValue());
118  }
119 
120  private void updateMapValues(TestMap.Builder builder) {
121  builder
122  .putInt32ToInt32Field(1, 111)
123  .removeInt32ToInt32Field(2)
124  .putInt32ToInt32Field(4, 44)
125  .putInt32ToStringField(1, "111")
126  .removeInt32ToStringField(2)
127  .putInt32ToStringField(4, "44")
128  .putInt32ToBytesField(1, TestUtil.toBytes("111"))
129  .removeInt32ToBytesField(2)
130  .putInt32ToBytesField(4, TestUtil.toBytes("44"))
131  .putInt32ToEnumField(1, TestMap.EnumValue.BAR)
132  .removeInt32ToEnumField(2)
133  .putInt32ToEnumField(4, TestMap.EnumValue.QUX)
134  .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build())
135  .removeInt32ToMessageField(2)
136  .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build())
137  .putStringToInt32Field("1", 111)
138  .removeStringToInt32Field("2")
139  .putStringToInt32Field("4", 44);
140  }
141 
142  public void testUpdateMapValues() {
143  TestMap.Builder mapBuilder = TestMap.newBuilder();
144  setMapValues(mapBuilder);
145  TestMap map = mapBuilder.build();
147 
148  mapBuilder = map.toBuilder();
149  updateMapValues(mapBuilder);
150  map = mapBuilder.build();
152  }
153 
154  private void assertMapValuesUpdated(TestMap message) {
155  assertEquals(3, message.getInt32ToInt32Field().size());
156  assertEquals(111, message.getInt32ToInt32Field().get(1).intValue());
157  assertEquals(33, message.getInt32ToInt32Field().get(3).intValue());
158  assertEquals(44, message.getInt32ToInt32Field().get(4).intValue());
159 
160  assertEquals(3, message.getInt32ToStringField().size());
161  assertEquals("111", message.getInt32ToStringField().get(1));
162  assertEquals("33", message.getInt32ToStringField().get(3));
163  assertEquals("44", message.getInt32ToStringField().get(4));
164 
165  assertEquals(3, message.getInt32ToBytesField().size());
166  assertEquals(TestUtil.toBytes("111"), message.getInt32ToBytesField().get(1));
167  assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3));
168  assertEquals(TestUtil.toBytes("44"), message.getInt32ToBytesField().get(4));
169 
170  assertEquals(3, message.getInt32ToEnumField().size());
171  assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1));
172  assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3));
173  assertEquals(TestMap.EnumValue.QUX, message.getInt32ToEnumField().get(4));
174 
175  assertEquals(3, message.getInt32ToMessageField().size());
176  assertEquals(111, message.getInt32ToMessageField().get(1).getValue());
177  assertEquals(33, message.getInt32ToMessageField().get(3).getValue());
178  assertEquals(44, message.getInt32ToMessageField().get(4).getValue());
179 
180  assertEquals(3, message.getStringToInt32Field().size());
181  assertEquals(111, message.getStringToInt32Field().get("1").intValue());
182  assertEquals(33, message.getStringToInt32Field().get("3").intValue());
183  assertEquals(44, message.getStringToInt32Field().get("4").intValue());
184  }
185 
186  private void assertMapValuesCleared(TestMapOrBuilder testMapOrBuilder) {
187  assertEquals(0, testMapOrBuilder.getInt32ToInt32Field().size());
188  assertEquals(0, testMapOrBuilder.getInt32ToInt32FieldCount());
189  assertEquals(0, testMapOrBuilder.getInt32ToStringField().size());
190  assertEquals(0, testMapOrBuilder.getInt32ToStringFieldCount());
191  assertEquals(0, testMapOrBuilder.getInt32ToBytesField().size());
192  assertEquals(0, testMapOrBuilder.getInt32ToBytesFieldCount());
193  assertEquals(0, testMapOrBuilder.getInt32ToEnumField().size());
194  assertEquals(0, testMapOrBuilder.getInt32ToEnumFieldCount());
195  assertEquals(0, testMapOrBuilder.getInt32ToMessageField().size());
196  assertEquals(0, testMapOrBuilder.getInt32ToMessageFieldCount());
197  assertEquals(0, testMapOrBuilder.getStringToInt32Field().size());
198  assertEquals(0, testMapOrBuilder.getStringToInt32FieldCount());
199  }
200 
202  // Since builders are implemented as a thin wrapper around a message
203  // instance, we attempt to verify that we can't cause the builder to modify
204  // a produced message.
205 
206  TestMap.Builder builder = TestMap.newBuilder();
207  TestMap message = builder.build();
208  builder.putInt32ToInt32Field(1, 2);
209  assertTrue(message.getInt32ToInt32Field().isEmpty());
210  message = builder.build();
211  assertEquals(newMap(1, 2), message.getInt32ToInt32Field());
212  assertEquals(newMap(1, 2), builder.getInt32ToInt32Field());
213  builder.putInt32ToInt32Field(2, 3);
214  assertEquals(newMap(1, 2), message.getInt32ToInt32Field());
215  assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field());
216  }
217 
218  public void testGetMapIsImmutable() {
219  TestMap.Builder builder = TestMap.newBuilder();
220  assertMapsAreImmutable(builder);
221  assertMapsAreImmutable(builder.build());
222 
223  setMapValues(builder);
224  assertMapsAreImmutable(builder);
225  assertMapsAreImmutable(builder.build());
226  }
227 
228  private void assertMapsAreImmutable(TestMapOrBuilder testMapOrBuilder) {
229  assertImmutable(testMapOrBuilder.getInt32ToInt32Field(), 1, 2);
230  assertImmutable(testMapOrBuilder.getInt32ToStringField(), 1, "2");
231  assertImmutable(testMapOrBuilder.getInt32ToBytesField(), 1, TestUtil.toBytes("2"));
232  assertImmutable(testMapOrBuilder.getInt32ToEnumField(), 1, TestMap.EnumValue.FOO);
233  assertImmutable(
234  testMapOrBuilder.getInt32ToMessageField(), 1, MessageValue.getDefaultInstance());
235  assertImmutable(testMapOrBuilder.getStringToInt32Field(), "1", 2);
236  }
237 
238  private <K, V> void assertImmutable(Map<K, V> map, K key, V value) {
239  try {
240  map.put(key, value);
241  fail();
242  } catch (UnsupportedOperationException e) {
243  // expected
244  }
245  if (!map.isEmpty()) {
246  try {
247  map.entrySet().remove(map.entrySet().iterator().next());
248  fail();
249  } catch (UnsupportedOperationException e) {
250  // expected
251  }
252  }
253  }
254 
255  public void testMutableMapLifecycle() {
256  TestMap.Builder builder = TestMap.newBuilder().putInt32ToInt32Field(1, 2);
257  assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field());
258  assertEquals(newMap(1, 2), builder.getInt32ToInt32Field());
259  builder.putInt32ToInt32Field(2, 3);
260  assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field());
261 
262  builder.putInt32ToEnumField(1, TestMap.EnumValue.BAR);
263  assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnumField());
264  assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField());
265  builder.putInt32ToEnumField(2, TestMap.EnumValue.FOO);
266  assertEquals(
267  newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), builder.getInt32ToEnumField());
268 
269  builder.putInt32ToStringField(1, "1");
270  assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField());
271  assertEquals(newMap(1, "1"), builder.getInt32ToStringField());
272  builder.putInt32ToStringField(2, "2");
273  assertEquals(newMap(1, "1", 2, "2"), builder.getInt32ToStringField());
274 
275  builder.putInt32ToMessageField(1, TestMap.MessageValue.getDefaultInstance());
276  assertEquals(
277  newMap(1, TestMap.MessageValue.getDefaultInstance()),
278  builder.build().getInt32ToMessageField());
279  assertEquals(
280  newMap(1, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField());
281  builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance());
282  assertEquals(
283  newMap(
284  1,
285  TestMap.MessageValue.getDefaultInstance(),
286  2,
287  TestMap.MessageValue.getDefaultInstance()),
288  builder.getInt32ToMessageField());
289  }
290 
291  public void testGettersAndSetters() throws Exception {
292  TestMap.Builder builder = TestMap.newBuilder();
293  TestMap message = builder.build();
295 
296  builder = message.toBuilder();
297  setMapValues(builder);
298  message = builder.build();
300 
301  builder = message.toBuilder();
302  updateMapValues(builder);
303  message = builder.build();
305 
306  builder = message.toBuilder();
307  builder.clear();
308  assertMapValuesCleared(builder);
309  message = builder.build();
311  }
312 
313  public void testPutAll() throws Exception {
314  TestMap.Builder sourceBuilder = TestMap.newBuilder();
315  setMapValues(sourceBuilder);
316  TestMap source = sourceBuilder.build();
318 
319  TestMap.Builder destination = TestMap.newBuilder();
320  copyMapValues(source, destination);
321  assertMapValuesSet(destination.build());
322  }
323 
324  public void testPutChecksNullKeysAndValues() throws Exception {
325  TestMap.Builder builder = TestMap.newBuilder();
326 
327  try {
328  builder.putInt32ToStringField(1, null);
329  fail();
330  } catch (NullPointerException e) {
331  // expected.
332  }
333 
334  try {
335  builder.putInt32ToBytesField(1, null);
336  fail();
337  } catch (NullPointerException e) {
338  // expected.
339  }
340 
341  try {
342  builder.putInt32ToEnumField(1, null);
343  fail();
344  } catch (NullPointerException e) {
345  // expected.
346  }
347 
348  try {
349  builder.putInt32ToMessageField(1, null);
350  fail();
351  } catch (NullPointerException e) {
352  // expected.
353  }
354 
355  try {
356  builder.putStringToInt32Field(null, 1);
357  fail();
358  } catch (NullPointerException e) {
359  // expected.
360  }
361  }
362 
363  public void testSerializeAndParse() throws Exception {
364  TestMap.Builder builder = TestMap.newBuilder();
365  setMapValues(builder);
366  TestMap message = builder.build();
367  assertEquals(message.getSerializedSize(), message.toByteString().size());
368  message = TestMap.parser().parseFrom(message.toByteString());
370 
371  builder = message.toBuilder();
372  updateMapValues(builder);
373  message = builder.build();
374  assertEquals(message.getSerializedSize(), message.toByteString().size());
375  message = TestMap.parser().parseFrom(message.toByteString());
377 
378  builder = message.toBuilder();
379  builder.clear();
380  message = builder.build();
381  assertEquals(message.getSerializedSize(), message.toByteString().size());
382  message = TestMap.parser().parseFrom(message.toByteString());
384  }
385 
386  private TestMap tryParseTestMap(BizarroTestMap bizarroMap) throws IOException {
387  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
388  CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream);
389  bizarroMap.writeTo(output);
390  output.flush();
391  return TestMap.parser().parseFrom(ByteString.copyFrom(byteArrayOutputStream.toByteArray()));
392  }
393 
394  public void testParseError() throws Exception {
395  ByteString bytes = TestUtil.toBytes("SOME BYTES");
396  String stringKey = "a string key";
397 
398  TestMap map =
399  tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToInt32Field(5, bytes).build());
400  assertEquals(0, map.getInt32ToInt32FieldOrDefault(5, -1));
401 
402  map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToStringField(stringKey, 5).build());
403  assertEquals("", map.getInt32ToStringFieldOrDefault(0, null));
404 
405  map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToBytesField(stringKey, 5).build());
406  assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY);
407 
408  map =
409  tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToEnumField(stringKey, bytes).build());
410  assertEquals(TestMap.EnumValue.FOO, map.getInt32ToEnumFieldOrDefault(0, null));
411 
412  try {
413  tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToMessageField(stringKey, bytes).build());
414  fail();
415  } catch (InvalidProtocolBufferException expected) {
416  assertTrue(expected.getUnfinishedMessage() instanceof TestMap);
417  map = (TestMap) expected.getUnfinishedMessage();
418  assertTrue(map.getInt32ToMessageField().isEmpty());
419  }
420 
421  map =
423  BizarroTestMap.newBuilder().putStringToInt32Field(stringKey, bytes).build());
424  assertEquals(0, map.getStringToInt32FieldOrDefault(stringKey, -1));
425  }
426 
427  public void testMergeFrom() throws Exception {
428  TestMap.Builder builder = TestMap.newBuilder();
429  setMapValues(builder);
430  TestMap message = builder.build();
431 
432  TestMap.Builder other = TestMap.newBuilder();
433  other.mergeFrom(message);
434  assertMapValuesSet(other.build());
435  }
436 
437  public void testEqualsAndHashCode() throws Exception {
438  // Test that generated equals() and hashCode() will disregard the order
439  // of map entries when comparing/hashing map fields.
440 
441  // We can't control the order of elements in a HashMap. The best we can do
442  // here is to add elements in different order.
443  TestMap m1 =
444  TestMap.newBuilder()
445  .putInt32ToInt32Field(1, 2)
446  .putInt32ToInt32Field(3, 4)
447  .putInt32ToInt32Field(5, 6)
448  .build();
449 
450  TestMap.Builder b2 =
451  TestMap.newBuilder()
452  .putInt32ToInt32Field(5, 6)
453  .putInt32ToInt32Field(1, 2)
454  .putInt32ToInt32Field(3, 4);
455  TestMap m2 = b2.build();
456 
457  assertEquals(m1, m2);
458  assertEquals(m1.hashCode(), m2.hashCode());
459 
460  // Make sure we did compare map fields.
461  b2.putInt32ToInt32Field(1, 0);
462  m2 = b2.build();
463  assertFalse(m1.equals(m2));
464  // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
465  // to be different.
466  }
467 
468  public void testUnknownEnumValues() throws Exception {
469  ByteString data =
470  TestUnknownEnumValue.newBuilder()
471  .putInt32ToInt32Field(1, 1)
472  .putInt32ToInt32Field(2, 54321)
473  .build()
474  .toByteString();
475 
476  TestMap message = TestMap.parseFrom(data);
477  // Entries with unknown enum values will be stored into UnknownFieldSet so
478  // there is only one entry in the map.
479  assertEquals(1, message.getInt32ToEnumField().size());
480  assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1));
481  // Serializing and parsing should preserve the unknown entry.
482  data = message.toByteString();
483  TestUnknownEnumValue messageWithUnknownEnums = TestUnknownEnumValue.parseFrom(data);
484  assertEquals(2, messageWithUnknownEnums.getInt32ToInt32Field().size());
485  assertEquals(1, messageWithUnknownEnums.getInt32ToInt32Field().get(1).intValue());
486  assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue());
487  }
488 
489  public void testIterationOrder() throws Exception {
490  TestMap.Builder builder = TestMap.newBuilder();
491  setMapValues(builder);
492  TestMap message = builder.build();
493 
494  assertEquals(
495  Arrays.asList("1", "2", "3"),
496  new ArrayList<String>(message.getStringToInt32Field().keySet()));
497  }
498 
499  private static <K, V> Map<K, V> newMap(K key1, V value1) {
500  Map<K, V> map = new HashMap<>();
501  map.put(key1, value1);
502  return map;
503  }
504 
505  private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2) {
506  Map<K, V> map = new HashMap<>();
507  map.put(key1, value1);
508  map.put(key2, value2);
509  return map;
510  }
511 
512  public void testGetMap() {
513  TestMap.Builder builder = TestMap.newBuilder();
514  setMapValues(builder);
515  TestMap message = builder.build();
516  assertEquals(message.getStringToInt32Field(), message.getStringToInt32FieldMap());
517  assertEquals(message.getInt32ToBytesField(), message.getInt32ToBytesFieldMap());
518  assertEquals(message.getInt32ToEnumField(), message.getInt32ToEnumFieldMap());
519  assertEquals(message.getInt32ToMessageField(), message.getInt32ToMessageFieldMap());
520  }
521 
522  public void testContains() {
523  TestMap.Builder builder = TestMap.newBuilder();
524  setMapValues(builder);
526  assertMapContainsSetValues(builder.build());
527  }
528 
529  private void assertMapContainsSetValues(TestMapOrBuilder testMapOrBuilder) {
530  assertTrue(testMapOrBuilder.containsInt32ToInt32Field(1));
531  assertTrue(testMapOrBuilder.containsInt32ToInt32Field(2));
532  assertTrue(testMapOrBuilder.containsInt32ToInt32Field(3));
533  assertFalse(testMapOrBuilder.containsInt32ToInt32Field(-1));
534 
535  assertTrue(testMapOrBuilder.containsInt32ToStringField(1));
536  assertTrue(testMapOrBuilder.containsInt32ToStringField(2));
537  assertTrue(testMapOrBuilder.containsInt32ToStringField(3));
538  assertFalse(testMapOrBuilder.containsInt32ToStringField(-1));
539 
540  assertTrue(testMapOrBuilder.containsInt32ToBytesField(1));
541  assertTrue(testMapOrBuilder.containsInt32ToBytesField(2));
542  assertTrue(testMapOrBuilder.containsInt32ToBytesField(3));
543  assertFalse(testMapOrBuilder.containsInt32ToBytesField(-1));
544 
545  assertTrue(testMapOrBuilder.containsInt32ToEnumField(1));
546  assertTrue(testMapOrBuilder.containsInt32ToEnumField(2));
547  assertTrue(testMapOrBuilder.containsInt32ToEnumField(3));
548  assertFalse(testMapOrBuilder.containsInt32ToEnumField(-1));
549 
550  assertTrue(testMapOrBuilder.containsInt32ToMessageField(1));
551  assertTrue(testMapOrBuilder.containsInt32ToMessageField(2));
552  assertTrue(testMapOrBuilder.containsInt32ToMessageField(3));
553  assertFalse(testMapOrBuilder.containsInt32ToMessageField(-1));
554 
555  assertTrue(testMapOrBuilder.containsStringToInt32Field("1"));
556  assertTrue(testMapOrBuilder.containsStringToInt32Field("2"));
557  assertTrue(testMapOrBuilder.containsStringToInt32Field("3"));
558  assertFalse(testMapOrBuilder.containsStringToInt32Field("-1"));
559  }
560 
561  public void testCount() {
562  TestMap.Builder builder = TestMap.newBuilder();
563  assertMapCounts(0, builder);
564 
565  setMapValues(builder);
566  assertMapCounts(3, builder);
567 
568  TestMap message = builder.build();
570 
571  builder = message.toBuilder().putInt32ToInt32Field(4, 44);
572  assertEquals(4, builder.getInt32ToInt32FieldCount());
573  assertEquals(4, builder.build().getInt32ToInt32FieldCount());
574 
575  // already present - should be unchanged
576  builder.putInt32ToInt32Field(4, 44);
577  assertEquals(4, builder.getInt32ToInt32FieldCount());
578  }
579 
580  private void assertMapCounts(int expectedCount, TestMapOrBuilder testMapOrBuilder) {
581  assertEquals(expectedCount, testMapOrBuilder.getInt32ToInt32FieldCount());
582  assertEquals(expectedCount, testMapOrBuilder.getInt32ToStringFieldCount());
583  assertEquals(expectedCount, testMapOrBuilder.getInt32ToBytesFieldCount());
584  assertEquals(expectedCount, testMapOrBuilder.getInt32ToEnumFieldCount());
585  assertEquals(expectedCount, testMapOrBuilder.getInt32ToMessageFieldCount());
586  assertEquals(expectedCount, testMapOrBuilder.getStringToInt32FieldCount());
587  }
588 
589  public void testGetOrDefault() {
590  TestMap.Builder builder = TestMap.newBuilder();
591  assertMapCounts(0, builder);
592  setMapValues(builder);
593  doTestGetOrDefault(builder);
594  doTestGetOrDefault(builder.build());
595  }
596 
597  public void doTestGetOrDefault(TestMapOrBuilder testMapOrBuilder) {
598  assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(1, -11));
599  assertEquals(-11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(-1, -11));
600 
601  assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrDefault(1, "-11"));
602  assertNull("-11", testMapOrBuilder.getInt32ToStringFieldOrDefault(-1, null));
603 
604  assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOrDefault(1, null));
605  assertNull(testMapOrBuilder.getInt32ToBytesFieldOrDefault(-1, null));
606 
607  assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDefault(1, null));
608  assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null));
609 
610  assertEquals(
611  MessageValue.newBuilder().setValue(11).build(),
612  testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null));
613  assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null));
614 
615  assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrDefault("1", -11));
616  assertEquals(-11, testMapOrBuilder.getStringToInt32FieldOrDefault("-1", -11));
617 
618  try {
619  testMapOrBuilder.getStringToInt32FieldOrDefault(null, -11);
620  fail();
621  } catch (NullPointerException e) {
622  // expected
623  }
624  }
625 
626  public void testGetOrThrow() {
627  TestMap.Builder builder = TestMap.newBuilder();
628  assertMapCounts(0, builder);
629  setMapValues(builder);
630  doTestGetOrDefault(builder);
631  doTestGetOrDefault(builder.build());
632  }
633 
634  public void doTestGetOrThrow(TestMapOrBuilder testMapOrBuilder) {
635  assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrThrow(1));
636  try {
637  testMapOrBuilder.getInt32ToInt32FieldOrThrow(-1);
638  fail();
639  } catch (IllegalArgumentException e) {
640  // expected
641  }
642 
643  assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrThrow(1));
644 
645  try {
646  testMapOrBuilder.getInt32ToStringFieldOrThrow(-1);
647  fail();
648  } catch (IllegalArgumentException e) {
649  // expected
650  }
651 
652  assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOrThrow(1));
653 
654  try {
655  testMapOrBuilder.getInt32ToBytesFieldOrThrow(-1);
656  fail();
657  } catch (IllegalArgumentException e) {
658  // expected
659  }
660 
661  assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrThrow(1));
662  try {
663  testMapOrBuilder.getInt32ToEnumFieldOrThrow(-1);
664  fail();
665  } catch (IllegalArgumentException e) {
666  // expected
667  }
668 
669  assertEquals(
670  MessageValue.newBuilder().setValue(11).build(),
671  testMapOrBuilder.getInt32ToMessageFieldOrThrow(1));
672  try {
673  testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1);
674  fail();
675  } catch (IllegalArgumentException e) {
676  // expected
677  }
678 
679  assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrThrow("1"));
680  try {
681  testMapOrBuilder.getStringToInt32FieldOrThrow("-1");
682  fail();
683  } catch (IllegalArgumentException e) {
684  // expected
685  }
686 
687  try {
688  testMapOrBuilder.getStringToInt32FieldOrThrow(null);
689  fail();
690  } catch (NullPointerException e) {
691  // expected
692  }
693  }
694 
695  public void testPut() {
696  TestMap.Builder builder = TestMap.newBuilder();
697  builder.putInt32ToInt32Field(1, 11);
698  assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1));
699 
700  builder.putInt32ToStringField(1, "a");
701  assertEquals("a", builder.getInt32ToStringFieldOrThrow(1));
702  try {
703  builder.putInt32ToStringField(1, null);
704  fail();
705  } catch (NullPointerException e) {
706  // expected
707  }
708 
709  builder.putInt32ToBytesField(1, TestUtil.toBytes("11"));
710  assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1));
711  try {
712  builder.putInt32ToBytesField(1, null);
713  fail();
714  } catch (NullPointerException e) {
715  // expected
716  }
717 
718  builder.putInt32ToEnumField(1, TestMap.EnumValue.FOO);
719  assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1));
720  try {
721  builder.putInt32ToEnumField(1, null);
722  fail();
723  } catch (NullPointerException e) {
724  // expected
725  }
726 
727  builder.putStringToInt32Field("a", 1);
728  assertEquals(1, builder.getStringToInt32FieldOrThrow("a"));
729  try {
730  builder.putStringToInt32Field(null, -1);
731  } catch (NullPointerException e) {
732  // expected
733  }
734  }
735 
736  public void testRemove() {
737  TestMap.Builder builder = TestMap.newBuilder();
738  setMapValues(builder);
739  assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1));
740  for (int times = 0; times < 2; times++) {
741  builder.removeInt32ToInt32Field(1);
742  assertEquals(-1, builder.getInt32ToInt32FieldOrDefault(1, -1));
743  }
744 
745  assertEquals("11", builder.getInt32ToStringFieldOrThrow(1));
746  for (int times = 0; times < 2; times++) {
747  builder.removeInt32ToStringField(1);
748  assertNull(builder.getInt32ToStringFieldOrDefault(1, null));
749  }
750 
751  assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1));
752  for (int times = 0; times < 2; times++) {
753  builder.removeInt32ToBytesField(1);
754  assertNull(builder.getInt32ToBytesFieldOrDefault(1, null));
755  }
756 
757  assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1));
758  for (int times = 0; times < 2; times++) {
759  builder.removeInt32ToEnumField(1);
760  assertNull(builder.getInt32ToEnumFieldOrDefault(1, null));
761  }
762 
763  assertEquals(11, builder.getStringToInt32FieldOrThrow("1"));
764  for (int times = 0; times < 2; times++) {
765  builder.removeStringToInt32Field("1");
766  assertEquals(-1, builder.getStringToInt32FieldOrDefault("1", -1));
767  }
768 
769  try {
770  builder.removeStringToInt32Field(null);
771  fail();
772  } catch (NullPointerException e) {
773  // expected
774  }
775  }
776 }
com.google.protobuf.MapForProto2LiteTest.testContains
void testContains()
Definition: MapForProto2LiteTest.java:522
K
#define K(t)
Definition: sha1.c:43
com.google.protobuf.InvalidProtocolBufferException.getUnfinishedMessage
MessageLite getUnfinishedMessage()
Definition: InvalidProtocolBufferException.java:71
com.google.protobuf.MapForProto2LiteTest.assertMapsAreImmutable
void assertMapsAreImmutable(TestMapOrBuilder testMapOrBuilder)
Definition: MapForProto2LiteTest.java:228
com.google.protobuf.MapForProto2LiteTest.testMutableMapLifecycle
void testMutableMapLifecycle()
Definition: MapForProto2LiteTest.java:255
com.google.protobuf.MapForProto2LiteTest.tryParseTestMap
TestMap tryParseTestMap(BizarroTestMap bizarroMap)
Definition: MapForProto2LiteTest.java:386
com.google.protobuf.MapForProto2LiteTest.testGettersAndSetters
void testGettersAndSetters()
Definition: MapForProto2LiteTest.java:291
com.google.protobuf.MapForProto2LiteTest.testMergeFrom
void testMergeFrom()
Definition: MapForProto2LiteTest.java:427
com.google.protobuf.MapForProto2LiteTest.assertMapContainsSetValues
void assertMapContainsSetValues(TestMapOrBuilder testMapOrBuilder)
Definition: MapForProto2LiteTest.java:529
com.google.protobuf.MapForProto2LiteTest.testSanityCopyOnWrite
void testSanityCopyOnWrite()
Definition: MapForProto2LiteTest.java:201
com.google.protobuf.MapForProto2LiteTest.doTestGetOrDefault
void doTestGetOrDefault(TestMapOrBuilder testMapOrBuilder)
Definition: MapForProto2LiteTest.java:597
com.google.protobuf.ByteString.EMPTY
static final ByteString EMPTY
Definition: ByteString.java:85
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:473
com.google.protobuf.MapForProto2LiteTest.testGetMapIsImmutable
void testGetMapIsImmutable()
Definition: MapForProto2LiteTest.java:218
com.google.protobuf.MapForProto2LiteTest.testGetOrThrow
void testGetOrThrow()
Definition: MapForProto2LiteTest.java:626
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
com.google.protobuf.MapForProto2LiteTest.testParseError
void testParseError()
Definition: MapForProto2LiteTest.java:394
com.google.protobuf.MapForProto2LiteTest.testGetOrDefault
void testGetOrDefault()
Definition: MapForProto2LiteTest.java:589
com.google.protobuf.TestUtil
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:254
com.google.protobuf.MapForProto2LiteTest.testPutAll
void testPutAll()
Definition: MapForProto2LiteTest.java:313
com.google.protobuf.MapForProto2LiteTest.testEqualsAndHashCode
void testEqualsAndHashCode()
Definition: MapForProto2LiteTest.java:437
com.google.protobuf.MapForProto2LiteTest.assertMapValuesCleared
void assertMapValuesCleared(TestMapOrBuilder testMapOrBuilder)
Definition: MapForProto2LiteTest.java:186
com.google.protobuf.MapForProto2LiteTest.copyMapValues
void copyMapValues(TestMap source, TestMap.Builder destination)
Definition: MapForProto2LiteTest.java:78
com.google.protobuf.MapForProto2LiteTest.testPut
void testPut()
Definition: MapForProto2LiteTest.java:695
com.google.protobuf.MapForProto2LiteTest.testUpdateMapValues
void testUpdateMapValues()
Definition: MapForProto2LiteTest.java:142
com.google.protobuf.MapForProto2LiteTest.updateMapValues
void updateMapValues(TestMap.Builder builder)
Definition: MapForProto2LiteTest.java:120
com.google.protobuf.MapForProto2LiteTest.testPutChecksNullKeysAndValues
void testPutChecksNullKeysAndValues()
Definition: MapForProto2LiteTest.java:324
com.google.protobuf.MapForProto2LiteTest
Definition: MapForProto2LiteTest.java:47
com.google.protobuf.MapForProto2LiteTest.newMap
static< K, V > Map< K, V > newMap(K key1, V value1, K key2, V value2)
Definition: MapForProto2LiteTest.java:505
source
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:3072
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
com.google.protobuf.MapForProto2LiteTest.newMap
static< K, V > Map< K, V > newMap(K key1, V value1)
Definition: MapForProto2LiteTest.java:499
com.google.protobuf.MapForProto2LiteTest.testSerializeAndParse
void testSerializeAndParse()
Definition: MapForProto2LiteTest.java:363
com.google.protobuf.MapForProto2LiteTest.testIterationOrder
void testIterationOrder()
Definition: MapForProto2LiteTest.java:489
com.google.protobuf.MapForProto2LiteTest.setMapValues
void setMapValues(TestMap.Builder builder)
Definition: MapForProto2LiteTest.java:49
java
com.google.protobuf.MapForProto2LiteTest.assertMapValuesUpdated
void assertMapValuesUpdated(TestMap message)
Definition: MapForProto2LiteTest.java:154
com.google.protobuf.MapForProto2LiteTest.assertMapCounts
void assertMapCounts(int expectedCount, TestMapOrBuilder testMapOrBuilder)
Definition: MapForProto2LiteTest.java:580
com.google.protobuf.CodedOutputStream.newInstance
static CodedOutputStream newInstance(final OutputStream output)
Definition: CodedOutputStream.java:92
com.google.protobuf.MapForProto2LiteTest.testUnknownEnumValues
void testUnknownEnumValues()
Definition: MapForProto2LiteTest.java:468
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
com.google.protobuf.CodedOutputStream
Definition: CodedOutputStream.java:59
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
com.google.protobuf.MapForProto2LiteTest.testGetMap
void testGetMap()
Definition: MapForProto2LiteTest.java:512
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
com.google.protobuf.MapForProto2LiteTest.testCount
void testCount()
Definition: MapForProto2LiteTest.java:561
com.google.protobuf.MapForProto2LiteTest.testRemove
void testRemove()
Definition: MapForProto2LiteTest.java:736
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
com.google.protobuf.MapForProto2LiteTest.assertMapValuesSet
void assertMapValuesSet(TestMap message)
Definition: MapForProto2LiteTest.java:88
com.google.protobuf.ByteString
Definition: ByteString.java:67
com.google.protobuf.MapForProto2LiteTest.testSetMapValues
void testSetMapValues()
Definition: MapForProto2LiteTest.java:71
com.google.protobuf.MapForProto2LiteTest.doTestGetOrThrow
void doTestGetOrThrow(TestMapOrBuilder testMapOrBuilder)
Definition: MapForProto2LiteTest.java:634


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:56