Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/UnknownFieldSetTest.java

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // http://code.google.com/p/protobuf/
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.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
14 // distribution. 14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its 15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from 16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission. 17 // this software without specific prior written permission.
18 // 18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
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.test;
32 import com.google.protobuf.*;
32 33
33 import protobuf_unittest.UnittestProto; 34 import protobuf_unittest.UnittestProto;
34 import protobuf_unittest.UnittestProto.ForeignEnum;
35 import protobuf_unittest.UnittestProto.TestAllExtensions; 35 import protobuf_unittest.UnittestProto.TestAllExtensions;
36 import protobuf_unittest.UnittestProto.TestAllTypes; 36 import protobuf_unittest.UnittestProto.TestAllTypes;
37 import protobuf_unittest.UnittestProto.TestEmptyMessage; 37 import protobuf_unittest.UnittestProto.TestEmptyMessage;
38 import protobuf_unittest.UnittestProto.TestEmptyMessageWithExtensions; 38 import protobuf_unittest.UnittestProto.TestEmptyMessageWithExtensions;
39 import protobuf_unittest.UnittestProto.TestPackedExtensions;
40 import protobuf_unittest.UnittestProto.TestPackedTypes;
41 39
42 import junit.framework.TestCase; 40 import junit.framework.TestCase;
43 41
44 import java.util.Arrays; 42 import java.util.Arrays;
45 import java.util.Map; 43 import java.util.Map;
46 44
47 /** 45 /**
48 * Tests related to unknown field handling. 46 * Tests related to unknown field handling.
49 * 47 *
50 * @author kenton@google.com (Kenton Varda) 48 * @author kenton@google.com (Kenton Varda)
51 */ 49 */
52 public class UnknownFieldSetTest extends TestCase { 50 public class UnknownFieldSetTest extends TestCase {
53 @Override
54 public void setUp() throws Exception { 51 public void setUp() throws Exception {
55 descriptor = TestAllTypes.getDescriptor(); 52 descriptor = TestAllTypes.getDescriptor();
56 allFields = TestUtil.getAllSet(); 53 allFields = TestUtil.getAllSet();
57 allFieldsData = allFields.toByteString(); 54 allFieldsData = allFields.toByteString();
58 emptyMessage = TestEmptyMessage.parseFrom(allFieldsData); 55 emptyMessage = TestEmptyMessage.parseFrom(allFieldsData);
59 unknownFields = emptyMessage.getUnknownFields(); 56 unknownFields = emptyMessage.getUnknownFields();
60 } 57 }
61 58
62 UnknownFieldSet.Field getField(String name) { 59 UnknownFieldSet.Field getField(String name) {
63 Descriptors.FieldDescriptor field = descriptor.findFieldByName(name); 60 Descriptors.FieldDescriptor field = descriptor.findFieldByName(name);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 UnknownFieldSet fields = 198 UnknownFieldSet fields =
202 UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clear().build(); 199 UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clear().build();
203 assertTrue(fields.asMap().isEmpty()); 200 assertTrue(fields.asMap().isEmpty());
204 } 201 }
205 202
206 public void testClearMessage() throws Exception { 203 public void testClearMessage() throws Exception {
207 TestEmptyMessage message = 204 TestEmptyMessage message =
208 TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).clear().build(); 205 TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).clear().build();
209 assertEquals(0, message.getSerializedSize()); 206 assertEquals(0, message.getSerializedSize());
210 } 207 }
211
212 public void testClearField() throws Exception {
213 int fieldNumber = unknownFields.asMap().keySet().iterator().next();
214 UnknownFieldSet fields =
215 UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clearField(fieldNu mber).build();
216 assertFalse(fields.hasField(fieldNumber));
217 }
218 208
219 public void testParseKnownAndUnknown() throws Exception { 209 public void testParseKnownAndUnknown() throws Exception {
220 // Test mixing known and unknown fields when parsing. 210 // Test mixing known and unknown fields when parsing.
221 211
222 UnknownFieldSet fields = 212 UnknownFieldSet fields =
223 UnknownFieldSet.newBuilder(unknownFields) 213 UnknownFieldSet.newBuilder(unknownFields)
224 .addField(123456, 214 .addField(123456,
225 UnknownFieldSet.Field.newBuilder().addVarint(654321).build()) 215 UnknownFieldSet.Field.newBuilder().addVarint(654321).build())
226 .build(); 216 .build();
227 217
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 private void checkEqualsIsConsistent(UnknownFieldSet set) { 428 private void checkEqualsIsConsistent(UnknownFieldSet set) {
439 // Object should be equal to itself. 429 // Object should be equal to itself.
440 assertEquals(set, set); 430 assertEquals(set, set);
441 431
442 // Object should be equal to a copy of itself. 432 // Object should be equal to a copy of itself.
443 UnknownFieldSet copy = UnknownFieldSet.newBuilder(set).build(); 433 UnknownFieldSet copy = UnknownFieldSet.newBuilder(set).build();
444 assertEquals(set, copy); 434 assertEquals(set, copy);
445 assertEquals(copy, set); 435 assertEquals(copy, set);
446 assertEquals(set.hashCode(), copy.hashCode()); 436 assertEquals(set.hashCode(), copy.hashCode());
447 } 437 }
448
449 // =================================================================
450
451 public void testSerializeLite() throws Exception {
452 UnittestLite.TestEmptyMessageLite emptyMessageLite =
453 UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData);
454 assertEquals(allFieldsData.size(), emptyMessageLite.getSerializedSize());
455 ByteString data = emptyMessageLite.toByteString();
456 TestAllTypes message = TestAllTypes.parseFrom(data);
457 TestUtil.assertAllFieldsSet(message);
458 assertEquals(allFieldsData, data);
459 }
460
461 public void testAllExtensionsLite() throws Exception {
462 TestAllExtensions allExtensions = TestUtil.getAllExtensionsSet();
463 ByteString allExtensionsData = allExtensions.toByteString();
464 UnittestLite.TestEmptyMessageLite emptyMessageLite =
465 UnittestLite.TestEmptyMessageLite.parser().parseFrom(allExtensionsData);
466 ByteString data = emptyMessageLite.toByteString();
467 TestAllExtensions message =
468 TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
469 TestUtil.assertAllExtensionsSet(message);
470 assertEquals(allExtensionsData, data);
471 }
472
473 public void testAllPackedFieldsLite() throws Exception {
474 TestPackedTypes allPackedFields = TestUtil.getPackedSet();
475 ByteString allPackedData = allPackedFields.toByteString();
476 UnittestLite.TestEmptyMessageLite emptyMessageLite =
477 UnittestLite.TestEmptyMessageLite.parseFrom(allPackedData);
478 ByteString data = emptyMessageLite.toByteString();
479 TestPackedTypes message =
480 TestPackedTypes.parseFrom(data, TestUtil.getExtensionRegistry());
481 TestUtil.assertPackedFieldsSet(message);
482 assertEquals(allPackedData, data);
483 }
484
485 public void testAllPackedExtensionsLite() throws Exception {
486 TestPackedExtensions allPackedExtensions = TestUtil.getPackedExtensionsSet() ;
487 ByteString allPackedExtensionsData = allPackedExtensions.toByteString();
488 UnittestLite.TestEmptyMessageLite emptyMessageLite =
489 UnittestLite.TestEmptyMessageLite.parseFrom(allPackedExtensionsData);
490 ByteString data = emptyMessageLite.toByteString();
491 TestPackedExtensions message =
492 TestPackedExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
493 TestUtil.assertPackedExtensionsSet(message);
494 assertEquals(allPackedExtensionsData, data);
495 }
496
497 public void testCopyFromLite() throws Exception {
498 UnittestLite.TestEmptyMessageLite emptyMessageLite =
499 UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData);
500 UnittestLite.TestEmptyMessageLite emptyMessageLite2 =
501 UnittestLite.TestEmptyMessageLite.newBuilder()
502 .mergeFrom(emptyMessageLite).build();
503 assertEquals(emptyMessageLite.toByteString(), emptyMessageLite2.toByteString ());
504 }
505
506 public void testMergeFromLite() throws Exception {
507 TestAllTypes message1 = TestAllTypes.newBuilder()
508 .setOptionalInt32(1)
509 .setOptionalString("foo")
510 .addRepeatedString("bar")
511 .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ)
512 .build();
513
514 TestAllTypes message2 = TestAllTypes.newBuilder()
515 .setOptionalInt64(2)
516 .setOptionalString("baz")
517 .addRepeatedString("qux")
518 .setOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ)
519 .build();
520
521 ByteString data1 = message1.toByteString();
522 UnittestLite.TestEmptyMessageLite emptyMessageLite1 =
523 UnittestLite.TestEmptyMessageLite.parseFrom(data1);
524 ByteString data2 = message2.toByteString();
525 UnittestLite.TestEmptyMessageLite emptyMessageLite2 =
526 UnittestLite.TestEmptyMessageLite.parseFrom(data2);
527
528 message1 = TestAllTypes.newBuilder(message1).mergeFrom(message2).build();
529 emptyMessageLite1 = UnittestLite.TestEmptyMessageLite.newBuilder(emptyMessag eLite1)
530 .mergeFrom(emptyMessageLite2).build();
531
532 data1 = emptyMessageLite1.toByteString();
533 message2 = TestAllTypes.parseFrom(data1);
534
535 assertEquals(message1, message2);
536 }
537
538 public void testWrongTypeTreatedAsUnknownLite() throws Exception {
539 // Test that fields of the wrong wire type are treated like unknown fields
540 // when parsing.
541
542 ByteString bizarroData = getBizarroData();
543 TestAllTypes allTypesMessage = TestAllTypes.parseFrom(bizarroData);
544 UnittestLite.TestEmptyMessageLite emptyMessageLite =
545 UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData);
546 ByteString data = emptyMessageLite.toByteString();
547 TestAllTypes allTypesMessage2 = TestAllTypes.parseFrom(data);
548
549 assertEquals(allTypesMessage.toString(), allTypesMessage2.toString());
550 }
551
552 public void testUnknownExtensionsLite() throws Exception {
553 // Make sure fields are properly parsed to the UnknownFieldSet even when
554 // they are declared as extension numbers.
555
556 UnittestLite.TestEmptyMessageWithExtensionsLite message =
557 UnittestLite.TestEmptyMessageWithExtensionsLite.parseFrom(allFieldsData);
558
559 assertEquals(allFieldsData, message.toByteString());
560 }
561
562 public void testWrongExtensionTypeTreatedAsUnknownLite() throws Exception {
563 // Test that fields of the wrong wire type are treated like unknown fields
564 // when parsing extensions.
565
566 ByteString bizarroData = getBizarroData();
567 TestAllExtensions allExtensionsMessage =
568 TestAllExtensions.parseFrom(bizarroData);
569 UnittestLite.TestEmptyMessageLite emptyMessageLite =
570 UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData);
571
572 // All fields should have been interpreted as unknown, so the byte strings
573 // should be the same.
574 assertEquals(emptyMessageLite.toByteString(),
575 allExtensionsMessage.toByteString());
576 }
577
578 public void testParseUnknownEnumValueLite() throws Exception {
579 Descriptors.FieldDescriptor singularField =
580 TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum");
581 Descriptors.FieldDescriptor repeatedField =
582 TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum");
583 assertNotNull(singularField);
584 assertNotNull(repeatedField);
585
586 ByteString data =
587 UnknownFieldSet.newBuilder()
588 .addField(singularField.getNumber(),
589 UnknownFieldSet.Field.newBuilder()
590 .addVarint(TestAllTypes.NestedEnum.BAR.getNumber())
591 .addVarint(5) // not valid
592 .build())
593 .addField(repeatedField.getNumber(),
594 UnknownFieldSet.Field.newBuilder()
595 .addVarint(TestAllTypes.NestedEnum.FOO.getNumber())
596 .addVarint(4) // not valid
597 .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber())
598 .addVarint(6) // not valid
599 .build())
600 .build()
601 .toByteString();
602
603 UnittestLite.TestEmptyMessageLite emptyMessageLite =
604 UnittestLite.TestEmptyMessageLite.parseFrom(data);
605 data = emptyMessageLite.toByteString();
606
607 {
608 TestAllTypes message = TestAllTypes.parseFrom(data);
609 assertEquals(TestAllTypes.NestedEnum.BAR,
610 message.getOptionalNestedEnum());
611 assertEquals(
612 Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ),
613 message.getRepeatedNestedEnumList());
614 assertEquals(Arrays.asList(5L),
615 message.getUnknownFields()
616 .getField(singularField.getNumber())
617 .getVarintList());
618 assertEquals(Arrays.asList(4L, 6L),
619 message.getUnknownFields()
620 .getField(repeatedField.getNumber())
621 .getVarintList());
622 }
623
624 {
625 TestAllExtensions message =
626 TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
627 assertEquals(TestAllTypes.NestedEnum.BAR,
628 message.getExtension(UnittestProto.optionalNestedEnumExtension));
629 assertEquals(
630 Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ),
631 message.getExtension(UnittestProto.repeatedNestedEnumExtension));
632 assertEquals(Arrays.asList(5L),
633 message.getUnknownFields()
634 .getField(singularField.getNumber())
635 .getVarintList());
636 assertEquals(Arrays.asList(4L, 6L),
637 message.getUnknownFields()
638 .getField(repeatedField.getNumber())
639 .getVarintList());
640 }
641 }
642
643 public void testClearLite() throws Exception {
644 UnittestLite.TestEmptyMessageLite emptyMessageLite1 =
645 UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData);
646 UnittestLite.TestEmptyMessageLite emptyMessageLite2 =
647 UnittestLite.TestEmptyMessageLite.newBuilder()
648 .mergeFrom(emptyMessageLite1).clear().build();
649 assertEquals(0, emptyMessageLite2.getSerializedSize());
650 ByteString data = emptyMessageLite2.toByteString();
651 assertEquals(0, data.size());
652 }
653
654 } 438 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698