| 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 // 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 | 32 import com.google.protobuf.*; |
| 33 import com.google.protobuf.UnittestLite.TestAllExtensionsLite; | |
| 34 import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; | |
| 35 import protobuf_unittest.UnittestMset.RawMessageSet; | |
| 36 import protobuf_unittest.UnittestMset.TestMessageSetExtension1; | |
| 37 import protobuf_unittest.UnittestMset.TestMessageSetExtension2; | |
| 38 import protobuf_unittest.UnittestProto; | |
| 39 import protobuf_unittest.UnittestProto.TestAllExtensions; | |
| 40 import protobuf_unittest.UnittestProto.TestAllTypes; | |
| 41 import protobuf_unittest.UnittestProto.TestFieldOrderings; | |
| 42 import protobuf_unittest.UnittestProto.TestOneof2; | |
| 43 import protobuf_unittest.UnittestProto.TestOneofBackwardsCompatible; | |
| 44 import protobuf_unittest.UnittestProto.TestPackedExtensions; | |
| 45 import protobuf_unittest.UnittestProto.TestPackedTypes; | |
| 46 import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; | |
| 47 | 33 |
| 48 import junit.framework.TestCase; | 34 import junit.framework.TestCase; |
| 49 | 35 |
| 50 import java.io.ByteArrayInputStream; | 36 import java.io.ByteArrayInputStream; |
| 51 import java.io.ByteArrayOutputStream; | 37 import java.io.ByteArrayOutputStream; |
| 52 import java.util.List; | 38 import java.util.List; |
| 53 | 39 |
| 40 import protobuf_unittest.UnittestProto; |
| 41 import protobuf_unittest.UnittestProto.TestAllExtensions; |
| 42 import protobuf_unittest.UnittestProto.TestAllTypes; |
| 43 import protobuf_unittest.UnittestProto.TestFieldOrderings; |
| 44 import protobuf_unittest.UnittestProto.TestPackedExtensions; |
| 45 import protobuf_unittest.UnittestProto.TestPackedTypes; |
| 46 import protobuf_unittest.UnittestMset.TestMessageSet; |
| 47 import protobuf_unittest.UnittestMset.RawMessageSet; |
| 48 import protobuf_unittest.UnittestMset.TestMessageSetExtension1; |
| 49 import protobuf_unittest.UnittestMset.TestMessageSetExtension2; |
| 50 |
| 54 /** | 51 /** |
| 55 * Tests related to parsing and serialization. | 52 * Tests related to parsing and serialization. |
| 56 * | 53 * |
| 57 * @author kenton@google.com (Kenton Varda) | 54 * @author kenton@google.com (Kenton Varda) |
| 58 */ | 55 */ |
| 59 public class WireFormatTest extends TestCase { | 56 public class WireFormatTest extends TestCase { |
| 60 public void testSerialization() throws Exception { | 57 public void testSerialization() throws Exception { |
| 61 TestAllTypes message = TestUtil.getAllSet(); | 58 TestAllTypes message = TestUtil.getAllSet(); |
| 62 | 59 |
| 63 ByteString rawBytes = message.toByteString(); | 60 ByteString rawBytes = message.toByteString(); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 message.writeTo(codedOutput); | 117 message.writeTo(codedOutput); |
| 121 | 118 |
| 122 codedOutput.flush(); | 119 codedOutput.flush(); |
| 123 | 120 |
| 124 TestPackedTypes message2 = TestPackedTypes.parseFrom( | 121 TestPackedTypes message2 = TestPackedTypes.parseFrom( |
| 125 outputStream.toByteArray()); | 122 outputStream.toByteArray()); |
| 126 | 123 |
| 127 TestUtil.assertPackedFieldsSet(message2); | 124 TestUtil.assertPackedFieldsSet(message2); |
| 128 } | 125 } |
| 129 | 126 |
| 130 public void testSerializeExtensionsLite() throws Exception { | |
| 131 // TestAllTypes and TestAllExtensions should have compatible wire formats, | |
| 132 // so if we serialize a TestAllExtensions then parse it as TestAllTypes | |
| 133 // it should work. | |
| 134 | |
| 135 TestAllExtensionsLite message = TestUtilLite.getAllLiteExtensionsSet(); | |
| 136 ByteString rawBytes = message.toByteString(); | |
| 137 assertEquals(rawBytes.size(), message.getSerializedSize()); | |
| 138 | |
| 139 TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); | |
| 140 | |
| 141 TestUtil.assertAllFieldsSet(message2); | |
| 142 } | |
| 143 | |
| 144 public void testSerializePackedExtensionsLite() throws Exception { | |
| 145 // TestPackedTypes and TestPackedExtensions should have compatible wire | |
| 146 // formats; check that they serialize to the same string. | |
| 147 TestPackedExtensionsLite message = TestUtilLite.getLitePackedExtensionsSet()
; | |
| 148 ByteString rawBytes = message.toByteString(); | |
| 149 | |
| 150 TestPackedTypes message2 = TestUtil.getPackedSet(); | |
| 151 ByteString rawBytes2 = message2.toByteString(); | |
| 152 | |
| 153 assertEquals(rawBytes, rawBytes2); | |
| 154 } | |
| 155 | |
| 156 public void testParseExtensions() throws Exception { | 127 public void testParseExtensions() throws Exception { |
| 157 // TestAllTypes and TestAllExtensions should have compatible wire formats, | 128 // TestAllTypes and TestAllExtensions should have compatible wire formats, |
| 158 // so if we serialize a TestAllTypes then parse it as TestAllExtensions | 129 // so if we serialize a TestAllTypes then parse it as TestAllExtensions |
| 159 // it should work. | 130 // it should work. |
| 160 | 131 |
| 161 TestAllTypes message = TestUtil.getAllSet(); | 132 TestAllTypes message = TestUtil.getAllSet(); |
| 162 ByteString rawBytes = message.toByteString(); | 133 ByteString rawBytes = message.toByteString(); |
| 163 | 134 |
| 164 ExtensionRegistry registry = TestUtil.getExtensionRegistry(); | 135 ExtensionRegistry registry = TestUtil.getExtensionRegistry(); |
| 165 | 136 |
| 166 TestAllExtensions message2 = | 137 TestAllExtensions message2 = |
| 167 TestAllExtensions.parseFrom(rawBytes, registry); | 138 TestAllExtensions.parseFrom(rawBytes, registry); |
| 168 | 139 |
| 169 TestUtil.assertAllExtensionsSet(message2); | 140 TestUtil.assertAllExtensionsSet(message2); |
| 170 } | 141 } |
| 171 | 142 |
| 172 public void testParsePackedExtensions() throws Exception { | 143 public void testParsePackedExtensions() throws Exception { |
| 173 // Ensure that packed extensions can be properly parsed. | 144 // Ensure that packed extensions can be properly parsed. |
| 174 TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); | 145 TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); |
| 175 ByteString rawBytes = message.toByteString(); | 146 ByteString rawBytes = message.toByteString(); |
| 176 | 147 |
| 177 ExtensionRegistry registry = TestUtil.getExtensionRegistry(); | 148 ExtensionRegistry registry = TestUtil.getExtensionRegistry(); |
| 178 | 149 |
| 179 TestPackedExtensions message2 = | 150 TestPackedExtensions message2 = |
| 180 TestPackedExtensions.parseFrom(rawBytes, registry); | 151 TestPackedExtensions.parseFrom(rawBytes, registry); |
| 181 | 152 |
| 182 TestUtil.assertPackedExtensionsSet(message2); | 153 TestUtil.assertPackedExtensionsSet(message2); |
| 183 } | 154 } |
| 184 | 155 |
| 185 public void testParseExtensionsLite() throws Exception { | |
| 186 // TestAllTypes and TestAllExtensions should have compatible wire formats, | |
| 187 // so if we serialize a TestAllTypes then parse it as TestAllExtensions | |
| 188 // it should work. | |
| 189 | |
| 190 TestAllTypes message = TestUtil.getAllSet(); | |
| 191 ByteString rawBytes = message.toByteString(); | |
| 192 | |
| 193 ExtensionRegistryLite registry_lite = TestUtilLite.getExtensionRegistryLite(
); | |
| 194 | |
| 195 TestAllExtensionsLite message2 = | |
| 196 TestAllExtensionsLite.parseFrom(rawBytes, registry_lite); | |
| 197 | |
| 198 TestUtil.assertAllExtensionsSet(message2); | |
| 199 | |
| 200 // Try again using a full extension registry. | |
| 201 ExtensionRegistry registry = TestUtil.getExtensionRegistry(); | |
| 202 | |
| 203 TestAllExtensionsLite message3 = | |
| 204 TestAllExtensionsLite.parseFrom(rawBytes, registry); | |
| 205 | |
| 206 TestUtil.assertAllExtensionsSet(message3); | |
| 207 } | |
| 208 | |
| 209 public void testParsePackedExtensionsLite() throws Exception { | |
| 210 // Ensure that packed extensions can be properly parsed. | |
| 211 TestPackedExtensionsLite message = TestUtilLite.getLitePackedExtensionsSet()
; | |
| 212 ByteString rawBytes = message.toByteString(); | |
| 213 | |
| 214 ExtensionRegistryLite registry = TestUtilLite.getExtensionRegistryLite(); | |
| 215 | |
| 216 TestPackedExtensionsLite message2 = | |
| 217 TestPackedExtensionsLite.parseFrom(rawBytes, registry); | |
| 218 | |
| 219 TestUtil.assertPackedExtensionsSet(message2); | |
| 220 } | |
| 221 | |
| 222 public void testExtensionsSerializedSize() throws Exception { | 156 public void testExtensionsSerializedSize() throws Exception { |
| 223 assertNotSame(TestUtil.getAllSet().getSerializedSize(), | 157 assertEquals(TestUtil.getAllSet().getSerializedSize(), |
| 224 TestUtil.getAllExtensionsSet().getSerializedSize()); | 158 TestUtil.getAllExtensionsSet().getSerializedSize()); |
| 225 } | 159 } |
| 226 | 160 |
| 227 public void testSerializeDelimited() throws Exception { | 161 public void testSerializeDelimited() throws Exception { |
| 228 ByteArrayOutputStream output = new ByteArrayOutputStream(); | 162 ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 229 TestUtil.getAllSet().writeDelimitedTo(output); | 163 TestUtil.getAllSet().writeDelimitedTo(output); |
| 230 output.write(12); | 164 output.write(12); |
| 231 TestUtil.getPackedSet().writeDelimitedTo(output); | 165 TestUtil.getPackedSet().writeDelimitedTo(output); |
| 232 output.write(34); | 166 output.write(34); |
| 233 | 167 |
| 234 ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); | 168 ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 | 455 |
| 522 // Parse as a TestMessageSet and store value into lazy field | 456 // Parse as a TestMessageSet and store value into lazy field |
| 523 TestMessageSet messageSet = | 457 TestMessageSet messageSet = |
| 524 TestMessageSet.parseFrom(data, extensionRegistry); | 458 TestMessageSet.parseFrom(data, extensionRegistry); |
| 525 // Merge lazy field check the contents. | 459 // Merge lazy field check the contents. |
| 526 messageSet = | 460 messageSet = |
| 527 messageSet.toBuilder().mergeFrom(data, extensionRegistry).build(); | 461 messageSet.toBuilder().mergeFrom(data, extensionRegistry).build(); |
| 528 assertEquals(123, messageSet.getExtension( | 462 assertEquals(123, messageSet.getExtension( |
| 529 TestMessageSetExtension1.messageSetExtension).getI()); | 463 TestMessageSetExtension1.messageSetExtension).getI()); |
| 530 } | 464 } |
| 531 | |
| 532 public void testMergeMessageSetExtensionEagerly() throws Exception { | |
| 533 testMergeMessageSetExtensionWithFlag(true); | |
| 534 } | |
| 535 | |
| 536 public void testMergeMessageSetExtensionNotEagerly() throws Exception { | |
| 537 testMergeMessageSetExtensionWithFlag(false); | |
| 538 } | |
| 539 | |
| 540 private void testMergeMessageSetExtensionWithFlag(boolean eagerParsing) | |
| 541 throws Exception { | |
| 542 ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); | |
| 543 ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); | |
| 544 extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); | |
| 545 | |
| 546 // Set up a RawMessageSet with a known messages. | |
| 547 int TYPE_ID_1 = | |
| 548 TestMessageSetExtension1 | |
| 549 .getDescriptor().getExtensions().get(0).getNumber(); | |
| 550 RawMessageSet raw = | |
| 551 RawMessageSet.newBuilder() | |
| 552 .addItem( | |
| 553 RawMessageSet.Item.newBuilder() | |
| 554 .setTypeId(TYPE_ID_1) | |
| 555 .setMessage( | |
| 556 TestMessageSetExtension1.newBuilder() | |
| 557 .setI(123) | |
| 558 .build().toByteString()) | |
| 559 .build()) | |
| 560 .build(); | |
| 561 | |
| 562 // Serialize RawMessageSet unnormally (message value before type id) | |
| 563 ByteString.CodedBuilder out = ByteString.newCodedBuilder( | |
| 564 raw.getSerializedSize()); | |
| 565 CodedOutputStream output = out.getCodedOutput(); | |
| 566 List<RawMessageSet.Item> items = raw.getItemList(); | |
| 567 for (int i = 0; i < items.size(); i++) { | |
| 568 RawMessageSet.Item item = items.get(i); | |
| 569 output.writeTag(1, WireFormat.WIRETYPE_START_GROUP); | |
| 570 output.writeBytes(3, item.getMessage()); | |
| 571 output.writeInt32(2, item.getTypeId()); | |
| 572 output.writeTag(1, WireFormat.WIRETYPE_END_GROUP); | |
| 573 } | |
| 574 ByteString data = out.build(); | |
| 575 | |
| 576 // Merge bytes into TestMessageSet and check the contents. | |
| 577 TestMessageSet messageSet = | |
| 578 TestMessageSet.newBuilder().mergeFrom(data, extensionRegistry).build(); | |
| 579 assertEquals(123, messageSet.getExtension( | |
| 580 TestMessageSetExtension1.messageSetExtension).getI()); | |
| 581 } | |
| 582 | |
| 583 // ================================================================ | |
| 584 // oneof | |
| 585 public void testOneofWireFormat() throws Exception { | |
| 586 TestOneof2.Builder builder = TestOneof2.newBuilder(); | |
| 587 TestUtil.setOneof(builder); | |
| 588 TestOneof2 message = builder.build(); | |
| 589 ByteString rawBytes = message.toByteString(); | |
| 590 | |
| 591 assertEquals(rawBytes.size(), message.getSerializedSize()); | |
| 592 | |
| 593 TestOneof2 message2 = TestOneof2.parseFrom(rawBytes); | |
| 594 TestUtil.assertOneofSet(message2); | |
| 595 } | |
| 596 | |
| 597 public void testOneofOnlyLastSet() throws Exception { | |
| 598 TestOneofBackwardsCompatible source = TestOneofBackwardsCompatible | |
| 599 .newBuilder().setFooInt(100).setFooString("101").build(); | |
| 600 | |
| 601 ByteString rawBytes = source.toByteString(); | |
| 602 TestOneof2 message = TestOneof2.parseFrom(rawBytes); | |
| 603 assertFalse(message.hasFooInt()); | |
| 604 assertTrue(message.hasFooString()); | |
| 605 } | |
| 606 } | 465 } |
| OLD | NEW |