GeneratedMessageTest.java revision fbaaef999ba563838ebd00874ed8a1c01fbf286d
1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// http://code.google.com/p/protobuf/ 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 31package com.google.protobuf; 32 33import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize; 34import protobuf_unittest.UnittestOptimizeFor.TestOptionalOptimizedForSize; 35import protobuf_unittest.UnittestOptimizeFor.TestRequiredOptimizedForSize; 36import protobuf_unittest.UnittestProto; 37import protobuf_unittest.UnittestProto.ForeignMessage; 38import protobuf_unittest.UnittestProto.ForeignEnum; 39import protobuf_unittest.UnittestProto.TestAllTypes; 40import protobuf_unittest.UnittestProto.TestAllExtensions; 41import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; 42import protobuf_unittest.MultipleFilesTestProto; 43import protobuf_unittest.MessageWithNoOuter; 44import protobuf_unittest.EnumWithNoOuter; 45import protobuf_unittest.ServiceWithNoOuter; 46import com.google.protobuf.UnittestLite; 47import com.google.protobuf.UnittestLite.TestAllExtensionsLite; 48 49import junit.framework.TestCase; 50import java.util.Arrays; 51 52/** 53 * Unit test for generated messages and generated code. See also 54 * {@link MessageTest}, which tests some generated message functionality. 55 * 56 * @author kenton@google.com Kenton Varda 57 */ 58public class GeneratedMessageTest extends TestCase { 59 TestUtil.ReflectionTester reflectionTester = 60 new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); 61 62 public void testDefaultInstance() throws Exception { 63 assertSame(TestAllTypes.getDefaultInstance(), 64 TestAllTypes.getDefaultInstance().getDefaultInstanceForType()); 65 assertSame(TestAllTypes.getDefaultInstance(), 66 TestAllTypes.newBuilder().getDefaultInstanceForType()); 67 } 68 69 public void testAccessors() throws Exception { 70 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 71 TestUtil.setAllFields(builder); 72 TestAllTypes message = builder.build(); 73 TestUtil.assertAllFieldsSet(message); 74 } 75 76 public void testDoubleBuildError() throws Exception { 77 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 78 builder.build(); 79 try { 80 builder.build(); 81 fail("Should have thrown exception."); 82 } catch (IllegalStateException e) { 83 // Success. 84 } 85 } 86 87 public void testClearAfterBuildError() throws Exception { 88 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 89 builder.build(); 90 try { 91 builder.clear(); 92 fail("Should have thrown exception."); 93 } catch (IllegalStateException e) { 94 // Success. 95 } 96 } 97 98 public void testSettersRejectNull() throws Exception { 99 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 100 try { 101 builder.setOptionalString(null); 102 fail("Exception was not thrown"); 103 } catch (NullPointerException e) { 104 // We expect this exception. 105 } 106 try { 107 builder.setOptionalBytes(null); 108 fail("Exception was not thrown"); 109 } catch (NullPointerException e) { 110 // We expect this exception. 111 } 112 try { 113 builder.setOptionalNestedMessage((TestAllTypes.NestedMessage) null); 114 fail("Exception was not thrown"); 115 } catch (NullPointerException e) { 116 // We expect this exception. 117 } 118 try { 119 builder.setOptionalNestedMessage( 120 (TestAllTypes.NestedMessage.Builder) null); 121 fail("Exception was not thrown"); 122 } catch (NullPointerException e) { 123 // We expect this exception. 124 } 125 try { 126 builder.setOptionalNestedEnum(null); 127 fail("Exception was not thrown"); 128 } catch (NullPointerException e) { 129 // We expect this exception. 130 } 131 try { 132 builder.addRepeatedString(null); 133 fail("Exception was not thrown"); 134 } catch (NullPointerException e) { 135 // We expect this exception. 136 } 137 try { 138 builder.addRepeatedBytes(null); 139 fail("Exception was not thrown"); 140 } catch (NullPointerException e) { 141 // We expect this exception. 142 } 143 try { 144 builder.addRepeatedNestedMessage((TestAllTypes.NestedMessage) null); 145 fail("Exception was not thrown"); 146 } catch (NullPointerException e) { 147 // We expect this exception. 148 } 149 try { 150 builder.addRepeatedNestedMessage( 151 (TestAllTypes.NestedMessage.Builder) null); 152 fail("Exception was not thrown"); 153 } catch (NullPointerException e) { 154 // We expect this exception. 155 } 156 try { 157 builder.addRepeatedNestedEnum(null); 158 fail("Exception was not thrown"); 159 } catch (NullPointerException e) { 160 // We expect this exception. 161 } 162 } 163 164 public void testRepeatedSetters() throws Exception { 165 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 166 TestUtil.setAllFields(builder); 167 TestUtil.modifyRepeatedFields(builder); 168 TestAllTypes message = builder.build(); 169 TestUtil.assertRepeatedFieldsModified(message); 170 } 171 172 public void testRepeatedSettersRejectNull() throws Exception { 173 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 174 175 builder.addRepeatedString("one"); 176 builder.addRepeatedString("two"); 177 try { 178 builder.setRepeatedString(1, null); 179 fail("Exception was not thrown"); 180 } catch (NullPointerException e) { 181 // We expect this exception. 182 } 183 184 builder.addRepeatedBytes(TestUtil.toBytes("one")); 185 builder.addRepeatedBytes(TestUtil.toBytes("two")); 186 try { 187 builder.setRepeatedBytes(1, null); 188 fail("Exception was not thrown"); 189 } catch (NullPointerException e) { 190 // We expect this exception. 191 } 192 193 builder.addRepeatedNestedMessage( 194 TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); 195 builder.addRepeatedNestedMessage( 196 TestAllTypes.NestedMessage.newBuilder().setBb(456).build()); 197 try { 198 builder.setRepeatedNestedMessage(1, (TestAllTypes.NestedMessage) null); 199 fail("Exception was not thrown"); 200 } catch (NullPointerException e) { 201 // We expect this exception. 202 } 203 try { 204 builder.setRepeatedNestedMessage( 205 1, (TestAllTypes.NestedMessage.Builder) null); 206 fail("Exception was not thrown"); 207 } catch (NullPointerException e) { 208 // We expect this exception. 209 } 210 211 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.FOO); 212 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); 213 try { 214 builder.setRepeatedNestedEnum(1, null); 215 fail("Exception was not thrown"); 216 } catch (NullPointerException e) { 217 // We expect this exception. 218 } 219 } 220 221 public void testRepeatedAppend() throws Exception { 222 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 223 224 builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4)); 225 builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ)); 226 227 ForeignMessage foreignMessage = 228 ForeignMessage.newBuilder().setC(12).build(); 229 builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage)); 230 231 TestAllTypes message = builder.build(); 232 assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4)); 233 assertEquals(message.getRepeatedForeignEnumList(), 234 Arrays.asList(ForeignEnum.FOREIGN_BAZ)); 235 assertEquals(1, message.getRepeatedForeignMessageCount()); 236 assertEquals(12, message.getRepeatedForeignMessage(0).getC()); 237 } 238 239 public void testRepeatedAppendRejectsNull() throws Exception { 240 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 241 242 ForeignMessage foreignMessage = 243 ForeignMessage.newBuilder().setC(12).build(); 244 try { 245 builder.addAllRepeatedForeignMessage( 246 Arrays.asList(foreignMessage, (ForeignMessage) null)); 247 fail("Exception was not thrown"); 248 } catch (NullPointerException e) { 249 // We expect this exception. 250 } 251 252 try { 253 builder.addAllRepeatedForeignEnum( 254 Arrays.asList(ForeignEnum.FOREIGN_BAZ, null)); 255 fail("Exception was not thrown"); 256 } catch (NullPointerException e) { 257 // We expect this exception. 258 } 259 260 try { 261 builder.addAllRepeatedString(Arrays.asList("one", null)); 262 fail("Exception was not thrown"); 263 } catch (NullPointerException e) { 264 // We expect this exception. 265 } 266 267 try { 268 builder.addAllRepeatedBytes(Arrays.asList(TestUtil.toBytes("one"), null)); 269 fail("Exception was not thrown"); 270 } catch (NullPointerException e) { 271 // We expect this exception. 272 } 273 } 274 275 public void testSettingForeignMessageUsingBuilder() throws Exception { 276 TestAllTypes message = TestAllTypes.newBuilder() 277 // Pass builder for foreign message instance. 278 .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123)) 279 .build(); 280 TestAllTypes expectedMessage = TestAllTypes.newBuilder() 281 // Create expected version passing foreign message instance explicitly. 282 .setOptionalForeignMessage( 283 ForeignMessage.newBuilder().setC(123).build()) 284 .build(); 285 // TODO(ngd): Upgrade to using real #equals method once implemented 286 assertEquals(expectedMessage.toString(), message.toString()); 287 } 288 289 public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception { 290 TestAllTypes message = TestAllTypes.newBuilder() 291 // Pass builder for foreign message instance. 292 .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456)) 293 .build(); 294 TestAllTypes expectedMessage = TestAllTypes.newBuilder() 295 // Create expected version passing foreign message instance explicitly. 296 .addRepeatedForeignMessage( 297 ForeignMessage.newBuilder().setC(456).build()) 298 .build(); 299 assertEquals(expectedMessage.toString(), message.toString()); 300 } 301 302 public void testDefaults() throws Exception { 303 TestUtil.assertClear(TestAllTypes.getDefaultInstance()); 304 TestUtil.assertClear(TestAllTypes.newBuilder().build()); 305 306 assertEquals("\u1234", 307 TestExtremeDefaultValues.getDefaultInstance().getUtf8String()); 308 } 309 310 public void testReflectionGetters() throws Exception { 311 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 312 TestUtil.setAllFields(builder); 313 TestAllTypes message = builder.build(); 314 reflectionTester.assertAllFieldsSetViaReflection(message); 315 } 316 317 public void testReflectionSetters() throws Exception { 318 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 319 reflectionTester.setAllFieldsViaReflection(builder); 320 TestAllTypes message = builder.build(); 321 TestUtil.assertAllFieldsSet(message); 322 } 323 324 public void testReflectionSettersRejectNull() throws Exception { 325 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 326 reflectionTester.assertReflectionSettersRejectNull(builder); 327 } 328 329 public void testReflectionRepeatedSetters() throws Exception { 330 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 331 reflectionTester.setAllFieldsViaReflection(builder); 332 reflectionTester.modifyRepeatedFieldsViaReflection(builder); 333 TestAllTypes message = builder.build(); 334 TestUtil.assertRepeatedFieldsModified(message); 335 } 336 337 public void testReflectionRepeatedSettersRejectNull() throws Exception { 338 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 339 reflectionTester.assertReflectionRepeatedSettersRejectNull(builder); 340 } 341 342 public void testReflectionDefaults() throws Exception { 343 reflectionTester.assertClearViaReflection( 344 TestAllTypes.getDefaultInstance()); 345 reflectionTester.assertClearViaReflection( 346 TestAllTypes.newBuilder().build()); 347 } 348 349 public void testEnumInterface() throws Exception { 350 assertTrue(TestAllTypes.getDefaultInstance().getDefaultNestedEnum() 351 instanceof ProtocolMessageEnum); 352 } 353 354 public void testEnumMap() throws Exception { 355 Internal.EnumLiteMap<ForeignEnum> map = ForeignEnum.internalGetValueMap(); 356 357 for (ForeignEnum value : ForeignEnum.values()) { 358 assertEquals(value, map.findValueByNumber(value.getNumber())); 359 } 360 361 assertTrue(map.findValueByNumber(12345) == null); 362 } 363 364 // ================================================================= 365 // Extensions. 366 367 TestUtil.ReflectionTester extensionsReflectionTester = 368 new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(), 369 TestUtil.getExtensionRegistry()); 370 371 public void testExtensionAccessors() throws Exception { 372 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 373 TestUtil.setAllExtensions(builder); 374 TestAllExtensions message = builder.build(); 375 TestUtil.assertAllExtensionsSet(message); 376 } 377 378 public void testExtensionRepeatedSetters() throws Exception { 379 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 380 TestUtil.setAllExtensions(builder); 381 TestUtil.modifyRepeatedExtensions(builder); 382 TestAllExtensions message = builder.build(); 383 TestUtil.assertRepeatedExtensionsModified(message); 384 } 385 386 public void testExtensionDefaults() throws Exception { 387 TestUtil.assertExtensionsClear(TestAllExtensions.getDefaultInstance()); 388 TestUtil.assertExtensionsClear(TestAllExtensions.newBuilder().build()); 389 } 390 391 public void testExtensionReflectionGetters() throws Exception { 392 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 393 TestUtil.setAllExtensions(builder); 394 TestAllExtensions message = builder.build(); 395 extensionsReflectionTester.assertAllFieldsSetViaReflection(message); 396 } 397 398 public void testExtensionReflectionSetters() throws Exception { 399 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 400 extensionsReflectionTester.setAllFieldsViaReflection(builder); 401 TestAllExtensions message = builder.build(); 402 TestUtil.assertAllExtensionsSet(message); 403 } 404 405 public void testExtensionReflectionSettersRejectNull() throws Exception { 406 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 407 extensionsReflectionTester.assertReflectionSettersRejectNull(builder); 408 } 409 410 public void testExtensionReflectionRepeatedSetters() throws Exception { 411 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 412 extensionsReflectionTester.setAllFieldsViaReflection(builder); 413 extensionsReflectionTester.modifyRepeatedFieldsViaReflection(builder); 414 TestAllExtensions message = builder.build(); 415 TestUtil.assertRepeatedExtensionsModified(message); 416 } 417 418 public void testExtensionReflectionRepeatedSettersRejectNull() 419 throws Exception { 420 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 421 extensionsReflectionTester.assertReflectionRepeatedSettersRejectNull( 422 builder); 423 } 424 425 public void testExtensionReflectionDefaults() throws Exception { 426 extensionsReflectionTester.assertClearViaReflection( 427 TestAllExtensions.getDefaultInstance()); 428 extensionsReflectionTester.assertClearViaReflection( 429 TestAllExtensions.newBuilder().build()); 430 } 431 432 public void testClearExtension() throws Exception { 433 // clearExtension() is not actually used in TestUtil, so try it manually. 434 assertFalse( 435 TestAllExtensions.newBuilder() 436 .setExtension(UnittestProto.optionalInt32Extension, 1) 437 .clearExtension(UnittestProto.optionalInt32Extension) 438 .hasExtension(UnittestProto.optionalInt32Extension)); 439 assertEquals(0, 440 TestAllExtensions.newBuilder() 441 .addExtension(UnittestProto.repeatedInt32Extension, 1) 442 .clearExtension(UnittestProto.repeatedInt32Extension) 443 .getExtensionCount(UnittestProto.repeatedInt32Extension)); 444 } 445 446 public void testExtensionCopy() throws Exception { 447 TestAllExtensions original = TestUtil.getAllExtensionsSet(); 448 TestAllExtensions copy = TestAllExtensions.newBuilder(original).build(); 449 TestUtil.assertAllExtensionsSet(copy); 450 } 451 452 public void testExtensionMergeFrom() throws Exception { 453 TestAllExtensions original = 454 TestAllExtensions.newBuilder() 455 .setExtension(UnittestProto.optionalInt32Extension, 1).build(); 456 TestAllExtensions merged = 457 TestAllExtensions.newBuilder().mergeFrom(original).build(); 458 assertTrue(merged.hasExtension(UnittestProto.optionalInt32Extension)); 459 assertEquals( 460 1, (int) merged.getExtension(UnittestProto.optionalInt32Extension)); 461 } 462 463 // ================================================================= 464 // Lite Extensions. 465 466 // We test lite extensions directly because they have a separate 467 // implementation from full extensions. In contrast, we do not test 468 // lite fields directly since they are implemented exactly the same as 469 // regular fields. 470 471 public void testLiteExtensionAccessors() throws Exception { 472 TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); 473 TestUtil.setAllExtensions(builder); 474 TestAllExtensionsLite message = builder.build(); 475 TestUtil.assertAllExtensionsSet(message); 476 } 477 478 public void testLiteExtensionRepeatedSetters() throws Exception { 479 TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); 480 TestUtil.setAllExtensions(builder); 481 TestUtil.modifyRepeatedExtensions(builder); 482 TestAllExtensionsLite message = builder.build(); 483 TestUtil.assertRepeatedExtensionsModified(message); 484 } 485 486 public void testLiteExtensionDefaults() throws Exception { 487 TestUtil.assertExtensionsClear(TestAllExtensionsLite.getDefaultInstance()); 488 TestUtil.assertExtensionsClear(TestAllExtensionsLite.newBuilder().build()); 489 } 490 491 public void testClearLiteExtension() throws Exception { 492 // clearExtension() is not actually used in TestUtil, so try it manually. 493 assertFalse( 494 TestAllExtensionsLite.newBuilder() 495 .setExtension(UnittestLite.optionalInt32ExtensionLite, 1) 496 .clearExtension(UnittestLite.optionalInt32ExtensionLite) 497 .hasExtension(UnittestLite.optionalInt32ExtensionLite)); 498 assertEquals(0, 499 TestAllExtensionsLite.newBuilder() 500 .addExtension(UnittestLite.repeatedInt32ExtensionLite, 1) 501 .clearExtension(UnittestLite.repeatedInt32ExtensionLite) 502 .getExtensionCount(UnittestLite.repeatedInt32ExtensionLite)); 503 } 504 505 public void testLiteExtensionCopy() throws Exception { 506 TestAllExtensionsLite original = TestUtil.getAllLiteExtensionsSet(); 507 TestAllExtensionsLite copy = 508 TestAllExtensionsLite.newBuilder(original).build(); 509 TestUtil.assertAllExtensionsSet(copy); 510 } 511 512 public void testLiteExtensionMergeFrom() throws Exception { 513 TestAllExtensionsLite original = 514 TestAllExtensionsLite.newBuilder() 515 .setExtension(UnittestLite.optionalInt32ExtensionLite, 1).build(); 516 TestAllExtensionsLite merged = 517 TestAllExtensionsLite.newBuilder().mergeFrom(original).build(); 518 assertTrue(merged.hasExtension(UnittestLite.optionalInt32ExtensionLite)); 519 assertEquals( 520 1, (int) merged.getExtension(UnittestLite.optionalInt32ExtensionLite)); 521 } 522 523 // ================================================================= 524 // multiple_files_test 525 526 public void testMultipleFilesOption() throws Exception { 527 // We mostly just want to check that things compile. 528 MessageWithNoOuter message = 529 MessageWithNoOuter.newBuilder() 530 .setNested(MessageWithNoOuter.NestedMessage.newBuilder().setI(1)) 531 .addForeign(TestAllTypes.newBuilder().setOptionalInt32(1)) 532 .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ) 533 .setForeignEnum(EnumWithNoOuter.BAR) 534 .build(); 535 assertEquals(message, MessageWithNoOuter.parseFrom(message.toByteString())); 536 537 assertEquals(MultipleFilesTestProto.getDescriptor(), 538 MessageWithNoOuter.getDescriptor().getFile()); 539 540 Descriptors.FieldDescriptor field = 541 MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum"); 542 assertEquals(EnumWithNoOuter.BAR.getValueDescriptor(), 543 message.getField(field)); 544 545 assertEquals(MultipleFilesTestProto.getDescriptor(), 546 ServiceWithNoOuter.getDescriptor().getFile()); 547 548 assertFalse( 549 TestAllExtensions.getDefaultInstance().hasExtension( 550 MultipleFilesTestProto.extensionWithOuter)); 551 } 552 553 public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize() 554 throws Exception { 555 TestOptionalOptimizedForSize message = 556 TestOptionalOptimizedForSize.getDefaultInstance(); 557 assertTrue(message.isInitialized()); 558 559 message = TestOptionalOptimizedForSize.newBuilder().setO( 560 TestRequiredOptimizedForSize.newBuilder().buildPartial() 561 ).buildPartial(); 562 assertFalse(message.isInitialized()); 563 564 message = TestOptionalOptimizedForSize.newBuilder().setO( 565 TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial() 566 ).buildPartial(); 567 assertTrue(message.isInitialized()); 568 } 569 570 public void testUninitializedExtensionInOptimizedForSize() 571 throws Exception { 572 TestOptimizedForSize.Builder builder = TestOptimizedForSize.newBuilder(); 573 builder.setExtension(TestOptimizedForSize.testExtension2, 574 TestRequiredOptimizedForSize.newBuilder().buildPartial()); 575 assertFalse(builder.isInitialized()); 576 assertFalse(builder.buildPartial().isInitialized()); 577 578 builder = TestOptimizedForSize.newBuilder(); 579 builder.setExtension(TestOptimizedForSize.testExtension2, 580 TestRequiredOptimizedForSize.newBuilder().setX(10).buildPartial()); 581 assertTrue(builder.isInitialized()); 582 assertTrue(builder.buildPartial().isInitialized()); 583 } 584 585 public void testToBuilder() throws Exception { 586 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 587 TestUtil.setAllFields(builder); 588 TestAllTypes message = builder.build(); 589 TestUtil.assertAllFieldsSet(message.toBuilder().build()); 590 } 591 592 public void testFieldConstantValues() throws Exception { 593 assertEquals(TestAllTypes.NestedMessage.BB_FIELD_NUMBER, 1); 594 assertEquals(TestAllTypes.OPTIONAL_INT32_FIELD_NUMBER, 1); 595 assertEquals(TestAllTypes.OPTIONALGROUP_FIELD_NUMBER, 16); 596 assertEquals(TestAllTypes.OPTIONAL_NESTED_MESSAGE_FIELD_NUMBER, 18); 597 assertEquals(TestAllTypes.OPTIONAL_NESTED_ENUM_FIELD_NUMBER, 21); 598 assertEquals(TestAllTypes.REPEATED_INT32_FIELD_NUMBER, 31); 599 assertEquals(TestAllTypes.REPEATEDGROUP_FIELD_NUMBER, 46); 600 assertEquals(TestAllTypes.REPEATED_NESTED_MESSAGE_FIELD_NUMBER, 48); 601 assertEquals(TestAllTypes.REPEATED_NESTED_ENUM_FIELD_NUMBER, 51); 602 } 603 604 public void testExtensionConstantValues() throws Exception { 605 assertEquals(UnittestProto.TestRequired.SINGLE_FIELD_NUMBER, 1000); 606 assertEquals(UnittestProto.TestRequired.MULTI_FIELD_NUMBER, 1001); 607 assertEquals(UnittestProto.OPTIONAL_INT32_EXTENSION_FIELD_NUMBER, 1); 608 assertEquals(UnittestProto.OPTIONALGROUP_EXTENSION_FIELD_NUMBER, 16); 609 assertEquals( 610 UnittestProto.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18); 611 assertEquals(UnittestProto.OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 21); 612 assertEquals(UnittestProto.REPEATED_INT32_EXTENSION_FIELD_NUMBER, 31); 613 assertEquals(UnittestProto.REPEATEDGROUP_EXTENSION_FIELD_NUMBER, 46); 614 assertEquals( 615 UnittestProto.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48); 616 assertEquals(UnittestProto.REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 51); 617 } 618} 619