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 static java.util.Collections.emptyList; 34 import static java.util.Collections.singletonList; 35 36 import com.google.protobuf.UnittestImportLite.ImportEnumLite; 37 import com.google.protobuf.UnittestImportPublicLite.PublicImportMessageLite; 38 import com.google.protobuf.UnittestLite; 39 import com.google.protobuf.UnittestLite.ForeignEnumLite; 40 import com.google.protobuf.UnittestLite.ForeignMessageLite; 41 import com.google.protobuf.UnittestLite.TestAllExtensionsLite; 42 import com.google.protobuf.UnittestLite.TestAllTypesLite; 43 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedEnum; 44 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedMessage; 45 import com.google.protobuf.UnittestLite.TestAllTypesLite.OneofFieldCase; 46 import com.google.protobuf.UnittestLite.TestAllTypesLite.OptionalGroup; 47 import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup; 48 import com.google.protobuf.UnittestLite.TestAllTypesLiteOrBuilder; 49 import com.google.protobuf.UnittestLite.TestNestedExtensionLite; 50 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; 51 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; 52 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; 53 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestOneofEquals; 54 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestRecursiveOneof; 55 56 import junit.framework.TestCase; 57 58 import java.io.ByteArrayInputStream; 59 import java.io.ByteArrayOutputStream; 60 import java.io.NotSerializableException; 61 import java.io.ObjectInputStream; 62 import java.io.ObjectOutputStream; 63 64 /** 65 * Test lite runtime. 66 * 67 * @author kenton@google.com Kenton Varda 68 */ 69 public class LiteTest extends TestCase { 70 @Override setUp()71 public void setUp() throws Exception { 72 // Test that nested extensions are initialized correctly even if the outer 73 // class has not been accessed directly. This was once a bug with lite 74 // messages. 75 // 76 // We put this in setUp() rather than in its own test method because we 77 // need to make sure it runs before any actual tests. 78 assertTrue(TestNestedExtensionLite.nestedExtension != null); 79 } 80 testLite()81 public void testLite() throws Exception { 82 // Since lite messages are a subset of regular messages, we can mostly 83 // assume that the functionality of lite messages is already thoroughly 84 // tested by the regular tests. All this test really verifies is that 85 // a proto with optimize_for = LITE_RUNTIME compiles correctly when 86 // linked only against the lite library. That is all tested at compile 87 // time, leaving not much to do in this method. Let's just do some random 88 // stuff to make sure the lite message is actually here and usable. 89 90 TestAllTypesLite message = 91 TestAllTypesLite.newBuilder() 92 .setOptionalInt32(123) 93 .addRepeatedString("hello") 94 .setOptionalNestedMessage( 95 TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) 96 .build(); 97 98 ByteString data = message.toByteString(); 99 100 TestAllTypesLite message2 = TestAllTypesLite.parseFrom(data); 101 102 assertEquals(123, message2.getOptionalInt32()); 103 assertEquals(1, message2.getRepeatedStringCount()); 104 assertEquals("hello", message2.getRepeatedString(0)); 105 assertEquals(7, message2.getOptionalNestedMessage().getBb()); 106 } 107 testLiteExtensions()108 public void testLiteExtensions() throws Exception { 109 // TODO(kenton): Unlike other features of the lite library, extensions are 110 // implemented completely differently from the regular library. We 111 // should probably test them more thoroughly. 112 113 TestAllExtensionsLite message = 114 TestAllExtensionsLite.newBuilder() 115 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) 116 .addExtension(UnittestLite.repeatedStringExtensionLite, "hello") 117 .setExtension(UnittestLite.optionalNestedEnumExtensionLite, 118 TestAllTypesLite.NestedEnum.BAZ) 119 .setExtension(UnittestLite.optionalNestedMessageExtensionLite, 120 TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) 121 .build(); 122 123 // Test copying a message, since coping extensions actually does use a 124 // different code path between lite and regular libraries, and as of this 125 // writing, parsing hasn't been implemented yet. 126 TestAllExtensionsLite message2 = message.toBuilder().build(); 127 128 assertEquals(123, (int) message2.getExtension( 129 UnittestLite.optionalInt32ExtensionLite)); 130 assertEquals(1, message2.getExtensionCount( 131 UnittestLite.repeatedStringExtensionLite)); 132 assertEquals(1, message2.getExtension( 133 UnittestLite.repeatedStringExtensionLite).size()); 134 assertEquals("hello", message2.getExtension( 135 UnittestLite.repeatedStringExtensionLite, 0)); 136 assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension( 137 UnittestLite.optionalNestedEnumExtensionLite)); 138 assertEquals(7, message2.getExtension( 139 UnittestLite.optionalNestedMessageExtensionLite).getBb()); 140 } 141 142 testClone()143 public void testClone() { 144 TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder() 145 .setOptionalInt32(123); 146 assertEquals( 147 expected.getOptionalInt32(), expected.clone().getOptionalInt32()); 148 149 TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder() 150 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123); 151 assertEquals( 152 expected2.getExtension(UnittestLite.optionalInt32ExtensionLite), 153 expected2.clone().getExtension(UnittestLite.optionalInt32ExtensionLite)); 154 } 155 testAddAll()156 public void testAddAll() { 157 try { 158 TestAllTypesLite.newBuilder() 159 .addAllRepeatedBytes(null); 160 fail(); 161 } catch (NullPointerException e) { 162 // expected. 163 } 164 } 165 testSanityCopyOnWrite()166 public void testSanityCopyOnWrite() throws InvalidProtocolBufferException { 167 // Since builders are implemented as a thin wrapper around a message 168 // instance, we attempt to verify that we can't cause the builder to modify 169 // a produced message. 170 171 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder(); 172 TestAllTypesLite message = builder.build(); 173 TestAllTypesLite messageAfterBuild; 174 builder.setOptionalBool(true); 175 assertEquals(false, message.getOptionalBool()); 176 assertEquals(true, builder.getOptionalBool()); 177 messageAfterBuild = builder.build(); 178 assertEquals(true, messageAfterBuild.getOptionalBool()); 179 assertEquals(false, message.getOptionalBool()); 180 builder.clearOptionalBool(); 181 assertEquals(false, builder.getOptionalBool()); 182 assertEquals(true, messageAfterBuild.getOptionalBool()); 183 184 message = builder.build(); 185 builder.setOptionalBytes(ByteString.copyFromUtf8("hi")); 186 assertEquals(ByteString.EMPTY, message.getOptionalBytes()); 187 assertEquals(ByteString.copyFromUtf8("hi"), builder.getOptionalBytes()); 188 messageAfterBuild = builder.build(); 189 assertEquals( 190 ByteString.copyFromUtf8("hi"), messageAfterBuild.getOptionalBytes()); 191 assertEquals(ByteString.EMPTY, message.getOptionalBytes()); 192 builder.clearOptionalBytes(); 193 assertEquals(ByteString.EMPTY, builder.getOptionalBytes()); 194 assertEquals( 195 ByteString.copyFromUtf8("hi"), messageAfterBuild.getOptionalBytes()); 196 197 message = builder.build(); 198 builder.setOptionalCord("hi"); 199 assertEquals("", message.getOptionalCord()); 200 assertEquals("hi", builder.getOptionalCord()); 201 messageAfterBuild = builder.build(); 202 assertEquals("hi", messageAfterBuild.getOptionalCord()); 203 assertEquals("", message.getOptionalCord()); 204 builder.clearOptionalCord(); 205 assertEquals("", builder.getOptionalCord()); 206 assertEquals("hi", messageAfterBuild.getOptionalCord()); 207 208 message = builder.build(); 209 builder.setOptionalCordBytes(ByteString.copyFromUtf8("no")); 210 assertEquals(ByteString.EMPTY, message.getOptionalCordBytes()); 211 assertEquals(ByteString.copyFromUtf8("no"), builder.getOptionalCordBytes()); 212 messageAfterBuild = builder.build(); 213 assertEquals( 214 ByteString.copyFromUtf8("no"), 215 messageAfterBuild.getOptionalCordBytes()); 216 assertEquals(ByteString.EMPTY, message.getOptionalCordBytes()); 217 builder.clearOptionalCord(); 218 assertEquals(ByteString.EMPTY, builder.getOptionalCordBytes()); 219 assertEquals( 220 ByteString.copyFromUtf8("no"), 221 messageAfterBuild.getOptionalCordBytes()); 222 223 message = builder.build(); 224 builder.setOptionalDouble(1); 225 assertEquals(0D, message.getOptionalDouble()); 226 assertEquals(1D, builder.getOptionalDouble()); 227 messageAfterBuild = builder.build(); 228 assertEquals(1D, messageAfterBuild.getOptionalDouble()); 229 assertEquals(0D, message.getOptionalDouble()); 230 builder.clearOptionalDouble(); 231 assertEquals(0D, builder.getOptionalDouble()); 232 assertEquals(1D, messageAfterBuild.getOptionalDouble()); 233 234 message = builder.build(); 235 builder.setOptionalFixed32(1); 236 assertEquals(0, message.getOptionalFixed32()); 237 assertEquals(1, builder.getOptionalFixed32()); 238 messageAfterBuild = builder.build(); 239 assertEquals(1, messageAfterBuild.getOptionalFixed32()); 240 assertEquals(0, message.getOptionalFixed32()); 241 builder.clearOptionalFixed32(); 242 assertEquals(0, builder.getOptionalFixed32()); 243 assertEquals(1, messageAfterBuild.getOptionalFixed32()); 244 245 message = builder.build(); 246 builder.setOptionalFixed64(1); 247 assertEquals(0L, message.getOptionalFixed64()); 248 assertEquals(1L, builder.getOptionalFixed64()); 249 messageAfterBuild = builder.build(); 250 assertEquals(1L, messageAfterBuild.getOptionalFixed64()); 251 assertEquals(0L, message.getOptionalFixed64()); 252 builder.clearOptionalFixed64(); 253 assertEquals(0L, builder.getOptionalFixed64()); 254 assertEquals(1L, messageAfterBuild.getOptionalFixed64()); 255 256 message = builder.build(); 257 builder.setOptionalFloat(1); 258 assertEquals(0F, message.getOptionalFloat()); 259 assertEquals(1F, builder.getOptionalFloat()); 260 messageAfterBuild = builder.build(); 261 assertEquals(1F, messageAfterBuild.getOptionalFloat()); 262 assertEquals(0F, message.getOptionalFloat()); 263 builder.clearOptionalFloat(); 264 assertEquals(0F, builder.getOptionalFloat()); 265 assertEquals(1F, messageAfterBuild.getOptionalFloat()); 266 267 message = builder.build(); 268 builder.setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR); 269 assertEquals( 270 ForeignEnumLite.FOREIGN_LITE_FOO, message.getOptionalForeignEnum()); 271 assertEquals( 272 ForeignEnumLite.FOREIGN_LITE_BAR, builder.getOptionalForeignEnum()); 273 messageAfterBuild = builder.build(); 274 assertEquals( 275 ForeignEnumLite.FOREIGN_LITE_BAR, 276 messageAfterBuild.getOptionalForeignEnum()); 277 assertEquals( 278 ForeignEnumLite.FOREIGN_LITE_FOO, message.getOptionalForeignEnum()); 279 builder.clearOptionalForeignEnum(); 280 assertEquals( 281 ForeignEnumLite.FOREIGN_LITE_FOO, builder.getOptionalForeignEnum()); 282 assertEquals( 283 ForeignEnumLite.FOREIGN_LITE_BAR, 284 messageAfterBuild.getOptionalForeignEnum()); 285 286 message = builder.build(); 287 ForeignMessageLite foreignMessage = ForeignMessageLite.newBuilder() 288 .setC(1) 289 .build(); 290 builder.setOptionalForeignMessage(foreignMessage); 291 assertEquals( 292 ForeignMessageLite.getDefaultInstance(), 293 message.getOptionalForeignMessage()); 294 assertEquals(foreignMessage, builder.getOptionalForeignMessage()); 295 messageAfterBuild = builder.build(); 296 assertEquals(foreignMessage, messageAfterBuild.getOptionalForeignMessage()); 297 assertEquals( 298 ForeignMessageLite.getDefaultInstance(), 299 message.getOptionalForeignMessage()); 300 builder.clearOptionalForeignMessage(); 301 assertEquals( 302 ForeignMessageLite.getDefaultInstance(), 303 builder.getOptionalForeignMessage()); 304 assertEquals(foreignMessage, messageAfterBuild.getOptionalForeignMessage()); 305 306 message = builder.build(); 307 ForeignMessageLite.Builder foreignMessageBuilder = 308 ForeignMessageLite.newBuilder() 309 .setC(3); 310 builder.setOptionalForeignMessage(foreignMessageBuilder); 311 assertEquals( 312 ForeignMessageLite.getDefaultInstance(), 313 message.getOptionalForeignMessage()); 314 assertEquals(foreignMessageBuilder.build(), builder.getOptionalForeignMessage()); 315 messageAfterBuild = builder.build(); 316 assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getOptionalForeignMessage()); 317 assertEquals( 318 ForeignMessageLite.getDefaultInstance(), 319 message.getOptionalForeignMessage()); 320 builder.clearOptionalForeignMessage(); 321 assertEquals( 322 ForeignMessageLite.getDefaultInstance(), 323 builder.getOptionalForeignMessage()); 324 assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getOptionalForeignMessage()); 325 326 message = builder.build(); 327 OptionalGroup optionalGroup = OptionalGroup.newBuilder() 328 .setA(1) 329 .build(); 330 builder.setOptionalGroup(optionalGroup); 331 assertEquals( 332 OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); 333 assertEquals(optionalGroup, builder.getOptionalGroup()); 334 messageAfterBuild = builder.build(); 335 assertEquals(optionalGroup, messageAfterBuild.getOptionalGroup()); 336 assertEquals( 337 OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); 338 builder.clearOptionalGroup(); 339 assertEquals( 340 OptionalGroup.getDefaultInstance(), builder.getOptionalGroup()); 341 assertEquals(optionalGroup, messageAfterBuild.getOptionalGroup()); 342 343 message = builder.build(); 344 OptionalGroup.Builder optionalGroupBuilder = OptionalGroup.newBuilder() 345 .setA(3); 346 builder.setOptionalGroup(optionalGroupBuilder); 347 assertEquals( 348 OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); 349 assertEquals(optionalGroupBuilder.build(), builder.getOptionalGroup()); 350 messageAfterBuild = builder.build(); 351 assertEquals(optionalGroupBuilder.build(), messageAfterBuild.getOptionalGroup()); 352 assertEquals( 353 OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); 354 builder.clearOptionalGroup(); 355 assertEquals( 356 OptionalGroup.getDefaultInstance(), builder.getOptionalGroup()); 357 assertEquals(optionalGroupBuilder.build(), messageAfterBuild.getOptionalGroup()); 358 359 message = builder.build(); 360 builder.setOptionalInt32(1); 361 assertEquals(0, message.getOptionalInt32()); 362 assertEquals(1, builder.getOptionalInt32()); 363 messageAfterBuild = builder.build(); 364 assertEquals(1, messageAfterBuild.getOptionalInt32()); 365 assertEquals(0, message.getOptionalInt32()); 366 builder.clearOptionalInt32(); 367 assertEquals(0, builder.getOptionalInt32()); 368 assertEquals(1, messageAfterBuild.getOptionalInt32()); 369 370 message = builder.build(); 371 builder.setOptionalInt64(1); 372 assertEquals(0L, message.getOptionalInt64()); 373 assertEquals(1L, builder.getOptionalInt64()); 374 messageAfterBuild = builder.build(); 375 assertEquals(1L, messageAfterBuild.getOptionalInt64()); 376 assertEquals(0L, message.getOptionalInt64()); 377 builder.clearOptionalInt64(); 378 assertEquals(0L, builder.getOptionalInt64()); 379 assertEquals(1L, messageAfterBuild.getOptionalInt64()); 380 381 message = builder.build(); 382 NestedMessage nestedMessage = NestedMessage.newBuilder() 383 .setBb(1) 384 .build(); 385 builder.setOptionalLazyMessage(nestedMessage); 386 assertEquals( 387 NestedMessage.getDefaultInstance(), 388 message.getOptionalLazyMessage()); 389 assertEquals(nestedMessage, builder.getOptionalLazyMessage()); 390 messageAfterBuild = builder.build(); 391 assertEquals(nestedMessage, messageAfterBuild.getOptionalLazyMessage()); 392 assertEquals( 393 NestedMessage.getDefaultInstance(), 394 message.getOptionalLazyMessage()); 395 builder.clearOptionalLazyMessage(); 396 assertEquals( 397 NestedMessage.getDefaultInstance(), builder.getOptionalLazyMessage()); 398 assertEquals(nestedMessage, messageAfterBuild.getOptionalLazyMessage()); 399 400 message = builder.build(); 401 NestedMessage.Builder nestedMessageBuilder = 402 NestedMessage.newBuilder() 403 .setBb(3); 404 builder.setOptionalLazyMessage(nestedMessageBuilder); 405 assertEquals( 406 NestedMessage.getDefaultInstance(), 407 message.getOptionalLazyMessage()); 408 assertEquals(nestedMessageBuilder.build(), builder.getOptionalLazyMessage()); 409 messageAfterBuild = builder.build(); 410 assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getOptionalLazyMessage()); 411 assertEquals( 412 NestedMessage.getDefaultInstance(), 413 message.getOptionalLazyMessage()); 414 builder.clearOptionalLazyMessage(); 415 assertEquals( 416 NestedMessage.getDefaultInstance(), builder.getOptionalLazyMessage()); 417 assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getOptionalLazyMessage()); 418 419 message = builder.build(); 420 builder.setOptionalSfixed32(1); 421 assertEquals(0, message.getOptionalSfixed32()); 422 assertEquals(1, builder.getOptionalSfixed32()); 423 messageAfterBuild = builder.build(); 424 assertEquals(1, messageAfterBuild.getOptionalSfixed32()); 425 assertEquals(0, message.getOptionalSfixed32()); 426 builder.clearOptionalSfixed32(); 427 assertEquals(0, builder.getOptionalSfixed32()); 428 assertEquals(1, messageAfterBuild.getOptionalSfixed32()); 429 430 message = builder.build(); 431 builder.setOptionalSfixed64(1); 432 assertEquals(0L, message.getOptionalSfixed64()); 433 assertEquals(1L, builder.getOptionalSfixed64()); 434 messageAfterBuild = builder.build(); 435 assertEquals(1L, messageAfterBuild.getOptionalSfixed64()); 436 assertEquals(0L, message.getOptionalSfixed64()); 437 builder.clearOptionalSfixed64(); 438 assertEquals(0L, builder.getOptionalSfixed64()); 439 assertEquals(1L, messageAfterBuild.getOptionalSfixed64()); 440 441 message = builder.build(); 442 builder.setOptionalSint32(1); 443 assertEquals(0, message.getOptionalSint32()); 444 assertEquals(1, builder.getOptionalSint32()); 445 messageAfterBuild = builder.build(); 446 assertEquals(1, messageAfterBuild.getOptionalSint32()); 447 builder.clearOptionalSint32(); 448 assertEquals(0, builder.getOptionalSint32()); 449 assertEquals(1, messageAfterBuild.getOptionalSint32()); 450 451 message = builder.build(); 452 builder.setOptionalSint64(1); 453 assertEquals(0L, message.getOptionalSint64()); 454 assertEquals(1L, builder.getOptionalSint64()); 455 messageAfterBuild = builder.build(); 456 assertEquals(1L, messageAfterBuild.getOptionalSint64()); 457 assertEquals(0L, message.getOptionalSint64()); 458 builder.clearOptionalSint64(); 459 assertEquals(0L, builder.getOptionalSint64()); 460 assertEquals(1L, messageAfterBuild.getOptionalSint64()); 461 462 message = builder.build(); 463 builder.setOptionalString("hi"); 464 assertEquals("", message.getOptionalString()); 465 assertEquals("hi", builder.getOptionalString()); 466 messageAfterBuild = builder.build(); 467 assertEquals("hi", messageAfterBuild.getOptionalString()); 468 assertEquals("", message.getOptionalString()); 469 builder.clearOptionalString(); 470 assertEquals("", builder.getOptionalString()); 471 assertEquals("hi", messageAfterBuild.getOptionalString()); 472 473 message = builder.build(); 474 builder.setOptionalStringBytes(ByteString.copyFromUtf8("no")); 475 assertEquals(ByteString.EMPTY, message.getOptionalStringBytes()); 476 assertEquals( 477 ByteString.copyFromUtf8("no"), builder.getOptionalStringBytes()); 478 messageAfterBuild = builder.build(); 479 assertEquals( 480 ByteString.copyFromUtf8("no"), 481 messageAfterBuild.getOptionalStringBytes()); 482 assertEquals(ByteString.EMPTY, message.getOptionalStringBytes()); 483 builder.clearOptionalString(); 484 assertEquals(ByteString.EMPTY, builder.getOptionalStringBytes()); 485 assertEquals( 486 ByteString.copyFromUtf8("no"), 487 messageAfterBuild.getOptionalStringBytes()); 488 489 message = builder.build(); 490 builder.setOptionalStringPiece("hi"); 491 assertEquals("", message.getOptionalStringPiece()); 492 assertEquals("hi", builder.getOptionalStringPiece()); 493 messageAfterBuild = builder.build(); 494 assertEquals("hi", messageAfterBuild.getOptionalStringPiece()); 495 assertEquals("", message.getOptionalStringPiece()); 496 builder.clearOptionalStringPiece(); 497 assertEquals("", builder.getOptionalStringPiece()); 498 assertEquals("hi", messageAfterBuild.getOptionalStringPiece()); 499 500 message = builder.build(); 501 builder.setOptionalStringPieceBytes(ByteString.copyFromUtf8("no")); 502 assertEquals(ByteString.EMPTY, message.getOptionalStringPieceBytes()); 503 assertEquals( 504 ByteString.copyFromUtf8("no"), builder.getOptionalStringPieceBytes()); 505 messageAfterBuild = builder.build(); 506 assertEquals( 507 ByteString.copyFromUtf8("no"), 508 messageAfterBuild.getOptionalStringPieceBytes()); 509 assertEquals(ByteString.EMPTY, message.getOptionalStringPieceBytes()); 510 builder.clearOptionalStringPiece(); 511 assertEquals(ByteString.EMPTY, builder.getOptionalStringPieceBytes()); 512 assertEquals( 513 ByteString.copyFromUtf8("no"), 514 messageAfterBuild.getOptionalStringPieceBytes()); 515 516 message = builder.build(); 517 builder.setOptionalUint32(1); 518 assertEquals(0, message.getOptionalUint32()); 519 assertEquals(1, builder.getOptionalUint32()); 520 messageAfterBuild = builder.build(); 521 assertEquals(1, messageAfterBuild.getOptionalUint32()); 522 assertEquals(0, message.getOptionalUint32()); 523 builder.clearOptionalUint32(); 524 assertEquals(0, builder.getOptionalUint32()); 525 assertEquals(1, messageAfterBuild.getOptionalUint32()); 526 527 message = builder.build(); 528 builder.setOptionalUint64(1); 529 assertEquals(0L, message.getOptionalUint64()); 530 assertEquals(1L, builder.getOptionalUint64()); 531 messageAfterBuild = builder.build(); 532 assertEquals(1L, messageAfterBuild.getOptionalUint64()); 533 assertEquals(0L, message.getOptionalUint64()); 534 builder.clearOptionalUint64(); 535 assertEquals(0L, builder.getOptionalUint64()); 536 assertEquals(1L, messageAfterBuild.getOptionalUint64()); 537 538 message = builder.build(); 539 builder.addAllRepeatedBool(singletonList(true)); 540 assertEquals(emptyList(), message.getRepeatedBoolList()); 541 assertEquals(singletonList(true), builder.getRepeatedBoolList()); 542 assertEquals(emptyList(), message.getRepeatedBoolList()); 543 messageAfterBuild = builder.build(); 544 builder.clearRepeatedBool(); 545 assertEquals(emptyList(), builder.getRepeatedBoolList()); 546 assertEquals(singletonList(true), messageAfterBuild.getRepeatedBoolList()); 547 548 message = builder.build(); 549 builder.addAllRepeatedBytes(singletonList(ByteString.copyFromUtf8("hi"))); 550 assertEquals(emptyList(), message.getRepeatedBytesList()); 551 assertEquals( 552 singletonList(ByteString.copyFromUtf8("hi")), 553 builder.getRepeatedBytesList()); 554 assertEquals(emptyList(), message.getRepeatedBytesList()); 555 messageAfterBuild = builder.build(); 556 builder.clearRepeatedBytes(); 557 assertEquals(emptyList(), builder.getRepeatedBytesList()); 558 assertEquals( 559 singletonList(ByteString.copyFromUtf8("hi")), 560 messageAfterBuild.getRepeatedBytesList()); 561 562 message = builder.build(); 563 builder.addAllRepeatedCord(singletonList("hi")); 564 assertEquals(emptyList(), message.getRepeatedCordList()); 565 assertEquals(singletonList("hi"), builder.getRepeatedCordList()); 566 assertEquals(emptyList(), message.getRepeatedCordList()); 567 messageAfterBuild = builder.build(); 568 builder.clearRepeatedCord(); 569 assertEquals(emptyList(), builder.getRepeatedCordList()); 570 assertEquals(singletonList("hi"), messageAfterBuild.getRepeatedCordList()); 571 572 message = builder.build(); 573 builder.addAllRepeatedDouble(singletonList(1D)); 574 assertEquals(emptyList(), message.getRepeatedDoubleList()); 575 assertEquals(singletonList(1D), builder.getRepeatedDoubleList()); 576 assertEquals(emptyList(), message.getRepeatedDoubleList()); 577 messageAfterBuild = builder.build(); 578 builder.clearRepeatedDouble(); 579 assertEquals(emptyList(), builder.getRepeatedDoubleList()); 580 assertEquals(singletonList(1D), messageAfterBuild.getRepeatedDoubleList()); 581 582 message = builder.build(); 583 builder.addAllRepeatedFixed32(singletonList(1)); 584 assertEquals(emptyList(), message.getRepeatedFixed32List()); 585 assertEquals(singletonList(1), builder.getRepeatedFixed32List()); 586 assertEquals(emptyList(), message.getRepeatedFixed32List()); 587 messageAfterBuild = builder.build(); 588 builder.clearRepeatedFixed32(); 589 assertEquals(emptyList(), builder.getRepeatedFixed32List()); 590 assertEquals(singletonList(1), messageAfterBuild.getRepeatedFixed32List()); 591 592 message = builder.build(); 593 builder.addAllRepeatedFixed64(singletonList(1L)); 594 assertEquals(emptyList(), message.getRepeatedFixed64List()); 595 assertEquals(singletonList(1L), builder.getRepeatedFixed64List()); 596 assertEquals(emptyList(), message.getRepeatedFixed64List()); 597 messageAfterBuild = builder.build(); 598 builder.clearRepeatedFixed64(); 599 assertEquals(emptyList(), builder.getRepeatedFixed64List()); 600 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedFixed64List()); 601 602 message = builder.build(); 603 builder.addAllRepeatedFloat(singletonList(1F)); 604 assertEquals(emptyList(), message.getRepeatedFloatList()); 605 assertEquals(singletonList(1F), builder.getRepeatedFloatList()); 606 assertEquals(emptyList(), message.getRepeatedFloatList()); 607 messageAfterBuild = builder.build(); 608 builder.clearRepeatedFloat(); 609 assertEquals(emptyList(), builder.getRepeatedFloatList()); 610 assertEquals(singletonList(1F), messageAfterBuild.getRepeatedFloatList()); 611 612 message = builder.build(); 613 builder.addAllRepeatedForeignEnum( 614 singletonList(ForeignEnumLite.FOREIGN_LITE_BAR)); 615 assertEquals(emptyList(), message.getRepeatedForeignEnumList()); 616 assertEquals( 617 singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), 618 builder.getRepeatedForeignEnumList()); 619 assertEquals(emptyList(), message.getRepeatedForeignEnumList()); 620 messageAfterBuild = builder.build(); 621 builder.clearRepeatedForeignEnum(); 622 assertEquals(emptyList(), builder.getRepeatedForeignEnumList()); 623 assertEquals( 624 singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), 625 messageAfterBuild.getRepeatedForeignEnumList()); 626 627 message = builder.build(); 628 builder.addAllRepeatedForeignMessage(singletonList(foreignMessage)); 629 assertEquals(emptyList(), message.getRepeatedForeignMessageList()); 630 assertEquals( 631 singletonList(foreignMessage), builder.getRepeatedForeignMessageList()); 632 assertEquals(emptyList(), message.getRepeatedForeignMessageList()); 633 messageAfterBuild = builder.build(); 634 builder.clearRepeatedForeignMessage(); 635 assertEquals(emptyList(), builder.getRepeatedForeignMessageList()); 636 assertEquals( 637 singletonList(foreignMessage), 638 messageAfterBuild.getRepeatedForeignMessageList()); 639 640 message = builder.build(); 641 builder.addAllRepeatedGroup( 642 singletonList(RepeatedGroup.getDefaultInstance())); 643 assertEquals(emptyList(), message.getRepeatedGroupList()); 644 assertEquals( 645 singletonList(RepeatedGroup.getDefaultInstance()), 646 builder.getRepeatedGroupList()); 647 assertEquals(emptyList(), message.getRepeatedGroupList()); 648 messageAfterBuild = builder.build(); 649 builder.clearRepeatedGroup(); 650 assertEquals(emptyList(), builder.getRepeatedGroupList()); 651 assertEquals( 652 singletonList(RepeatedGroup.getDefaultInstance()), 653 messageAfterBuild.getRepeatedGroupList()); 654 655 message = builder.build(); 656 builder.addAllRepeatedInt32(singletonList(1)); 657 assertEquals(emptyList(), message.getRepeatedInt32List()); 658 assertEquals(singletonList(1), builder.getRepeatedInt32List()); 659 assertEquals(emptyList(), message.getRepeatedInt32List()); 660 messageAfterBuild = builder.build(); 661 builder.clearRepeatedInt32(); 662 assertEquals(emptyList(), builder.getRepeatedInt32List()); 663 assertEquals(singletonList(1), messageAfterBuild.getRepeatedInt32List()); 664 665 message = builder.build(); 666 builder.addAllRepeatedInt64(singletonList(1L)); 667 assertEquals(emptyList(), message.getRepeatedInt64List()); 668 assertEquals(singletonList(1L), builder.getRepeatedInt64List()); 669 assertEquals(emptyList(), message.getRepeatedInt64List()); 670 messageAfterBuild = builder.build(); 671 builder.clearRepeatedInt64(); 672 assertEquals(emptyList(), builder.getRepeatedInt64List()); 673 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedInt64List()); 674 675 message = builder.build(); 676 builder.addAllRepeatedLazyMessage(singletonList(nestedMessage)); 677 assertEquals(emptyList(), message.getRepeatedLazyMessageList()); 678 assertEquals( 679 singletonList(nestedMessage), builder.getRepeatedLazyMessageList()); 680 assertEquals(emptyList(), message.getRepeatedLazyMessageList()); 681 messageAfterBuild = builder.build(); 682 builder.clearRepeatedLazyMessage(); 683 assertEquals(emptyList(), builder.getRepeatedLazyMessageList()); 684 assertEquals( 685 singletonList(nestedMessage), 686 messageAfterBuild.getRepeatedLazyMessageList()); 687 688 message = builder.build(); 689 builder.addAllRepeatedSfixed32(singletonList(1)); 690 assertEquals(emptyList(), message.getRepeatedSfixed32List()); 691 assertEquals(singletonList(1), builder.getRepeatedSfixed32List()); 692 assertEquals(emptyList(), message.getRepeatedSfixed32List()); 693 messageAfterBuild = builder.build(); 694 builder.clearRepeatedSfixed32(); 695 assertEquals(emptyList(), builder.getRepeatedSfixed32List()); 696 assertEquals(singletonList(1), messageAfterBuild.getRepeatedSfixed32List()); 697 698 message = builder.build(); 699 builder.addAllRepeatedSfixed64(singletonList(1L)); 700 assertEquals(emptyList(), message.getRepeatedSfixed64List()); 701 assertEquals(singletonList(1L), builder.getRepeatedSfixed64List()); 702 assertEquals(emptyList(), message.getRepeatedSfixed64List()); 703 messageAfterBuild = builder.build(); 704 builder.clearRepeatedSfixed64(); 705 assertEquals(emptyList(), builder.getRepeatedSfixed64List()); 706 assertEquals( 707 singletonList(1L), messageAfterBuild.getRepeatedSfixed64List()); 708 709 message = builder.build(); 710 builder.addAllRepeatedSint32(singletonList(1)); 711 assertEquals(emptyList(), message.getRepeatedSint32List()); 712 assertEquals(singletonList(1), builder.getRepeatedSint32List()); 713 assertEquals(emptyList(), message.getRepeatedSint32List()); 714 messageAfterBuild = builder.build(); 715 builder.clearRepeatedSint32(); 716 assertEquals(emptyList(), builder.getRepeatedSint32List()); 717 assertEquals(singletonList(1), messageAfterBuild.getRepeatedSint32List()); 718 719 message = builder.build(); 720 builder.addAllRepeatedSint64(singletonList(1L)); 721 assertEquals(emptyList(), message.getRepeatedSint64List()); 722 assertEquals(singletonList(1L), builder.getRepeatedSint64List()); 723 assertEquals(emptyList(), message.getRepeatedSint64List()); 724 messageAfterBuild = builder.build(); 725 builder.clearRepeatedSint64(); 726 assertEquals(emptyList(), builder.getRepeatedSint64List()); 727 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedSint64List()); 728 729 message = builder.build(); 730 builder.addAllRepeatedString(singletonList("hi")); 731 assertEquals(emptyList(), message.getRepeatedStringList()); 732 assertEquals(singletonList("hi"), builder.getRepeatedStringList()); 733 assertEquals(emptyList(), message.getRepeatedStringList()); 734 messageAfterBuild = builder.build(); 735 builder.clearRepeatedString(); 736 assertEquals(emptyList(), builder.getRepeatedStringList()); 737 assertEquals( 738 singletonList("hi"), messageAfterBuild.getRepeatedStringList()); 739 740 message = builder.build(); 741 builder.addAllRepeatedStringPiece(singletonList("hi")); 742 assertEquals(emptyList(), message.getRepeatedStringPieceList()); 743 assertEquals(singletonList("hi"), builder.getRepeatedStringPieceList()); 744 assertEquals(emptyList(), message.getRepeatedStringPieceList()); 745 messageAfterBuild = builder.build(); 746 builder.clearRepeatedStringPiece(); 747 assertEquals(emptyList(), builder.getRepeatedStringPieceList()); 748 assertEquals( 749 singletonList("hi"), messageAfterBuild.getRepeatedStringPieceList()); 750 751 message = builder.build(); 752 builder.addAllRepeatedUint32(singletonList(1)); 753 assertEquals(emptyList(), message.getRepeatedUint32List()); 754 assertEquals(singletonList(1), builder.getRepeatedUint32List()); 755 assertEquals(emptyList(), message.getRepeatedUint32List()); 756 messageAfterBuild = builder.build(); 757 builder.clearRepeatedUint32(); 758 assertEquals(emptyList(), builder.getRepeatedUint32List()); 759 assertEquals(singletonList(1), messageAfterBuild.getRepeatedUint32List()); 760 761 message = builder.build(); 762 builder.addAllRepeatedUint64(singletonList(1L)); 763 assertEquals(emptyList(), message.getRepeatedUint64List()); 764 assertEquals(singletonList(1L), builder.getRepeatedUint64List()); 765 assertEquals(emptyList(), message.getRepeatedUint64List()); 766 messageAfterBuild = builder.build(); 767 builder.clearRepeatedUint64(); 768 assertEquals(emptyList(), builder.getRepeatedUint64List()); 769 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedUint64List()); 770 771 message = builder.build(); 772 builder.addRepeatedBool(true); 773 assertEquals(emptyList(), message.getRepeatedBoolList()); 774 assertEquals(singletonList(true), builder.getRepeatedBoolList()); 775 assertEquals(emptyList(), message.getRepeatedBoolList()); 776 messageAfterBuild = builder.build(); 777 builder.clearRepeatedBool(); 778 assertEquals(emptyList(), builder.getRepeatedBoolList()); 779 assertEquals(singletonList(true), messageAfterBuild.getRepeatedBoolList()); 780 781 message = builder.build(); 782 builder.addRepeatedBytes(ByteString.copyFromUtf8("hi")); 783 assertEquals(emptyList(), message.getRepeatedBytesList()); 784 assertEquals( 785 singletonList(ByteString.copyFromUtf8("hi")), 786 builder.getRepeatedBytesList()); 787 assertEquals(emptyList(), message.getRepeatedBytesList()); 788 messageAfterBuild = builder.build(); 789 builder.clearRepeatedBytes(); 790 assertEquals(emptyList(), builder.getRepeatedBytesList()); 791 assertEquals( 792 singletonList(ByteString.copyFromUtf8("hi")), 793 messageAfterBuild.getRepeatedBytesList()); 794 795 message = builder.build(); 796 builder.addRepeatedCord("hi"); 797 assertEquals(emptyList(), message.getRepeatedCordList()); 798 assertEquals(singletonList("hi"), builder.getRepeatedCordList()); 799 assertEquals(emptyList(), message.getRepeatedCordList()); 800 messageAfterBuild = builder.build(); 801 builder.clearRepeatedCord(); 802 assertEquals(emptyList(), builder.getRepeatedCordList()); 803 assertEquals(singletonList("hi"), messageAfterBuild.getRepeatedCordList()); 804 805 message = builder.build(); 806 builder.addRepeatedDouble(1D); 807 assertEquals(emptyList(), message.getRepeatedDoubleList()); 808 assertEquals(singletonList(1D), builder.getRepeatedDoubleList()); 809 assertEquals(emptyList(), message.getRepeatedDoubleList()); 810 messageAfterBuild = builder.build(); 811 builder.clearRepeatedDouble(); 812 assertEquals(emptyList(), builder.getRepeatedDoubleList()); 813 assertEquals(singletonList(1D), messageAfterBuild.getRepeatedDoubleList()); 814 815 message = builder.build(); 816 builder.addRepeatedFixed32(1); 817 assertEquals(emptyList(), message.getRepeatedFixed32List()); 818 assertEquals(singletonList(1), builder.getRepeatedFixed32List()); 819 assertEquals(emptyList(), message.getRepeatedFixed32List()); 820 messageAfterBuild = builder.build(); 821 builder.clearRepeatedFixed32(); 822 assertEquals(emptyList(), builder.getRepeatedFixed32List()); 823 assertEquals(singletonList(1), messageAfterBuild.getRepeatedFixed32List()); 824 825 message = builder.build(); 826 builder.addRepeatedFixed64(1L); 827 assertEquals(emptyList(), message.getRepeatedFixed64List()); 828 assertEquals(singletonList(1L), builder.getRepeatedFixed64List()); 829 assertEquals(emptyList(), message.getRepeatedFixed64List()); 830 messageAfterBuild = builder.build(); 831 builder.clearRepeatedFixed64(); 832 assertEquals(emptyList(), builder.getRepeatedFixed64List()); 833 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedFixed64List()); 834 835 message = builder.build(); 836 builder.addRepeatedFloat(1F); 837 assertEquals(emptyList(), message.getRepeatedFloatList()); 838 assertEquals(singletonList(1F), builder.getRepeatedFloatList()); 839 assertEquals(emptyList(), message.getRepeatedFloatList()); 840 messageAfterBuild = builder.build(); 841 builder.clearRepeatedFloat(); 842 assertEquals(emptyList(), builder.getRepeatedFloatList()); 843 assertEquals(singletonList(1F), messageAfterBuild.getRepeatedFloatList()); 844 845 message = builder.build(); 846 builder.addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR); 847 assertEquals(emptyList(), message.getRepeatedForeignEnumList()); 848 assertEquals( 849 singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), 850 builder.getRepeatedForeignEnumList()); 851 assertEquals(emptyList(), message.getRepeatedForeignEnumList()); 852 messageAfterBuild = builder.build(); 853 builder.clearRepeatedForeignEnum(); 854 assertEquals(emptyList(), builder.getRepeatedForeignEnumList()); 855 assertEquals( 856 singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), 857 messageAfterBuild.getRepeatedForeignEnumList()); 858 859 message = builder.build(); 860 builder.addRepeatedForeignMessage(foreignMessage); 861 assertEquals(emptyList(), message.getRepeatedForeignMessageList()); 862 assertEquals( 863 singletonList(foreignMessage), builder.getRepeatedForeignMessageList()); 864 assertEquals(emptyList(), message.getRepeatedForeignMessageList()); 865 messageAfterBuild = builder.build(); 866 builder.removeRepeatedForeignMessage(0); 867 assertEquals(emptyList(), builder.getRepeatedForeignMessageList()); 868 assertEquals( 869 singletonList(foreignMessage), 870 messageAfterBuild.getRepeatedForeignMessageList()); 871 872 message = builder.build(); 873 builder.addRepeatedGroup(RepeatedGroup.getDefaultInstance()); 874 assertEquals(emptyList(), message.getRepeatedGroupList()); 875 assertEquals( 876 singletonList(RepeatedGroup.getDefaultInstance()), 877 builder.getRepeatedGroupList()); 878 assertEquals(emptyList(), message.getRepeatedGroupList()); 879 messageAfterBuild = builder.build(); 880 builder.removeRepeatedGroup(0); 881 assertEquals(emptyList(), builder.getRepeatedGroupList()); 882 assertEquals( 883 singletonList(RepeatedGroup.getDefaultInstance()), 884 messageAfterBuild.getRepeatedGroupList()); 885 886 message = builder.build(); 887 builder.addRepeatedInt32(1); 888 assertEquals(emptyList(), message.getRepeatedInt32List()); 889 assertEquals(singletonList(1), builder.getRepeatedInt32List()); 890 assertEquals(emptyList(), message.getRepeatedInt32List()); 891 messageAfterBuild = builder.build(); 892 builder.clearRepeatedInt32(); 893 assertEquals(emptyList(), builder.getRepeatedInt32List()); 894 assertEquals(singletonList(1), messageAfterBuild.getRepeatedInt32List()); 895 896 message = builder.build(); 897 builder.addRepeatedInt64(1L); 898 assertEquals(emptyList(), message.getRepeatedInt64List()); 899 assertEquals(singletonList(1L), builder.getRepeatedInt64List()); 900 assertEquals(emptyList(), message.getRepeatedInt64List()); 901 messageAfterBuild = builder.build(); 902 builder.clearRepeatedInt64(); 903 assertEquals(emptyList(), builder.getRepeatedInt64List()); 904 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedInt64List()); 905 906 message = builder.build(); 907 builder.addRepeatedLazyMessage(nestedMessage); 908 assertEquals(emptyList(), message.getRepeatedLazyMessageList()); 909 assertEquals( 910 singletonList(nestedMessage), builder.getRepeatedLazyMessageList()); 911 assertEquals(emptyList(), message.getRepeatedLazyMessageList()); 912 messageAfterBuild = builder.build(); 913 builder.removeRepeatedLazyMessage(0); 914 assertEquals(emptyList(), builder.getRepeatedLazyMessageList()); 915 assertEquals( 916 singletonList(nestedMessage), 917 messageAfterBuild.getRepeatedLazyMessageList()); 918 919 message = builder.build(); 920 builder.addRepeatedSfixed32(1); 921 assertEquals(emptyList(), message.getRepeatedSfixed32List()); 922 assertEquals(singletonList(1), builder.getRepeatedSfixed32List()); 923 assertEquals(emptyList(), message.getRepeatedSfixed32List()); 924 messageAfterBuild = builder.build(); 925 builder.clearRepeatedSfixed32(); 926 assertEquals(emptyList(), builder.getRepeatedSfixed32List()); 927 assertEquals(singletonList(1), messageAfterBuild.getRepeatedSfixed32List()); 928 929 message = builder.build(); 930 builder.addRepeatedSfixed64(1L); 931 assertEquals(emptyList(), message.getRepeatedSfixed64List()); 932 assertEquals(singletonList(1L), builder.getRepeatedSfixed64List()); 933 assertEquals(emptyList(), message.getRepeatedSfixed64List()); 934 messageAfterBuild = builder.build(); 935 builder.clearRepeatedSfixed64(); 936 assertEquals(emptyList(), builder.getRepeatedSfixed64List()); 937 assertEquals( 938 singletonList(1L), messageAfterBuild.getRepeatedSfixed64List()); 939 940 message = builder.build(); 941 builder.addRepeatedSint32(1); 942 assertEquals(emptyList(), message.getRepeatedSint32List()); 943 assertEquals(singletonList(1), builder.getRepeatedSint32List()); 944 assertEquals(emptyList(), message.getRepeatedSint32List()); 945 messageAfterBuild = builder.build(); 946 builder.clearRepeatedSint32(); 947 assertEquals(emptyList(), builder.getRepeatedSint32List()); 948 assertEquals(singletonList(1), messageAfterBuild.getRepeatedSint32List()); 949 950 message = builder.build(); 951 builder.addRepeatedSint64(1L); 952 assertEquals(emptyList(), message.getRepeatedSint64List()); 953 assertEquals(singletonList(1L), builder.getRepeatedSint64List()); 954 assertEquals(emptyList(), message.getRepeatedSint64List()); 955 messageAfterBuild = builder.build(); 956 builder.clearRepeatedSint64(); 957 assertEquals(emptyList(), builder.getRepeatedSint64List()); 958 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedSint64List()); 959 960 message = builder.build(); 961 builder.addRepeatedString("hi"); 962 assertEquals(emptyList(), message.getRepeatedStringList()); 963 assertEquals(singletonList("hi"), builder.getRepeatedStringList()); 964 assertEquals(emptyList(), message.getRepeatedStringList()); 965 messageAfterBuild = builder.build(); 966 builder.clearRepeatedString(); 967 assertEquals(emptyList(), builder.getRepeatedStringList()); 968 assertEquals( 969 singletonList("hi"), messageAfterBuild.getRepeatedStringList()); 970 971 message = builder.build(); 972 builder.addRepeatedStringPiece("hi"); 973 assertEquals(emptyList(), message.getRepeatedStringPieceList()); 974 assertEquals(singletonList("hi"), builder.getRepeatedStringPieceList()); 975 assertEquals(emptyList(), message.getRepeatedStringPieceList()); 976 messageAfterBuild = builder.build(); 977 builder.clearRepeatedStringPiece(); 978 assertEquals(emptyList(), builder.getRepeatedStringPieceList()); 979 assertEquals( 980 singletonList("hi"), messageAfterBuild.getRepeatedStringPieceList()); 981 982 message = builder.build(); 983 builder.addRepeatedUint32(1); 984 assertEquals(emptyList(), message.getRepeatedUint32List()); 985 assertEquals(singletonList(1), builder.getRepeatedUint32List()); 986 assertEquals(emptyList(), message.getRepeatedUint32List()); 987 messageAfterBuild = builder.build(); 988 builder.clearRepeatedUint32(); 989 assertEquals(emptyList(), builder.getRepeatedUint32List()); 990 assertEquals(singletonList(1), messageAfterBuild.getRepeatedUint32List()); 991 992 message = builder.build(); 993 builder.addRepeatedUint64(1L); 994 assertEquals(emptyList(), message.getRepeatedUint64List()); 995 assertEquals(singletonList(1L), builder.getRepeatedUint64List()); 996 assertEquals(emptyList(), message.getRepeatedUint64List()); 997 messageAfterBuild = builder.build(); 998 builder.clearRepeatedUint64(); 999 assertEquals(emptyList(), builder.getRepeatedUint64List()); 1000 assertEquals(singletonList(1L), messageAfterBuild.getRepeatedUint64List()); 1001 1002 message = builder.build(); 1003 builder.addRepeatedBool(true); 1004 messageAfterBuild = builder.build(); 1005 assertEquals(0, message.getRepeatedBoolCount()); 1006 builder.setRepeatedBool(0, false); 1007 assertEquals(true, messageAfterBuild.getRepeatedBool(0)); 1008 assertEquals(false, builder.getRepeatedBool(0)); 1009 builder.clearRepeatedBool(); 1010 1011 message = builder.build(); 1012 builder.addRepeatedBytes(ByteString.copyFromUtf8("hi")); 1013 messageAfterBuild = builder.build(); 1014 assertEquals(0, message.getRepeatedBytesCount()); 1015 builder.setRepeatedBytes(0, ByteString.EMPTY); 1016 assertEquals( 1017 ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedBytes(0)); 1018 assertEquals(ByteString.EMPTY, builder.getRepeatedBytes(0)); 1019 builder.clearRepeatedBytes(); 1020 1021 message = builder.build(); 1022 builder.addRepeatedCord("hi"); 1023 messageAfterBuild = builder.build(); 1024 assertEquals(0, message.getRepeatedCordCount()); 1025 builder.setRepeatedCord(0, ""); 1026 assertEquals("hi", messageAfterBuild.getRepeatedCord(0)); 1027 assertEquals("", builder.getRepeatedCord(0)); 1028 builder.clearRepeatedCord(); 1029 message = builder.build(); 1030 1031 builder.addRepeatedCordBytes(ByteString.copyFromUtf8("hi")); 1032 messageAfterBuild = builder.build(); 1033 assertEquals(0, message.getRepeatedCordCount()); 1034 builder.setRepeatedCord(0, ""); 1035 assertEquals( 1036 ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedCordBytes(0)); 1037 assertEquals(ByteString.EMPTY, builder.getRepeatedCordBytes(0)); 1038 builder.clearRepeatedCord(); 1039 1040 message = builder.build(); 1041 builder.addRepeatedDouble(1D); 1042 messageAfterBuild = builder.build(); 1043 assertEquals(0, message.getRepeatedDoubleCount()); 1044 builder.setRepeatedDouble(0, 0D); 1045 assertEquals(1D, messageAfterBuild.getRepeatedDouble(0)); 1046 assertEquals(0D, builder.getRepeatedDouble(0)); 1047 builder.clearRepeatedDouble(); 1048 1049 message = builder.build(); 1050 builder.addRepeatedFixed32(1); 1051 messageAfterBuild = builder.build(); 1052 assertEquals(0, message.getRepeatedFixed32Count()); 1053 builder.setRepeatedFixed32(0, 0); 1054 assertEquals(1, messageAfterBuild.getRepeatedFixed32(0)); 1055 assertEquals(0, builder.getRepeatedFixed32(0)); 1056 builder.clearRepeatedFixed32(); 1057 1058 message = builder.build(); 1059 builder.addRepeatedFixed64(1L); 1060 messageAfterBuild = builder.build(); 1061 assertEquals(0, message.getRepeatedFixed64Count()); 1062 builder.setRepeatedFixed64(0, 0L); 1063 assertEquals(1L, messageAfterBuild.getRepeatedFixed64(0)); 1064 assertEquals(0L, builder.getRepeatedFixed64(0)); 1065 builder.clearRepeatedFixed64(); 1066 1067 message = builder.build(); 1068 builder.addRepeatedFloat(1F); 1069 messageAfterBuild = builder.build(); 1070 assertEquals(0, message.getRepeatedFloatCount()); 1071 builder.setRepeatedFloat(0, 0F); 1072 assertEquals(1F, messageAfterBuild.getRepeatedFloat(0)); 1073 assertEquals(0F, builder.getRepeatedFloat(0)); 1074 builder.clearRepeatedFloat(); 1075 1076 message = builder.build(); 1077 builder.addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR); 1078 messageAfterBuild = builder.build(); 1079 assertEquals(0, message.getRepeatedForeignEnumCount()); 1080 builder.setRepeatedForeignEnum(0, ForeignEnumLite.FOREIGN_LITE_FOO); 1081 assertEquals( 1082 ForeignEnumLite.FOREIGN_LITE_BAR, 1083 messageAfterBuild.getRepeatedForeignEnum(0)); 1084 assertEquals( 1085 ForeignEnumLite.FOREIGN_LITE_FOO, builder.getRepeatedForeignEnum(0)); 1086 builder.clearRepeatedForeignEnum(); 1087 1088 message = builder.build(); 1089 builder.addRepeatedForeignMessage(foreignMessage); 1090 messageAfterBuild = builder.build(); 1091 assertEquals(0, message.getRepeatedForeignMessageCount()); 1092 builder.setRepeatedForeignMessage( 1093 0, ForeignMessageLite.getDefaultInstance()); 1094 assertEquals( 1095 foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); 1096 assertEquals( 1097 ForeignMessageLite.getDefaultInstance(), 1098 builder.getRepeatedForeignMessage(0)); 1099 builder.clearRepeatedForeignMessage(); 1100 1101 message = builder.build(); 1102 builder.addRepeatedForeignMessage(foreignMessageBuilder); 1103 messageAfterBuild = builder.build(); 1104 assertEquals(0, message.getRepeatedForeignMessageCount()); 1105 builder.setRepeatedForeignMessage( 1106 0, ForeignMessageLite.getDefaultInstance()); 1107 assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getRepeatedForeignMessage(0)); 1108 assertEquals( 1109 ForeignMessageLite.getDefaultInstance(), 1110 builder.getRepeatedForeignMessage(0)); 1111 builder.clearRepeatedForeignMessage(); 1112 1113 message = builder.build(); 1114 builder.addRepeatedForeignMessage(0, foreignMessage); 1115 messageAfterBuild = builder.build(); 1116 assertEquals(0, message.getRepeatedForeignMessageCount()); 1117 builder.setRepeatedForeignMessage(0, foreignMessageBuilder); 1118 assertEquals( 1119 foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); 1120 assertEquals(foreignMessageBuilder.build(), builder.getRepeatedForeignMessage(0)); 1121 builder.clearRepeatedForeignMessage(); 1122 1123 message = builder.build(); 1124 RepeatedGroup repeatedGroup = RepeatedGroup.newBuilder() 1125 .setA(1) 1126 .build(); 1127 builder.addRepeatedGroup(repeatedGroup); 1128 messageAfterBuild = builder.build(); 1129 assertEquals(0, message.getRepeatedGroupCount()); 1130 builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); 1131 assertEquals(repeatedGroup, messageAfterBuild.getRepeatedGroup(0)); 1132 assertEquals( 1133 RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); 1134 builder.clearRepeatedGroup(); 1135 1136 message = builder.build(); 1137 builder.addRepeatedGroup(0, repeatedGroup); 1138 messageAfterBuild = builder.build(); 1139 assertEquals(0, message.getRepeatedGroupCount()); 1140 builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); 1141 assertEquals(repeatedGroup, messageAfterBuild.getRepeatedGroup(0)); 1142 assertEquals( 1143 RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); 1144 builder.clearRepeatedGroup(); 1145 1146 message = builder.build(); 1147 RepeatedGroup.Builder repeatedGroupBuilder = RepeatedGroup.newBuilder() 1148 .setA(3); 1149 builder.addRepeatedGroup(repeatedGroupBuilder); 1150 messageAfterBuild = builder.build(); 1151 assertEquals(0, message.getRepeatedGroupCount()); 1152 builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); 1153 assertEquals(repeatedGroupBuilder.build(), messageAfterBuild.getRepeatedGroup(0)); 1154 assertEquals( 1155 RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); 1156 builder.clearRepeatedGroup(); 1157 1158 message = builder.build(); 1159 builder.addRepeatedGroup(0, repeatedGroupBuilder); 1160 messageAfterBuild = builder.build(); 1161 assertEquals(0, message.getRepeatedGroupCount()); 1162 builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); 1163 assertEquals(repeatedGroupBuilder.build(), messageAfterBuild.getRepeatedGroup(0)); 1164 assertEquals( 1165 RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); 1166 builder.clearRepeatedGroup(); 1167 1168 message = builder.build(); 1169 builder.addRepeatedInt32(1); 1170 messageAfterBuild = builder.build(); 1171 assertEquals(0, message.getRepeatedInt32Count()); 1172 builder.setRepeatedInt32(0, 0); 1173 assertEquals(1, messageAfterBuild.getRepeatedInt32(0)); 1174 assertEquals(0, builder.getRepeatedInt32(0)); 1175 builder.clearRepeatedInt32(); 1176 1177 message = builder.build(); 1178 builder.addRepeatedInt64(1L); 1179 messageAfterBuild = builder.build(); 1180 assertEquals(0L, message.getRepeatedInt64Count()); 1181 builder.setRepeatedInt64(0, 0L); 1182 assertEquals(1L, messageAfterBuild.getRepeatedInt64(0)); 1183 assertEquals(0L, builder.getRepeatedInt64(0)); 1184 builder.clearRepeatedInt64(); 1185 1186 message = builder.build(); 1187 builder.addRepeatedLazyMessage(nestedMessage); 1188 messageAfterBuild = builder.build(); 1189 assertEquals(0, message.getRepeatedLazyMessageCount()); 1190 builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); 1191 assertEquals(nestedMessage, messageAfterBuild.getRepeatedLazyMessage(0)); 1192 assertEquals( 1193 NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); 1194 builder.clearRepeatedLazyMessage(); 1195 1196 message = builder.build(); 1197 builder.addRepeatedLazyMessage(0, nestedMessage); 1198 messageAfterBuild = builder.build(); 1199 assertEquals(0, message.getRepeatedLazyMessageCount()); 1200 builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); 1201 assertEquals(nestedMessage, messageAfterBuild.getRepeatedLazyMessage(0)); 1202 assertEquals( 1203 NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); 1204 builder.clearRepeatedLazyMessage(); 1205 1206 message = builder.build(); 1207 builder.addRepeatedLazyMessage(nestedMessageBuilder); 1208 messageAfterBuild = builder.build(); 1209 assertEquals(0, message.getRepeatedLazyMessageCount()); 1210 builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); 1211 assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getRepeatedLazyMessage(0)); 1212 assertEquals( 1213 NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); 1214 builder.clearRepeatedLazyMessage(); 1215 1216 message = builder.build(); 1217 builder.addRepeatedLazyMessage(0, nestedMessageBuilder); 1218 messageAfterBuild = builder.build(); 1219 assertEquals(0, message.getRepeatedLazyMessageCount()); 1220 builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); 1221 assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getRepeatedLazyMessage(0)); 1222 assertEquals( 1223 NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); 1224 builder.clearRepeatedLazyMessage(); 1225 1226 message = builder.build(); 1227 builder.addRepeatedSfixed32(1); 1228 messageAfterBuild = builder.build(); 1229 assertEquals(0, message.getRepeatedSfixed32Count()); 1230 builder.setRepeatedSfixed32(0, 0); 1231 assertEquals(1, messageAfterBuild.getRepeatedSfixed32(0)); 1232 assertEquals(0, builder.getRepeatedSfixed32(0)); 1233 builder.clearRepeatedSfixed32(); 1234 1235 message = builder.build(); 1236 builder.addRepeatedSfixed64(1L); 1237 messageAfterBuild = builder.build(); 1238 assertEquals(0L, message.getRepeatedSfixed64Count()); 1239 builder.setRepeatedSfixed64(0, 0L); 1240 assertEquals(1L, messageAfterBuild.getRepeatedSfixed64(0)); 1241 assertEquals(0L, builder.getRepeatedSfixed64(0)); 1242 builder.clearRepeatedSfixed64(); 1243 1244 message = builder.build(); 1245 builder.addRepeatedSint32(1); 1246 messageAfterBuild = builder.build(); 1247 assertEquals(0, message.getRepeatedSint32Count()); 1248 builder.setRepeatedSint32(0, 0); 1249 assertEquals(1, messageAfterBuild.getRepeatedSint32(0)); 1250 assertEquals(0, builder.getRepeatedSint32(0)); 1251 builder.clearRepeatedSint32(); 1252 1253 message = builder.build(); 1254 builder.addRepeatedSint64(1L); 1255 messageAfterBuild = builder.build(); 1256 assertEquals(0L, message.getRepeatedSint64Count()); 1257 builder.setRepeatedSint64(0, 0L); 1258 assertEquals(1L, messageAfterBuild.getRepeatedSint64(0)); 1259 assertEquals(0L, builder.getRepeatedSint64(0)); 1260 builder.clearRepeatedSint64(); 1261 1262 message = builder.build(); 1263 builder.addRepeatedString("hi"); 1264 messageAfterBuild = builder.build(); 1265 assertEquals(0L, message.getRepeatedStringCount()); 1266 builder.setRepeatedString(0, ""); 1267 assertEquals("hi", messageAfterBuild.getRepeatedString(0)); 1268 assertEquals("", builder.getRepeatedString(0)); 1269 builder.clearRepeatedString(); 1270 1271 message = builder.build(); 1272 builder.addRepeatedStringBytes(ByteString.copyFromUtf8("hi")); 1273 messageAfterBuild = builder.build(); 1274 assertEquals(0L, message.getRepeatedStringCount()); 1275 builder.setRepeatedString(0, ""); 1276 assertEquals( 1277 ByteString.copyFromUtf8("hi"), 1278 messageAfterBuild.getRepeatedStringBytes(0)); 1279 assertEquals(ByteString.EMPTY, builder.getRepeatedStringBytes(0)); 1280 builder.clearRepeatedString(); 1281 1282 message = builder.build(); 1283 builder.addRepeatedStringPiece("hi"); 1284 messageAfterBuild = builder.build(); 1285 assertEquals(0L, message.getRepeatedStringPieceCount()); 1286 builder.setRepeatedStringPiece(0, ""); 1287 assertEquals("hi", messageAfterBuild.getRepeatedStringPiece(0)); 1288 assertEquals("", builder.getRepeatedStringPiece(0)); 1289 builder.clearRepeatedStringPiece(); 1290 1291 message = builder.build(); 1292 builder.addRepeatedStringPieceBytes(ByteString.copyFromUtf8("hi")); 1293 messageAfterBuild = builder.build(); 1294 assertEquals(0L, message.getRepeatedStringPieceCount()); 1295 builder.setRepeatedStringPiece(0, ""); 1296 assertEquals( 1297 ByteString.copyFromUtf8("hi"), 1298 messageAfterBuild.getRepeatedStringPieceBytes(0)); 1299 assertEquals(ByteString.EMPTY, builder.getRepeatedStringPieceBytes(0)); 1300 builder.clearRepeatedStringPiece(); 1301 1302 message = builder.build(); 1303 builder.addRepeatedUint32(1); 1304 messageAfterBuild = builder.build(); 1305 assertEquals(0, message.getRepeatedUint32Count()); 1306 builder.setRepeatedUint32(0, 0); 1307 assertEquals(1, messageAfterBuild.getRepeatedUint32(0)); 1308 assertEquals(0, builder.getRepeatedUint32(0)); 1309 builder.clearRepeatedUint32(); 1310 1311 message = builder.build(); 1312 builder.addRepeatedUint64(1L); 1313 messageAfterBuild = builder.build(); 1314 assertEquals(0L, message.getRepeatedUint64Count()); 1315 builder.setRepeatedUint64(0, 0L); 1316 assertEquals(1L, messageAfterBuild.getRepeatedUint64(0)); 1317 assertEquals(0L, builder.getRepeatedUint64(0)); 1318 builder.clearRepeatedUint64(); 1319 1320 message = builder.build(); 1321 assertEquals(0, message.getSerializedSize()); 1322 builder.mergeFrom(TestAllTypesLite.newBuilder() 1323 .setOptionalBool(true) 1324 .build()); 1325 assertEquals(0, message.getSerializedSize()); 1326 assertEquals(true, builder.build().getOptionalBool()); 1327 builder.clearOptionalBool(); 1328 1329 message = builder.build(); 1330 assertEquals(0, message.getSerializedSize()); 1331 builder.mergeFrom(TestAllTypesLite.newBuilder() 1332 .setOptionalBool(true) 1333 .build()); 1334 assertEquals(0, message.getSerializedSize()); 1335 assertEquals(true, builder.build().getOptionalBool()); 1336 builder.clear(); 1337 assertEquals(0, builder.build().getSerializedSize()); 1338 1339 message = builder.build(); 1340 assertEquals(0, message.getSerializedSize()); 1341 builder.mergeOptionalForeignMessage(foreignMessage); 1342 assertEquals(0, message.getSerializedSize()); 1343 assertEquals( 1344 foreignMessage.getC(), 1345 builder.build().getOptionalForeignMessage().getC()); 1346 builder.clearOptionalForeignMessage(); 1347 1348 message = builder.build(); 1349 assertEquals(0, message.getSerializedSize()); 1350 builder.mergeOptionalLazyMessage(nestedMessage); 1351 assertEquals(0, message.getSerializedSize()); 1352 assertEquals( 1353 nestedMessage.getBb(), 1354 builder.build().getOptionalLazyMessage().getBb()); 1355 builder.clearOptionalLazyMessage(); 1356 1357 message = builder.build(); 1358 builder.setOneofString("hi"); 1359 assertEquals( 1360 OneofFieldCase.ONEOFFIELD_NOT_SET, message.getOneofFieldCase()); 1361 assertEquals(OneofFieldCase.ONEOF_STRING, builder.getOneofFieldCase()); 1362 assertEquals("hi", builder.getOneofString()); 1363 messageAfterBuild = builder.build(); 1364 assertEquals( 1365 OneofFieldCase.ONEOF_STRING, messageAfterBuild.getOneofFieldCase()); 1366 assertEquals("hi", messageAfterBuild.getOneofString()); 1367 builder.setOneofUint32(1); 1368 assertEquals( 1369 OneofFieldCase.ONEOF_STRING, messageAfterBuild.getOneofFieldCase()); 1370 assertEquals("hi", messageAfterBuild.getOneofString()); 1371 assertEquals(OneofFieldCase.ONEOF_UINT32, builder.getOneofFieldCase()); 1372 assertEquals(1, builder.getOneofUint32()); 1373 TestAllTypesLiteOrBuilder messageOrBuilder = builder; 1374 assertEquals(OneofFieldCase.ONEOF_UINT32, messageOrBuilder.getOneofFieldCase()); 1375 1376 TestAllExtensionsLite.Builder extendableMessageBuilder = 1377 TestAllExtensionsLite.newBuilder(); 1378 TestAllExtensionsLite extendableMessage = extendableMessageBuilder.build(); 1379 extendableMessageBuilder.setExtension( 1380 UnittestLite.optionalInt32ExtensionLite, 1); 1381 assertFalse(extendableMessage.hasExtension( 1382 UnittestLite.optionalInt32ExtensionLite)); 1383 extendableMessage = extendableMessageBuilder.build(); 1384 assertEquals( 1385 1, (int) extendableMessageBuilder.getExtension( 1386 UnittestLite.optionalInt32ExtensionLite)); 1387 assertEquals( 1388 1, (int) extendableMessage.getExtension( 1389 UnittestLite.optionalInt32ExtensionLite)); 1390 extendableMessageBuilder.setExtension( 1391 UnittestLite.optionalInt32ExtensionLite, 3); 1392 assertEquals( 1393 3, (int) extendableMessageBuilder.getExtension( 1394 UnittestLite.optionalInt32ExtensionLite)); 1395 assertEquals( 1396 1, (int) extendableMessage.getExtension( 1397 UnittestLite.optionalInt32ExtensionLite)); 1398 extendableMessage = extendableMessageBuilder.build(); 1399 assertEquals( 1400 3, (int) extendableMessageBuilder.getExtension( 1401 UnittestLite.optionalInt32ExtensionLite)); 1402 assertEquals( 1403 3, (int) extendableMessage.getExtension( 1404 UnittestLite.optionalInt32ExtensionLite)); 1405 1406 // No extension registry, so it should be in unknown fields. 1407 extendableMessage = 1408 TestAllExtensionsLite.parseFrom(extendableMessage.toByteArray()); 1409 assertFalse(extendableMessage.hasExtension( 1410 UnittestLite.optionalInt32ExtensionLite)); 1411 1412 extendableMessageBuilder = extendableMessage.toBuilder(); 1413 extendableMessageBuilder.mergeFrom(TestAllExtensionsLite.newBuilder() 1414 .setExtension(UnittestLite.optionalFixed32ExtensionLite, 11) 1415 .build()); 1416 1417 extendableMessage = extendableMessageBuilder.build(); 1418 ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); 1419 UnittestLite.registerAllExtensions(registry); 1420 extendableMessage = TestAllExtensionsLite.parseFrom( 1421 extendableMessage.toByteArray(), registry); 1422 1423 // The unknown field was preserved. 1424 assertEquals( 1425 3, (int) extendableMessage.getExtension( 1426 UnittestLite.optionalInt32ExtensionLite)); 1427 assertEquals( 1428 11, (int) extendableMessage.getExtension( 1429 UnittestLite.optionalFixed32ExtensionLite)); 1430 } 1431 testToStringDefaultInstance()1432 public void testToStringDefaultInstance() throws Exception { 1433 assertToStringEquals("", TestAllTypesLite.getDefaultInstance()); 1434 } 1435 testToStringPrimitives()1436 public void testToStringPrimitives() throws Exception { 1437 TestAllTypesLite proto = TestAllTypesLite.newBuilder() 1438 .setOptionalInt32(1) 1439 .setOptionalInt64(9223372036854775807L) 1440 .build(); 1441 assertToStringEquals("optional_int32: 1\noptional_int64: 9223372036854775807", proto); 1442 1443 proto = TestAllTypesLite.newBuilder() 1444 .setOptionalBool(true) 1445 .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) 1446 .build(); 1447 assertToStringEquals("optional_bool: true\noptional_nested_enum: BAZ", proto); 1448 1449 proto = TestAllTypesLite.newBuilder() 1450 .setOptionalFloat(2.72f) 1451 .setOptionalDouble(3.14) 1452 .build(); 1453 assertToStringEquals("optional_double: 3.14\noptional_float: 2.72", proto); 1454 } 1455 testToStringStringFields()1456 public void testToStringStringFields() throws Exception { 1457 TestAllTypesLite proto = TestAllTypesLite.newBuilder() 1458 .setOptionalString("foo\"bar\nbaz\\") 1459 .build(); 1460 assertToStringEquals("optional_string: \"foo\\\"bar\\nbaz\\\\\"", proto); 1461 1462 proto = TestAllTypesLite.newBuilder() 1463 .setOptionalString("\u6587") 1464 .build(); 1465 assertToStringEquals("optional_string: \"\\346\\226\\207\"", proto); 1466 } 1467 testToStringNestedMessage()1468 public void testToStringNestedMessage() throws Exception { 1469 TestAllTypesLite proto = TestAllTypesLite.newBuilder() 1470 .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.getDefaultInstance()) 1471 .build(); 1472 assertToStringEquals("optional_nested_message {\n}", proto); 1473 1474 proto = TestAllTypesLite.newBuilder() 1475 .setOptionalNestedMessage( 1476 TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) 1477 .build(); 1478 assertToStringEquals("optional_nested_message {\n bb: 7\n}", proto); 1479 } 1480 testToStringRepeatedFields()1481 public void testToStringRepeatedFields() throws Exception { 1482 TestAllTypesLite proto = TestAllTypesLite.newBuilder() 1483 .addRepeatedInt32(32) 1484 .addRepeatedInt32(32) 1485 .addRepeatedInt64(64) 1486 .build(); 1487 assertToStringEquals("repeated_int32: 32\nrepeated_int32: 32\nrepeated_int64: 64", proto); 1488 1489 proto = TestAllTypesLite.newBuilder() 1490 .addRepeatedLazyMessage( 1491 TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) 1492 .addRepeatedLazyMessage( 1493 TestAllTypesLite.NestedMessage.newBuilder().setBb(8)) 1494 .build(); 1495 assertToStringEquals( 1496 "repeated_lazy_message {\n bb: 7\n}\nrepeated_lazy_message {\n bb: 8\n}", 1497 proto); 1498 } 1499 testToStringForeignFields()1500 public void testToStringForeignFields() throws Exception { 1501 TestAllTypesLite proto = TestAllTypesLite.newBuilder() 1502 .setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) 1503 .setOptionalForeignMessage( 1504 ForeignMessageLite.newBuilder() 1505 .setC(3)) 1506 .build(); 1507 assertToStringEquals( 1508 "optional_foreign_enum: FOREIGN_LITE_BAR\noptional_foreign_message {\n c: 3\n}", 1509 proto); 1510 } 1511 testToStringExtensions()1512 public void testToStringExtensions() throws Exception { 1513 TestAllExtensionsLite message = TestAllExtensionsLite.newBuilder() 1514 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) 1515 .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") 1516 .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") 1517 .setExtension(UnittestLite.optionalNestedEnumExtensionLite, 1518 TestAllTypesLite.NestedEnum.BAZ) 1519 .setExtension(UnittestLite.optionalNestedMessageExtensionLite, 1520 TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) 1521 .build(); 1522 assertToStringEquals( 1523 "[1]: 123\n[18] {\n bb: 7\n}\n[21]: 3\n[44]: \"spam\"\n[44]: \"eggs\"", 1524 message); 1525 } 1526 testToStringUnknownFields()1527 public void testToStringUnknownFields() throws Exception { 1528 TestAllExtensionsLite messageWithExtensions = TestAllExtensionsLite.newBuilder() 1529 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) 1530 .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") 1531 .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") 1532 .setExtension(UnittestLite.optionalNestedEnumExtensionLite, 1533 TestAllTypesLite.NestedEnum.BAZ) 1534 .setExtension(UnittestLite.optionalNestedMessageExtensionLite, 1535 TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) 1536 .build(); 1537 TestAllExtensionsLite messageWithUnknownFields = TestAllExtensionsLite.parseFrom( 1538 messageWithExtensions.toByteArray()); 1539 assertToStringEquals( 1540 "1: 123\n18: \"\\b\\a\"\n21: 3\n44: \"spam\"\n44: \"eggs\"", 1541 messageWithUnknownFields); 1542 } 1543 testToStringLazyMessage()1544 public void testToStringLazyMessage() throws Exception { 1545 TestAllTypesLite message = TestAllTypesLite.newBuilder() 1546 .setOptionalLazyMessage(NestedMessage.newBuilder().setBb(1).build()) 1547 .build(); 1548 assertToStringEquals("optional_lazy_message {\n bb: 1\n}", message); 1549 } 1550 testToStringGroup()1551 public void testToStringGroup() throws Exception { 1552 TestAllTypesLite message = TestAllTypesLite.newBuilder() 1553 .setOptionalGroup(OptionalGroup.newBuilder().setA(1).build()) 1554 .build(); 1555 assertToStringEquals("optional_group {\n a: 1\n}", message); 1556 } 1557 testToStringOneof()1558 public void testToStringOneof() throws Exception { 1559 TestAllTypesLite message = TestAllTypesLite.newBuilder() 1560 .setOneofString("hello") 1561 .build(); 1562 assertToStringEquals("oneof_string: \"hello\"", message); 1563 } 1564 1565 // Asserts that the toString() representation of the message matches the expected. This verifies 1566 // the first line starts with a comment; but, does not factor in said comment as part of the 1567 // comparison as it contains unstable addresses. assertToStringEquals(String expected, MessageLite message)1568 private static void assertToStringEquals(String expected, MessageLite message) { 1569 String toString = message.toString(); 1570 assertEquals('#', toString.charAt(0)); 1571 if (toString.indexOf("\n") >= 0) { 1572 toString = toString.substring(toString.indexOf("\n") + 1); 1573 } else { 1574 toString = ""; 1575 } 1576 assertEquals(expected, toString); 1577 } 1578 testParseLazy()1579 public void testParseLazy() throws Exception { 1580 ByteString bb = TestAllTypesLite.newBuilder() 1581 .setOptionalLazyMessage(NestedMessage.newBuilder() 1582 .setBb(11) 1583 .build()) 1584 .build().toByteString(); 1585 ByteString cc = TestAllTypesLite.newBuilder() 1586 .setOptionalLazyMessage(NestedMessage.newBuilder() 1587 .setCc(22) 1588 .build()) 1589 .build().toByteString(); 1590 1591 ByteString concat = bb.concat(cc); 1592 TestAllTypesLite message = TestAllTypesLite.parseFrom(concat); 1593 1594 assertEquals(11, message.getOptionalLazyMessage().getBb()); 1595 assertEquals(22L, message.getOptionalLazyMessage().getCc()); 1596 } 1597 testParseLazy_oneOf()1598 public void testParseLazy_oneOf() throws Exception { 1599 ByteString bb = TestAllTypesLite.newBuilder() 1600 .setOneofLazyNestedMessage(NestedMessage.newBuilder() 1601 .setBb(11) 1602 .build()) 1603 .build().toByteString(); 1604 ByteString cc = TestAllTypesLite.newBuilder() 1605 .setOneofLazyNestedMessage(NestedMessage.newBuilder() 1606 .setCc(22) 1607 .build()) 1608 .build().toByteString(); 1609 1610 ByteString concat = bb.concat(cc); 1611 TestAllTypesLite message = TestAllTypesLite.parseFrom(concat); 1612 1613 assertEquals(11, message.getOneofLazyNestedMessage().getBb()); 1614 assertEquals(22L, message.getOneofLazyNestedMessage().getCc()); 1615 } 1616 testMergeFromStream_repeatedField()1617 public void testMergeFromStream_repeatedField() throws Exception { 1618 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() 1619 .addRepeatedString("hello"); 1620 builder.mergeFrom(CodedInputStream.newInstance(builder.build().toByteArray())); 1621 1622 assertEquals(2, builder.getRepeatedStringCount()); 1623 } 1624 testMergeFromStream_invalidBytes()1625 public void testMergeFromStream_invalidBytes() throws Exception { 1626 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() 1627 .setDefaultBool(true); 1628 try { 1629 builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(Internal.UTF_8))); 1630 fail(); 1631 } catch (InvalidProtocolBufferException expected) {} 1632 } 1633 testMergeFrom_sanity()1634 public void testMergeFrom_sanity() throws Exception { 1635 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); 1636 byte[] bytes = one.toByteArray(); 1637 TestAllTypesLite two = TestAllTypesLite.parseFrom(bytes); 1638 1639 one = one.toBuilder().mergeFrom(one).build(); 1640 two = two.toBuilder().mergeFrom(bytes).build(); 1641 assertEquals(one, two); 1642 assertEquals(two, one); 1643 assertEquals(one.hashCode(), two.hashCode()); 1644 } 1645 testEquals_notEqual()1646 public void testEquals_notEqual() throws Exception { 1647 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); 1648 byte[] bytes = one.toByteArray(); 1649 TestAllTypesLite two = one.toBuilder().mergeFrom(one).mergeFrom(bytes).build(); 1650 1651 assertFalse(one.equals(two)); 1652 assertFalse(two.equals(one)); 1653 1654 assertFalse(one.equals(TestAllTypesLite.getDefaultInstance())); 1655 assertFalse(TestAllTypesLite.getDefaultInstance().equals(one)); 1656 1657 TestAllTypesLite oneFieldSet = TestAllTypesLite.newBuilder() 1658 .setDefaultBool(true) 1659 .build(); 1660 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1661 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1662 1663 oneFieldSet = TestAllTypesLite.newBuilder() 1664 .setDefaultBytes(ByteString.EMPTY) 1665 .build(); 1666 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1667 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1668 1669 oneFieldSet = TestAllTypesLite.newBuilder() 1670 .setDefaultCord("") 1671 .build(); 1672 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1673 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1674 1675 oneFieldSet = TestAllTypesLite.newBuilder() 1676 .setDefaultCordBytes(ByteString.EMPTY) 1677 .build(); 1678 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1679 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1680 1681 oneFieldSet = TestAllTypesLite.newBuilder() 1682 .setDefaultDouble(0) 1683 .build(); 1684 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1685 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1686 1687 oneFieldSet = TestAllTypesLite.newBuilder() 1688 .setDefaultFixed32(0) 1689 .build(); 1690 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1691 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1692 1693 oneFieldSet = TestAllTypesLite.newBuilder() 1694 .setDefaultFixed64(0) 1695 .build(); 1696 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1697 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1698 1699 oneFieldSet = TestAllTypesLite.newBuilder() 1700 .setDefaultFloat(0) 1701 .build(); 1702 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1703 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1704 1705 oneFieldSet = TestAllTypesLite.newBuilder() 1706 .setDefaultForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) 1707 .build(); 1708 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1709 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1710 1711 oneFieldSet = TestAllTypesLite.newBuilder() 1712 .setDefaultImportEnum(ImportEnumLite.IMPORT_LITE_BAR) 1713 .build(); 1714 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1715 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1716 1717 oneFieldSet = TestAllTypesLite.newBuilder() 1718 .setDefaultInt32(0) 1719 .build(); 1720 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1721 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1722 1723 oneFieldSet = TestAllTypesLite.newBuilder() 1724 .setDefaultInt64(0) 1725 .build(); 1726 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1727 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1728 1729 oneFieldSet = TestAllTypesLite.newBuilder() 1730 .setDefaultNestedEnum(NestedEnum.BAR) 1731 .build(); 1732 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1733 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1734 1735 oneFieldSet = TestAllTypesLite.newBuilder() 1736 .setDefaultSfixed32(0) 1737 .build(); 1738 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1739 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1740 1741 oneFieldSet = TestAllTypesLite.newBuilder() 1742 .setDefaultSfixed64(0) 1743 .build(); 1744 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1745 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1746 1747 oneFieldSet = TestAllTypesLite.newBuilder() 1748 .setDefaultSint32(0) 1749 .build(); 1750 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1751 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1752 1753 oneFieldSet = TestAllTypesLite.newBuilder() 1754 .setDefaultSint64(0) 1755 .build(); 1756 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1757 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1758 1759 oneFieldSet = TestAllTypesLite.newBuilder() 1760 .setDefaultString("") 1761 .build(); 1762 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1763 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1764 1765 oneFieldSet = TestAllTypesLite.newBuilder() 1766 .setDefaultStringBytes(ByteString.EMPTY) 1767 .build(); 1768 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1769 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1770 1771 oneFieldSet = TestAllTypesLite.newBuilder() 1772 .setDefaultStringPiece("") 1773 .build(); 1774 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1775 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1776 1777 oneFieldSet = TestAllTypesLite.newBuilder() 1778 .setDefaultStringPieceBytes(ByteString.EMPTY) 1779 .build(); 1780 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1781 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1782 1783 oneFieldSet = TestAllTypesLite.newBuilder() 1784 .setDefaultUint32(0) 1785 .build(); 1786 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1787 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1788 1789 oneFieldSet = TestAllTypesLite.newBuilder() 1790 .setDefaultUint64(0) 1791 .build(); 1792 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1793 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1794 1795 oneFieldSet = TestAllTypesLite.newBuilder() 1796 .addRepeatedBool(true) 1797 .build(); 1798 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1799 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1800 1801 oneFieldSet = TestAllTypesLite.newBuilder() 1802 .addRepeatedBytes(ByteString.EMPTY) 1803 .build(); 1804 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1805 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1806 1807 oneFieldSet = TestAllTypesLite.newBuilder() 1808 .addRepeatedCord("") 1809 .build(); 1810 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1811 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1812 1813 oneFieldSet = TestAllTypesLite.newBuilder() 1814 .addRepeatedCordBytes(ByteString.EMPTY) 1815 .build(); 1816 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1817 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1818 1819 oneFieldSet = TestAllTypesLite.newBuilder() 1820 .addRepeatedDouble(0) 1821 .build(); 1822 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1823 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1824 1825 oneFieldSet = TestAllTypesLite.newBuilder() 1826 .addRepeatedFixed32(0) 1827 .build(); 1828 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1829 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1830 1831 oneFieldSet = TestAllTypesLite.newBuilder() 1832 .addRepeatedFixed64(0) 1833 .build(); 1834 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1835 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1836 1837 oneFieldSet = TestAllTypesLite.newBuilder() 1838 .addRepeatedFloat(0) 1839 .build(); 1840 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1841 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1842 1843 oneFieldSet = TestAllTypesLite.newBuilder() 1844 .addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) 1845 .build(); 1846 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1847 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1848 1849 oneFieldSet = TestAllTypesLite.newBuilder() 1850 .addRepeatedImportEnum(ImportEnumLite.IMPORT_LITE_BAR) 1851 .build(); 1852 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1853 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1854 1855 oneFieldSet = TestAllTypesLite.newBuilder() 1856 .addRepeatedInt32(0) 1857 .build(); 1858 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1859 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1860 1861 oneFieldSet = TestAllTypesLite.newBuilder() 1862 .addRepeatedInt64(0) 1863 .build(); 1864 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1865 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1866 1867 oneFieldSet = TestAllTypesLite.newBuilder() 1868 .addRepeatedNestedEnum(NestedEnum.BAR) 1869 .build(); 1870 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1871 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1872 1873 oneFieldSet = TestAllTypesLite.newBuilder() 1874 .addRepeatedSfixed32(0) 1875 .build(); 1876 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1877 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1878 1879 oneFieldSet = TestAllTypesLite.newBuilder() 1880 .addRepeatedSfixed64(0) 1881 .build(); 1882 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1883 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1884 1885 oneFieldSet = TestAllTypesLite.newBuilder() 1886 .addRepeatedSint32(0) 1887 .build(); 1888 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1889 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1890 1891 oneFieldSet = TestAllTypesLite.newBuilder() 1892 .addRepeatedSint64(0) 1893 .build(); 1894 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1895 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1896 1897 oneFieldSet = TestAllTypesLite.newBuilder() 1898 .addRepeatedString("") 1899 .build(); 1900 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1901 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1902 1903 oneFieldSet = TestAllTypesLite.newBuilder() 1904 .addRepeatedStringBytes(ByteString.EMPTY) 1905 .build(); 1906 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1907 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1908 1909 oneFieldSet = TestAllTypesLite.newBuilder() 1910 .addRepeatedStringPiece("") 1911 .build(); 1912 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1913 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1914 1915 oneFieldSet = TestAllTypesLite.newBuilder() 1916 .addRepeatedStringPieceBytes(ByteString.EMPTY) 1917 .build(); 1918 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1919 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1920 1921 oneFieldSet = TestAllTypesLite.newBuilder() 1922 .addRepeatedUint32(0) 1923 .build(); 1924 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1925 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1926 1927 oneFieldSet = TestAllTypesLite.newBuilder() 1928 .addRepeatedUint64(0) 1929 .build(); 1930 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1931 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1932 1933 oneFieldSet = TestAllTypesLite.newBuilder() 1934 .setOptionalBool(true) 1935 .build(); 1936 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1937 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1938 1939 oneFieldSet = TestAllTypesLite.newBuilder() 1940 .setOptionalBytes(ByteString.EMPTY) 1941 .build(); 1942 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1943 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1944 1945 oneFieldSet = TestAllTypesLite.newBuilder() 1946 .setOptionalCord("") 1947 .build(); 1948 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1949 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1950 1951 oneFieldSet = TestAllTypesLite.newBuilder() 1952 .setOptionalCordBytes(ByteString.EMPTY) 1953 .build(); 1954 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1955 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1956 1957 oneFieldSet = TestAllTypesLite.newBuilder() 1958 .setOptionalDouble(0) 1959 .build(); 1960 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1961 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1962 1963 oneFieldSet = TestAllTypesLite.newBuilder() 1964 .setOptionalFixed32(0) 1965 .build(); 1966 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1967 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1968 1969 oneFieldSet = TestAllTypesLite.newBuilder() 1970 .setOptionalFixed64(0) 1971 .build(); 1972 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1973 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1974 1975 oneFieldSet = TestAllTypesLite.newBuilder() 1976 .setOptionalFloat(0) 1977 .build(); 1978 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1979 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1980 1981 oneFieldSet = TestAllTypesLite.newBuilder() 1982 .setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) 1983 .build(); 1984 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1985 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1986 1987 oneFieldSet = TestAllTypesLite.newBuilder() 1988 .setOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAR) 1989 .build(); 1990 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1991 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1992 1993 oneFieldSet = TestAllTypesLite.newBuilder() 1994 .setOptionalInt32(0) 1995 .build(); 1996 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 1997 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 1998 1999 oneFieldSet = TestAllTypesLite.newBuilder() 2000 .setOptionalInt64(0) 2001 .build(); 2002 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2003 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2004 2005 oneFieldSet = TestAllTypesLite.newBuilder() 2006 .setOptionalNestedEnum(NestedEnum.BAR) 2007 .build(); 2008 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2009 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2010 2011 oneFieldSet = TestAllTypesLite.newBuilder() 2012 .setOptionalSfixed32(0) 2013 .build(); 2014 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2015 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2016 2017 oneFieldSet = TestAllTypesLite.newBuilder() 2018 .setOptionalSfixed64(0) 2019 .build(); 2020 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2021 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2022 2023 oneFieldSet = TestAllTypesLite.newBuilder() 2024 .setOptionalSint32(0) 2025 .build(); 2026 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2027 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2028 2029 oneFieldSet = TestAllTypesLite.newBuilder() 2030 .setOptionalSint64(0) 2031 .build(); 2032 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2033 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2034 2035 oneFieldSet = TestAllTypesLite.newBuilder() 2036 .setOptionalString("") 2037 .build(); 2038 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2039 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2040 2041 oneFieldSet = TestAllTypesLite.newBuilder() 2042 .setOptionalStringBytes(ByteString.EMPTY) 2043 .build(); 2044 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2045 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2046 2047 oneFieldSet = TestAllTypesLite.newBuilder() 2048 .setOptionalStringPiece("") 2049 .build(); 2050 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2051 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2052 2053 oneFieldSet = TestAllTypesLite.newBuilder() 2054 .setOptionalStringPieceBytes(ByteString.EMPTY) 2055 .build(); 2056 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2057 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2058 2059 oneFieldSet = TestAllTypesLite.newBuilder() 2060 .setOptionalUint32(0) 2061 .build(); 2062 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2063 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2064 2065 oneFieldSet = TestAllTypesLite.newBuilder() 2066 .setOptionalUint64(0) 2067 .build(); 2068 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2069 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2070 2071 oneFieldSet = TestAllTypesLite.newBuilder() 2072 .setOneofBytes(ByteString.EMPTY) 2073 .build(); 2074 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2075 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2076 2077 oneFieldSet = TestAllTypesLite.newBuilder() 2078 .setOneofLazyNestedMessage(NestedMessage.getDefaultInstance()) 2079 .build(); 2080 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2081 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2082 2083 oneFieldSet = TestAllTypesLite.newBuilder() 2084 .setOneofNestedMessage(NestedMessage.getDefaultInstance()) 2085 .build(); 2086 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2087 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2088 2089 oneFieldSet = TestAllTypesLite.newBuilder() 2090 .setOneofString("") 2091 .build(); 2092 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2093 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2094 2095 oneFieldSet = TestAllTypesLite.newBuilder() 2096 .setOneofStringBytes(ByteString.EMPTY) 2097 .build(); 2098 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2099 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2100 2101 oneFieldSet = TestAllTypesLite.newBuilder() 2102 .setOneofUint32(0) 2103 .build(); 2104 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2105 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2106 2107 oneFieldSet = TestAllTypesLite.newBuilder() 2108 .setOptionalForeignMessage(ForeignMessageLite.getDefaultInstance()) 2109 .build(); 2110 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2111 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2112 2113 oneFieldSet = TestAllTypesLite.newBuilder() 2114 .setOptionalGroup(OptionalGroup.getDefaultInstance()) 2115 .build(); 2116 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2117 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2118 2119 oneFieldSet = TestAllTypesLite.newBuilder() 2120 .setOptionalPublicImportMessage(PublicImportMessageLite.getDefaultInstance()) 2121 .build(); 2122 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2123 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2124 2125 oneFieldSet = TestAllTypesLite.newBuilder() 2126 .setOptionalLazyMessage(NestedMessage.getDefaultInstance()) 2127 .build(); 2128 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2129 2130 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2131 oneFieldSet = TestAllTypesLite.newBuilder() 2132 .addRepeatedLazyMessage(NestedMessage.getDefaultInstance()) 2133 .build(); 2134 assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); 2135 assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); 2136 } 2137 testEquals()2138 public void testEquals() throws Exception { 2139 // Check that two identical objs are equal. 2140 Foo foo1a = Foo.newBuilder() 2141 .setValue(1) 2142 .addBar(Bar.newBuilder().setName("foo1")) 2143 .build(); 2144 Foo foo1b = Foo.newBuilder() 2145 .setValue(1) 2146 .addBar(Bar.newBuilder().setName("foo1")) 2147 .build(); 2148 Foo foo2 = Foo.newBuilder() 2149 .setValue(1) 2150 .addBar(Bar.newBuilder().setName("foo2")) 2151 .build(); 2152 2153 // Check that equals is doing value rather than object equality. 2154 assertEquals(foo1a, foo1b); 2155 assertEquals(foo1a.hashCode(), foo1b.hashCode()); 2156 2157 // Check that a diffeent object is not equal. 2158 assertFalse(foo1a.equals(foo2)); 2159 2160 // Check that two objects which have different types but the same field values are not 2161 // considered to be equal. 2162 Bar bar = Bar.newBuilder().setName("bar").build(); 2163 BarPrime barPrime = BarPrime.newBuilder().setName("bar").build(); 2164 assertFalse(bar.equals(barPrime)); 2165 } 2166 testOneofEquals()2167 public void testOneofEquals() throws Exception { 2168 TestOneofEquals.Builder builder = TestOneofEquals.newBuilder(); 2169 TestOneofEquals message1 = builder.build(); 2170 // Set message2's name field to default value. The two messages should be different when we 2171 // check with the oneof case. 2172 builder.setName(""); 2173 TestOneofEquals message2 = builder.build(); 2174 assertFalse(message1.equals(message2)); 2175 } 2176 testEquals_sanity()2177 public void testEquals_sanity() throws Exception { 2178 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); 2179 TestAllTypesLite two = TestAllTypesLite.parseFrom(one.toByteArray()); 2180 assertEquals(one, two); 2181 assertEquals(one.hashCode(), two.hashCode()); 2182 2183 assertEquals( 2184 one.toBuilder().mergeFrom(two).build(), 2185 two.toBuilder().mergeFrom(two.toByteArray()).build()); 2186 } 2187 testEqualsAndHashCodeWithUnknownFields()2188 public void testEqualsAndHashCodeWithUnknownFields() throws InvalidProtocolBufferException { 2189 Foo fooWithOnlyValue = Foo.newBuilder() 2190 .setValue(1) 2191 .build(); 2192 2193 Foo fooWithValueAndExtension = fooWithOnlyValue.toBuilder() 2194 .setValue(1) 2195 .setExtension(Bar.fooExt, Bar.newBuilder() 2196 .setName("name") 2197 .build()) 2198 .build(); 2199 2200 Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.toByteArray()); 2201 2202 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFields); 2203 assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnknownFields); 2204 } 2205 2206 // Test to ensure we avoid a class cast exception with oneofs. testEquals_oneOfMessages()2207 public void testEquals_oneOfMessages() { 2208 TestAllTypesLite mine = TestAllTypesLite.newBuilder() 2209 .setOneofString("Hello") 2210 .build(); 2211 2212 TestAllTypesLite other = TestAllTypesLite.newBuilder() 2213 .setOneofNestedMessage(NestedMessage.getDefaultInstance()) 2214 .build(); 2215 2216 assertFalse(mine.equals(other)); 2217 assertFalse(other.equals(mine)); 2218 } 2219 assertEqualsAndHashCodeAreFalse(Object o1, Object o2)2220 private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) { 2221 assertFalse(o1.equals(o2)); 2222 assertFalse(o1.hashCode() == o2.hashCode()); 2223 } 2224 testRecursiveHashcode()2225 public void testRecursiveHashcode() { 2226 // This tests that we don't infinite loop. 2227 TestRecursiveOneof.getDefaultInstance().hashCode(); 2228 } 2229 } 2230