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