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.UnittestProto;
     34 import com.google.protobuf.UnittestLite;
     35 
     36 // The static imports are to avoid 100+ char lines.  The following is roughly equivalent to
     37 // import static protobuf_unittest.UnittestProto.*;
     38 import static protobuf_unittest.UnittestProto.defaultInt32Extension;
     39 import static protobuf_unittest.UnittestProto.defaultInt64Extension;
     40 import static protobuf_unittest.UnittestProto.defaultUint32Extension;
     41 import static protobuf_unittest.UnittestProto.defaultUint64Extension;
     42 import static protobuf_unittest.UnittestProto.defaultSint32Extension;
     43 import static protobuf_unittest.UnittestProto.defaultSint64Extension;
     44 import static protobuf_unittest.UnittestProto.defaultFixed32Extension;
     45 import static protobuf_unittest.UnittestProto.defaultFixed64Extension;
     46 import static protobuf_unittest.UnittestProto.defaultSfixed32Extension;
     47 import static protobuf_unittest.UnittestProto.defaultSfixed64Extension;
     48 import static protobuf_unittest.UnittestProto.defaultFloatExtension;
     49 import static protobuf_unittest.UnittestProto.defaultDoubleExtension;
     50 import static protobuf_unittest.UnittestProto.defaultBoolExtension;
     51 import static protobuf_unittest.UnittestProto.defaultStringExtension;
     52 import static protobuf_unittest.UnittestProto.defaultBytesExtension;
     53 import static protobuf_unittest.UnittestProto.defaultNestedEnumExtension;
     54 import static protobuf_unittest.UnittestProto.defaultForeignEnumExtension;
     55 import static protobuf_unittest.UnittestProto.defaultImportEnumExtension;
     56 import static protobuf_unittest.UnittestProto.defaultStringPieceExtension;
     57 import static protobuf_unittest.UnittestProto.defaultCordExtension;
     58 
     59 import static protobuf_unittest.UnittestProto.optionalInt32Extension;
     60 import static protobuf_unittest.UnittestProto.optionalInt64Extension;
     61 import static protobuf_unittest.UnittestProto.optionalUint32Extension;
     62 import static protobuf_unittest.UnittestProto.optionalUint64Extension;
     63 import static protobuf_unittest.UnittestProto.optionalSint32Extension;
     64 import static protobuf_unittest.UnittestProto.optionalSint64Extension;
     65 import static protobuf_unittest.UnittestProto.optionalFixed32Extension;
     66 import static protobuf_unittest.UnittestProto.optionalFixed64Extension;
     67 import static protobuf_unittest.UnittestProto.optionalSfixed32Extension;
     68 import static protobuf_unittest.UnittestProto.optionalSfixed64Extension;
     69 import static protobuf_unittest.UnittestProto.optionalFloatExtension;
     70 import static protobuf_unittest.UnittestProto.optionalDoubleExtension;
     71 import static protobuf_unittest.UnittestProto.optionalBoolExtension;
     72 import static protobuf_unittest.UnittestProto.optionalStringExtension;
     73 import static protobuf_unittest.UnittestProto.optionalBytesExtension;
     74 import static protobuf_unittest.UnittestProto.optionalGroupExtension;
     75 import static protobuf_unittest.UnittestProto.optionalNestedMessageExtension;
     76 import static protobuf_unittest.UnittestProto.optionalForeignMessageExtension;
     77 import static protobuf_unittest.UnittestProto.optionalImportMessageExtension;
     78 import static protobuf_unittest.UnittestProto.optionalNestedEnumExtension;
     79 import static protobuf_unittest.UnittestProto.optionalForeignEnumExtension;
     80 import static protobuf_unittest.UnittestProto.optionalImportEnumExtension;
     81 import static protobuf_unittest.UnittestProto.optionalStringPieceExtension;
     82 import static protobuf_unittest.UnittestProto.optionalCordExtension;
     83 
     84 import static protobuf_unittest.UnittestProto.repeatedInt32Extension;
     85 import static protobuf_unittest.UnittestProto.repeatedInt64Extension;
     86 import static protobuf_unittest.UnittestProto.repeatedUint32Extension;
     87 import static protobuf_unittest.UnittestProto.repeatedUint64Extension;
     88 import static protobuf_unittest.UnittestProto.repeatedSint32Extension;
     89 import static protobuf_unittest.UnittestProto.repeatedSint64Extension;
     90 import static protobuf_unittest.UnittestProto.repeatedFixed32Extension;
     91 import static protobuf_unittest.UnittestProto.repeatedFixed64Extension;
     92 import static protobuf_unittest.UnittestProto.repeatedSfixed32Extension;
     93 import static protobuf_unittest.UnittestProto.repeatedSfixed64Extension;
     94 import static protobuf_unittest.UnittestProto.repeatedFloatExtension;
     95 import static protobuf_unittest.UnittestProto.repeatedDoubleExtension;
     96 import static protobuf_unittest.UnittestProto.repeatedBoolExtension;
     97 import static protobuf_unittest.UnittestProto.repeatedStringExtension;
     98 import static protobuf_unittest.UnittestProto.repeatedBytesExtension;
     99 import static protobuf_unittest.UnittestProto.repeatedGroupExtension;
    100 import static protobuf_unittest.UnittestProto.repeatedNestedMessageExtension;
    101 import static protobuf_unittest.UnittestProto.repeatedForeignMessageExtension;
    102 import static protobuf_unittest.UnittestProto.repeatedImportMessageExtension;
    103 import static protobuf_unittest.UnittestProto.repeatedNestedEnumExtension;
    104 import static protobuf_unittest.UnittestProto.repeatedForeignEnumExtension;
    105 import static protobuf_unittest.UnittestProto.repeatedImportEnumExtension;
    106 import static protobuf_unittest.UnittestProto.repeatedStringPieceExtension;
    107 import static protobuf_unittest.UnittestProto.repeatedCordExtension;
    108 
    109 import static protobuf_unittest.UnittestProto.OptionalGroup_extension;
    110 import static protobuf_unittest.UnittestProto.RepeatedGroup_extension;
    111 
    112 import static protobuf_unittest.UnittestProto.packedInt32Extension;
    113 import static protobuf_unittest.UnittestProto.packedInt64Extension;
    114 import static protobuf_unittest.UnittestProto.packedUint32Extension;
    115 import static protobuf_unittest.UnittestProto.packedUint64Extension;
    116 import static protobuf_unittest.UnittestProto.packedSint32Extension;
    117 import static protobuf_unittest.UnittestProto.packedSint64Extension;
    118 import static protobuf_unittest.UnittestProto.packedFixed32Extension;
    119 import static protobuf_unittest.UnittestProto.packedFixed64Extension;
    120 import static protobuf_unittest.UnittestProto.packedSfixed32Extension;
    121 import static protobuf_unittest.UnittestProto.packedSfixed64Extension;
    122 import static protobuf_unittest.UnittestProto.packedFloatExtension;
    123 import static protobuf_unittest.UnittestProto.packedDoubleExtension;
    124 import static protobuf_unittest.UnittestProto.packedBoolExtension;
    125 import static protobuf_unittest.UnittestProto.packedEnumExtension;
    126 
    127 import static com.google.protobuf.UnittestLite.defaultInt32ExtensionLite;
    128 import static com.google.protobuf.UnittestLite.defaultInt64ExtensionLite;
    129 import static com.google.protobuf.UnittestLite.defaultUint32ExtensionLite;
    130 import static com.google.protobuf.UnittestLite.defaultUint64ExtensionLite;
    131 import static com.google.protobuf.UnittestLite.defaultSint32ExtensionLite;
    132 import static com.google.protobuf.UnittestLite.defaultSint64ExtensionLite;
    133 import static com.google.protobuf.UnittestLite.defaultFixed32ExtensionLite;
    134 import static com.google.protobuf.UnittestLite.defaultFixed64ExtensionLite;
    135 import static com.google.protobuf.UnittestLite.defaultSfixed32ExtensionLite;
    136 import static com.google.protobuf.UnittestLite.defaultSfixed64ExtensionLite;
    137 import static com.google.protobuf.UnittestLite.defaultFloatExtensionLite;
    138 import static com.google.protobuf.UnittestLite.defaultDoubleExtensionLite;
    139 import static com.google.protobuf.UnittestLite.defaultBoolExtensionLite;
    140 import static com.google.protobuf.UnittestLite.defaultStringExtensionLite;
    141 import static com.google.protobuf.UnittestLite.defaultBytesExtensionLite;
    142 import static com.google.protobuf.UnittestLite.defaultNestedEnumExtensionLite;
    143 import static com.google.protobuf.UnittestLite.defaultForeignEnumExtensionLite;
    144 import static com.google.protobuf.UnittestLite.defaultImportEnumExtensionLite;
    145 import static com.google.protobuf.UnittestLite.defaultStringPieceExtensionLite;
    146 import static com.google.protobuf.UnittestLite.defaultCordExtensionLite;
    147 
    148 import static com.google.protobuf.UnittestLite.optionalInt32ExtensionLite;
    149 import static com.google.protobuf.UnittestLite.optionalInt64ExtensionLite;
    150 import static com.google.protobuf.UnittestLite.optionalUint32ExtensionLite;
    151 import static com.google.protobuf.UnittestLite.optionalUint64ExtensionLite;
    152 import static com.google.protobuf.UnittestLite.optionalSint32ExtensionLite;
    153 import static com.google.protobuf.UnittestLite.optionalSint64ExtensionLite;
    154 import static com.google.protobuf.UnittestLite.optionalFixed32ExtensionLite;
    155 import static com.google.protobuf.UnittestLite.optionalFixed64ExtensionLite;
    156 import static com.google.protobuf.UnittestLite.optionalSfixed32ExtensionLite;
    157 import static com.google.protobuf.UnittestLite.optionalSfixed64ExtensionLite;
    158 import static com.google.protobuf.UnittestLite.optionalFloatExtensionLite;
    159 import static com.google.protobuf.UnittestLite.optionalDoubleExtensionLite;
    160 import static com.google.protobuf.UnittestLite.optionalBoolExtensionLite;
    161 import static com.google.protobuf.UnittestLite.optionalStringExtensionLite;
    162 import static com.google.protobuf.UnittestLite.optionalBytesExtensionLite;
    163 import static com.google.protobuf.UnittestLite.optionalGroupExtensionLite;
    164 import static com.google.protobuf.UnittestLite.optionalNestedMessageExtensionLite;
    165 import static com.google.protobuf.UnittestLite.optionalForeignMessageExtensionLite;
    166 import static com.google.protobuf.UnittestLite.optionalImportMessageExtensionLite;
    167 import static com.google.protobuf.UnittestLite.optionalNestedEnumExtensionLite;
    168 import static com.google.protobuf.UnittestLite.optionalForeignEnumExtensionLite;
    169 import static com.google.protobuf.UnittestLite.optionalImportEnumExtensionLite;
    170 import static com.google.protobuf.UnittestLite.optionalStringPieceExtensionLite;
    171 import static com.google.protobuf.UnittestLite.optionalCordExtensionLite;
    172 
    173 import static com.google.protobuf.UnittestLite.repeatedInt32ExtensionLite;
    174 import static com.google.protobuf.UnittestLite.repeatedInt64ExtensionLite;
    175 import static com.google.protobuf.UnittestLite.repeatedUint32ExtensionLite;
    176 import static com.google.protobuf.UnittestLite.repeatedUint64ExtensionLite;
    177 import static com.google.protobuf.UnittestLite.repeatedSint32ExtensionLite;
    178 import static com.google.protobuf.UnittestLite.repeatedSint64ExtensionLite;
    179 import static com.google.protobuf.UnittestLite.repeatedFixed32ExtensionLite;
    180 import static com.google.protobuf.UnittestLite.repeatedFixed64ExtensionLite;
    181 import static com.google.protobuf.UnittestLite.repeatedSfixed32ExtensionLite;
    182 import static com.google.protobuf.UnittestLite.repeatedSfixed64ExtensionLite;
    183 import static com.google.protobuf.UnittestLite.repeatedFloatExtensionLite;
    184 import static com.google.protobuf.UnittestLite.repeatedDoubleExtensionLite;
    185 import static com.google.protobuf.UnittestLite.repeatedBoolExtensionLite;
    186 import static com.google.protobuf.UnittestLite.repeatedStringExtensionLite;
    187 import static com.google.protobuf.UnittestLite.repeatedBytesExtensionLite;
    188 import static com.google.protobuf.UnittestLite.repeatedGroupExtensionLite;
    189 import static com.google.protobuf.UnittestLite.repeatedNestedMessageExtensionLite;
    190 import static com.google.protobuf.UnittestLite.repeatedForeignMessageExtensionLite;
    191 import static com.google.protobuf.UnittestLite.repeatedImportMessageExtensionLite;
    192 import static com.google.protobuf.UnittestLite.repeatedNestedEnumExtensionLite;
    193 import static com.google.protobuf.UnittestLite.repeatedForeignEnumExtensionLite;
    194 import static com.google.protobuf.UnittestLite.repeatedImportEnumExtensionLite;
    195 import static com.google.protobuf.UnittestLite.repeatedStringPieceExtensionLite;
    196 import static com.google.protobuf.UnittestLite.repeatedCordExtensionLite;
    197 
    198 import static com.google.protobuf.UnittestLite.OptionalGroup_extension_lite;
    199 import static com.google.protobuf.UnittestLite.RepeatedGroup_extension_lite;
    200 
    201 import static com.google.protobuf.UnittestLite.packedInt32ExtensionLite;
    202 import static com.google.protobuf.UnittestLite.packedInt64ExtensionLite;
    203 import static com.google.protobuf.UnittestLite.packedUint32ExtensionLite;
    204 import static com.google.protobuf.UnittestLite.packedUint64ExtensionLite;
    205 import static com.google.protobuf.UnittestLite.packedSint32ExtensionLite;
    206 import static com.google.protobuf.UnittestLite.packedSint64ExtensionLite;
    207 import static com.google.protobuf.UnittestLite.packedFixed32ExtensionLite;
    208 import static com.google.protobuf.UnittestLite.packedFixed64ExtensionLite;
    209 import static com.google.protobuf.UnittestLite.packedSfixed32ExtensionLite;
    210 import static com.google.protobuf.UnittestLite.packedSfixed64ExtensionLite;
    211 import static com.google.protobuf.UnittestLite.packedFloatExtensionLite;
    212 import static com.google.protobuf.UnittestLite.packedDoubleExtensionLite;
    213 import static com.google.protobuf.UnittestLite.packedBoolExtensionLite;
    214 import static com.google.protobuf.UnittestLite.packedEnumExtensionLite;
    215 
    216 import protobuf_unittest.UnittestProto.TestAllExtensions;
    217 import protobuf_unittest.UnittestProto.TestAllTypes;
    218 import protobuf_unittest.UnittestProto.TestPackedExtensions;
    219 import protobuf_unittest.UnittestProto.TestPackedTypes;
    220 import protobuf_unittest.UnittestProto.TestUnpackedTypes;
    221 import protobuf_unittest.UnittestProto.ForeignMessage;
    222 import protobuf_unittest.UnittestProto.ForeignEnum;
    223 import com.google.protobuf.test.UnittestImport.ImportMessage;
    224 import com.google.protobuf.test.UnittestImport.ImportEnum;
    225 
    226 import com.google.protobuf.UnittestLite.TestAllTypesLite;
    227 import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
    228 import com.google.protobuf.UnittestLite.TestPackedExtensionsLite;
    229 import com.google.protobuf.UnittestLite.ForeignMessageLite;
    230 import com.google.protobuf.UnittestLite.ForeignEnumLite;
    231 import com.google.protobuf.UnittestImportLite.ImportMessageLite;
    232 import com.google.protobuf.UnittestImportLite.ImportEnumLite;
    233 
    234 import junit.framework.Assert;
    235 
    236 import java.io.File;
    237 import java.io.IOException;
    238 import java.io.RandomAccessFile;
    239 
    240 /**
    241  * Contains methods for setting all fields of {@code TestAllTypes} to
    242  * some vaules as well as checking that all the fields are set to those values.
    243  * These are useful for testing various protocol message features, e.g.
    244  * set all fields of a message, serialize it, parse it, and check that all
    245  * fields are set.
    246  *
    247  * @author kenton (at) google.com Kenton Varda
    248  */
    249 class TestUtil {
    250   private TestUtil() {}
    251 
    252   /** Helper to convert a String to ByteString. */
    253   static ByteString toBytes(String str) {
    254     try {
    255       return ByteString.copyFrom(str.getBytes("UTF-8"));
    256     } catch(java.io.UnsupportedEncodingException e) {
    257       throw new RuntimeException("UTF-8 not supported.", e);
    258     }
    259   }
    260 
    261   /**
    262    * Get a {@code TestAllTypes} with all fields set as they would be by
    263    * {@link #setAllFields(TestAllTypes.Builder)}.
    264    */
    265   public static TestAllTypes getAllSet() {
    266     TestAllTypes.Builder builder = TestAllTypes.newBuilder();
    267     setAllFields(builder);
    268     return builder.build();
    269   }
    270 
    271   /**
    272    * Get a {@code TestAllExtensions} with all fields set as they would be by
    273    * {@link #setAllExtensions(TestAllExtensions.Builder)}.
    274    */
    275   public static TestAllExtensions getAllExtensionsSet() {
    276     TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
    277     setAllExtensions(builder);
    278     return builder.build();
    279   }
    280 
    281   public static TestAllExtensionsLite getAllLiteExtensionsSet() {
    282     TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder();
    283     setAllExtensions(builder);
    284     return builder.build();
    285   }
    286 
    287   public static TestPackedTypes getPackedSet() {
    288     TestPackedTypes.Builder builder = TestPackedTypes.newBuilder();
    289     setPackedFields(builder);
    290     return builder.build();
    291   }
    292 
    293   public static TestUnpackedTypes getUnpackedSet() {
    294     TestUnpackedTypes.Builder builder = TestUnpackedTypes.newBuilder();
    295     setUnpackedFields(builder);
    296     return builder.build();
    297   }
    298 
    299   public static TestPackedExtensions getPackedExtensionsSet() {
    300     TestPackedExtensions.Builder builder = TestPackedExtensions.newBuilder();
    301     setPackedExtensions(builder);
    302     return builder.build();
    303   }
    304 
    305   public static TestPackedExtensionsLite getLitePackedExtensionsSet() {
    306     TestPackedExtensionsLite.Builder builder =
    307         TestPackedExtensionsLite.newBuilder();
    308     setPackedExtensions(builder);
    309     return builder.build();
    310   }
    311 
    312   /**
    313    * Set every field of {@code message} to the values expected by
    314    * {@code assertAllFieldsSet()}.
    315    */
    316   public static void setAllFields(TestAllTypes.Builder message) {
    317     message.setOptionalInt32   (101);
    318     message.setOptionalInt64   (102);
    319     message.setOptionalUint32  (103);
    320     message.setOptionalUint64  (104);
    321     message.setOptionalSint32  (105);
    322     message.setOptionalSint64  (106);
    323     message.setOptionalFixed32 (107);
    324     message.setOptionalFixed64 (108);
    325     message.setOptionalSfixed32(109);
    326     message.setOptionalSfixed64(110);
    327     message.setOptionalFloat   (111);
    328     message.setOptionalDouble  (112);
    329     message.setOptionalBool    (true);
    330     message.setOptionalString  ("115");
    331     message.setOptionalBytes   (toBytes("116"));
    332 
    333     message.setOptionalGroup(
    334       TestAllTypes.OptionalGroup.newBuilder().setA(117).build());
    335     message.setOptionalNestedMessage(
    336       TestAllTypes.NestedMessage.newBuilder().setBb(118).build());
    337     message.setOptionalForeignMessage(
    338       ForeignMessage.newBuilder().setC(119).build());
    339     message.setOptionalImportMessage(
    340       ImportMessage.newBuilder().setD(120).build());
    341 
    342     message.setOptionalNestedEnum (TestAllTypes.NestedEnum.BAZ);
    343     message.setOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ);
    344     message.setOptionalImportEnum (ImportEnum.IMPORT_BAZ);
    345 
    346     message.setOptionalStringPiece("124");
    347     message.setOptionalCord("125");
    348 
    349     // -----------------------------------------------------------------
    350 
    351     message.addRepeatedInt32   (201);
    352     message.addRepeatedInt64   (202);
    353     message.addRepeatedUint32  (203);
    354     message.addRepeatedUint64  (204);
    355     message.addRepeatedSint32  (205);
    356     message.addRepeatedSint64  (206);
    357     message.addRepeatedFixed32 (207);
    358     message.addRepeatedFixed64 (208);
    359     message.addRepeatedSfixed32(209);
    360     message.addRepeatedSfixed64(210);
    361     message.addRepeatedFloat   (211);
    362     message.addRepeatedDouble  (212);
    363     message.addRepeatedBool    (true);
    364     message.addRepeatedString  ("215");
    365     message.addRepeatedBytes   (toBytes("216"));
    366 
    367     message.addRepeatedGroup(
    368       TestAllTypes.RepeatedGroup.newBuilder().setA(217).build());
    369     message.addRepeatedNestedMessage(
    370       TestAllTypes.NestedMessage.newBuilder().setBb(218).build());
    371     message.addRepeatedForeignMessage(
    372       ForeignMessage.newBuilder().setC(219).build());
    373     message.addRepeatedImportMessage(
    374       ImportMessage.newBuilder().setD(220).build());
    375 
    376     message.addRepeatedNestedEnum (TestAllTypes.NestedEnum.BAR);
    377     message.addRepeatedForeignEnum(ForeignEnum.FOREIGN_BAR);
    378     message.addRepeatedImportEnum (ImportEnum.IMPORT_BAR);
    379 
    380     message.addRepeatedStringPiece("224");
    381     message.addRepeatedCord("225");
    382 
    383     // Add a second one of each field.
    384     message.addRepeatedInt32   (301);
    385     message.addRepeatedInt64   (302);
    386     message.addRepeatedUint32  (303);
    387     message.addRepeatedUint64  (304);
    388     message.addRepeatedSint32  (305);
    389     message.addRepeatedSint64  (306);
    390     message.addRepeatedFixed32 (307);
    391     message.addRepeatedFixed64 (308);
    392     message.addRepeatedSfixed32(309);
    393     message.addRepeatedSfixed64(310);
    394     message.addRepeatedFloat   (311);
    395     message.addRepeatedDouble  (312);
    396     message.addRepeatedBool    (false);
    397     message.addRepeatedString  ("315");
    398     message.addRepeatedBytes   (toBytes("316"));
    399 
    400     message.addRepeatedGroup(
    401       TestAllTypes.RepeatedGroup.newBuilder().setA(317).build());
    402     message.addRepeatedNestedMessage(
    403       TestAllTypes.NestedMessage.newBuilder().setBb(318).build());
    404     message.addRepeatedForeignMessage(
    405       ForeignMessage.newBuilder().setC(319).build());
    406     message.addRepeatedImportMessage(
    407       ImportMessage.newBuilder().setD(320).build());
    408 
    409     message.addRepeatedNestedEnum (TestAllTypes.NestedEnum.BAZ);
    410     message.addRepeatedForeignEnum(ForeignEnum.FOREIGN_BAZ);
    411     message.addRepeatedImportEnum (ImportEnum.IMPORT_BAZ);
    412 
    413     message.addRepeatedStringPiece("324");
    414     message.addRepeatedCord("325");
    415 
    416     // -----------------------------------------------------------------
    417 
    418     message.setDefaultInt32   (401);
    419     message.setDefaultInt64   (402);
    420     message.setDefaultUint32  (403);
    421     message.setDefaultUint64  (404);
    422     message.setDefaultSint32  (405);
    423     message.setDefaultSint64  (406);
    424     message.setDefaultFixed32 (407);
    425     message.setDefaultFixed64 (408);
    426     message.setDefaultSfixed32(409);
    427     message.setDefaultSfixed64(410);
    428     message.setDefaultFloat   (411);
    429     message.setDefaultDouble  (412);
    430     message.setDefaultBool    (false);
    431     message.setDefaultString  ("415");
    432     message.setDefaultBytes   (toBytes("416"));
    433 
    434     message.setDefaultNestedEnum (TestAllTypes.NestedEnum.FOO);
    435     message.setDefaultForeignEnum(ForeignEnum.FOREIGN_FOO);
    436     message.setDefaultImportEnum (ImportEnum.IMPORT_FOO);
    437 
    438     message.setDefaultStringPiece("424");
    439     message.setDefaultCord("425");
    440   }
    441 
    442   // -------------------------------------------------------------------
    443 
    444   /**
    445    * Modify the repeated fields of {@code message} to contain the values
    446    * expected by {@code assertRepeatedFieldsModified()}.
    447    */
    448   public static void modifyRepeatedFields(TestAllTypes.Builder message) {
    449     message.setRepeatedInt32   (1, 501);
    450     message.setRepeatedInt64   (1, 502);
    451     message.setRepeatedUint32  (1, 503);
    452     message.setRepeatedUint64  (1, 504);
    453     message.setRepeatedSint32  (1, 505);
    454     message.setRepeatedSint64  (1, 506);
    455     message.setRepeatedFixed32 (1, 507);
    456     message.setRepeatedFixed64 (1, 508);
    457     message.setRepeatedSfixed32(1, 509);
    458     message.setRepeatedSfixed64(1, 510);
    459     message.setRepeatedFloat   (1, 511);
    460     message.setRepeatedDouble  (1, 512);
    461     message.setRepeatedBool    (1, true);
    462     message.setRepeatedString  (1, "515");
    463     message.setRepeatedBytes   (1, toBytes("516"));
    464 
    465     message.setRepeatedGroup(1,
    466       TestAllTypes.RepeatedGroup.newBuilder().setA(517).build());
    467     message.setRepeatedNestedMessage(1,
    468       TestAllTypes.NestedMessage.newBuilder().setBb(518).build());
    469     message.setRepeatedForeignMessage(1,
    470       ForeignMessage.newBuilder().setC(519).build());
    471     message.setRepeatedImportMessage(1,
    472       ImportMessage.newBuilder().setD(520).build());
    473 
    474     message.setRepeatedNestedEnum (1, TestAllTypes.NestedEnum.FOO);
    475     message.setRepeatedForeignEnum(1, ForeignEnum.FOREIGN_FOO);
    476     message.setRepeatedImportEnum (1, ImportEnum.IMPORT_FOO);
    477 
    478     message.setRepeatedStringPiece(1, "524");
    479     message.setRepeatedCord(1, "525");
    480   }
    481 
    482   // -------------------------------------------------------------------
    483 
    484   /**
    485    * Assert (using {@code junit.framework.Assert}} that all fields of
    486    * {@code message} are set to the values assigned by {@code setAllFields}.
    487    */
    488   public static void assertAllFieldsSet(TestAllTypes message) {
    489     Assert.assertTrue(message.hasOptionalInt32   ());
    490     Assert.assertTrue(message.hasOptionalInt64   ());
    491     Assert.assertTrue(message.hasOptionalUint32  ());
    492     Assert.assertTrue(message.hasOptionalUint64  ());
    493     Assert.assertTrue(message.hasOptionalSint32  ());
    494     Assert.assertTrue(message.hasOptionalSint64  ());
    495     Assert.assertTrue(message.hasOptionalFixed32 ());
    496     Assert.assertTrue(message.hasOptionalFixed64 ());
    497     Assert.assertTrue(message.hasOptionalSfixed32());
    498     Assert.assertTrue(message.hasOptionalSfixed64());
    499     Assert.assertTrue(message.hasOptionalFloat   ());
    500     Assert.assertTrue(message.hasOptionalDouble  ());
    501     Assert.assertTrue(message.hasOptionalBool    ());
    502     Assert.assertTrue(message.hasOptionalString  ());
    503     Assert.assertTrue(message.hasOptionalBytes   ());
    504 
    505     Assert.assertTrue(message.hasOptionalGroup         ());
    506     Assert.assertTrue(message.hasOptionalNestedMessage ());
    507     Assert.assertTrue(message.hasOptionalForeignMessage());
    508     Assert.assertTrue(message.hasOptionalImportMessage ());
    509 
    510     Assert.assertTrue(message.getOptionalGroup         ().hasA());
    511     Assert.assertTrue(message.getOptionalNestedMessage ().hasBb());
    512     Assert.assertTrue(message.getOptionalForeignMessage().hasC());
    513     Assert.assertTrue(message.getOptionalImportMessage ().hasD());
    514 
    515     Assert.assertTrue(message.hasOptionalNestedEnum ());
    516     Assert.assertTrue(message.hasOptionalForeignEnum());
    517     Assert.assertTrue(message.hasOptionalImportEnum ());
    518 
    519     Assert.assertTrue(message.hasOptionalStringPiece());
    520     Assert.assertTrue(message.hasOptionalCord());
    521 
    522     Assert.assertEquals(101  , message.getOptionalInt32   ());
    523     Assert.assertEquals(102  , message.getOptionalInt64   ());
    524     Assert.assertEquals(103  , message.getOptionalUint32  ());
    525     Assert.assertEquals(104  , message.getOptionalUint64  ());
    526     Assert.assertEquals(105  , message.getOptionalSint32  ());
    527     Assert.assertEquals(106  , message.getOptionalSint64  ());
    528     Assert.assertEquals(107  , message.getOptionalFixed32 ());
    529     Assert.assertEquals(108  , message.getOptionalFixed64 ());
    530     Assert.assertEquals(109  , message.getOptionalSfixed32());
    531     Assert.assertEquals(110  , message.getOptionalSfixed64());
    532     Assert.assertEquals(111  , message.getOptionalFloat   (), 0.0);
    533     Assert.assertEquals(112  , message.getOptionalDouble  (), 0.0);
    534     Assert.assertEquals(true , message.getOptionalBool    ());
    535     Assert.assertEquals("115", message.getOptionalString  ());
    536     Assert.assertEquals(toBytes("116"), message.getOptionalBytes());
    537 
    538     Assert.assertEquals(117, message.getOptionalGroup         ().getA());
    539     Assert.assertEquals(118, message.getOptionalNestedMessage ().getBb());
    540     Assert.assertEquals(119, message.getOptionalForeignMessage().getC());
    541     Assert.assertEquals(120, message.getOptionalImportMessage ().getD());
    542 
    543     Assert.assertEquals(TestAllTypes.NestedEnum.BAZ, message.getOptionalNestedEnum());
    544     Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getOptionalForeignEnum());
    545     Assert.assertEquals(ImportEnum.IMPORT_BAZ, message.getOptionalImportEnum());
    546 
    547     Assert.assertEquals("124", message.getOptionalStringPiece());
    548     Assert.assertEquals("125", message.getOptionalCord());
    549 
    550     // -----------------------------------------------------------------
    551 
    552     Assert.assertEquals(2, message.getRepeatedInt32Count   ());
    553     Assert.assertEquals(2, message.getRepeatedInt64Count   ());
    554     Assert.assertEquals(2, message.getRepeatedUint32Count  ());
    555     Assert.assertEquals(2, message.getRepeatedUint64Count  ());
    556     Assert.assertEquals(2, message.getRepeatedSint32Count  ());
    557     Assert.assertEquals(2, message.getRepeatedSint64Count  ());
    558     Assert.assertEquals(2, message.getRepeatedFixed32Count ());
    559     Assert.assertEquals(2, message.getRepeatedFixed64Count ());
    560     Assert.assertEquals(2, message.getRepeatedSfixed32Count());
    561     Assert.assertEquals(2, message.getRepeatedSfixed64Count());
    562     Assert.assertEquals(2, message.getRepeatedFloatCount   ());
    563     Assert.assertEquals(2, message.getRepeatedDoubleCount  ());
    564     Assert.assertEquals(2, message.getRepeatedBoolCount    ());
    565     Assert.assertEquals(2, message.getRepeatedStringCount  ());
    566     Assert.assertEquals(2, message.getRepeatedBytesCount   ());
    567 
    568     Assert.assertEquals(2, message.getRepeatedGroupCount         ());
    569     Assert.assertEquals(2, message.getRepeatedNestedMessageCount ());
    570     Assert.assertEquals(2, message.getRepeatedForeignMessageCount());
    571     Assert.assertEquals(2, message.getRepeatedImportMessageCount ());
    572     Assert.assertEquals(2, message.getRepeatedNestedEnumCount    ());
    573     Assert.assertEquals(2, message.getRepeatedForeignEnumCount   ());
    574     Assert.assertEquals(2, message.getRepeatedImportEnumCount    ());
    575 
    576     Assert.assertEquals(2, message.getRepeatedStringPieceCount());
    577     Assert.assertEquals(2, message.getRepeatedCordCount());
    578 
    579     Assert.assertEquals(201  , message.getRepeatedInt32   (0));
    580     Assert.assertEquals(202  , message.getRepeatedInt64   (0));
    581     Assert.assertEquals(203  , message.getRepeatedUint32  (0));
    582     Assert.assertEquals(204  , message.getRepeatedUint64  (0));
    583     Assert.assertEquals(205  , message.getRepeatedSint32  (0));
    584     Assert.assertEquals(206  , message.getRepeatedSint64  (0));
    585     Assert.assertEquals(207  , message.getRepeatedFixed32 (0));
    586     Assert.assertEquals(208  , message.getRepeatedFixed64 (0));
    587     Assert.assertEquals(209  , message.getRepeatedSfixed32(0));
    588     Assert.assertEquals(210  , message.getRepeatedSfixed64(0));
    589     Assert.assertEquals(211  , message.getRepeatedFloat   (0), 0.0);
    590     Assert.assertEquals(212  , message.getRepeatedDouble  (0), 0.0);
    591     Assert.assertEquals(true , message.getRepeatedBool    (0));
    592     Assert.assertEquals("215", message.getRepeatedString  (0));
    593     Assert.assertEquals(toBytes("216"), message.getRepeatedBytes(0));
    594 
    595     Assert.assertEquals(217, message.getRepeatedGroup         (0).getA());
    596     Assert.assertEquals(218, message.getRepeatedNestedMessage (0).getBb());
    597     Assert.assertEquals(219, message.getRepeatedForeignMessage(0).getC());
    598     Assert.assertEquals(220, message.getRepeatedImportMessage (0).getD());
    599 
    600     Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum (0));
    601     Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getRepeatedForeignEnum(0));
    602     Assert.assertEquals(ImportEnum.IMPORT_BAR, message.getRepeatedImportEnum(0));
    603 
    604     Assert.assertEquals("224", message.getRepeatedStringPiece(0));
    605     Assert.assertEquals("225", message.getRepeatedCord(0));
    606 
    607     Assert.assertEquals(301  , message.getRepeatedInt32   (1));
    608     Assert.assertEquals(302  , message.getRepeatedInt64   (1));
    609     Assert.assertEquals(303  , message.getRepeatedUint32  (1));
    610     Assert.assertEquals(304  , message.getRepeatedUint64  (1));
    611     Assert.assertEquals(305  , message.getRepeatedSint32  (1));
    612     Assert.assertEquals(306  , message.getRepeatedSint64  (1));
    613     Assert.assertEquals(307  , message.getRepeatedFixed32 (1));
    614     Assert.assertEquals(308  , message.getRepeatedFixed64 (1));
    615     Assert.assertEquals(309  , message.getRepeatedSfixed32(1));
    616     Assert.assertEquals(310  , message.getRepeatedSfixed64(1));
    617     Assert.assertEquals(311  , message.getRepeatedFloat   (1), 0.0);
    618     Assert.assertEquals(312  , message.getRepeatedDouble  (1), 0.0);
    619     Assert.assertEquals(false, message.getRepeatedBool    (1));
    620     Assert.assertEquals("315", message.getRepeatedString  (1));
    621     Assert.assertEquals(toBytes("316"), message.getRepeatedBytes(1));
    622 
    623     Assert.assertEquals(317, message.getRepeatedGroup         (1).getA());
    624     Assert.assertEquals(318, message.getRepeatedNestedMessage (1).getBb());
    625     Assert.assertEquals(319, message.getRepeatedForeignMessage(1).getC());
    626     Assert.assertEquals(320, message.getRepeatedImportMessage (1).getD());
    627 
    628     Assert.assertEquals(TestAllTypes.NestedEnum.BAZ, message.getRepeatedNestedEnum (1));
    629     Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getRepeatedForeignEnum(1));
    630     Assert.assertEquals(ImportEnum.IMPORT_BAZ, message.getRepeatedImportEnum(1));
    631 
    632     Assert.assertEquals("324", message.getRepeatedStringPiece(1));
    633     Assert.assertEquals("325", message.getRepeatedCord(1));
    634 
    635     // -----------------------------------------------------------------
    636 
    637     Assert.assertTrue(message.hasDefaultInt32   ());
    638     Assert.assertTrue(message.hasDefaultInt64   ());
    639     Assert.assertTrue(message.hasDefaultUint32  ());
    640     Assert.assertTrue(message.hasDefaultUint64  ());
    641     Assert.assertTrue(message.hasDefaultSint32  ());
    642     Assert.assertTrue(message.hasDefaultSint64  ());
    643     Assert.assertTrue(message.hasDefaultFixed32 ());
    644     Assert.assertTrue(message.hasDefaultFixed64 ());
    645     Assert.assertTrue(message.hasDefaultSfixed32());
    646     Assert.assertTrue(message.hasDefaultSfixed64());
    647     Assert.assertTrue(message.hasDefaultFloat   ());
    648     Assert.assertTrue(message.hasDefaultDouble  ());
    649     Assert.assertTrue(message.hasDefaultBool    ());
    650     Assert.assertTrue(message.hasDefaultString  ());
    651     Assert.assertTrue(message.hasDefaultBytes   ());
    652 
    653     Assert.assertTrue(message.hasDefaultNestedEnum ());
    654     Assert.assertTrue(message.hasDefaultForeignEnum());
    655     Assert.assertTrue(message.hasDefaultImportEnum ());
    656 
    657     Assert.assertTrue(message.hasDefaultStringPiece());
    658     Assert.assertTrue(message.hasDefaultCord());
    659 
    660     Assert.assertEquals(401  , message.getDefaultInt32   ());
    661     Assert.assertEquals(402  , message.getDefaultInt64   ());
    662     Assert.assertEquals(403  , message.getDefaultUint32  ());
    663     Assert.assertEquals(404  , message.getDefaultUint64  ());
    664     Assert.assertEquals(405  , message.getDefaultSint32  ());
    665     Assert.assertEquals(406  , message.getDefaultSint64  ());
    666     Assert.assertEquals(407  , message.getDefaultFixed32 ());
    667     Assert.assertEquals(408  , message.getDefaultFixed64 ());
    668     Assert.assertEquals(409  , message.getDefaultSfixed32());
    669     Assert.assertEquals(410  , message.getDefaultSfixed64());
    670     Assert.assertEquals(411  , message.getDefaultFloat   (), 0.0);
    671     Assert.assertEquals(412  , message.getDefaultDouble  (), 0.0);
    672     Assert.assertEquals(false, message.getDefaultBool    ());
    673     Assert.assertEquals("415", message.getDefaultString  ());
    674     Assert.assertEquals(toBytes("416"), message.getDefaultBytes());
    675 
    676     Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getDefaultNestedEnum ());
    677     Assert.assertEquals(ForeignEnum.FOREIGN_FOO, message.getDefaultForeignEnum());
    678     Assert.assertEquals(ImportEnum.IMPORT_FOO, message.getDefaultImportEnum());
    679 
    680     Assert.assertEquals("424", message.getDefaultStringPiece());
    681     Assert.assertEquals("425", message.getDefaultCord());
    682   }
    683 
    684   // -------------------------------------------------------------------
    685 
    686   /**
    687    * Assert (using {@code junit.framework.Assert}} that all fields of
    688    * {@code message} are cleared, and that getting the fields returns their
    689    * default values.
    690    */
    691   public static void assertClear(TestAllTypes message) {
    692     // hasBlah() should initially be false for all optional fields.
    693     Assert.assertFalse(message.hasOptionalInt32   ());
    694     Assert.assertFalse(message.hasOptionalInt64   ());
    695     Assert.assertFalse(message.hasOptionalUint32  ());
    696     Assert.assertFalse(message.hasOptionalUint64  ());
    697     Assert.assertFalse(message.hasOptionalSint32  ());
    698     Assert.assertFalse(message.hasOptionalSint64  ());
    699     Assert.assertFalse(message.hasOptionalFixed32 ());
    700     Assert.assertFalse(message.hasOptionalFixed64 ());
    701     Assert.assertFalse(message.hasOptionalSfixed32());
    702     Assert.assertFalse(message.hasOptionalSfixed64());
    703     Assert.assertFalse(message.hasOptionalFloat   ());
    704     Assert.assertFalse(message.hasOptionalDouble  ());
    705     Assert.assertFalse(message.hasOptionalBool    ());
    706     Assert.assertFalse(message.hasOptionalString  ());
    707     Assert.assertFalse(message.hasOptionalBytes   ());
    708 
    709     Assert.assertFalse(message.hasOptionalGroup         ());
    710     Assert.assertFalse(message.hasOptionalNestedMessage ());
    711     Assert.assertFalse(message.hasOptionalForeignMessage());
    712     Assert.assertFalse(message.hasOptionalImportMessage ());
    713 
    714     Assert.assertFalse(message.hasOptionalNestedEnum ());
    715     Assert.assertFalse(message.hasOptionalForeignEnum());
    716     Assert.assertFalse(message.hasOptionalImportEnum ());
    717 
    718     Assert.assertFalse(message.hasOptionalStringPiece());
    719     Assert.assertFalse(message.hasOptionalCord());
    720 
    721     // Optional fields without defaults are set to zero or something like it.
    722     Assert.assertEquals(0    , message.getOptionalInt32   ());
    723     Assert.assertEquals(0    , message.getOptionalInt64   ());
    724     Assert.assertEquals(0    , message.getOptionalUint32  ());
    725     Assert.assertEquals(0    , message.getOptionalUint64  ());
    726     Assert.assertEquals(0    , message.getOptionalSint32  ());
    727     Assert.assertEquals(0    , message.getOptionalSint64  ());
    728     Assert.assertEquals(0    , message.getOptionalFixed32 ());
    729     Assert.assertEquals(0    , message.getOptionalFixed64 ());
    730     Assert.assertEquals(0    , message.getOptionalSfixed32());
    731     Assert.assertEquals(0    , message.getOptionalSfixed64());
    732     Assert.assertEquals(0    , message.getOptionalFloat   (), 0.0);
    733     Assert.assertEquals(0    , message.getOptionalDouble  (), 0.0);
    734     Assert.assertEquals(false, message.getOptionalBool    ());
    735     Assert.assertEquals(""   , message.getOptionalString  ());
    736     Assert.assertEquals(ByteString.EMPTY, message.getOptionalBytes());
    737 
    738     // Embedded messages should also be clear.
    739     Assert.assertFalse(message.getOptionalGroup         ().hasA());
    740     Assert.assertFalse(message.getOptionalNestedMessage ().hasBb());
    741     Assert.assertFalse(message.getOptionalForeignMessage().hasC());
    742     Assert.assertFalse(message.getOptionalImportMessage ().hasD());
    743 
    744     Assert.assertEquals(0, message.getOptionalGroup         ().getA());
    745     Assert.assertEquals(0, message.getOptionalNestedMessage ().getBb());
    746     Assert.assertEquals(0, message.getOptionalForeignMessage().getC());
    747     Assert.assertEquals(0, message.getOptionalImportMessage ().getD());
    748 
    749     // Enums without defaults are set to the first value in the enum.
    750     Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getOptionalNestedEnum ());
    751     Assert.assertEquals(ForeignEnum.FOREIGN_FOO, message.getOptionalForeignEnum());
    752     Assert.assertEquals(ImportEnum.IMPORT_FOO, message.getOptionalImportEnum());
    753 
    754     Assert.assertEquals("", message.getOptionalStringPiece());
    755     Assert.assertEquals("", message.getOptionalCord());
    756 
    757     // Repeated fields are empty.
    758     Assert.assertEquals(0, message.getRepeatedInt32Count   ());
    759     Assert.assertEquals(0, message.getRepeatedInt64Count   ());
    760     Assert.assertEquals(0, message.getRepeatedUint32Count  ());
    761     Assert.assertEquals(0, message.getRepeatedUint64Count  ());
    762     Assert.assertEquals(0, message.getRepeatedSint32Count  ());
    763     Assert.assertEquals(0, message.getRepeatedSint64Count  ());
    764     Assert.assertEquals(0, message.getRepeatedFixed32Count ());
    765     Assert.assertEquals(0, message.getRepeatedFixed64Count ());
    766     Assert.assertEquals(0, message.getRepeatedSfixed32Count());
    767     Assert.assertEquals(0, message.getRepeatedSfixed64Count());
    768     Assert.assertEquals(0, message.getRepeatedFloatCount   ());
    769     Assert.assertEquals(0, message.getRepeatedDoubleCount  ());
    770     Assert.assertEquals(0, message.getRepeatedBoolCount    ());
    771     Assert.assertEquals(0, message.getRepeatedStringCount  ());
    772     Assert.assertEquals(0, message.getRepeatedBytesCount   ());
    773 
    774     Assert.assertEquals(0, message.getRepeatedGroupCount         ());
    775     Assert.assertEquals(0, message.getRepeatedNestedMessageCount ());
    776     Assert.assertEquals(0, message.getRepeatedForeignMessageCount());
    777     Assert.assertEquals(0, message.getRepeatedImportMessageCount ());
    778     Assert.assertEquals(0, message.getRepeatedNestedEnumCount    ());
    779     Assert.assertEquals(0, message.getRepeatedForeignEnumCount   ());
    780     Assert.assertEquals(0, message.getRepeatedImportEnumCount    ());
    781 
    782     Assert.assertEquals(0, message.getRepeatedStringPieceCount());
    783     Assert.assertEquals(0, message.getRepeatedCordCount());
    784 
    785     // hasBlah() should also be false for all default fields.
    786     Assert.assertFalse(message.hasDefaultInt32   ());
    787     Assert.assertFalse(message.hasDefaultInt64   ());
    788     Assert.assertFalse(message.hasDefaultUint32  ());
    789     Assert.assertFalse(message.hasDefaultUint64  ());
    790     Assert.assertFalse(message.hasDefaultSint32  ());
    791     Assert.assertFalse(message.hasDefaultSint64  ());
    792     Assert.assertFalse(message.hasDefaultFixed32 ());
    793     Assert.assertFalse(message.hasDefaultFixed64 ());
    794     Assert.assertFalse(message.hasDefaultSfixed32());
    795     Assert.assertFalse(message.hasDefaultSfixed64());
    796     Assert.assertFalse(message.hasDefaultFloat   ());
    797     Assert.assertFalse(message.hasDefaultDouble  ());
    798     Assert.assertFalse(message.hasDefaultBool    ());
    799     Assert.assertFalse(message.hasDefaultString  ());
    800     Assert.assertFalse(message.hasDefaultBytes   ());
    801 
    802     Assert.assertFalse(message.hasDefaultNestedEnum ());
    803     Assert.assertFalse(message.hasDefaultForeignEnum());
    804     Assert.assertFalse(message.hasDefaultImportEnum ());
    805 
    806     Assert.assertFalse(message.hasDefaultStringPiece());
    807     Assert.assertFalse(message.hasDefaultCord());
    808 
    809     // Fields with defaults have their default values (duh).
    810     Assert.assertEquals( 41    , message.getDefaultInt32   ());
    811     Assert.assertEquals( 42    , message.getDefaultInt64   ());
    812     Assert.assertEquals( 43    , message.getDefaultUint32  ());
    813     Assert.assertEquals( 44    , message.getDefaultUint64  ());
    814     Assert.assertEquals(-45    , message.getDefaultSint32  ());
    815     Assert.assertEquals( 46    , message.getDefaultSint64  ());
    816     Assert.assertEquals( 47    , message.getDefaultFixed32 ());
    817     Assert.assertEquals( 48    , message.getDefaultFixed64 ());
    818     Assert.assertEquals( 49    , message.getDefaultSfixed32());
    819     Assert.assertEquals(-50    , message.getDefaultSfixed64());
    820     Assert.assertEquals( 51.5  , message.getDefaultFloat   (), 0.0);
    821     Assert.assertEquals( 52e3  , message.getDefaultDouble  (), 0.0);
    822     Assert.assertEquals(true   , message.getDefaultBool    ());
    823     Assert.assertEquals("hello", message.getDefaultString  ());
    824     Assert.assertEquals(toBytes("world"), message.getDefaultBytes());
    825 
    826     Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getDefaultNestedEnum ());
    827     Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getDefaultForeignEnum());
    828     Assert.assertEquals(ImportEnum.IMPORT_BAR, message.getDefaultImportEnum());
    829 
    830     Assert.assertEquals("abc", message.getDefaultStringPiece());
    831     Assert.assertEquals("123", message.getDefaultCord());
    832   }
    833 
    834   // -------------------------------------------------------------------
    835 
    836   /**
    837    * Assert (using {@code junit.framework.Assert}} that all fields of
    838    * {@code message} are set to the values assigned by {@code setAllFields}
    839    * followed by {@code modifyRepeatedFields}.
    840    */
    841   public static void assertRepeatedFieldsModified(TestAllTypes message) {
    842     // ModifyRepeatedFields only sets the second repeated element of each
    843     // field.  In addition to verifying this, we also verify that the first
    844     // element and size were *not* modified.
    845     Assert.assertEquals(2, message.getRepeatedInt32Count   ());
    846     Assert.assertEquals(2, message.getRepeatedInt64Count   ());
    847     Assert.assertEquals(2, message.getRepeatedUint32Count  ());
    848     Assert.assertEquals(2, message.getRepeatedUint64Count  ());
    849     Assert.assertEquals(2, message.getRepeatedSint32Count  ());
    850     Assert.assertEquals(2, message.getRepeatedSint64Count  ());
    851     Assert.assertEquals(2, message.getRepeatedFixed32Count ());
    852     Assert.assertEquals(2, message.getRepeatedFixed64Count ());
    853     Assert.assertEquals(2, message.getRepeatedSfixed32Count());
    854     Assert.assertEquals(2, message.getRepeatedSfixed64Count());
    855     Assert.assertEquals(2, message.getRepeatedFloatCount   ());
    856     Assert.assertEquals(2, message.getRepeatedDoubleCount  ());
    857     Assert.assertEquals(2, message.getRepeatedBoolCount    ());
    858     Assert.assertEquals(2, message.getRepeatedStringCount  ());
    859     Assert.assertEquals(2, message.getRepeatedBytesCount   ());
    860 
    861     Assert.assertEquals(2, message.getRepeatedGroupCount         ());
    862     Assert.assertEquals(2, message.getRepeatedNestedMessageCount ());
    863     Assert.assertEquals(2, message.getRepeatedForeignMessageCount());
    864     Assert.assertEquals(2, message.getRepeatedImportMessageCount ());
    865     Assert.assertEquals(2, message.getRepeatedNestedEnumCount    ());
    866     Assert.assertEquals(2, message.getRepeatedForeignEnumCount   ());
    867     Assert.assertEquals(2, message.getRepeatedImportEnumCount    ());
    868 
    869     Assert.assertEquals(2, message.getRepeatedStringPieceCount());
    870     Assert.assertEquals(2, message.getRepeatedCordCount());
    871 
    872     Assert.assertEquals(201  , message.getRepeatedInt32   (0));
    873     Assert.assertEquals(202L , message.getRepeatedInt64   (0));
    874     Assert.assertEquals(203  , message.getRepeatedUint32  (0));
    875     Assert.assertEquals(204L , message.getRepeatedUint64  (0));
    876     Assert.assertEquals(205  , message.getRepeatedSint32  (0));
    877     Assert.assertEquals(206L , message.getRepeatedSint64  (0));
    878     Assert.assertEquals(207  , message.getRepeatedFixed32 (0));
    879     Assert.assertEquals(208L , message.getRepeatedFixed64 (0));
    880     Assert.assertEquals(209  , message.getRepeatedSfixed32(0));
    881     Assert.assertEquals(210L , message.getRepeatedSfixed64(0));
    882     Assert.assertEquals(211F , message.getRepeatedFloat   (0));
    883     Assert.assertEquals(212D , message.getRepeatedDouble  (0));
    884     Assert.assertEquals(true , message.getRepeatedBool    (0));
    885     Assert.assertEquals("215", message.getRepeatedString  (0));
    886     Assert.assertEquals(toBytes("216"), message.getRepeatedBytes(0));
    887 
    888     Assert.assertEquals(217, message.getRepeatedGroup         (0).getA());
    889     Assert.assertEquals(218, message.getRepeatedNestedMessage (0).getBb());
    890     Assert.assertEquals(219, message.getRepeatedForeignMessage(0).getC());
    891     Assert.assertEquals(220, message.getRepeatedImportMessage (0).getD());
    892 
    893     Assert.assertEquals(TestAllTypes.NestedEnum.BAR, message.getRepeatedNestedEnum (0));
    894     Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getRepeatedForeignEnum(0));
    895     Assert.assertEquals(ImportEnum.IMPORT_BAR, message.getRepeatedImportEnum(0));
    896 
    897     Assert.assertEquals("224", message.getRepeatedStringPiece(0));
    898     Assert.assertEquals("225", message.getRepeatedCord(0));
    899 
    900     // Actually verify the second (modified) elements now.
    901     Assert.assertEquals(501  , message.getRepeatedInt32   (1));
    902     Assert.assertEquals(502L , message.getRepeatedInt64   (1));
    903     Assert.assertEquals(503  , message.getRepeatedUint32  (1));
    904     Assert.assertEquals(504L , message.getRepeatedUint64  (1));
    905     Assert.assertEquals(505  , message.getRepeatedSint32  (1));
    906     Assert.assertEquals(506L , message.getRepeatedSint64  (1));
    907     Assert.assertEquals(507  , message.getRepeatedFixed32 (1));
    908     Assert.assertEquals(508L , message.getRepeatedFixed64 (1));
    909     Assert.assertEquals(509  , message.getRepeatedSfixed32(1));
    910     Assert.assertEquals(510L , message.getRepeatedSfixed64(1));
    911     Assert.assertEquals(511F , message.getRepeatedFloat   (1));
    912     Assert.assertEquals(512D , message.getRepeatedDouble  (1));
    913     Assert.assertEquals(true , message.getRepeatedBool    (1));
    914     Assert.assertEquals("515", message.getRepeatedString  (1));
    915     Assert.assertEquals(toBytes("516"), message.getRepeatedBytes(1));
    916 
    917     Assert.assertEquals(517, message.getRepeatedGroup         (1).getA());
    918     Assert.assertEquals(518, message.getRepeatedNestedMessage (1).getBb());
    919     Assert.assertEquals(519, message.getRepeatedForeignMessage(1).getC());
    920     Assert.assertEquals(520, message.getRepeatedImportMessage (1).getD());
    921 
    922     Assert.assertEquals(TestAllTypes.NestedEnum.FOO, message.getRepeatedNestedEnum (1));
    923     Assert.assertEquals(ForeignEnum.FOREIGN_FOO, message.getRepeatedForeignEnum(1));
    924     Assert.assertEquals(ImportEnum.IMPORT_FOO, message.getRepeatedImportEnum(1));
    925 
    926     Assert.assertEquals("524", message.getRepeatedStringPiece(1));
    927     Assert.assertEquals("525", message.getRepeatedCord(1));
    928   }
    929 
    930   /**
    931    * Set every field of {@code message} to a unique value.
    932    */
    933   public static void setPackedFields(TestPackedTypes.Builder message) {
    934     message.addPackedInt32   (601);
    935     message.addPackedInt64   (602);
    936     message.addPackedUint32  (603);
    937     message.addPackedUint64  (604);
    938     message.addPackedSint32  (605);
    939     message.addPackedSint64  (606);
    940     message.addPackedFixed32 (607);
    941     message.addPackedFixed64 (608);
    942     message.addPackedSfixed32(609);
    943     message.addPackedSfixed64(610);
    944     message.addPackedFloat   (611);
    945     message.addPackedDouble  (612);
    946     message.addPackedBool    (true);
    947     message.addPackedEnum    (ForeignEnum.FOREIGN_BAR);
    948     // Add a second one of each field.
    949     message.addPackedInt32   (701);
    950     message.addPackedInt64   (702);
    951     message.addPackedUint32  (703);
    952     message.addPackedUint64  (704);
    953     message.addPackedSint32  (705);
    954     message.addPackedSint64  (706);
    955     message.addPackedFixed32 (707);
    956     message.addPackedFixed64 (708);
    957     message.addPackedSfixed32(709);
    958     message.addPackedSfixed64(710);
    959     message.addPackedFloat   (711);
    960     message.addPackedDouble  (712);
    961     message.addPackedBool    (false);
    962     message.addPackedEnum    (ForeignEnum.FOREIGN_BAZ);
    963   }
    964 
    965   /**
    966    * Set every field of {@code message} to a unique value. Must correspond with
    967    * the values applied by {@code setPackedFields}.
    968    */
    969   public static void setUnpackedFields(TestUnpackedTypes.Builder message) {
    970     message.addUnpackedInt32   (601);
    971     message.addUnpackedInt64   (602);
    972     message.addUnpackedUint32  (603);
    973     message.addUnpackedUint64  (604);
    974     message.addUnpackedSint32  (605);
    975     message.addUnpackedSint64  (606);
    976     message.addUnpackedFixed32 (607);
    977     message.addUnpackedFixed64 (608);
    978     message.addUnpackedSfixed32(609);
    979     message.addUnpackedSfixed64(610);
    980     message.addUnpackedFloat   (611);
    981     message.addUnpackedDouble  (612);
    982     message.addUnpackedBool    (true);
    983     message.addUnpackedEnum    (ForeignEnum.FOREIGN_BAR);
    984     // Add a second one of each field.
    985     message.addUnpackedInt32   (701);
    986     message.addUnpackedInt64   (702);
    987     message.addUnpackedUint32  (703);
    988     message.addUnpackedUint64  (704);
    989     message.addUnpackedSint32  (705);
    990     message.addUnpackedSint64  (706);
    991     message.addUnpackedFixed32 (707);
    992     message.addUnpackedFixed64 (708);
    993     message.addUnpackedSfixed32(709);
    994     message.addUnpackedSfixed64(710);
    995     message.addUnpackedFloat   (711);
    996     message.addUnpackedDouble  (712);
    997     message.addUnpackedBool    (false);
    998     message.addUnpackedEnum    (ForeignEnum.FOREIGN_BAZ);
    999   }
   1000 
   1001   /**
   1002    * Assert (using {@code junit.framework.Assert}} that all fields of
   1003    * {@code message} are set to the values assigned by {@code setPackedFields}.
   1004    */
   1005   public static void assertPackedFieldsSet(TestPackedTypes message) {
   1006     Assert.assertEquals(2, message.getPackedInt32Count   ());
   1007     Assert.assertEquals(2, message.getPackedInt64Count   ());
   1008     Assert.assertEquals(2, message.getPackedUint32Count  ());
   1009     Assert.assertEquals(2, message.getPackedUint64Count  ());
   1010     Assert.assertEquals(2, message.getPackedSint32Count  ());
   1011     Assert.assertEquals(2, message.getPackedSint64Count  ());
   1012     Assert.assertEquals(2, message.getPackedFixed32Count ());
   1013     Assert.assertEquals(2, message.getPackedFixed64Count ());
   1014     Assert.assertEquals(2, message.getPackedSfixed32Count());
   1015     Assert.assertEquals(2, message.getPackedSfixed64Count());
   1016     Assert.assertEquals(2, message.getPackedFloatCount   ());
   1017     Assert.assertEquals(2, message.getPackedDoubleCount  ());
   1018     Assert.assertEquals(2, message.getPackedBoolCount    ());
   1019     Assert.assertEquals(2, message.getPackedEnumCount   ());
   1020     Assert.assertEquals(601  , message.getPackedInt32   (0));
   1021     Assert.assertEquals(602  , message.getPackedInt64   (0));
   1022     Assert.assertEquals(603  , message.getPackedUint32  (0));
   1023     Assert.assertEquals(604  , message.getPackedUint64  (0));
   1024     Assert.assertEquals(605  , message.getPackedSint32  (0));
   1025     Assert.assertEquals(606  , message.getPackedSint64  (0));
   1026     Assert.assertEquals(607  , message.getPackedFixed32 (0));
   1027     Assert.assertEquals(608  , message.getPackedFixed64 (0));
   1028     Assert.assertEquals(609  , message.getPackedSfixed32(0));
   1029     Assert.assertEquals(610  , message.getPackedSfixed64(0));
   1030     Assert.assertEquals(611  , message.getPackedFloat   (0), 0.0);
   1031     Assert.assertEquals(612  , message.getPackedDouble  (0), 0.0);
   1032     Assert.assertEquals(true , message.getPackedBool    (0));
   1033     Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getPackedEnum(0));
   1034     Assert.assertEquals(701  , message.getPackedInt32   (1));
   1035     Assert.assertEquals(702  , message.getPackedInt64   (1));
   1036     Assert.assertEquals(703  , message.getPackedUint32  (1));
   1037     Assert.assertEquals(704  , message.getPackedUint64  (1));
   1038     Assert.assertEquals(705  , message.getPackedSint32  (1));
   1039     Assert.assertEquals(706  , message.getPackedSint64  (1));
   1040     Assert.assertEquals(707  , message.getPackedFixed32 (1));
   1041     Assert.assertEquals(708  , message.getPackedFixed64 (1));
   1042     Assert.assertEquals(709  , message.getPackedSfixed32(1));
   1043     Assert.assertEquals(710  , message.getPackedSfixed64(1));
   1044     Assert.assertEquals(711  , message.getPackedFloat   (1), 0.0);
   1045     Assert.assertEquals(712  , message.getPackedDouble  (1), 0.0);
   1046     Assert.assertEquals(false, message.getPackedBool    (1));
   1047     Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getPackedEnum(1));
   1048   }
   1049 
   1050   /**
   1051    * Assert (using {@code junit.framework.Assert}} that all fields of
   1052    * {@code message} are set to the values assigned by {@code setUnpackedFields}.
   1053    */
   1054   public static void assertUnpackedFieldsSet(TestUnpackedTypes message) {
   1055     Assert.assertEquals(2, message.getUnpackedInt32Count   ());
   1056     Assert.assertEquals(2, message.getUnpackedInt64Count   ());
   1057     Assert.assertEquals(2, message.getUnpackedUint32Count  ());
   1058     Assert.assertEquals(2, message.getUnpackedUint64Count  ());
   1059     Assert.assertEquals(2, message.getUnpackedSint32Count  ());
   1060     Assert.assertEquals(2, message.getUnpackedSint64Count  ());
   1061     Assert.assertEquals(2, message.getUnpackedFixed32Count ());
   1062     Assert.assertEquals(2, message.getUnpackedFixed64Count ());
   1063     Assert.assertEquals(2, message.getUnpackedSfixed32Count());
   1064     Assert.assertEquals(2, message.getUnpackedSfixed64Count());
   1065     Assert.assertEquals(2, message.getUnpackedFloatCount   ());
   1066     Assert.assertEquals(2, message.getUnpackedDoubleCount  ());
   1067     Assert.assertEquals(2, message.getUnpackedBoolCount    ());
   1068     Assert.assertEquals(2, message.getUnpackedEnumCount   ());
   1069     Assert.assertEquals(601  , message.getUnpackedInt32   (0));
   1070     Assert.assertEquals(602  , message.getUnpackedInt64   (0));
   1071     Assert.assertEquals(603  , message.getUnpackedUint32  (0));
   1072     Assert.assertEquals(604  , message.getUnpackedUint64  (0));
   1073     Assert.assertEquals(605  , message.getUnpackedSint32  (0));
   1074     Assert.assertEquals(606  , message.getUnpackedSint64  (0));
   1075     Assert.assertEquals(607  , message.getUnpackedFixed32 (0));
   1076     Assert.assertEquals(608  , message.getUnpackedFixed64 (0));
   1077     Assert.assertEquals(609  , message.getUnpackedSfixed32(0));
   1078     Assert.assertEquals(610  , message.getUnpackedSfixed64(0));
   1079     Assert.assertEquals(611  , message.getUnpackedFloat   (0), 0.0);
   1080     Assert.assertEquals(612  , message.getUnpackedDouble  (0), 0.0);
   1081     Assert.assertEquals(true , message.getUnpackedBool    (0));
   1082     Assert.assertEquals(ForeignEnum.FOREIGN_BAR, message.getUnpackedEnum(0));
   1083     Assert.assertEquals(701  , message.getUnpackedInt32   (1));
   1084     Assert.assertEquals(702  , message.getUnpackedInt64   (1));
   1085     Assert.assertEquals(703  , message.getUnpackedUint32  (1));
   1086     Assert.assertEquals(704  , message.getUnpackedUint64  (1));
   1087     Assert.assertEquals(705  , message.getUnpackedSint32  (1));
   1088     Assert.assertEquals(706  , message.getUnpackedSint64  (1));
   1089     Assert.assertEquals(707  , message.getUnpackedFixed32 (1));
   1090     Assert.assertEquals(708  , message.getUnpackedFixed64 (1));
   1091     Assert.assertEquals(709  , message.getUnpackedSfixed32(1));
   1092     Assert.assertEquals(710  , message.getUnpackedSfixed64(1));
   1093     Assert.assertEquals(711  , message.getUnpackedFloat   (1), 0.0);
   1094     Assert.assertEquals(712  , message.getUnpackedDouble  (1), 0.0);
   1095     Assert.assertEquals(false, message.getUnpackedBool    (1));
   1096     Assert.assertEquals(ForeignEnum.FOREIGN_BAZ, message.getUnpackedEnum(1));
   1097   }
   1098 
   1099   // ===================================================================
   1100   // Like above, but for extensions
   1101 
   1102   // Java gets confused with things like assertEquals(int, Integer):  it can't
   1103   // decide whether to call assertEquals(int, int) or assertEquals(Object,
   1104   // Object).  So we define these methods to help it.
   1105   private static void assertEqualsExactType(int a, int b) {
   1106     Assert.assertEquals(a, b);
   1107   }
   1108   private static void assertEqualsExactType(long a, long b) {
   1109     Assert.assertEquals(a, b);
   1110   }
   1111   private static void assertEqualsExactType(float a, float b) {
   1112     Assert.assertEquals(a, b, 0.0);
   1113   }
   1114   private static void assertEqualsExactType(double a, double b) {
   1115     Assert.assertEquals(a, b, 0.0);
   1116   }
   1117   private static void assertEqualsExactType(boolean a, boolean b) {
   1118     Assert.assertEquals(a, b);
   1119   }
   1120   private static void assertEqualsExactType(String a, String b) {
   1121     Assert.assertEquals(a, b);
   1122   }
   1123   private static void assertEqualsExactType(ByteString a, ByteString b) {
   1124     Assert.assertEquals(a, b);
   1125   }
   1126   private static void assertEqualsExactType(TestAllTypes.NestedEnum a,
   1127                                             TestAllTypes.NestedEnum b) {
   1128     Assert.assertEquals(a, b);
   1129   }
   1130   private static void assertEqualsExactType(ForeignEnum a, ForeignEnum b) {
   1131     Assert.assertEquals(a, b);
   1132   }
   1133   private static void assertEqualsExactType(ImportEnum a, ImportEnum b) {
   1134     Assert.assertEquals(a, b);
   1135   }
   1136   private static void assertEqualsExactType(TestAllTypesLite.NestedEnum a,
   1137                                             TestAllTypesLite.NestedEnum b) {
   1138     Assert.assertEquals(a, b);
   1139   }
   1140   private static void assertEqualsExactType(ForeignEnumLite a,
   1141                                             ForeignEnumLite b) {
   1142     Assert.assertEquals(a, b);
   1143   }
   1144   private static void assertEqualsExactType(ImportEnumLite a,
   1145                                             ImportEnumLite b) {
   1146     Assert.assertEquals(a, b);
   1147   }
   1148 
   1149   /**
   1150    * Get an unmodifiable {@link ExtensionRegistry} containing all the
   1151    * extensions of {@code TestAllExtensions}.
   1152    */
   1153   public static ExtensionRegistry getExtensionRegistry() {
   1154     ExtensionRegistry registry = ExtensionRegistry.newInstance();
   1155     registerAllExtensions(registry);
   1156     return registry.getUnmodifiable();
   1157   }
   1158 
   1159   public static ExtensionRegistryLite getExtensionRegistryLite() {
   1160     ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance();
   1161     registerAllExtensionsLite(registry);
   1162     return registry.getUnmodifiable();
   1163   }
   1164 
   1165   /**
   1166    * Register all of {@code TestAllExtensions}'s extensions with the
   1167    * given {@link ExtensionRegistry}.
   1168    */
   1169   public static void registerAllExtensions(ExtensionRegistry registry) {
   1170     UnittestProto.registerAllExtensions(registry);
   1171     registerAllExtensionsLite(registry);
   1172   }
   1173 
   1174   public static void registerAllExtensionsLite(ExtensionRegistryLite registry) {
   1175     UnittestLite.registerAllExtensions(registry);
   1176   }
   1177 
   1178   /**
   1179    * Set every field of {@code message} to the values expected by
   1180    * {@code assertAllExtensionsSet()}.
   1181    */
   1182   public static void setAllExtensions(TestAllExtensions.Builder message) {
   1183     message.setExtension(optionalInt32Extension   , 101);
   1184     message.setExtension(optionalInt64Extension   , 102L);
   1185     message.setExtension(optionalUint32Extension  , 103);
   1186     message.setExtension(optionalUint64Extension  , 104L);
   1187     message.setExtension(optionalSint32Extension  , 105);
   1188     message.setExtension(optionalSint64Extension  , 106L);
   1189     message.setExtension(optionalFixed32Extension , 107);
   1190     message.setExtension(optionalFixed64Extension , 108L);
   1191     message.setExtension(optionalSfixed32Extension, 109);
   1192     message.setExtension(optionalSfixed64Extension, 110L);
   1193     message.setExtension(optionalFloatExtension   , 111F);
   1194     message.setExtension(optionalDoubleExtension  , 112D);
   1195     message.setExtension(optionalBoolExtension    , true);
   1196     message.setExtension(optionalStringExtension  , "115");
   1197     message.setExtension(optionalBytesExtension   , toBytes("116"));
   1198 
   1199     message.setExtension(optionalGroupExtension,
   1200       OptionalGroup_extension.newBuilder().setA(117).build());
   1201     message.setExtension(optionalNestedMessageExtension,
   1202       TestAllTypes.NestedMessage.newBuilder().setBb(118).build());
   1203     message.setExtension(optionalForeignMessageExtension,
   1204       ForeignMessage.newBuilder().setC(119).build());
   1205     message.setExtension(optionalImportMessageExtension,
   1206       ImportMessage.newBuilder().setD(120).build());
   1207 
   1208     message.setExtension(optionalNestedEnumExtension, TestAllTypes.NestedEnum.BAZ);
   1209     message.setExtension(optionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ);
   1210     message.setExtension(optionalImportEnumExtension, ImportEnum.IMPORT_BAZ);
   1211 
   1212     message.setExtension(optionalStringPieceExtension, "124");
   1213     message.setExtension(optionalCordExtension, "125");
   1214 
   1215     // -----------------------------------------------------------------
   1216 
   1217     message.addExtension(repeatedInt32Extension   , 201);
   1218     message.addExtension(repeatedInt64Extension   , 202L);
   1219     message.addExtension(repeatedUint32Extension  , 203);
   1220     message.addExtension(repeatedUint64Extension  , 204L);
   1221     message.addExtension(repeatedSint32Extension  , 205);
   1222     message.addExtension(repeatedSint64Extension  , 206L);
   1223     message.addExtension(repeatedFixed32Extension , 207);
   1224     message.addExtension(repeatedFixed64Extension , 208L);
   1225     message.addExtension(repeatedSfixed32Extension, 209);
   1226     message.addExtension(repeatedSfixed64Extension, 210L);
   1227     message.addExtension(repeatedFloatExtension   , 211F);
   1228     message.addExtension(repeatedDoubleExtension  , 212D);
   1229     message.addExtension(repeatedBoolExtension    , true);
   1230     message.addExtension(repeatedStringExtension  , "215");
   1231     message.addExtension(repeatedBytesExtension   , toBytes("216"));
   1232 
   1233     message.addExtension(repeatedGroupExtension,
   1234       RepeatedGroup_extension.newBuilder().setA(217).build());
   1235     message.addExtension(repeatedNestedMessageExtension,
   1236       TestAllTypes.NestedMessage.newBuilder().setBb(218).build());
   1237     message.addExtension(repeatedForeignMessageExtension,
   1238       ForeignMessage.newBuilder().setC(219).build());
   1239     message.addExtension(repeatedImportMessageExtension,
   1240       ImportMessage.newBuilder().setD(220).build());
   1241 
   1242     message.addExtension(repeatedNestedEnumExtension, TestAllTypes.NestedEnum.BAR);
   1243     message.addExtension(repeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAR);
   1244     message.addExtension(repeatedImportEnumExtension, ImportEnum.IMPORT_BAR);
   1245 
   1246     message.addExtension(repeatedStringPieceExtension, "224");
   1247     message.addExtension(repeatedCordExtension, "225");
   1248 
   1249     // Add a second one of each field.
   1250     message.addExtension(repeatedInt32Extension   , 301);
   1251     message.addExtension(repeatedInt64Extension   , 302L);
   1252     message.addExtension(repeatedUint32Extension  , 303);
   1253     message.addExtension(repeatedUint64Extension  , 304L);
   1254     message.addExtension(repeatedSint32Extension  , 305);
   1255     message.addExtension(repeatedSint64Extension  , 306L);
   1256     message.addExtension(repeatedFixed32Extension , 307);
   1257     message.addExtension(repeatedFixed64Extension , 308L);
   1258     message.addExtension(repeatedSfixed32Extension, 309);
   1259     message.addExtension(repeatedSfixed64Extension, 310L);
   1260     message.addExtension(repeatedFloatExtension   , 311F);
   1261     message.addExtension(repeatedDoubleExtension  , 312D);
   1262     message.addExtension(repeatedBoolExtension    , false);
   1263     message.addExtension(repeatedStringExtension  , "315");
   1264     message.addExtension(repeatedBytesExtension   , toBytes("316"));
   1265 
   1266     message.addExtension(repeatedGroupExtension,
   1267       RepeatedGroup_extension.newBuilder().setA(317).build());
   1268     message.addExtension(repeatedNestedMessageExtension,
   1269       TestAllTypes.NestedMessage.newBuilder().setBb(318).build());
   1270     message.addExtension(repeatedForeignMessageExtension,
   1271       ForeignMessage.newBuilder().setC(319).build());
   1272     message.addExtension(repeatedImportMessageExtension,
   1273       ImportMessage.newBuilder().setD(320).build());
   1274 
   1275     message.addExtension(repeatedNestedEnumExtension, TestAllTypes.NestedEnum.BAZ);
   1276     message.addExtension(repeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ);
   1277     message.addExtension(repeatedImportEnumExtension, ImportEnum.IMPORT_BAZ);
   1278 
   1279     message.addExtension(repeatedStringPieceExtension, "324");
   1280     message.addExtension(repeatedCordExtension, "325");
   1281 
   1282     // -----------------------------------------------------------------
   1283 
   1284     message.setExtension(defaultInt32Extension   , 401);
   1285     message.setExtension(defaultInt64Extension   , 402L);
   1286     message.setExtension(defaultUint32Extension  , 403);
   1287     message.setExtension(defaultUint64Extension  , 404L);
   1288     message.setExtension(defaultSint32Extension  , 405);
   1289     message.setExtension(defaultSint64Extension  , 406L);
   1290     message.setExtension(defaultFixed32Extension , 407);
   1291     message.setExtension(defaultFixed64Extension , 408L);
   1292     message.setExtension(defaultSfixed32Extension, 409);
   1293     message.setExtension(defaultSfixed64Extension, 410L);
   1294     message.setExtension(defaultFloatExtension   , 411F);
   1295     message.setExtension(defaultDoubleExtension  , 412D);
   1296     message.setExtension(defaultBoolExtension    , false);
   1297     message.setExtension(defaultStringExtension  , "415");
   1298     message.setExtension(defaultBytesExtension   , toBytes("416"));
   1299 
   1300     message.setExtension(defaultNestedEnumExtension, TestAllTypes.NestedEnum.FOO);
   1301     message.setExtension(defaultForeignEnumExtension, ForeignEnum.FOREIGN_FOO);
   1302     message.setExtension(defaultImportEnumExtension, ImportEnum.IMPORT_FOO);
   1303 
   1304     message.setExtension(defaultStringPieceExtension, "424");
   1305     message.setExtension(defaultCordExtension, "425");
   1306   }
   1307 
   1308   // -------------------------------------------------------------------
   1309 
   1310   /**
   1311    * Modify the repeated extensions of {@code message} to contain the values
   1312    * expected by {@code assertRepeatedExtensionsModified()}.
   1313    */
   1314   public static void modifyRepeatedExtensions(
   1315       TestAllExtensions.Builder message) {
   1316     message.setExtension(repeatedInt32Extension   , 1, 501);
   1317     message.setExtension(repeatedInt64Extension   , 1, 502L);
   1318     message.setExtension(repeatedUint32Extension  , 1, 503);
   1319     message.setExtension(repeatedUint64Extension  , 1, 504L);
   1320     message.setExtension(repeatedSint32Extension  , 1, 505);
   1321     message.setExtension(repeatedSint64Extension  , 1, 506L);
   1322     message.setExtension(repeatedFixed32Extension , 1, 507);
   1323     message.setExtension(repeatedFixed64Extension , 1, 508L);
   1324     message.setExtension(repeatedSfixed32Extension, 1, 509);
   1325     message.setExtension(repeatedSfixed64Extension, 1, 510L);
   1326     message.setExtension(repeatedFloatExtension   , 1, 511F);
   1327     message.setExtension(repeatedDoubleExtension  , 1, 512D);
   1328     message.setExtension(repeatedBoolExtension    , 1, true);
   1329     message.setExtension(repeatedStringExtension  , 1, "515");
   1330     message.setExtension(repeatedBytesExtension   , 1, toBytes("516"));
   1331 
   1332     message.setExtension(repeatedGroupExtension, 1,
   1333       RepeatedGroup_extension.newBuilder().setA(517).build());
   1334     message.setExtension(repeatedNestedMessageExtension, 1,
   1335       TestAllTypes.NestedMessage.newBuilder().setBb(518).build());
   1336     message.setExtension(repeatedForeignMessageExtension, 1,
   1337       ForeignMessage.newBuilder().setC(519).build());
   1338     message.setExtension(repeatedImportMessageExtension, 1,
   1339       ImportMessage.newBuilder().setD(520).build());
   1340 
   1341     message.setExtension(repeatedNestedEnumExtension , 1, TestAllTypes.NestedEnum.FOO);
   1342     message.setExtension(repeatedForeignEnumExtension, 1, ForeignEnum.FOREIGN_FOO);
   1343     message.setExtension(repeatedImportEnumExtension , 1, ImportEnum.IMPORT_FOO);
   1344 
   1345     message.setExtension(repeatedStringPieceExtension, 1, "524");
   1346     message.setExtension(repeatedCordExtension, 1, "525");
   1347   }
   1348 
   1349   // -------------------------------------------------------------------
   1350 
   1351   /**
   1352    * Assert (using {@code junit.framework.Assert}} that all extensions of
   1353    * {@code message} are set to the values assigned by {@code setAllExtensions}.
   1354    */
   1355   public static void assertAllExtensionsSet(TestAllExtensions message) {
   1356     Assert.assertTrue(message.hasExtension(optionalInt32Extension   ));
   1357     Assert.assertTrue(message.hasExtension(optionalInt64Extension   ));
   1358     Assert.assertTrue(message.hasExtension(optionalUint32Extension  ));
   1359     Assert.assertTrue(message.hasExtension(optionalUint64Extension  ));
   1360     Assert.assertTrue(message.hasExtension(optionalSint32Extension  ));
   1361     Assert.assertTrue(message.hasExtension(optionalSint64Extension  ));
   1362     Assert.assertTrue(message.hasExtension(optionalFixed32Extension ));
   1363     Assert.assertTrue(message.hasExtension(optionalFixed64Extension ));
   1364     Assert.assertTrue(message.hasExtension(optionalSfixed32Extension));
   1365     Assert.assertTrue(message.hasExtension(optionalSfixed64Extension));
   1366     Assert.assertTrue(message.hasExtension(optionalFloatExtension   ));
   1367     Assert.assertTrue(message.hasExtension(optionalDoubleExtension  ));
   1368     Assert.assertTrue(message.hasExtension(optionalBoolExtension    ));
   1369     Assert.assertTrue(message.hasExtension(optionalStringExtension  ));
   1370     Assert.assertTrue(message.hasExtension(optionalBytesExtension   ));
   1371 
   1372     Assert.assertTrue(message.hasExtension(optionalGroupExtension         ));
   1373     Assert.assertTrue(message.hasExtension(optionalNestedMessageExtension ));
   1374     Assert.assertTrue(message.hasExtension(optionalForeignMessageExtension));
   1375     Assert.assertTrue(message.hasExtension(optionalImportMessageExtension ));
   1376 
   1377     Assert.assertTrue(message.getExtension(optionalGroupExtension         ).hasA());
   1378     Assert.assertTrue(message.getExtension(optionalNestedMessageExtension ).hasBb());
   1379     Assert.assertTrue(message.getExtension(optionalForeignMessageExtension).hasC());
   1380     Assert.assertTrue(message.getExtension(optionalImportMessageExtension ).hasD());
   1381 
   1382     Assert.assertTrue(message.hasExtension(optionalNestedEnumExtension ));
   1383     Assert.assertTrue(message.hasExtension(optionalForeignEnumExtension));
   1384     Assert.assertTrue(message.hasExtension(optionalImportEnumExtension ));
   1385 
   1386     Assert.assertTrue(message.hasExtension(optionalStringPieceExtension));
   1387     Assert.assertTrue(message.hasExtension(optionalCordExtension));
   1388 
   1389     assertEqualsExactType(101  , message.getExtension(optionalInt32Extension   ));
   1390     assertEqualsExactType(102L , message.getExtension(optionalInt64Extension   ));
   1391     assertEqualsExactType(103  , message.getExtension(optionalUint32Extension  ));
   1392     assertEqualsExactType(104L , message.getExtension(optionalUint64Extension  ));
   1393     assertEqualsExactType(105  , message.getExtension(optionalSint32Extension  ));
   1394     assertEqualsExactType(106L , message.getExtension(optionalSint64Extension  ));
   1395     assertEqualsExactType(107  , message.getExtension(optionalFixed32Extension ));
   1396     assertEqualsExactType(108L , message.getExtension(optionalFixed64Extension ));
   1397     assertEqualsExactType(109  , message.getExtension(optionalSfixed32Extension));
   1398     assertEqualsExactType(110L , message.getExtension(optionalSfixed64Extension));
   1399     assertEqualsExactType(111F , message.getExtension(optionalFloatExtension   ));
   1400     assertEqualsExactType(112D , message.getExtension(optionalDoubleExtension  ));
   1401     assertEqualsExactType(true , message.getExtension(optionalBoolExtension    ));
   1402     assertEqualsExactType("115", message.getExtension(optionalStringExtension  ));
   1403     assertEqualsExactType(toBytes("116"), message.getExtension(optionalBytesExtension));
   1404 
   1405     assertEqualsExactType(117, message.getExtension(optionalGroupExtension         ).getA());
   1406     assertEqualsExactType(118, message.getExtension(optionalNestedMessageExtension ).getBb());
   1407     assertEqualsExactType(119, message.getExtension(optionalForeignMessageExtension).getC());
   1408     assertEqualsExactType(120, message.getExtension(optionalImportMessageExtension ).getD());
   1409 
   1410     assertEqualsExactType(TestAllTypes.NestedEnum.BAZ,
   1411       message.getExtension(optionalNestedEnumExtension));
   1412     assertEqualsExactType(ForeignEnum.FOREIGN_BAZ,
   1413       message.getExtension(optionalForeignEnumExtension));
   1414     assertEqualsExactType(ImportEnum.IMPORT_BAZ,
   1415       message.getExtension(optionalImportEnumExtension));
   1416 
   1417     assertEqualsExactType("124", message.getExtension(optionalStringPieceExtension));
   1418     assertEqualsExactType("125", message.getExtension(optionalCordExtension));
   1419 
   1420     // -----------------------------------------------------------------
   1421 
   1422     Assert.assertEquals(2, message.getExtensionCount(repeatedInt32Extension   ));
   1423     Assert.assertEquals(2, message.getExtensionCount(repeatedInt64Extension   ));
   1424     Assert.assertEquals(2, message.getExtensionCount(repeatedUint32Extension  ));
   1425     Assert.assertEquals(2, message.getExtensionCount(repeatedUint64Extension  ));
   1426     Assert.assertEquals(2, message.getExtensionCount(repeatedSint32Extension  ));
   1427     Assert.assertEquals(2, message.getExtensionCount(repeatedSint64Extension  ));
   1428     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32Extension ));
   1429     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64Extension ));
   1430     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32Extension));
   1431     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64Extension));
   1432     Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtension   ));
   1433     Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtension  ));
   1434     Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtension    ));
   1435     Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtension  ));
   1436     Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtension   ));
   1437 
   1438     Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtension         ));
   1439     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtension ));
   1440     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtension));
   1441     Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtension ));
   1442     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtension    ));
   1443     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtension   ));
   1444     Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtension    ));
   1445 
   1446     Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtension));
   1447     Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtension));
   1448 
   1449     assertEqualsExactType(201  , message.getExtension(repeatedInt32Extension   , 0));
   1450     assertEqualsExactType(202L , message.getExtension(repeatedInt64Extension   , 0));
   1451     assertEqualsExactType(203  , message.getExtension(repeatedUint32Extension  , 0));
   1452     assertEqualsExactType(204L , message.getExtension(repeatedUint64Extension  , 0));
   1453     assertEqualsExactType(205  , message.getExtension(repeatedSint32Extension  , 0));
   1454     assertEqualsExactType(206L , message.getExtension(repeatedSint64Extension  , 0));
   1455     assertEqualsExactType(207  , message.getExtension(repeatedFixed32Extension , 0));
   1456     assertEqualsExactType(208L , message.getExtension(repeatedFixed64Extension , 0));
   1457     assertEqualsExactType(209  , message.getExtension(repeatedSfixed32Extension, 0));
   1458     assertEqualsExactType(210L , message.getExtension(repeatedSfixed64Extension, 0));
   1459     assertEqualsExactType(211F , message.getExtension(repeatedFloatExtension   , 0));
   1460     assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtension  , 0));
   1461     assertEqualsExactType(true , message.getExtension(repeatedBoolExtension    , 0));
   1462     assertEqualsExactType("215", message.getExtension(repeatedStringExtension  , 0));
   1463     assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtension, 0));
   1464 
   1465     assertEqualsExactType(217, message.getExtension(repeatedGroupExtension         , 0).getA());
   1466     assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtension , 0).getBb());
   1467     assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtension, 0).getC());
   1468     assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtension , 0).getD());
   1469 
   1470     assertEqualsExactType(TestAllTypes.NestedEnum.BAR,
   1471       message.getExtension(repeatedNestedEnumExtension, 0));
   1472     assertEqualsExactType(ForeignEnum.FOREIGN_BAR,
   1473       message.getExtension(repeatedForeignEnumExtension, 0));
   1474     assertEqualsExactType(ImportEnum.IMPORT_BAR,
   1475       message.getExtension(repeatedImportEnumExtension, 0));
   1476 
   1477     assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtension, 0));
   1478     assertEqualsExactType("225", message.getExtension(repeatedCordExtension, 0));
   1479 
   1480     assertEqualsExactType(301  , message.getExtension(repeatedInt32Extension   , 1));
   1481     assertEqualsExactType(302L , message.getExtension(repeatedInt64Extension   , 1));
   1482     assertEqualsExactType(303  , message.getExtension(repeatedUint32Extension  , 1));
   1483     assertEqualsExactType(304L , message.getExtension(repeatedUint64Extension  , 1));
   1484     assertEqualsExactType(305  , message.getExtension(repeatedSint32Extension  , 1));
   1485     assertEqualsExactType(306L , message.getExtension(repeatedSint64Extension  , 1));
   1486     assertEqualsExactType(307  , message.getExtension(repeatedFixed32Extension , 1));
   1487     assertEqualsExactType(308L , message.getExtension(repeatedFixed64Extension , 1));
   1488     assertEqualsExactType(309  , message.getExtension(repeatedSfixed32Extension, 1));
   1489     assertEqualsExactType(310L , message.getExtension(repeatedSfixed64Extension, 1));
   1490     assertEqualsExactType(311F , message.getExtension(repeatedFloatExtension   , 1));
   1491     assertEqualsExactType(312D , message.getExtension(repeatedDoubleExtension  , 1));
   1492     assertEqualsExactType(false, message.getExtension(repeatedBoolExtension    , 1));
   1493     assertEqualsExactType("315", message.getExtension(repeatedStringExtension  , 1));
   1494     assertEqualsExactType(toBytes("316"), message.getExtension(repeatedBytesExtension, 1));
   1495 
   1496     assertEqualsExactType(317, message.getExtension(repeatedGroupExtension         , 1).getA());
   1497     assertEqualsExactType(318, message.getExtension(repeatedNestedMessageExtension , 1).getBb());
   1498     assertEqualsExactType(319, message.getExtension(repeatedForeignMessageExtension, 1).getC());
   1499     assertEqualsExactType(320, message.getExtension(repeatedImportMessageExtension , 1).getD());
   1500 
   1501     assertEqualsExactType(TestAllTypes.NestedEnum.BAZ,
   1502       message.getExtension(repeatedNestedEnumExtension, 1));
   1503     assertEqualsExactType(ForeignEnum.FOREIGN_BAZ,
   1504       message.getExtension(repeatedForeignEnumExtension, 1));
   1505     assertEqualsExactType(ImportEnum.IMPORT_BAZ,
   1506       message.getExtension(repeatedImportEnumExtension, 1));
   1507 
   1508     assertEqualsExactType("324", message.getExtension(repeatedStringPieceExtension, 1));
   1509     assertEqualsExactType("325", message.getExtension(repeatedCordExtension, 1));
   1510 
   1511     // -----------------------------------------------------------------
   1512 
   1513     Assert.assertTrue(message.hasExtension(defaultInt32Extension   ));
   1514     Assert.assertTrue(message.hasExtension(defaultInt64Extension   ));
   1515     Assert.assertTrue(message.hasExtension(defaultUint32Extension  ));
   1516     Assert.assertTrue(message.hasExtension(defaultUint64Extension  ));
   1517     Assert.assertTrue(message.hasExtension(defaultSint32Extension  ));
   1518     Assert.assertTrue(message.hasExtension(defaultSint64Extension  ));
   1519     Assert.assertTrue(message.hasExtension(defaultFixed32Extension ));
   1520     Assert.assertTrue(message.hasExtension(defaultFixed64Extension ));
   1521     Assert.assertTrue(message.hasExtension(defaultSfixed32Extension));
   1522     Assert.assertTrue(message.hasExtension(defaultSfixed64Extension));
   1523     Assert.assertTrue(message.hasExtension(defaultFloatExtension   ));
   1524     Assert.assertTrue(message.hasExtension(defaultDoubleExtension  ));
   1525     Assert.assertTrue(message.hasExtension(defaultBoolExtension    ));
   1526     Assert.assertTrue(message.hasExtension(defaultStringExtension  ));
   1527     Assert.assertTrue(message.hasExtension(defaultBytesExtension   ));
   1528 
   1529     Assert.assertTrue(message.hasExtension(defaultNestedEnumExtension ));
   1530     Assert.assertTrue(message.hasExtension(defaultForeignEnumExtension));
   1531     Assert.assertTrue(message.hasExtension(defaultImportEnumExtension ));
   1532 
   1533     Assert.assertTrue(message.hasExtension(defaultStringPieceExtension));
   1534     Assert.assertTrue(message.hasExtension(defaultCordExtension));
   1535 
   1536     assertEqualsExactType(401  , message.getExtension(defaultInt32Extension   ));
   1537     assertEqualsExactType(402L , message.getExtension(defaultInt64Extension   ));
   1538     assertEqualsExactType(403  , message.getExtension(defaultUint32Extension  ));
   1539     assertEqualsExactType(404L , message.getExtension(defaultUint64Extension  ));
   1540     assertEqualsExactType(405  , message.getExtension(defaultSint32Extension  ));
   1541     assertEqualsExactType(406L , message.getExtension(defaultSint64Extension  ));
   1542     assertEqualsExactType(407  , message.getExtension(defaultFixed32Extension ));
   1543     assertEqualsExactType(408L , message.getExtension(defaultFixed64Extension ));
   1544     assertEqualsExactType(409  , message.getExtension(defaultSfixed32Extension));
   1545     assertEqualsExactType(410L , message.getExtension(defaultSfixed64Extension));
   1546     assertEqualsExactType(411F , message.getExtension(defaultFloatExtension   ));
   1547     assertEqualsExactType(412D , message.getExtension(defaultDoubleExtension  ));
   1548     assertEqualsExactType(false, message.getExtension(defaultBoolExtension    ));
   1549     assertEqualsExactType("415", message.getExtension(defaultStringExtension  ));
   1550     assertEqualsExactType(toBytes("416"), message.getExtension(defaultBytesExtension));
   1551 
   1552     assertEqualsExactType(TestAllTypes.NestedEnum.FOO,
   1553       message.getExtension(defaultNestedEnumExtension ));
   1554     assertEqualsExactType(ForeignEnum.FOREIGN_FOO,
   1555       message.getExtension(defaultForeignEnumExtension));
   1556     assertEqualsExactType(ImportEnum.IMPORT_FOO,
   1557       message.getExtension(defaultImportEnumExtension));
   1558 
   1559     assertEqualsExactType("424", message.getExtension(defaultStringPieceExtension));
   1560     assertEqualsExactType("425", message.getExtension(defaultCordExtension));
   1561   }
   1562 
   1563   // -------------------------------------------------------------------
   1564 
   1565   /**
   1566    * Assert (using {@code junit.framework.Assert}} that all extensions of
   1567    * {@code message} are cleared, and that getting the extensions returns their
   1568    * default values.
   1569    */
   1570   public static void assertExtensionsClear(TestAllExtensions message) {
   1571     // hasBlah() should initially be false for all optional fields.
   1572     Assert.assertFalse(message.hasExtension(optionalInt32Extension   ));
   1573     Assert.assertFalse(message.hasExtension(optionalInt64Extension   ));
   1574     Assert.assertFalse(message.hasExtension(optionalUint32Extension  ));
   1575     Assert.assertFalse(message.hasExtension(optionalUint64Extension  ));
   1576     Assert.assertFalse(message.hasExtension(optionalSint32Extension  ));
   1577     Assert.assertFalse(message.hasExtension(optionalSint64Extension  ));
   1578     Assert.assertFalse(message.hasExtension(optionalFixed32Extension ));
   1579     Assert.assertFalse(message.hasExtension(optionalFixed64Extension ));
   1580     Assert.assertFalse(message.hasExtension(optionalSfixed32Extension));
   1581     Assert.assertFalse(message.hasExtension(optionalSfixed64Extension));
   1582     Assert.assertFalse(message.hasExtension(optionalFloatExtension   ));
   1583     Assert.assertFalse(message.hasExtension(optionalDoubleExtension  ));
   1584     Assert.assertFalse(message.hasExtension(optionalBoolExtension    ));
   1585     Assert.assertFalse(message.hasExtension(optionalStringExtension  ));
   1586     Assert.assertFalse(message.hasExtension(optionalBytesExtension   ));
   1587 
   1588     Assert.assertFalse(message.hasExtension(optionalGroupExtension         ));
   1589     Assert.assertFalse(message.hasExtension(optionalNestedMessageExtension ));
   1590     Assert.assertFalse(message.hasExtension(optionalForeignMessageExtension));
   1591     Assert.assertFalse(message.hasExtension(optionalImportMessageExtension ));
   1592 
   1593     Assert.assertFalse(message.hasExtension(optionalNestedEnumExtension ));
   1594     Assert.assertFalse(message.hasExtension(optionalForeignEnumExtension));
   1595     Assert.assertFalse(message.hasExtension(optionalImportEnumExtension ));
   1596 
   1597     Assert.assertFalse(message.hasExtension(optionalStringPieceExtension));
   1598     Assert.assertFalse(message.hasExtension(optionalCordExtension));
   1599 
   1600     // Optional fields without defaults are set to zero or something like it.
   1601     assertEqualsExactType(0    , message.getExtension(optionalInt32Extension   ));
   1602     assertEqualsExactType(0L   , message.getExtension(optionalInt64Extension   ));
   1603     assertEqualsExactType(0    , message.getExtension(optionalUint32Extension  ));
   1604     assertEqualsExactType(0L   , message.getExtension(optionalUint64Extension  ));
   1605     assertEqualsExactType(0    , message.getExtension(optionalSint32Extension  ));
   1606     assertEqualsExactType(0L   , message.getExtension(optionalSint64Extension  ));
   1607     assertEqualsExactType(0    , message.getExtension(optionalFixed32Extension ));
   1608     assertEqualsExactType(0L   , message.getExtension(optionalFixed64Extension ));
   1609     assertEqualsExactType(0    , message.getExtension(optionalSfixed32Extension));
   1610     assertEqualsExactType(0L   , message.getExtension(optionalSfixed64Extension));
   1611     assertEqualsExactType(0F   , message.getExtension(optionalFloatExtension   ));
   1612     assertEqualsExactType(0D   , message.getExtension(optionalDoubleExtension  ));
   1613     assertEqualsExactType(false, message.getExtension(optionalBoolExtension    ));
   1614     assertEqualsExactType(""   , message.getExtension(optionalStringExtension  ));
   1615     assertEqualsExactType(ByteString.EMPTY, message.getExtension(optionalBytesExtension));
   1616 
   1617     // Embedded messages should also be clear.
   1618     Assert.assertFalse(message.getExtension(optionalGroupExtension         ).hasA());
   1619     Assert.assertFalse(message.getExtension(optionalNestedMessageExtension ).hasBb());
   1620     Assert.assertFalse(message.getExtension(optionalForeignMessageExtension).hasC());
   1621     Assert.assertFalse(message.getExtension(optionalImportMessageExtension ).hasD());
   1622 
   1623     assertEqualsExactType(0, message.getExtension(optionalGroupExtension         ).getA());
   1624     assertEqualsExactType(0, message.getExtension(optionalNestedMessageExtension ).getBb());
   1625     assertEqualsExactType(0, message.getExtension(optionalForeignMessageExtension).getC());
   1626     assertEqualsExactType(0, message.getExtension(optionalImportMessageExtension ).getD());
   1627 
   1628     // Enums without defaults are set to the first value in the enum.
   1629     assertEqualsExactType(TestAllTypes.NestedEnum.FOO,
   1630       message.getExtension(optionalNestedEnumExtension ));
   1631     assertEqualsExactType(ForeignEnum.FOREIGN_FOO,
   1632       message.getExtension(optionalForeignEnumExtension));
   1633     assertEqualsExactType(ImportEnum.IMPORT_FOO,
   1634       message.getExtension(optionalImportEnumExtension));
   1635 
   1636     assertEqualsExactType("", message.getExtension(optionalStringPieceExtension));
   1637     assertEqualsExactType("", message.getExtension(optionalCordExtension));
   1638 
   1639     // Repeated fields are empty.
   1640     Assert.assertEquals(0, message.getExtensionCount(repeatedInt32Extension   ));
   1641     Assert.assertEquals(0, message.getExtensionCount(repeatedInt64Extension   ));
   1642     Assert.assertEquals(0, message.getExtensionCount(repeatedUint32Extension  ));
   1643     Assert.assertEquals(0, message.getExtensionCount(repeatedUint64Extension  ));
   1644     Assert.assertEquals(0, message.getExtensionCount(repeatedSint32Extension  ));
   1645     Assert.assertEquals(0, message.getExtensionCount(repeatedSint64Extension  ));
   1646     Assert.assertEquals(0, message.getExtensionCount(repeatedFixed32Extension ));
   1647     Assert.assertEquals(0, message.getExtensionCount(repeatedFixed64Extension ));
   1648     Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed32Extension));
   1649     Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed64Extension));
   1650     Assert.assertEquals(0, message.getExtensionCount(repeatedFloatExtension   ));
   1651     Assert.assertEquals(0, message.getExtensionCount(repeatedDoubleExtension  ));
   1652     Assert.assertEquals(0, message.getExtensionCount(repeatedBoolExtension    ));
   1653     Assert.assertEquals(0, message.getExtensionCount(repeatedStringExtension  ));
   1654     Assert.assertEquals(0, message.getExtensionCount(repeatedBytesExtension   ));
   1655 
   1656     Assert.assertEquals(0, message.getExtensionCount(repeatedGroupExtension         ));
   1657     Assert.assertEquals(0, message.getExtensionCount(repeatedNestedMessageExtension ));
   1658     Assert.assertEquals(0, message.getExtensionCount(repeatedForeignMessageExtension));
   1659     Assert.assertEquals(0, message.getExtensionCount(repeatedImportMessageExtension ));
   1660     Assert.assertEquals(0, message.getExtensionCount(repeatedNestedEnumExtension    ));
   1661     Assert.assertEquals(0, message.getExtensionCount(repeatedForeignEnumExtension   ));
   1662     Assert.assertEquals(0, message.getExtensionCount(repeatedImportEnumExtension    ));
   1663 
   1664     Assert.assertEquals(0, message.getExtensionCount(repeatedStringPieceExtension));
   1665     Assert.assertEquals(0, message.getExtensionCount(repeatedCordExtension));
   1666 
   1667     // Repeated fields are empty via getExtension().size().
   1668     Assert.assertEquals(0, message.getExtension(repeatedInt32Extension   ).size());
   1669     Assert.assertEquals(0, message.getExtension(repeatedInt64Extension   ).size());
   1670     Assert.assertEquals(0, message.getExtension(repeatedUint32Extension  ).size());
   1671     Assert.assertEquals(0, message.getExtension(repeatedUint64Extension  ).size());
   1672     Assert.assertEquals(0, message.getExtension(repeatedSint32Extension  ).size());
   1673     Assert.assertEquals(0, message.getExtension(repeatedSint64Extension  ).size());
   1674     Assert.assertEquals(0, message.getExtension(repeatedFixed32Extension ).size());
   1675     Assert.assertEquals(0, message.getExtension(repeatedFixed64Extension ).size());
   1676     Assert.assertEquals(0, message.getExtension(repeatedSfixed32Extension).size());
   1677     Assert.assertEquals(0, message.getExtension(repeatedSfixed64Extension).size());
   1678     Assert.assertEquals(0, message.getExtension(repeatedFloatExtension   ).size());
   1679     Assert.assertEquals(0, message.getExtension(repeatedDoubleExtension  ).size());
   1680     Assert.assertEquals(0, message.getExtension(repeatedBoolExtension    ).size());
   1681     Assert.assertEquals(0, message.getExtension(repeatedStringExtension  ).size());
   1682     Assert.assertEquals(0, message.getExtension(repeatedBytesExtension   ).size());
   1683 
   1684     Assert.assertEquals(0, message.getExtension(repeatedGroupExtension         ).size());
   1685     Assert.assertEquals(0, message.getExtension(repeatedNestedMessageExtension ).size());
   1686     Assert.assertEquals(0, message.getExtension(repeatedForeignMessageExtension).size());
   1687     Assert.assertEquals(0, message.getExtension(repeatedImportMessageExtension ).size());
   1688     Assert.assertEquals(0, message.getExtension(repeatedNestedEnumExtension    ).size());
   1689     Assert.assertEquals(0, message.getExtension(repeatedForeignEnumExtension   ).size());
   1690     Assert.assertEquals(0, message.getExtension(repeatedImportEnumExtension    ).size());
   1691 
   1692     Assert.assertEquals(0, message.getExtension(repeatedStringPieceExtension).size());
   1693     Assert.assertEquals(0, message.getExtension(repeatedCordExtension).size());
   1694 
   1695     // hasBlah() should also be false for all default fields.
   1696     Assert.assertFalse(message.hasExtension(defaultInt32Extension   ));
   1697     Assert.assertFalse(message.hasExtension(defaultInt64Extension   ));
   1698     Assert.assertFalse(message.hasExtension(defaultUint32Extension  ));
   1699     Assert.assertFalse(message.hasExtension(defaultUint64Extension  ));
   1700     Assert.assertFalse(message.hasExtension(defaultSint32Extension  ));
   1701     Assert.assertFalse(message.hasExtension(defaultSint64Extension  ));
   1702     Assert.assertFalse(message.hasExtension(defaultFixed32Extension ));
   1703     Assert.assertFalse(message.hasExtension(defaultFixed64Extension ));
   1704     Assert.assertFalse(message.hasExtension(defaultSfixed32Extension));
   1705     Assert.assertFalse(message.hasExtension(defaultSfixed64Extension));
   1706     Assert.assertFalse(message.hasExtension(defaultFloatExtension   ));
   1707     Assert.assertFalse(message.hasExtension(defaultDoubleExtension  ));
   1708     Assert.assertFalse(message.hasExtension(defaultBoolExtension    ));
   1709     Assert.assertFalse(message.hasExtension(defaultStringExtension  ));
   1710     Assert.assertFalse(message.hasExtension(defaultBytesExtension   ));
   1711 
   1712     Assert.assertFalse(message.hasExtension(defaultNestedEnumExtension ));
   1713     Assert.assertFalse(message.hasExtension(defaultForeignEnumExtension));
   1714     Assert.assertFalse(message.hasExtension(defaultImportEnumExtension ));
   1715 
   1716     Assert.assertFalse(message.hasExtension(defaultStringPieceExtension));
   1717     Assert.assertFalse(message.hasExtension(defaultCordExtension));
   1718 
   1719     // Fields with defaults have their default values (duh).
   1720     assertEqualsExactType( 41    , message.getExtension(defaultInt32Extension   ));
   1721     assertEqualsExactType( 42L   , message.getExtension(defaultInt64Extension   ));
   1722     assertEqualsExactType( 43    , message.getExtension(defaultUint32Extension  ));
   1723     assertEqualsExactType( 44L   , message.getExtension(defaultUint64Extension  ));
   1724     assertEqualsExactType(-45    , message.getExtension(defaultSint32Extension  ));
   1725     assertEqualsExactType( 46L   , message.getExtension(defaultSint64Extension  ));
   1726     assertEqualsExactType( 47    , message.getExtension(defaultFixed32Extension ));
   1727     assertEqualsExactType( 48L   , message.getExtension(defaultFixed64Extension ));
   1728     assertEqualsExactType( 49    , message.getExtension(defaultSfixed32Extension));
   1729     assertEqualsExactType(-50L   , message.getExtension(defaultSfixed64Extension));
   1730     assertEqualsExactType( 51.5F , message.getExtension(defaultFloatExtension   ));
   1731     assertEqualsExactType( 52e3D , message.getExtension(defaultDoubleExtension  ));
   1732     assertEqualsExactType(true   , message.getExtension(defaultBoolExtension    ));
   1733     assertEqualsExactType("hello", message.getExtension(defaultStringExtension  ));
   1734     assertEqualsExactType(toBytes("world"), message.getExtension(defaultBytesExtension));
   1735 
   1736     assertEqualsExactType(TestAllTypes.NestedEnum.BAR,
   1737       message.getExtension(defaultNestedEnumExtension ));
   1738     assertEqualsExactType(ForeignEnum.FOREIGN_BAR,
   1739       message.getExtension(defaultForeignEnumExtension));
   1740     assertEqualsExactType(ImportEnum.IMPORT_BAR,
   1741       message.getExtension(defaultImportEnumExtension));
   1742 
   1743     assertEqualsExactType("abc", message.getExtension(defaultStringPieceExtension));
   1744     assertEqualsExactType("123", message.getExtension(defaultCordExtension));
   1745   }
   1746 
   1747   // -------------------------------------------------------------------
   1748 
   1749   /**
   1750    * Assert (using {@code junit.framework.Assert}} that all extensions of
   1751    * {@code message} are set to the values assigned by {@code setAllExtensions}
   1752    * followed by {@code modifyRepeatedExtensions}.
   1753    */
   1754   public static void assertRepeatedExtensionsModified(
   1755       TestAllExtensions message) {
   1756     // ModifyRepeatedFields only sets the second repeated element of each
   1757     // field.  In addition to verifying this, we also verify that the first
   1758     // element and size were *not* modified.
   1759     Assert.assertEquals(2, message.getExtensionCount(repeatedInt32Extension   ));
   1760     Assert.assertEquals(2, message.getExtensionCount(repeatedInt64Extension   ));
   1761     Assert.assertEquals(2, message.getExtensionCount(repeatedUint32Extension  ));
   1762     Assert.assertEquals(2, message.getExtensionCount(repeatedUint64Extension  ));
   1763     Assert.assertEquals(2, message.getExtensionCount(repeatedSint32Extension  ));
   1764     Assert.assertEquals(2, message.getExtensionCount(repeatedSint64Extension  ));
   1765     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32Extension ));
   1766     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64Extension ));
   1767     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32Extension));
   1768     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64Extension));
   1769     Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtension   ));
   1770     Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtension  ));
   1771     Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtension    ));
   1772     Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtension  ));
   1773     Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtension   ));
   1774 
   1775     Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtension         ));
   1776     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtension ));
   1777     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtension));
   1778     Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtension ));
   1779     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtension    ));
   1780     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtension   ));
   1781     Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtension    ));
   1782 
   1783     Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtension));
   1784     Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtension));
   1785 
   1786     assertEqualsExactType(201  , message.getExtension(repeatedInt32Extension   , 0));
   1787     assertEqualsExactType(202L , message.getExtension(repeatedInt64Extension   , 0));
   1788     assertEqualsExactType(203  , message.getExtension(repeatedUint32Extension  , 0));
   1789     assertEqualsExactType(204L , message.getExtension(repeatedUint64Extension  , 0));
   1790     assertEqualsExactType(205  , message.getExtension(repeatedSint32Extension  , 0));
   1791     assertEqualsExactType(206L , message.getExtension(repeatedSint64Extension  , 0));
   1792     assertEqualsExactType(207  , message.getExtension(repeatedFixed32Extension , 0));
   1793     assertEqualsExactType(208L , message.getExtension(repeatedFixed64Extension , 0));
   1794     assertEqualsExactType(209  , message.getExtension(repeatedSfixed32Extension, 0));
   1795     assertEqualsExactType(210L , message.getExtension(repeatedSfixed64Extension, 0));
   1796     assertEqualsExactType(211F , message.getExtension(repeatedFloatExtension   , 0));
   1797     assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtension  , 0));
   1798     assertEqualsExactType(true , message.getExtension(repeatedBoolExtension    , 0));
   1799     assertEqualsExactType("215", message.getExtension(repeatedStringExtension  , 0));
   1800     assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtension, 0));
   1801 
   1802     assertEqualsExactType(217, message.getExtension(repeatedGroupExtension         , 0).getA());
   1803     assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtension , 0).getBb());
   1804     assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtension, 0).getC());
   1805     assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtension , 0).getD());
   1806 
   1807     assertEqualsExactType(TestAllTypes.NestedEnum.BAR,
   1808       message.getExtension(repeatedNestedEnumExtension, 0));
   1809     assertEqualsExactType(ForeignEnum.FOREIGN_BAR,
   1810       message.getExtension(repeatedForeignEnumExtension, 0));
   1811     assertEqualsExactType(ImportEnum.IMPORT_BAR,
   1812       message.getExtension(repeatedImportEnumExtension, 0));
   1813 
   1814     assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtension, 0));
   1815     assertEqualsExactType("225", message.getExtension(repeatedCordExtension, 0));
   1816 
   1817     // Actually verify the second (modified) elements now.
   1818     assertEqualsExactType(501  , message.getExtension(repeatedInt32Extension   , 1));
   1819     assertEqualsExactType(502L , message.getExtension(repeatedInt64Extension   , 1));
   1820     assertEqualsExactType(503  , message.getExtension(repeatedUint32Extension  , 1));
   1821     assertEqualsExactType(504L , message.getExtension(repeatedUint64Extension  , 1));
   1822     assertEqualsExactType(505  , message.getExtension(repeatedSint32Extension  , 1));
   1823     assertEqualsExactType(506L , message.getExtension(repeatedSint64Extension  , 1));
   1824     assertEqualsExactType(507  , message.getExtension(repeatedFixed32Extension , 1));
   1825     assertEqualsExactType(508L , message.getExtension(repeatedFixed64Extension , 1));
   1826     assertEqualsExactType(509  , message.getExtension(repeatedSfixed32Extension, 1));
   1827     assertEqualsExactType(510L , message.getExtension(repeatedSfixed64Extension, 1));
   1828     assertEqualsExactType(511F , message.getExtension(repeatedFloatExtension   , 1));
   1829     assertEqualsExactType(512D , message.getExtension(repeatedDoubleExtension  , 1));
   1830     assertEqualsExactType(true , message.getExtension(repeatedBoolExtension    , 1));
   1831     assertEqualsExactType("515", message.getExtension(repeatedStringExtension  , 1));
   1832     assertEqualsExactType(toBytes("516"), message.getExtension(repeatedBytesExtension, 1));
   1833 
   1834     assertEqualsExactType(517, message.getExtension(repeatedGroupExtension         , 1).getA());
   1835     assertEqualsExactType(518, message.getExtension(repeatedNestedMessageExtension , 1).getBb());
   1836     assertEqualsExactType(519, message.getExtension(repeatedForeignMessageExtension, 1).getC());
   1837     assertEqualsExactType(520, message.getExtension(repeatedImportMessageExtension , 1).getD());
   1838 
   1839     assertEqualsExactType(TestAllTypes.NestedEnum.FOO,
   1840       message.getExtension(repeatedNestedEnumExtension, 1));
   1841     assertEqualsExactType(ForeignEnum.FOREIGN_FOO,
   1842       message.getExtension(repeatedForeignEnumExtension, 1));
   1843     assertEqualsExactType(ImportEnum.IMPORT_FOO,
   1844       message.getExtension(repeatedImportEnumExtension, 1));
   1845 
   1846     assertEqualsExactType("524", message.getExtension(repeatedStringPieceExtension, 1));
   1847     assertEqualsExactType("525", message.getExtension(repeatedCordExtension, 1));
   1848   }
   1849 
   1850   public static void setPackedExtensions(TestPackedExtensions.Builder message) {
   1851     message.addExtension(packedInt32Extension   , 601);
   1852     message.addExtension(packedInt64Extension   , 602L);
   1853     message.addExtension(packedUint32Extension  , 603);
   1854     message.addExtension(packedUint64Extension  , 604L);
   1855     message.addExtension(packedSint32Extension  , 605);
   1856     message.addExtension(packedSint64Extension  , 606L);
   1857     message.addExtension(packedFixed32Extension , 607);
   1858     message.addExtension(packedFixed64Extension , 608L);
   1859     message.addExtension(packedSfixed32Extension, 609);
   1860     message.addExtension(packedSfixed64Extension, 610L);
   1861     message.addExtension(packedFloatExtension   , 611F);
   1862     message.addExtension(packedDoubleExtension  , 612D);
   1863     message.addExtension(packedBoolExtension    , true);
   1864     message.addExtension(packedEnumExtension, ForeignEnum.FOREIGN_BAR);
   1865     // Add a second one of each field.
   1866     message.addExtension(packedInt32Extension   , 701);
   1867     message.addExtension(packedInt64Extension   , 702L);
   1868     message.addExtension(packedUint32Extension  , 703);
   1869     message.addExtension(packedUint64Extension  , 704L);
   1870     message.addExtension(packedSint32Extension  , 705);
   1871     message.addExtension(packedSint64Extension  , 706L);
   1872     message.addExtension(packedFixed32Extension , 707);
   1873     message.addExtension(packedFixed64Extension , 708L);
   1874     message.addExtension(packedSfixed32Extension, 709);
   1875     message.addExtension(packedSfixed64Extension, 710L);
   1876     message.addExtension(packedFloatExtension   , 711F);
   1877     message.addExtension(packedDoubleExtension  , 712D);
   1878     message.addExtension(packedBoolExtension    , false);
   1879     message.addExtension(packedEnumExtension, ForeignEnum.FOREIGN_BAZ);
   1880   }
   1881 
   1882   public static void assertPackedExtensionsSet(TestPackedExtensions message) {
   1883     Assert.assertEquals(2, message.getExtensionCount(packedInt32Extension   ));
   1884     Assert.assertEquals(2, message.getExtensionCount(packedInt64Extension   ));
   1885     Assert.assertEquals(2, message.getExtensionCount(packedUint32Extension  ));
   1886     Assert.assertEquals(2, message.getExtensionCount(packedUint64Extension  ));
   1887     Assert.assertEquals(2, message.getExtensionCount(packedSint32Extension  ));
   1888     Assert.assertEquals(2, message.getExtensionCount(packedSint64Extension  ));
   1889     Assert.assertEquals(2, message.getExtensionCount(packedFixed32Extension ));
   1890     Assert.assertEquals(2, message.getExtensionCount(packedFixed64Extension ));
   1891     Assert.assertEquals(2, message.getExtensionCount(packedSfixed32Extension));
   1892     Assert.assertEquals(2, message.getExtensionCount(packedSfixed64Extension));
   1893     Assert.assertEquals(2, message.getExtensionCount(packedFloatExtension   ));
   1894     Assert.assertEquals(2, message.getExtensionCount(packedDoubleExtension  ));
   1895     Assert.assertEquals(2, message.getExtensionCount(packedBoolExtension    ));
   1896     Assert.assertEquals(2, message.getExtensionCount(packedEnumExtension));
   1897     assertEqualsExactType(601  , message.getExtension(packedInt32Extension   , 0));
   1898     assertEqualsExactType(602L , message.getExtension(packedInt64Extension   , 0));
   1899     assertEqualsExactType(603  , message.getExtension(packedUint32Extension  , 0));
   1900     assertEqualsExactType(604L , message.getExtension(packedUint64Extension  , 0));
   1901     assertEqualsExactType(605  , message.getExtension(packedSint32Extension  , 0));
   1902     assertEqualsExactType(606L , message.getExtension(packedSint64Extension  , 0));
   1903     assertEqualsExactType(607  , message.getExtension(packedFixed32Extension , 0));
   1904     assertEqualsExactType(608L , message.getExtension(packedFixed64Extension , 0));
   1905     assertEqualsExactType(609  , message.getExtension(packedSfixed32Extension, 0));
   1906     assertEqualsExactType(610L , message.getExtension(packedSfixed64Extension, 0));
   1907     assertEqualsExactType(611F , message.getExtension(packedFloatExtension   , 0));
   1908     assertEqualsExactType(612D , message.getExtension(packedDoubleExtension  , 0));
   1909     assertEqualsExactType(true , message.getExtension(packedBoolExtension    , 0));
   1910     assertEqualsExactType(ForeignEnum.FOREIGN_BAR,
   1911                           message.getExtension(packedEnumExtension, 0));
   1912     assertEqualsExactType(701  , message.getExtension(packedInt32Extension   , 1));
   1913     assertEqualsExactType(702L , message.getExtension(packedInt64Extension   , 1));
   1914     assertEqualsExactType(703  , message.getExtension(packedUint32Extension  , 1));
   1915     assertEqualsExactType(704L , message.getExtension(packedUint64Extension  , 1));
   1916     assertEqualsExactType(705  , message.getExtension(packedSint32Extension  , 1));
   1917     assertEqualsExactType(706L , message.getExtension(packedSint64Extension  , 1));
   1918     assertEqualsExactType(707  , message.getExtension(packedFixed32Extension , 1));
   1919     assertEqualsExactType(708L , message.getExtension(packedFixed64Extension , 1));
   1920     assertEqualsExactType(709  , message.getExtension(packedSfixed32Extension, 1));
   1921     assertEqualsExactType(710L , message.getExtension(packedSfixed64Extension, 1));
   1922     assertEqualsExactType(711F , message.getExtension(packedFloatExtension   , 1));
   1923     assertEqualsExactType(712D , message.getExtension(packedDoubleExtension  , 1));
   1924     assertEqualsExactType(false, message.getExtension(packedBoolExtension    , 1));
   1925     assertEqualsExactType(ForeignEnum.FOREIGN_BAZ,
   1926                           message.getExtension(packedEnumExtension, 1));
   1927   }
   1928 
   1929   // ===================================================================
   1930   // Lite extensions
   1931 
   1932   /**
   1933    * Set every field of {@code message} to the values expected by
   1934    * {@code assertAllExtensionsSet()}.
   1935    */
   1936   public static void setAllExtensions(TestAllExtensionsLite.Builder message) {
   1937     message.setExtension(optionalInt32ExtensionLite   , 101);
   1938     message.setExtension(optionalInt64ExtensionLite   , 102L);
   1939     message.setExtension(optionalUint32ExtensionLite  , 103);
   1940     message.setExtension(optionalUint64ExtensionLite  , 104L);
   1941     message.setExtension(optionalSint32ExtensionLite  , 105);
   1942     message.setExtension(optionalSint64ExtensionLite  , 106L);
   1943     message.setExtension(optionalFixed32ExtensionLite , 107);
   1944     message.setExtension(optionalFixed64ExtensionLite , 108L);
   1945     message.setExtension(optionalSfixed32ExtensionLite, 109);
   1946     message.setExtension(optionalSfixed64ExtensionLite, 110L);
   1947     message.setExtension(optionalFloatExtensionLite   , 111F);
   1948     message.setExtension(optionalDoubleExtensionLite  , 112D);
   1949     message.setExtension(optionalBoolExtensionLite    , true);
   1950     message.setExtension(optionalStringExtensionLite  , "115");
   1951     message.setExtension(optionalBytesExtensionLite   , toBytes("116"));
   1952 
   1953     message.setExtension(optionalGroupExtensionLite,
   1954       OptionalGroup_extension_lite.newBuilder().setA(117).build());
   1955     message.setExtension(optionalNestedMessageExtensionLite,
   1956       TestAllTypesLite.NestedMessage.newBuilder().setBb(118).build());
   1957     message.setExtension(optionalForeignMessageExtensionLite,
   1958       ForeignMessageLite.newBuilder().setC(119).build());
   1959     message.setExtension(optionalImportMessageExtensionLite,
   1960       ImportMessageLite.newBuilder().setD(120).build());
   1961 
   1962     message.setExtension(optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ);
   1963     message.setExtension(optionalForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ);
   1964     message.setExtension(optionalImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_BAZ);
   1965 
   1966     message.setExtension(optionalStringPieceExtensionLite, "124");
   1967     message.setExtension(optionalCordExtensionLite, "125");
   1968 
   1969     // -----------------------------------------------------------------
   1970 
   1971     message.addExtension(repeatedInt32ExtensionLite   , 201);
   1972     message.addExtension(repeatedInt64ExtensionLite   , 202L);
   1973     message.addExtension(repeatedUint32ExtensionLite  , 203);
   1974     message.addExtension(repeatedUint64ExtensionLite  , 204L);
   1975     message.addExtension(repeatedSint32ExtensionLite  , 205);
   1976     message.addExtension(repeatedSint64ExtensionLite  , 206L);
   1977     message.addExtension(repeatedFixed32ExtensionLite , 207);
   1978     message.addExtension(repeatedFixed64ExtensionLite , 208L);
   1979     message.addExtension(repeatedSfixed32ExtensionLite, 209);
   1980     message.addExtension(repeatedSfixed64ExtensionLite, 210L);
   1981     message.addExtension(repeatedFloatExtensionLite   , 211F);
   1982     message.addExtension(repeatedDoubleExtensionLite  , 212D);
   1983     message.addExtension(repeatedBoolExtensionLite    , true);
   1984     message.addExtension(repeatedStringExtensionLite  , "215");
   1985     message.addExtension(repeatedBytesExtensionLite   , toBytes("216"));
   1986 
   1987     message.addExtension(repeatedGroupExtensionLite,
   1988       RepeatedGroup_extension_lite.newBuilder().setA(217).build());
   1989     message.addExtension(repeatedNestedMessageExtensionLite,
   1990       TestAllTypesLite.NestedMessage.newBuilder().setBb(218).build());
   1991     message.addExtension(repeatedForeignMessageExtensionLite,
   1992       ForeignMessageLite.newBuilder().setC(219).build());
   1993     message.addExtension(repeatedImportMessageExtensionLite,
   1994       ImportMessageLite.newBuilder().setD(220).build());
   1995 
   1996     message.addExtension(repeatedNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAR);
   1997     message.addExtension(repeatedForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAR);
   1998     message.addExtension(repeatedImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_BAR);
   1999 
   2000     message.addExtension(repeatedStringPieceExtensionLite, "224");
   2001     message.addExtension(repeatedCordExtensionLite, "225");
   2002 
   2003     // Add a second one of each field.
   2004     message.addExtension(repeatedInt32ExtensionLite   , 301);
   2005     message.addExtension(repeatedInt64ExtensionLite   , 302L);
   2006     message.addExtension(repeatedUint32ExtensionLite  , 303);
   2007     message.addExtension(repeatedUint64ExtensionLite  , 304L);
   2008     message.addExtension(repeatedSint32ExtensionLite  , 305);
   2009     message.addExtension(repeatedSint64ExtensionLite  , 306L);
   2010     message.addExtension(repeatedFixed32ExtensionLite , 307);
   2011     message.addExtension(repeatedFixed64ExtensionLite , 308L);
   2012     message.addExtension(repeatedSfixed32ExtensionLite, 309);
   2013     message.addExtension(repeatedSfixed64ExtensionLite, 310L);
   2014     message.addExtension(repeatedFloatExtensionLite   , 311F);
   2015     message.addExtension(repeatedDoubleExtensionLite  , 312D);
   2016     message.addExtension(repeatedBoolExtensionLite    , false);
   2017     message.addExtension(repeatedStringExtensionLite  , "315");
   2018     message.addExtension(repeatedBytesExtensionLite   , toBytes("316"));
   2019 
   2020     message.addExtension(repeatedGroupExtensionLite,
   2021       RepeatedGroup_extension_lite.newBuilder().setA(317).build());
   2022     message.addExtension(repeatedNestedMessageExtensionLite,
   2023       TestAllTypesLite.NestedMessage.newBuilder().setBb(318).build());
   2024     message.addExtension(repeatedForeignMessageExtensionLite,
   2025       ForeignMessageLite.newBuilder().setC(319).build());
   2026     message.addExtension(repeatedImportMessageExtensionLite,
   2027       ImportMessageLite.newBuilder().setD(320).build());
   2028 
   2029     message.addExtension(repeatedNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ);
   2030     message.addExtension(repeatedForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ);
   2031     message.addExtension(repeatedImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_BAZ);
   2032 
   2033     message.addExtension(repeatedStringPieceExtensionLite, "324");
   2034     message.addExtension(repeatedCordExtensionLite, "325");
   2035 
   2036     // -----------------------------------------------------------------
   2037 
   2038     message.setExtension(defaultInt32ExtensionLite   , 401);
   2039     message.setExtension(defaultInt64ExtensionLite   , 402L);
   2040     message.setExtension(defaultUint32ExtensionLite  , 403);
   2041     message.setExtension(defaultUint64ExtensionLite  , 404L);
   2042     message.setExtension(defaultSint32ExtensionLite  , 405);
   2043     message.setExtension(defaultSint64ExtensionLite  , 406L);
   2044     message.setExtension(defaultFixed32ExtensionLite , 407);
   2045     message.setExtension(defaultFixed64ExtensionLite , 408L);
   2046     message.setExtension(defaultSfixed32ExtensionLite, 409);
   2047     message.setExtension(defaultSfixed64ExtensionLite, 410L);
   2048     message.setExtension(defaultFloatExtensionLite   , 411F);
   2049     message.setExtension(defaultDoubleExtensionLite  , 412D);
   2050     message.setExtension(defaultBoolExtensionLite    , false);
   2051     message.setExtension(defaultStringExtensionLite  , "415");
   2052     message.setExtension(defaultBytesExtensionLite   , toBytes("416"));
   2053 
   2054     message.setExtension(defaultNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.FOO);
   2055     message.setExtension(defaultForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_FOO);
   2056     message.setExtension(defaultImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_FOO);
   2057 
   2058     message.setExtension(defaultStringPieceExtensionLite, "424");
   2059     message.setExtension(defaultCordExtensionLite, "425");
   2060   }
   2061 
   2062   // -------------------------------------------------------------------
   2063 
   2064   /**
   2065    * Modify the repeated extensions of {@code message} to contain the values
   2066    * expected by {@code assertRepeatedExtensionsModified()}.
   2067    */
   2068   public static void modifyRepeatedExtensions(
   2069       TestAllExtensionsLite.Builder message) {
   2070     message.setExtension(repeatedInt32ExtensionLite   , 1, 501);
   2071     message.setExtension(repeatedInt64ExtensionLite   , 1, 502L);
   2072     message.setExtension(repeatedUint32ExtensionLite  , 1, 503);
   2073     message.setExtension(repeatedUint64ExtensionLite  , 1, 504L);
   2074     message.setExtension(repeatedSint32ExtensionLite  , 1, 505);
   2075     message.setExtension(repeatedSint64ExtensionLite  , 1, 506L);
   2076     message.setExtension(repeatedFixed32ExtensionLite , 1, 507);
   2077     message.setExtension(repeatedFixed64ExtensionLite , 1, 508L);
   2078     message.setExtension(repeatedSfixed32ExtensionLite, 1, 509);
   2079     message.setExtension(repeatedSfixed64ExtensionLite, 1, 510L);
   2080     message.setExtension(repeatedFloatExtensionLite   , 1, 511F);
   2081     message.setExtension(repeatedDoubleExtensionLite  , 1, 512D);
   2082     message.setExtension(repeatedBoolExtensionLite    , 1, true);
   2083     message.setExtension(repeatedStringExtensionLite  , 1, "515");
   2084     message.setExtension(repeatedBytesExtensionLite   , 1, toBytes("516"));
   2085 
   2086     message.setExtension(repeatedGroupExtensionLite, 1,
   2087       RepeatedGroup_extension_lite.newBuilder().setA(517).build());
   2088     message.setExtension(repeatedNestedMessageExtensionLite, 1,
   2089       TestAllTypesLite.NestedMessage.newBuilder().setBb(518).build());
   2090     message.setExtension(repeatedForeignMessageExtensionLite, 1,
   2091       ForeignMessageLite.newBuilder().setC(519).build());
   2092     message.setExtension(repeatedImportMessageExtensionLite, 1,
   2093       ImportMessageLite.newBuilder().setD(520).build());
   2094 
   2095     message.setExtension(repeatedNestedEnumExtensionLite , 1, TestAllTypesLite.NestedEnum.FOO);
   2096     message.setExtension(repeatedForeignEnumExtensionLite, 1, ForeignEnumLite.FOREIGN_LITE_FOO);
   2097     message.setExtension(repeatedImportEnumExtensionLite , 1, ImportEnumLite.IMPORT_LITE_FOO);
   2098 
   2099     message.setExtension(repeatedStringPieceExtensionLite, 1, "524");
   2100     message.setExtension(repeatedCordExtensionLite, 1, "525");
   2101   }
   2102 
   2103   // -------------------------------------------------------------------
   2104 
   2105   /**
   2106    * Assert (using {@code junit.framework.Assert}} that all extensions of
   2107    * {@code message} are set to the values assigned by {@code setAllExtensions}.
   2108    */
   2109   public static void assertAllExtensionsSet(TestAllExtensionsLite message) {
   2110     Assert.assertTrue(message.hasExtension(optionalInt32ExtensionLite   ));
   2111     Assert.assertTrue(message.hasExtension(optionalInt64ExtensionLite   ));
   2112     Assert.assertTrue(message.hasExtension(optionalUint32ExtensionLite  ));
   2113     Assert.assertTrue(message.hasExtension(optionalUint64ExtensionLite  ));
   2114     Assert.assertTrue(message.hasExtension(optionalSint32ExtensionLite  ));
   2115     Assert.assertTrue(message.hasExtension(optionalSint64ExtensionLite  ));
   2116     Assert.assertTrue(message.hasExtension(optionalFixed32ExtensionLite ));
   2117     Assert.assertTrue(message.hasExtension(optionalFixed64ExtensionLite ));
   2118     Assert.assertTrue(message.hasExtension(optionalSfixed32ExtensionLite));
   2119     Assert.assertTrue(message.hasExtension(optionalSfixed64ExtensionLite));
   2120     Assert.assertTrue(message.hasExtension(optionalFloatExtensionLite   ));
   2121     Assert.assertTrue(message.hasExtension(optionalDoubleExtensionLite  ));
   2122     Assert.assertTrue(message.hasExtension(optionalBoolExtensionLite    ));
   2123     Assert.assertTrue(message.hasExtension(optionalStringExtensionLite  ));
   2124     Assert.assertTrue(message.hasExtension(optionalBytesExtensionLite   ));
   2125 
   2126     Assert.assertTrue(message.hasExtension(optionalGroupExtensionLite         ));
   2127     Assert.assertTrue(message.hasExtension(optionalNestedMessageExtensionLite ));
   2128     Assert.assertTrue(message.hasExtension(optionalForeignMessageExtensionLite));
   2129     Assert.assertTrue(message.hasExtension(optionalImportMessageExtensionLite ));
   2130 
   2131     Assert.assertTrue(message.getExtension(optionalGroupExtensionLite         ).hasA());
   2132     Assert.assertTrue(message.getExtension(optionalNestedMessageExtensionLite ).hasBb());
   2133     Assert.assertTrue(message.getExtension(optionalForeignMessageExtensionLite).hasC());
   2134     Assert.assertTrue(message.getExtension(optionalImportMessageExtensionLite ).hasD());
   2135 
   2136     Assert.assertTrue(message.hasExtension(optionalNestedEnumExtensionLite ));
   2137     Assert.assertTrue(message.hasExtension(optionalForeignEnumExtensionLite));
   2138     Assert.assertTrue(message.hasExtension(optionalImportEnumExtensionLite ));
   2139 
   2140     Assert.assertTrue(message.hasExtension(optionalStringPieceExtensionLite));
   2141     Assert.assertTrue(message.hasExtension(optionalCordExtensionLite));
   2142 
   2143     assertEqualsExactType(101  , message.getExtension(optionalInt32ExtensionLite   ));
   2144     assertEqualsExactType(102L , message.getExtension(optionalInt64ExtensionLite   ));
   2145     assertEqualsExactType(103  , message.getExtension(optionalUint32ExtensionLite  ));
   2146     assertEqualsExactType(104L , message.getExtension(optionalUint64ExtensionLite  ));
   2147     assertEqualsExactType(105  , message.getExtension(optionalSint32ExtensionLite  ));
   2148     assertEqualsExactType(106L , message.getExtension(optionalSint64ExtensionLite  ));
   2149     assertEqualsExactType(107  , message.getExtension(optionalFixed32ExtensionLite ));
   2150     assertEqualsExactType(108L , message.getExtension(optionalFixed64ExtensionLite ));
   2151     assertEqualsExactType(109  , message.getExtension(optionalSfixed32ExtensionLite));
   2152     assertEqualsExactType(110L , message.getExtension(optionalSfixed64ExtensionLite));
   2153     assertEqualsExactType(111F , message.getExtension(optionalFloatExtensionLite   ));
   2154     assertEqualsExactType(112D , message.getExtension(optionalDoubleExtensionLite  ));
   2155     assertEqualsExactType(true , message.getExtension(optionalBoolExtensionLite    ));
   2156     assertEqualsExactType("115", message.getExtension(optionalStringExtensionLite  ));
   2157     assertEqualsExactType(toBytes("116"), message.getExtension(optionalBytesExtensionLite));
   2158 
   2159     assertEqualsExactType(117, message.getExtension(optionalGroupExtensionLite         ).getA());
   2160     assertEqualsExactType(118, message.getExtension(optionalNestedMessageExtensionLite ).getBb());
   2161     assertEqualsExactType(119, message.getExtension(optionalForeignMessageExtensionLite).getC());
   2162     assertEqualsExactType(120, message.getExtension(optionalImportMessageExtensionLite ).getD());
   2163 
   2164     assertEqualsExactType(TestAllTypesLite.NestedEnum.BAZ,
   2165       message.getExtension(optionalNestedEnumExtensionLite));
   2166     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAZ,
   2167       message.getExtension(optionalForeignEnumExtensionLite));
   2168     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAZ,
   2169       message.getExtension(optionalImportEnumExtensionLite));
   2170 
   2171     assertEqualsExactType("124", message.getExtension(optionalStringPieceExtensionLite));
   2172     assertEqualsExactType("125", message.getExtension(optionalCordExtensionLite));
   2173 
   2174     // -----------------------------------------------------------------
   2175 
   2176     Assert.assertEquals(2, message.getExtensionCount(repeatedInt32ExtensionLite   ));
   2177     Assert.assertEquals(2, message.getExtensionCount(repeatedInt64ExtensionLite   ));
   2178     Assert.assertEquals(2, message.getExtensionCount(repeatedUint32ExtensionLite  ));
   2179     Assert.assertEquals(2, message.getExtensionCount(repeatedUint64ExtensionLite  ));
   2180     Assert.assertEquals(2, message.getExtensionCount(repeatedSint32ExtensionLite  ));
   2181     Assert.assertEquals(2, message.getExtensionCount(repeatedSint64ExtensionLite  ));
   2182     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32ExtensionLite ));
   2183     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64ExtensionLite ));
   2184     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32ExtensionLite));
   2185     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64ExtensionLite));
   2186     Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtensionLite   ));
   2187     Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtensionLite  ));
   2188     Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtensionLite    ));
   2189     Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtensionLite  ));
   2190     Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtensionLite   ));
   2191 
   2192     Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtensionLite         ));
   2193     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtensionLite ));
   2194     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtensionLite));
   2195     Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtensionLite ));
   2196     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtensionLite    ));
   2197     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtensionLite   ));
   2198     Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtensionLite    ));
   2199 
   2200     Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtensionLite));
   2201     Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtensionLite));
   2202 
   2203     assertEqualsExactType(201  , message.getExtension(repeatedInt32ExtensionLite   , 0));
   2204     assertEqualsExactType(202L , message.getExtension(repeatedInt64ExtensionLite   , 0));
   2205     assertEqualsExactType(203  , message.getExtension(repeatedUint32ExtensionLite  , 0));
   2206     assertEqualsExactType(204L , message.getExtension(repeatedUint64ExtensionLite  , 0));
   2207     assertEqualsExactType(205  , message.getExtension(repeatedSint32ExtensionLite  , 0));
   2208     assertEqualsExactType(206L , message.getExtension(repeatedSint64ExtensionLite  , 0));
   2209     assertEqualsExactType(207  , message.getExtension(repeatedFixed32ExtensionLite , 0));
   2210     assertEqualsExactType(208L , message.getExtension(repeatedFixed64ExtensionLite , 0));
   2211     assertEqualsExactType(209  , message.getExtension(repeatedSfixed32ExtensionLite, 0));
   2212     assertEqualsExactType(210L , message.getExtension(repeatedSfixed64ExtensionLite, 0));
   2213     assertEqualsExactType(211F , message.getExtension(repeatedFloatExtensionLite   , 0));
   2214     assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtensionLite  , 0));
   2215     assertEqualsExactType(true , message.getExtension(repeatedBoolExtensionLite    , 0));
   2216     assertEqualsExactType("215", message.getExtension(repeatedStringExtensionLite  , 0));
   2217     assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtensionLite, 0));
   2218 
   2219     assertEqualsExactType(217, message.getExtension(repeatedGroupExtensionLite         ,0).getA());
   2220     assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtensionLite ,0).getBb());
   2221     assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtensionLite,0).getC());
   2222     assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtensionLite ,0).getD());
   2223 
   2224     assertEqualsExactType(TestAllTypesLite.NestedEnum.BAR,
   2225       message.getExtension(repeatedNestedEnumExtensionLite, 0));
   2226     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR,
   2227       message.getExtension(repeatedForeignEnumExtensionLite, 0));
   2228     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAR,
   2229       message.getExtension(repeatedImportEnumExtensionLite, 0));
   2230 
   2231     assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtensionLite, 0));
   2232     assertEqualsExactType("225", message.getExtension(repeatedCordExtensionLite, 0));
   2233 
   2234     assertEqualsExactType(301  , message.getExtension(repeatedInt32ExtensionLite   , 1));
   2235     assertEqualsExactType(302L , message.getExtension(repeatedInt64ExtensionLite   , 1));
   2236     assertEqualsExactType(303  , message.getExtension(repeatedUint32ExtensionLite  , 1));
   2237     assertEqualsExactType(304L , message.getExtension(repeatedUint64ExtensionLite  , 1));
   2238     assertEqualsExactType(305  , message.getExtension(repeatedSint32ExtensionLite  , 1));
   2239     assertEqualsExactType(306L , message.getExtension(repeatedSint64ExtensionLite  , 1));
   2240     assertEqualsExactType(307  , message.getExtension(repeatedFixed32ExtensionLite , 1));
   2241     assertEqualsExactType(308L , message.getExtension(repeatedFixed64ExtensionLite , 1));
   2242     assertEqualsExactType(309  , message.getExtension(repeatedSfixed32ExtensionLite, 1));
   2243     assertEqualsExactType(310L , message.getExtension(repeatedSfixed64ExtensionLite, 1));
   2244     assertEqualsExactType(311F , message.getExtension(repeatedFloatExtensionLite   , 1));
   2245     assertEqualsExactType(312D , message.getExtension(repeatedDoubleExtensionLite  , 1));
   2246     assertEqualsExactType(false, message.getExtension(repeatedBoolExtensionLite    , 1));
   2247     assertEqualsExactType("315", message.getExtension(repeatedStringExtensionLite  , 1));
   2248     assertEqualsExactType(toBytes("316"), message.getExtension(repeatedBytesExtensionLite, 1));
   2249 
   2250     assertEqualsExactType(317, message.getExtension(repeatedGroupExtensionLite         ,1).getA());
   2251     assertEqualsExactType(318, message.getExtension(repeatedNestedMessageExtensionLite ,1).getBb());
   2252     assertEqualsExactType(319, message.getExtension(repeatedForeignMessageExtensionLite,1).getC());
   2253     assertEqualsExactType(320, message.getExtension(repeatedImportMessageExtensionLite ,1).getD());
   2254 
   2255     assertEqualsExactType(TestAllTypesLite.NestedEnum.BAZ,
   2256       message.getExtension(repeatedNestedEnumExtensionLite, 1));
   2257     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAZ,
   2258       message.getExtension(repeatedForeignEnumExtensionLite, 1));
   2259     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAZ,
   2260       message.getExtension(repeatedImportEnumExtensionLite, 1));
   2261 
   2262     assertEqualsExactType("324", message.getExtension(repeatedStringPieceExtensionLite, 1));
   2263     assertEqualsExactType("325", message.getExtension(repeatedCordExtensionLite, 1));
   2264 
   2265     // -----------------------------------------------------------------
   2266 
   2267     Assert.assertTrue(message.hasExtension(defaultInt32ExtensionLite   ));
   2268     Assert.assertTrue(message.hasExtension(defaultInt64ExtensionLite   ));
   2269     Assert.assertTrue(message.hasExtension(defaultUint32ExtensionLite  ));
   2270     Assert.assertTrue(message.hasExtension(defaultUint64ExtensionLite  ));
   2271     Assert.assertTrue(message.hasExtension(defaultSint32ExtensionLite  ));
   2272     Assert.assertTrue(message.hasExtension(defaultSint64ExtensionLite  ));
   2273     Assert.assertTrue(message.hasExtension(defaultFixed32ExtensionLite ));
   2274     Assert.assertTrue(message.hasExtension(defaultFixed64ExtensionLite ));
   2275     Assert.assertTrue(message.hasExtension(defaultSfixed32ExtensionLite));
   2276     Assert.assertTrue(message.hasExtension(defaultSfixed64ExtensionLite));
   2277     Assert.assertTrue(message.hasExtension(defaultFloatExtensionLite   ));
   2278     Assert.assertTrue(message.hasExtension(defaultDoubleExtensionLite  ));
   2279     Assert.assertTrue(message.hasExtension(defaultBoolExtensionLite    ));
   2280     Assert.assertTrue(message.hasExtension(defaultStringExtensionLite  ));
   2281     Assert.assertTrue(message.hasExtension(defaultBytesExtensionLite   ));
   2282 
   2283     Assert.assertTrue(message.hasExtension(defaultNestedEnumExtensionLite ));
   2284     Assert.assertTrue(message.hasExtension(defaultForeignEnumExtensionLite));
   2285     Assert.assertTrue(message.hasExtension(defaultImportEnumExtensionLite ));
   2286 
   2287     Assert.assertTrue(message.hasExtension(defaultStringPieceExtensionLite));
   2288     Assert.assertTrue(message.hasExtension(defaultCordExtensionLite));
   2289 
   2290     assertEqualsExactType(401  , message.getExtension(defaultInt32ExtensionLite   ));
   2291     assertEqualsExactType(402L , message.getExtension(defaultInt64ExtensionLite   ));
   2292     assertEqualsExactType(403  , message.getExtension(defaultUint32ExtensionLite  ));
   2293     assertEqualsExactType(404L , message.getExtension(defaultUint64ExtensionLite  ));
   2294     assertEqualsExactType(405  , message.getExtension(defaultSint32ExtensionLite  ));
   2295     assertEqualsExactType(406L , message.getExtension(defaultSint64ExtensionLite  ));
   2296     assertEqualsExactType(407  , message.getExtension(defaultFixed32ExtensionLite ));
   2297     assertEqualsExactType(408L , message.getExtension(defaultFixed64ExtensionLite ));
   2298     assertEqualsExactType(409  , message.getExtension(defaultSfixed32ExtensionLite));
   2299     assertEqualsExactType(410L , message.getExtension(defaultSfixed64ExtensionLite));
   2300     assertEqualsExactType(411F , message.getExtension(defaultFloatExtensionLite   ));
   2301     assertEqualsExactType(412D , message.getExtension(defaultDoubleExtensionLite  ));
   2302     assertEqualsExactType(false, message.getExtension(defaultBoolExtensionLite    ));
   2303     assertEqualsExactType("415", message.getExtension(defaultStringExtensionLite  ));
   2304     assertEqualsExactType(toBytes("416"), message.getExtension(defaultBytesExtensionLite));
   2305 
   2306     assertEqualsExactType(TestAllTypesLite.NestedEnum.FOO,
   2307       message.getExtension(defaultNestedEnumExtensionLite ));
   2308     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_FOO,
   2309       message.getExtension(defaultForeignEnumExtensionLite));
   2310     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_FOO,
   2311       message.getExtension(defaultImportEnumExtensionLite));
   2312 
   2313     assertEqualsExactType("424", message.getExtension(defaultStringPieceExtensionLite));
   2314     assertEqualsExactType("425", message.getExtension(defaultCordExtensionLite));
   2315   }
   2316 
   2317   // -------------------------------------------------------------------
   2318 
   2319   /**
   2320    * Assert (using {@code junit.framework.Assert}} that all extensions of
   2321    * {@code message} are cleared, and that getting the extensions returns their
   2322    * default values.
   2323    */
   2324   public static void assertExtensionsClear(TestAllExtensionsLite message) {
   2325     // hasBlah() should initially be false for all optional fields.
   2326     Assert.assertFalse(message.hasExtension(optionalInt32ExtensionLite   ));
   2327     Assert.assertFalse(message.hasExtension(optionalInt64ExtensionLite   ));
   2328     Assert.assertFalse(message.hasExtension(optionalUint32ExtensionLite  ));
   2329     Assert.assertFalse(message.hasExtension(optionalUint64ExtensionLite  ));
   2330     Assert.assertFalse(message.hasExtension(optionalSint32ExtensionLite  ));
   2331     Assert.assertFalse(message.hasExtension(optionalSint64ExtensionLite  ));
   2332     Assert.assertFalse(message.hasExtension(optionalFixed32ExtensionLite ));
   2333     Assert.assertFalse(message.hasExtension(optionalFixed64ExtensionLite ));
   2334     Assert.assertFalse(message.hasExtension(optionalSfixed32ExtensionLite));
   2335     Assert.assertFalse(message.hasExtension(optionalSfixed64ExtensionLite));
   2336     Assert.assertFalse(message.hasExtension(optionalFloatExtensionLite   ));
   2337     Assert.assertFalse(message.hasExtension(optionalDoubleExtensionLite  ));
   2338     Assert.assertFalse(message.hasExtension(optionalBoolExtensionLite    ));
   2339     Assert.assertFalse(message.hasExtension(optionalStringExtensionLite  ));
   2340     Assert.assertFalse(message.hasExtension(optionalBytesExtensionLite   ));
   2341 
   2342     Assert.assertFalse(message.hasExtension(optionalGroupExtensionLite         ));
   2343     Assert.assertFalse(message.hasExtension(optionalNestedMessageExtensionLite ));
   2344     Assert.assertFalse(message.hasExtension(optionalForeignMessageExtensionLite));
   2345     Assert.assertFalse(message.hasExtension(optionalImportMessageExtensionLite ));
   2346 
   2347     Assert.assertFalse(message.hasExtension(optionalNestedEnumExtensionLite ));
   2348     Assert.assertFalse(message.hasExtension(optionalForeignEnumExtensionLite));
   2349     Assert.assertFalse(message.hasExtension(optionalImportEnumExtensionLite ));
   2350 
   2351     Assert.assertFalse(message.hasExtension(optionalStringPieceExtensionLite));
   2352     Assert.assertFalse(message.hasExtension(optionalCordExtensionLite));
   2353 
   2354     // Optional fields without defaults are set to zero or something like it.
   2355     assertEqualsExactType(0    , message.getExtension(optionalInt32ExtensionLite   ));
   2356     assertEqualsExactType(0L   , message.getExtension(optionalInt64ExtensionLite   ));
   2357     assertEqualsExactType(0    , message.getExtension(optionalUint32ExtensionLite  ));
   2358     assertEqualsExactType(0L   , message.getExtension(optionalUint64ExtensionLite  ));
   2359     assertEqualsExactType(0    , message.getExtension(optionalSint32ExtensionLite  ));
   2360     assertEqualsExactType(0L   , message.getExtension(optionalSint64ExtensionLite  ));
   2361     assertEqualsExactType(0    , message.getExtension(optionalFixed32ExtensionLite ));
   2362     assertEqualsExactType(0L   , message.getExtension(optionalFixed64ExtensionLite ));
   2363     assertEqualsExactType(0    , message.getExtension(optionalSfixed32ExtensionLite));
   2364     assertEqualsExactType(0L   , message.getExtension(optionalSfixed64ExtensionLite));
   2365     assertEqualsExactType(0F   , message.getExtension(optionalFloatExtensionLite   ));
   2366     assertEqualsExactType(0D   , message.getExtension(optionalDoubleExtensionLite  ));
   2367     assertEqualsExactType(false, message.getExtension(optionalBoolExtensionLite    ));
   2368     assertEqualsExactType(""   , message.getExtension(optionalStringExtensionLite  ));
   2369     assertEqualsExactType(ByteString.EMPTY, message.getExtension(optionalBytesExtensionLite));
   2370 
   2371     // Embedded messages should also be clear.
   2372     Assert.assertFalse(message.getExtension(optionalGroupExtensionLite         ).hasA());
   2373     Assert.assertFalse(message.getExtension(optionalNestedMessageExtensionLite ).hasBb());
   2374     Assert.assertFalse(message.getExtension(optionalForeignMessageExtensionLite).hasC());
   2375     Assert.assertFalse(message.getExtension(optionalImportMessageExtensionLite ).hasD());
   2376 
   2377     assertEqualsExactType(0, message.getExtension(optionalGroupExtensionLite         ).getA());
   2378     assertEqualsExactType(0, message.getExtension(optionalNestedMessageExtensionLite ).getBb());
   2379     assertEqualsExactType(0, message.getExtension(optionalForeignMessageExtensionLite).getC());
   2380     assertEqualsExactType(0, message.getExtension(optionalImportMessageExtensionLite ).getD());
   2381 
   2382     // Enums without defaults are set to the first value in the enum.
   2383     assertEqualsExactType(TestAllTypesLite.NestedEnum.FOO,
   2384       message.getExtension(optionalNestedEnumExtensionLite ));
   2385     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_FOO,
   2386       message.getExtension(optionalForeignEnumExtensionLite));
   2387     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_FOO,
   2388       message.getExtension(optionalImportEnumExtensionLite));
   2389 
   2390     assertEqualsExactType("", message.getExtension(optionalStringPieceExtensionLite));
   2391     assertEqualsExactType("", message.getExtension(optionalCordExtensionLite));
   2392 
   2393     // Repeated fields are empty.
   2394     Assert.assertEquals(0, message.getExtensionCount(repeatedInt32ExtensionLite   ));
   2395     Assert.assertEquals(0, message.getExtensionCount(repeatedInt64ExtensionLite   ));
   2396     Assert.assertEquals(0, message.getExtensionCount(repeatedUint32ExtensionLite  ));
   2397     Assert.assertEquals(0, message.getExtensionCount(repeatedUint64ExtensionLite  ));
   2398     Assert.assertEquals(0, message.getExtensionCount(repeatedSint32ExtensionLite  ));
   2399     Assert.assertEquals(0, message.getExtensionCount(repeatedSint64ExtensionLite  ));
   2400     Assert.assertEquals(0, message.getExtensionCount(repeatedFixed32ExtensionLite ));
   2401     Assert.assertEquals(0, message.getExtensionCount(repeatedFixed64ExtensionLite ));
   2402     Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed32ExtensionLite));
   2403     Assert.assertEquals(0, message.getExtensionCount(repeatedSfixed64ExtensionLite));
   2404     Assert.assertEquals(0, message.getExtensionCount(repeatedFloatExtensionLite   ));
   2405     Assert.assertEquals(0, message.getExtensionCount(repeatedDoubleExtensionLite  ));
   2406     Assert.assertEquals(0, message.getExtensionCount(repeatedBoolExtensionLite    ));
   2407     Assert.assertEquals(0, message.getExtensionCount(repeatedStringExtensionLite  ));
   2408     Assert.assertEquals(0, message.getExtensionCount(repeatedBytesExtensionLite   ));
   2409 
   2410     Assert.assertEquals(0, message.getExtensionCount(repeatedGroupExtensionLite         ));
   2411     Assert.assertEquals(0, message.getExtensionCount(repeatedNestedMessageExtensionLite ));
   2412     Assert.assertEquals(0, message.getExtensionCount(repeatedForeignMessageExtensionLite));
   2413     Assert.assertEquals(0, message.getExtensionCount(repeatedImportMessageExtensionLite ));
   2414     Assert.assertEquals(0, message.getExtensionCount(repeatedNestedEnumExtensionLite    ));
   2415     Assert.assertEquals(0, message.getExtensionCount(repeatedForeignEnumExtensionLite   ));
   2416     Assert.assertEquals(0, message.getExtensionCount(repeatedImportEnumExtensionLite    ));
   2417 
   2418     Assert.assertEquals(0, message.getExtensionCount(repeatedStringPieceExtensionLite));
   2419     Assert.assertEquals(0, message.getExtensionCount(repeatedCordExtensionLite));
   2420 
   2421     // hasBlah() should also be false for all default fields.
   2422     Assert.assertFalse(message.hasExtension(defaultInt32ExtensionLite   ));
   2423     Assert.assertFalse(message.hasExtension(defaultInt64ExtensionLite   ));
   2424     Assert.assertFalse(message.hasExtension(defaultUint32ExtensionLite  ));
   2425     Assert.assertFalse(message.hasExtension(defaultUint64ExtensionLite  ));
   2426     Assert.assertFalse(message.hasExtension(defaultSint32ExtensionLite  ));
   2427     Assert.assertFalse(message.hasExtension(defaultSint64ExtensionLite  ));
   2428     Assert.assertFalse(message.hasExtension(defaultFixed32ExtensionLite ));
   2429     Assert.assertFalse(message.hasExtension(defaultFixed64ExtensionLite ));
   2430     Assert.assertFalse(message.hasExtension(defaultSfixed32ExtensionLite));
   2431     Assert.assertFalse(message.hasExtension(defaultSfixed64ExtensionLite));
   2432     Assert.assertFalse(message.hasExtension(defaultFloatExtensionLite   ));
   2433     Assert.assertFalse(message.hasExtension(defaultDoubleExtensionLite  ));
   2434     Assert.assertFalse(message.hasExtension(defaultBoolExtensionLite    ));
   2435     Assert.assertFalse(message.hasExtension(defaultStringExtensionLite  ));
   2436     Assert.assertFalse(message.hasExtension(defaultBytesExtensionLite   ));
   2437 
   2438     Assert.assertFalse(message.hasExtension(defaultNestedEnumExtensionLite ));
   2439     Assert.assertFalse(message.hasExtension(defaultForeignEnumExtensionLite));
   2440     Assert.assertFalse(message.hasExtension(defaultImportEnumExtensionLite ));
   2441 
   2442     Assert.assertFalse(message.hasExtension(defaultStringPieceExtensionLite));
   2443     Assert.assertFalse(message.hasExtension(defaultCordExtensionLite));
   2444 
   2445     // Fields with defaults have their default values (duh).
   2446     assertEqualsExactType( 41    , message.getExtension(defaultInt32ExtensionLite   ));
   2447     assertEqualsExactType( 42L   , message.getExtension(defaultInt64ExtensionLite   ));
   2448     assertEqualsExactType( 43    , message.getExtension(defaultUint32ExtensionLite  ));
   2449     assertEqualsExactType( 44L   , message.getExtension(defaultUint64ExtensionLite  ));
   2450     assertEqualsExactType(-45    , message.getExtension(defaultSint32ExtensionLite  ));
   2451     assertEqualsExactType( 46L   , message.getExtension(defaultSint64ExtensionLite  ));
   2452     assertEqualsExactType( 47    , message.getExtension(defaultFixed32ExtensionLite ));
   2453     assertEqualsExactType( 48L   , message.getExtension(defaultFixed64ExtensionLite ));
   2454     assertEqualsExactType( 49    , message.getExtension(defaultSfixed32ExtensionLite));
   2455     assertEqualsExactType(-50L   , message.getExtension(defaultSfixed64ExtensionLite));
   2456     assertEqualsExactType( 51.5F , message.getExtension(defaultFloatExtensionLite   ));
   2457     assertEqualsExactType( 52e3D , message.getExtension(defaultDoubleExtensionLite  ));
   2458     assertEqualsExactType(true   , message.getExtension(defaultBoolExtensionLite    ));
   2459     assertEqualsExactType("hello", message.getExtension(defaultStringExtensionLite  ));
   2460     assertEqualsExactType(toBytes("world"), message.getExtension(defaultBytesExtensionLite));
   2461 
   2462     assertEqualsExactType(TestAllTypesLite.NestedEnum.BAR,
   2463       message.getExtension(defaultNestedEnumExtensionLite ));
   2464     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR,
   2465       message.getExtension(defaultForeignEnumExtensionLite));
   2466     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAR,
   2467       message.getExtension(defaultImportEnumExtensionLite));
   2468 
   2469     assertEqualsExactType("abc", message.getExtension(defaultStringPieceExtensionLite));
   2470     assertEqualsExactType("123", message.getExtension(defaultCordExtensionLite));
   2471   }
   2472 
   2473   // -------------------------------------------------------------------
   2474 
   2475   /**
   2476    * Assert (using {@code junit.framework.Assert}} that all extensions of
   2477    * {@code message} are set to the values assigned by {@code setAllExtensions}
   2478    * followed by {@code modifyRepeatedExtensions}.
   2479    */
   2480   public static void assertRepeatedExtensionsModified(
   2481       TestAllExtensionsLite message) {
   2482     // ModifyRepeatedFields only sets the second repeated element of each
   2483     // field.  In addition to verifying this, we also verify that the first
   2484     // element and size were *not* modified.
   2485     Assert.assertEquals(2, message.getExtensionCount(repeatedInt32ExtensionLite   ));
   2486     Assert.assertEquals(2, message.getExtensionCount(repeatedInt64ExtensionLite   ));
   2487     Assert.assertEquals(2, message.getExtensionCount(repeatedUint32ExtensionLite  ));
   2488     Assert.assertEquals(2, message.getExtensionCount(repeatedUint64ExtensionLite  ));
   2489     Assert.assertEquals(2, message.getExtensionCount(repeatedSint32ExtensionLite  ));
   2490     Assert.assertEquals(2, message.getExtensionCount(repeatedSint64ExtensionLite  ));
   2491     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed32ExtensionLite ));
   2492     Assert.assertEquals(2, message.getExtensionCount(repeatedFixed64ExtensionLite ));
   2493     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed32ExtensionLite));
   2494     Assert.assertEquals(2, message.getExtensionCount(repeatedSfixed64ExtensionLite));
   2495     Assert.assertEquals(2, message.getExtensionCount(repeatedFloatExtensionLite   ));
   2496     Assert.assertEquals(2, message.getExtensionCount(repeatedDoubleExtensionLite  ));
   2497     Assert.assertEquals(2, message.getExtensionCount(repeatedBoolExtensionLite    ));
   2498     Assert.assertEquals(2, message.getExtensionCount(repeatedStringExtensionLite  ));
   2499     Assert.assertEquals(2, message.getExtensionCount(repeatedBytesExtensionLite   ));
   2500 
   2501     Assert.assertEquals(2, message.getExtensionCount(repeatedGroupExtensionLite         ));
   2502     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedMessageExtensionLite ));
   2503     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignMessageExtensionLite));
   2504     Assert.assertEquals(2, message.getExtensionCount(repeatedImportMessageExtensionLite ));
   2505     Assert.assertEquals(2, message.getExtensionCount(repeatedNestedEnumExtensionLite    ));
   2506     Assert.assertEquals(2, message.getExtensionCount(repeatedForeignEnumExtensionLite   ));
   2507     Assert.assertEquals(2, message.getExtensionCount(repeatedImportEnumExtensionLite    ));
   2508 
   2509     Assert.assertEquals(2, message.getExtensionCount(repeatedStringPieceExtensionLite));
   2510     Assert.assertEquals(2, message.getExtensionCount(repeatedCordExtensionLite));
   2511 
   2512     assertEqualsExactType(201  , message.getExtension(repeatedInt32ExtensionLite   , 0));
   2513     assertEqualsExactType(202L , message.getExtension(repeatedInt64ExtensionLite   , 0));
   2514     assertEqualsExactType(203  , message.getExtension(repeatedUint32ExtensionLite  , 0));
   2515     assertEqualsExactType(204L , message.getExtension(repeatedUint64ExtensionLite  , 0));
   2516     assertEqualsExactType(205  , message.getExtension(repeatedSint32ExtensionLite  , 0));
   2517     assertEqualsExactType(206L , message.getExtension(repeatedSint64ExtensionLite  , 0));
   2518     assertEqualsExactType(207  , message.getExtension(repeatedFixed32ExtensionLite , 0));
   2519     assertEqualsExactType(208L , message.getExtension(repeatedFixed64ExtensionLite , 0));
   2520     assertEqualsExactType(209  , message.getExtension(repeatedSfixed32ExtensionLite, 0));
   2521     assertEqualsExactType(210L , message.getExtension(repeatedSfixed64ExtensionLite, 0));
   2522     assertEqualsExactType(211F , message.getExtension(repeatedFloatExtensionLite   , 0));
   2523     assertEqualsExactType(212D , message.getExtension(repeatedDoubleExtensionLite  , 0));
   2524     assertEqualsExactType(true , message.getExtension(repeatedBoolExtensionLite    , 0));
   2525     assertEqualsExactType("215", message.getExtension(repeatedStringExtensionLite  , 0));
   2526     assertEqualsExactType(toBytes("216"), message.getExtension(repeatedBytesExtensionLite, 0));
   2527 
   2528     assertEqualsExactType(217, message.getExtension(repeatedGroupExtensionLite         ,0).getA());
   2529     assertEqualsExactType(218, message.getExtension(repeatedNestedMessageExtensionLite ,0).getBb());
   2530     assertEqualsExactType(219, message.getExtension(repeatedForeignMessageExtensionLite,0).getC());
   2531     assertEqualsExactType(220, message.getExtension(repeatedImportMessageExtensionLite ,0).getD());
   2532 
   2533     assertEqualsExactType(TestAllTypesLite.NestedEnum.BAR,
   2534       message.getExtension(repeatedNestedEnumExtensionLite, 0));
   2535     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR,
   2536       message.getExtension(repeatedForeignEnumExtensionLite, 0));
   2537     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_BAR,
   2538       message.getExtension(repeatedImportEnumExtensionLite, 0));
   2539 
   2540     assertEqualsExactType("224", message.getExtension(repeatedStringPieceExtensionLite, 0));
   2541     assertEqualsExactType("225", message.getExtension(repeatedCordExtensionLite, 0));
   2542 
   2543     // Actually verify the second (modified) elements now.
   2544     assertEqualsExactType(501  , message.getExtension(repeatedInt32ExtensionLite   , 1));
   2545     assertEqualsExactType(502L , message.getExtension(repeatedInt64ExtensionLite   , 1));
   2546     assertEqualsExactType(503  , message.getExtension(repeatedUint32ExtensionLite  , 1));
   2547     assertEqualsExactType(504L , message.getExtension(repeatedUint64ExtensionLite  , 1));
   2548     assertEqualsExactType(505  , message.getExtension(repeatedSint32ExtensionLite  , 1));
   2549     assertEqualsExactType(506L , message.getExtension(repeatedSint64ExtensionLite  , 1));
   2550     assertEqualsExactType(507  , message.getExtension(repeatedFixed32ExtensionLite , 1));
   2551     assertEqualsExactType(508L , message.getExtension(repeatedFixed64ExtensionLite , 1));
   2552     assertEqualsExactType(509  , message.getExtension(repeatedSfixed32ExtensionLite, 1));
   2553     assertEqualsExactType(510L , message.getExtension(repeatedSfixed64ExtensionLite, 1));
   2554     assertEqualsExactType(511F , message.getExtension(repeatedFloatExtensionLite   , 1));
   2555     assertEqualsExactType(512D , message.getExtension(repeatedDoubleExtensionLite  , 1));
   2556     assertEqualsExactType(true , message.getExtension(repeatedBoolExtensionLite    , 1));
   2557     assertEqualsExactType("515", message.getExtension(repeatedStringExtensionLite  , 1));
   2558     assertEqualsExactType(toBytes("516"), message.getExtension(repeatedBytesExtensionLite, 1));
   2559 
   2560     assertEqualsExactType(517, message.getExtension(repeatedGroupExtensionLite         ,1).getA());
   2561     assertEqualsExactType(518, message.getExtension(repeatedNestedMessageExtensionLite ,1).getBb());
   2562     assertEqualsExactType(519, message.getExtension(repeatedForeignMessageExtensionLite,1).getC());
   2563     assertEqualsExactType(520, message.getExtension(repeatedImportMessageExtensionLite ,1).getD());
   2564 
   2565     assertEqualsExactType(TestAllTypesLite.NestedEnum.FOO,
   2566       message.getExtension(repeatedNestedEnumExtensionLite, 1));
   2567     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_FOO,
   2568       message.getExtension(repeatedForeignEnumExtensionLite, 1));
   2569     assertEqualsExactType(ImportEnumLite.IMPORT_LITE_FOO,
   2570       message.getExtension(repeatedImportEnumExtensionLite, 1));
   2571 
   2572     assertEqualsExactType("524", message.getExtension(repeatedStringPieceExtensionLite, 1));
   2573     assertEqualsExactType("525", message.getExtension(repeatedCordExtensionLite, 1));
   2574   }
   2575 
   2576   public static void setPackedExtensions(TestPackedExtensionsLite.Builder message) {
   2577     message.addExtension(packedInt32ExtensionLite   , 601);
   2578     message.addExtension(packedInt64ExtensionLite   , 602L);
   2579     message.addExtension(packedUint32ExtensionLite  , 603);
   2580     message.addExtension(packedUint64ExtensionLite  , 604L);
   2581     message.addExtension(packedSint32ExtensionLite  , 605);
   2582     message.addExtension(packedSint64ExtensionLite  , 606L);
   2583     message.addExtension(packedFixed32ExtensionLite , 607);
   2584     message.addExtension(packedFixed64ExtensionLite , 608L);
   2585     message.addExtension(packedSfixed32ExtensionLite, 609);
   2586     message.addExtension(packedSfixed64ExtensionLite, 610L);
   2587     message.addExtension(packedFloatExtensionLite   , 611F);
   2588     message.addExtension(packedDoubleExtensionLite  , 612D);
   2589     message.addExtension(packedBoolExtensionLite    , true);
   2590     message.addExtension(packedEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAR);
   2591     // Add a second one of each field.
   2592     message.addExtension(packedInt32ExtensionLite   , 701);
   2593     message.addExtension(packedInt64ExtensionLite   , 702L);
   2594     message.addExtension(packedUint32ExtensionLite  , 703);
   2595     message.addExtension(packedUint64ExtensionLite  , 704L);
   2596     message.addExtension(packedSint32ExtensionLite  , 705);
   2597     message.addExtension(packedSint64ExtensionLite  , 706L);
   2598     message.addExtension(packedFixed32ExtensionLite , 707);
   2599     message.addExtension(packedFixed64ExtensionLite , 708L);
   2600     message.addExtension(packedSfixed32ExtensionLite, 709);
   2601     message.addExtension(packedSfixed64ExtensionLite, 710L);
   2602     message.addExtension(packedFloatExtensionLite   , 711F);
   2603     message.addExtension(packedDoubleExtensionLite  , 712D);
   2604     message.addExtension(packedBoolExtensionLite    , false);
   2605     message.addExtension(packedEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ);
   2606   }
   2607 
   2608   public static void assertPackedExtensionsSet(TestPackedExtensionsLite message) {
   2609     Assert.assertEquals(2, message.getExtensionCount(packedInt32ExtensionLite   ));
   2610     Assert.assertEquals(2, message.getExtensionCount(packedInt64ExtensionLite   ));
   2611     Assert.assertEquals(2, message.getExtensionCount(packedUint32ExtensionLite  ));
   2612     Assert.assertEquals(2, message.getExtensionCount(packedUint64ExtensionLite  ));
   2613     Assert.assertEquals(2, message.getExtensionCount(packedSint32ExtensionLite  ));
   2614     Assert.assertEquals(2, message.getExtensionCount(packedSint64ExtensionLite  ));
   2615     Assert.assertEquals(2, message.getExtensionCount(packedFixed32ExtensionLite ));
   2616     Assert.assertEquals(2, message.getExtensionCount(packedFixed64ExtensionLite ));
   2617     Assert.assertEquals(2, message.getExtensionCount(packedSfixed32ExtensionLite));
   2618     Assert.assertEquals(2, message.getExtensionCount(packedSfixed64ExtensionLite));
   2619     Assert.assertEquals(2, message.getExtensionCount(packedFloatExtensionLite   ));
   2620     Assert.assertEquals(2, message.getExtensionCount(packedDoubleExtensionLite  ));
   2621     Assert.assertEquals(2, message.getExtensionCount(packedBoolExtensionLite    ));
   2622     Assert.assertEquals(2, message.getExtensionCount(packedEnumExtensionLite));
   2623     assertEqualsExactType(601  , message.getExtension(packedInt32ExtensionLite   , 0));
   2624     assertEqualsExactType(602L , message.getExtension(packedInt64ExtensionLite   , 0));
   2625     assertEqualsExactType(603  , message.getExtension(packedUint32ExtensionLite  , 0));
   2626     assertEqualsExactType(604L , message.getExtension(packedUint64ExtensionLite  , 0));
   2627     assertEqualsExactType(605  , message.getExtension(packedSint32ExtensionLite  , 0));
   2628     assertEqualsExactType(606L , message.getExtension(packedSint64ExtensionLite  , 0));
   2629     assertEqualsExactType(607  , message.getExtension(packedFixed32ExtensionLite , 0));
   2630     assertEqualsExactType(608L , message.getExtension(packedFixed64ExtensionLite , 0));
   2631     assertEqualsExactType(609  , message.getExtension(packedSfixed32ExtensionLite, 0));
   2632     assertEqualsExactType(610L , message.getExtension(packedSfixed64ExtensionLite, 0));
   2633     assertEqualsExactType(611F , message.getExtension(packedFloatExtensionLite   , 0));
   2634     assertEqualsExactType(612D , message.getExtension(packedDoubleExtensionLite  , 0));
   2635     assertEqualsExactType(true , message.getExtension(packedBoolExtensionLite    , 0));
   2636     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAR,
   2637                           message.getExtension(packedEnumExtensionLite, 0));
   2638     assertEqualsExactType(701  , message.getExtension(packedInt32ExtensionLite   , 1));
   2639     assertEqualsExactType(702L , message.getExtension(packedInt64ExtensionLite   , 1));
   2640     assertEqualsExactType(703  , message.getExtension(packedUint32ExtensionLite  , 1));
   2641     assertEqualsExactType(704L , message.getExtension(packedUint64ExtensionLite  , 1));
   2642     assertEqualsExactType(705  , message.getExtension(packedSint32ExtensionLite  , 1));
   2643     assertEqualsExactType(706L , message.getExtension(packedSint64ExtensionLite  , 1));
   2644     assertEqualsExactType(707  , message.getExtension(packedFixed32ExtensionLite , 1));
   2645     assertEqualsExactType(708L , message.getExtension(packedFixed64ExtensionLite , 1));
   2646     assertEqualsExactType(709  , message.getExtension(packedSfixed32ExtensionLite, 1));
   2647     assertEqualsExactType(710L , message.getExtension(packedSfixed64ExtensionLite, 1));
   2648     assertEqualsExactType(711F , message.getExtension(packedFloatExtensionLite   , 1));
   2649     assertEqualsExactType(712D , message.getExtension(packedDoubleExtensionLite  , 1));
   2650     assertEqualsExactType(false, message.getExtension(packedBoolExtensionLite    , 1));
   2651     assertEqualsExactType(ForeignEnumLite.FOREIGN_LITE_BAZ,
   2652                           message.getExtension(packedEnumExtensionLite, 1));
   2653   }
   2654 
   2655   // =================================================================
   2656 
   2657   /**
   2658    * Performs the same things that the methods of {@code TestUtil} do, but
   2659    * via the reflection interface.  This is its own class because it needs
   2660    * to know what descriptor to use.
   2661    */
   2662   public static class ReflectionTester {
   2663     private final Descriptors.Descriptor baseDescriptor;
   2664     private final ExtensionRegistry extensionRegistry;
   2665 
   2666     private final Descriptors.FileDescriptor file;
   2667     private final Descriptors.FileDescriptor importFile;
   2668 
   2669     private final Descriptors.Descriptor optionalGroup;
   2670     private final Descriptors.Descriptor repeatedGroup;
   2671     private final Descriptors.Descriptor nestedMessage;
   2672     private final Descriptors.Descriptor foreignMessage;
   2673     private final Descriptors.Descriptor importMessage;
   2674 
   2675     private final Descriptors.FieldDescriptor groupA;
   2676     private final Descriptors.FieldDescriptor repeatedGroupA;
   2677     private final Descriptors.FieldDescriptor nestedB;
   2678     private final Descriptors.FieldDescriptor foreignC;
   2679     private final Descriptors.FieldDescriptor importD;
   2680 
   2681     private final Descriptors.EnumDescriptor nestedEnum;
   2682     private final Descriptors.EnumDescriptor foreignEnum;
   2683     private final Descriptors.EnumDescriptor importEnum;
   2684 
   2685     private final Descriptors.EnumValueDescriptor nestedFoo;
   2686     private final Descriptors.EnumValueDescriptor nestedBar;
   2687     private final Descriptors.EnumValueDescriptor nestedBaz;
   2688     private final Descriptors.EnumValueDescriptor foreignFoo;
   2689     private final Descriptors.EnumValueDescriptor foreignBar;
   2690     private final Descriptors.EnumValueDescriptor foreignBaz;
   2691     private final Descriptors.EnumValueDescriptor importFoo;
   2692     private final Descriptors.EnumValueDescriptor importBar;
   2693     private final Descriptors.EnumValueDescriptor importBaz;
   2694 
   2695     /**
   2696      * Construct a {@code ReflectionTester} that will expect messages using
   2697      * the given descriptor.
   2698      *
   2699      * Normally {@code baseDescriptor} should be a descriptor for the type
   2700      * {@code TestAllTypes}, defined in
   2701      * {@code google/protobuf/unittest.proto}.  However, if
   2702      * {@code extensionRegistry} is non-null, then {@code baseDescriptor} should
   2703      * be for {@code TestAllExtensions} instead, and instead of reading and
   2704      * writing normal fields, the tester will read and write extensions.
   2705      * All of {@code TestAllExtensions}' extensions must be registered in the
   2706      * registry.
   2707      */
   2708     public ReflectionTester(Descriptors.Descriptor baseDescriptor,
   2709                             ExtensionRegistry extensionRegistry) {
   2710       this.baseDescriptor = baseDescriptor;
   2711       this.extensionRegistry = extensionRegistry;
   2712 
   2713       this.file = baseDescriptor.getFile();
   2714       Assert.assertEquals(1, file.getDependencies().size());
   2715       this.importFile = file.getDependencies().get(0);
   2716 
   2717       Descriptors.Descriptor testAllTypes;
   2718       if (baseDescriptor.getName() == "TestAllTypes") {
   2719         testAllTypes = baseDescriptor;
   2720       } else {
   2721         testAllTypes = file.findMessageTypeByName("TestAllTypes");
   2722         Assert.assertNotNull(testAllTypes);
   2723       }
   2724 
   2725       if (extensionRegistry == null) {
   2726         // Use testAllTypes, rather than baseDescriptor, to allow
   2727         // initialization using TestPackedTypes descriptors. These objects
   2728         // won't be used by the methods for packed fields.
   2729         this.optionalGroup =
   2730           testAllTypes.findNestedTypeByName("OptionalGroup");
   2731         this.repeatedGroup =
   2732           testAllTypes.findNestedTypeByName("RepeatedGroup");
   2733       } else {
   2734         this.optionalGroup =
   2735           file.findMessageTypeByName("OptionalGroup_extension");
   2736         this.repeatedGroup =
   2737           file.findMessageTypeByName("RepeatedGroup_extension");
   2738       }
   2739       this.nestedMessage = testAllTypes.findNestedTypeByName("NestedMessage");
   2740       this.foreignMessage = file.findMessageTypeByName("ForeignMessage");
   2741       this.importMessage = importFile.findMessageTypeByName("ImportMessage");
   2742 
   2743       this.nestedEnum = testAllTypes.findEnumTypeByName("NestedEnum");
   2744       this.foreignEnum = file.findEnumTypeByName("ForeignEnum");
   2745       this.importEnum = importFile.findEnumTypeByName("ImportEnum");
   2746 
   2747       Assert.assertNotNull(optionalGroup );
   2748       Assert.assertNotNull(repeatedGroup );
   2749       Assert.assertNotNull(nestedMessage );
   2750       Assert.assertNotNull(foreignMessage);
   2751       Assert.assertNotNull(importMessage );
   2752       Assert.assertNotNull(nestedEnum    );
   2753       Assert.assertNotNull(foreignEnum   );
   2754       Assert.assertNotNull(importEnum    );
   2755 
   2756       this.nestedB  = nestedMessage .findFieldByName("bb");
   2757       this.foreignC = foreignMessage.findFieldByName("c");
   2758       this.importD  = importMessage .findFieldByName("d");
   2759       this.nestedFoo = nestedEnum.findValueByName("FOO");
   2760       this.nestedBar = nestedEnum.findValueByName("BAR");
   2761       this.nestedBaz = nestedEnum.findValueByName("BAZ");
   2762       this.foreignFoo = foreignEnum.findValueByName("FOREIGN_FOO");
   2763       this.foreignBar = foreignEnum.findValueByName("FOREIGN_BAR");
   2764       this.foreignBaz = foreignEnum.findValueByName("FOREIGN_BAZ");
   2765       this.importFoo = importEnum.findValueByName("IMPORT_FOO");
   2766       this.importBar = importEnum.findValueByName("IMPORT_BAR");
   2767       this.importBaz = importEnum.findValueByName("IMPORT_BAZ");
   2768 
   2769       this.groupA = optionalGroup.findFieldByName("a");
   2770       this.repeatedGroupA = repeatedGroup.findFieldByName("a");
   2771 
   2772       Assert.assertNotNull(groupA        );
   2773       Assert.assertNotNull(repeatedGroupA);
   2774       Assert.assertNotNull(nestedB       );
   2775       Assert.assertNotNull(foreignC      );
   2776       Assert.assertNotNull(importD       );
   2777       Assert.assertNotNull(nestedFoo     );
   2778       Assert.assertNotNull(nestedBar     );
   2779       Assert.assertNotNull(nestedBaz     );
   2780       Assert.assertNotNull(foreignFoo    );
   2781       Assert.assertNotNull(foreignBar    );
   2782       Assert.assertNotNull(foreignBaz    );
   2783       Assert.assertNotNull(importFoo     );
   2784       Assert.assertNotNull(importBar     );
   2785       Assert.assertNotNull(importBaz     );
   2786     }
   2787 
   2788     /**
   2789      * Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
   2790      */
   2791     private Descriptors.FieldDescriptor f(String name) {
   2792       Descriptors.FieldDescriptor result;
   2793       if (extensionRegistry == null) {
   2794         result = baseDescriptor.findFieldByName(name);
   2795       } else {
   2796         result = file.findExtensionByName(name + "_extension");
   2797       }
   2798       Assert.assertNotNull(result);
   2799       return result;
   2800     }
   2801 
   2802     /**
   2803      * Calls {@code parent.newBuilderForField()} or uses the
   2804      * {@code ExtensionRegistry} to find an appropriate builder, depending
   2805      * on what type is being tested.
   2806      */
   2807     private Message.Builder newBuilderForField(
   2808         Message.Builder parent, Descriptors.FieldDescriptor field) {
   2809       if (extensionRegistry == null) {
   2810         return parent.newBuilderForField(field);
   2811       } else {
   2812         ExtensionRegistry.ExtensionInfo extension =
   2813           extensionRegistry.findExtensionByNumber(field.getContainingType(),
   2814                                                   field.getNumber());
   2815         Assert.assertNotNull(extension);
   2816         Assert.assertNotNull(extension.defaultInstance);
   2817         return extension.defaultInstance.newBuilderForType();
   2818       }
   2819     }
   2820 
   2821     // -------------------------------------------------------------------
   2822 
   2823     /**
   2824      * Set every field of {@code message} to the values expected by
   2825      * {@code assertAllFieldsSet()}, using the {@link Message.Builder}
   2826      * reflection interface.
   2827      */
   2828     void setAllFieldsViaReflection(Message.Builder message) {
   2829       message.setField(f("optional_int32"   ), 101 );
   2830       message.setField(f("optional_int64"   ), 102L);
   2831       message.setField(f("optional_uint32"  ), 103 );
   2832       message.setField(f("optional_uint64"  ), 104L);
   2833       message.setField(f("optional_sint32"  ), 105 );
   2834       message.setField(f("optional_sint64"  ), 106L);
   2835       message.setField(f("optional_fixed32" ), 107 );
   2836       message.setField(f("optional_fixed64" ), 108L);
   2837       message.setField(f("optional_sfixed32"), 109 );
   2838       message.setField(f("optional_sfixed64"), 110L);
   2839       message.setField(f("optional_float"   ), 111F);
   2840       message.setField(f("optional_double"  ), 112D);
   2841       message.setField(f("optional_bool"    ), true);
   2842       message.setField(f("optional_string"  ), "115");
   2843       message.setField(f("optional_bytes"   ), toBytes("116"));
   2844 
   2845       message.setField(f("optionalgroup"),
   2846         newBuilderForField(message, f("optionalgroup"))
   2847                .setField(groupA, 117).build());
   2848       message.setField(f("optional_nested_message"),
   2849         newBuilderForField(message, f("optional_nested_message"))
   2850                .setField(nestedB, 118).build());
   2851       message.setField(f("optional_foreign_message"),
   2852         newBuilderForField(message, f("optional_foreign_message"))
   2853                .setField(foreignC, 119).build());
   2854       message.setField(f("optional_import_message"),
   2855         newBuilderForField(message, f("optional_import_message"))
   2856                .setField(importD, 120).build());
   2857 
   2858       message.setField(f("optional_nested_enum" ),  nestedBaz);
   2859       message.setField(f("optional_foreign_enum"), foreignBaz);
   2860       message.setField(f("optional_import_enum" ),  importBaz);
   2861 
   2862       message.setField(f("optional_string_piece" ), "124");
   2863       message.setField(f("optional_cord" ), "125");
   2864 
   2865       // -----------------------------------------------------------------
   2866 
   2867       message.addRepeatedField(f("repeated_int32"   ), 201 );
   2868       message.addRepeatedField(f("repeated_int64"   ), 202L);
   2869       message.addRepeatedField(f("repeated_uint32"  ), 203 );
   2870       message.addRepeatedField(f("repeated_uint64"  ), 204L);
   2871       message.addRepeatedField(f("repeated_sint32"  ), 205 );
   2872       message.addRepeatedField(f("repeated_sint64"  ), 206L);
   2873       message.addRepeatedField(f("repeated_fixed32" ), 207 );
   2874       message.addRepeatedField(f("repeated_fixed64" ), 208L);
   2875       message.addRepeatedField(f("repeated_sfixed32"), 209 );
   2876       message.addRepeatedField(f("repeated_sfixed64"), 210L);
   2877       message.addRepeatedField(f("repeated_float"   ), 211F);
   2878       message.addRepeatedField(f("repeated_double"  ), 212D);
   2879       message.addRepeatedField(f("repeated_bool"    ), true);
   2880       message.addRepeatedField(f("repeated_string"  ), "215");
   2881       message.addRepeatedField(f("repeated_bytes"   ), toBytes("216"));
   2882 
   2883       message.addRepeatedField(f("repeatedgroup"),
   2884         newBuilderForField(message, f("repeatedgroup"))
   2885                .setField(repeatedGroupA, 217).build());
   2886       message.addRepeatedField(f("repeated_nested_message"),
   2887         newBuilderForField(message, f("repeated_nested_message"))
   2888                .setField(nestedB, 218).build());
   2889       message.addRepeatedField(f("repeated_foreign_message"),
   2890         newBuilderForField(message, f("repeated_foreign_message"))
   2891                .setField(foreignC, 219).build());
   2892       message.addRepeatedField(f("repeated_import_message"),
   2893         newBuilderForField(message, f("repeated_import_message"))
   2894                .setField(importD, 220).build());
   2895 
   2896       message.addRepeatedField(f("repeated_nested_enum" ),  nestedBar);
   2897       message.addRepeatedField(f("repeated_foreign_enum"), foreignBar);
   2898       message.addRepeatedField(f("repeated_import_enum" ),  importBar);
   2899 
   2900       message.addRepeatedField(f("repeated_string_piece" ), "224");
   2901       message.addRepeatedField(f("repeated_cord" ), "225");
   2902 
   2903       // Add a second one of each field.
   2904       message.addRepeatedField(f("repeated_int32"   ), 301 );
   2905       message.addRepeatedField(f("repeated_int64"   ), 302L);
   2906       message.addRepeatedField(f("repeated_uint32"  ), 303 );
   2907       message.addRepeatedField(f("repeated_uint64"  ), 304L);
   2908       message.addRepeatedField(f("repeated_sint32"  ), 305 );
   2909       message.addRepeatedField(f("repeated_sint64"  ), 306L);
   2910       message.addRepeatedField(f("repeated_fixed32" ), 307 );
   2911       message.addRepeatedField(f("repeated_fixed64" ), 308L);
   2912       message.addRepeatedField(f("repeated_sfixed32"), 309 );
   2913       message.addRepeatedField(f("repeated_sfixed64"), 310L);
   2914       message.addRepeatedField(f("repeated_float"   ), 311F);
   2915       message.addRepeatedField(f("repeated_double"  ), 312D);
   2916       message.addRepeatedField(f("repeated_bool"    ), false);
   2917       message.addRepeatedField(f("repeated_string"  ), "315");
   2918       message.addRepeatedField(f("repeated_bytes"   ), toBytes("316"));
   2919 
   2920       message.addRepeatedField(f("repeatedgroup"),
   2921         newBuilderForField(message, f("repeatedgroup"))
   2922                .setField(repeatedGroupA, 317).build());
   2923       message.addRepeatedField(f("repeated_nested_message"),
   2924         newBuilderForField(message, f("repeated_nested_message"))
   2925                .setField(nestedB, 318).build());
   2926       message.addRepeatedField(f("repeated_foreign_message"),
   2927         newBuilderForField(message, f("repeated_foreign_message"))
   2928                .setField(foreignC, 319).build());
   2929       message.addRepeatedField(f("repeated_import_message"),
   2930         newBuilderForField(message, f("repeated_import_message"))
   2931                .setField(importD, 320).build());
   2932 
   2933       message.addRepeatedField(f("repeated_nested_enum" ),  nestedBaz);
   2934       message.addRepeatedField(f("repeated_foreign_enum"), foreignBaz);
   2935       message.addRepeatedField(f("repeated_import_enum" ),  importBaz);
   2936 
   2937       message.addRepeatedField(f("repeated_string_piece" ), "324");
   2938       message.addRepeatedField(f("repeated_cord" ), "325");
   2939 
   2940       // -----------------------------------------------------------------
   2941 
   2942       message.setField(f("default_int32"   ), 401 );
   2943       message.setField(f("default_int64"   ), 402L);
   2944       message.setField(f("default_uint32"  ), 403 );
   2945       message.setField(f("default_uint64"  ), 404L);
   2946       message.setField(f("default_sint32"  ), 405 );
   2947       message.setField(f("default_sint64"  ), 406L);
   2948       message.setField(f("default_fixed32" ), 407 );
   2949       message.setField(f("default_fixed64" ), 408L);
   2950       message.setField(f("default_sfixed32"), 409 );
   2951       message.setField(f("default_sfixed64"), 410L);
   2952       message.setField(f("default_float"   ), 411F);
   2953       message.setField(f("default_double"  ), 412D);
   2954       message.setField(f("default_bool"    ), false);
   2955       message.setField(f("default_string"  ), "415");
   2956       message.setField(f("default_bytes"   ), toBytes("416"));
   2957 
   2958       message.setField(f("default_nested_enum" ),  nestedFoo);
   2959       message.setField(f("default_foreign_enum"), foreignFoo);
   2960       message.setField(f("default_import_enum" ),  importFoo);
   2961 
   2962       message.setField(f("default_string_piece" ), "424");
   2963       message.setField(f("default_cord" ), "425");
   2964     }
   2965 
   2966     // -------------------------------------------------------------------
   2967 
   2968     /**
   2969      * Modify the repeated fields of {@code message} to contain the values
   2970      * expected by {@code assertRepeatedFieldsModified()}, using the
   2971      * {@link Message.Builder} reflection interface.
   2972      */
   2973     void modifyRepeatedFieldsViaReflection(Message.Builder message) {
   2974       message.setRepeatedField(f("repeated_int32"   ), 1, 501 );
   2975       message.setRepeatedField(f("repeated_int64"   ), 1, 502L);
   2976       message.setRepeatedField(f("repeated_uint32"  ), 1, 503 );
   2977       message.setRepeatedField(f("repeated_uint64"  ), 1, 504L);
   2978       message.setRepeatedField(f("repeated_sint32"  ), 1, 505 );
   2979       message.setRepeatedField(f("repeated_sint64"  ), 1, 506L);
   2980       message.setRepeatedField(f("repeated_fixed32" ), 1, 507 );
   2981       message.setRepeatedField(f("repeated_fixed64" ), 1, 508L);
   2982       message.setRepeatedField(f("repeated_sfixed32"), 1, 509 );
   2983       message.setRepeatedField(f("repeated_sfixed64"), 1, 510L);
   2984       message.setRepeatedField(f("repeated_float"   ), 1, 511F);
   2985       message.setRepeatedField(f("repeated_double"  ), 1, 512D);
   2986       message.setRepeatedField(f("repeated_bool"    ), 1, true);
   2987       message.setRepeatedField(f("repeated_string"  ), 1, "515");
   2988       message.setRepeatedField(f("repeated_bytes"   ), 1, toBytes("516"));
   2989 
   2990       message.setRepeatedField(f("repeatedgroup"), 1,
   2991         newBuilderForField(message, f("repeatedgroup"))
   2992                .setField(repeatedGroupA, 517).build());
   2993       message.setRepeatedField(f("repeated_nested_message"), 1,
   2994         newBuilderForField(message, f("repeated_nested_message"))
   2995                .setField(nestedB, 518).build());
   2996       message.setRepeatedField(f("repeated_foreign_message"), 1,
   2997         newBuilderForField(message, f("repeated_foreign_message"))
   2998                .setField(foreignC, 519).build());
   2999       message.setRepeatedField(f("repeated_import_message"), 1,
   3000         newBuilderForField(message, f("repeated_import_message"))
   3001                .setField(importD, 520).build());
   3002 
   3003       message.setRepeatedField(f("repeated_nested_enum" ), 1,  nestedFoo);
   3004       message.setRepeatedField(f("repeated_foreign_enum"), 1, foreignFoo);
   3005       message.setRepeatedField(f("repeated_import_enum" ), 1,  importFoo);
   3006 
   3007       message.setRepeatedField(f("repeated_string_piece"), 1, "524");
   3008       message.setRepeatedField(f("repeated_cord"), 1, "525");
   3009     }
   3010 
   3011     // -------------------------------------------------------------------
   3012 
   3013     /**
   3014      * Assert (using {@code junit.framework.Assert}} that all fields of
   3015      * {@code message} are set to the values assigned by {@code setAllFields},
   3016      * using the {@link Message} reflection interface.
   3017      */
   3018     public void assertAllFieldsSetViaReflection(Message message) {
   3019       Assert.assertTrue(message.hasField(f("optional_int32"   )));
   3020       Assert.assertTrue(message.hasField(f("optional_int64"   )));
   3021       Assert.assertTrue(message.hasField(f("optional_uint32"  )));
   3022       Assert.assertTrue(message.hasField(f("optional_uint64"  )));
   3023       Assert.assertTrue(message.hasField(f("optional_sint32"  )));
   3024       Assert.assertTrue(message.hasField(f("optional_sint64"  )));
   3025       Assert.assertTrue(message.hasField(f("optional_fixed32" )));
   3026       Assert.assertTrue(message.hasField(f("optional_fixed64" )));
   3027       Assert.assertTrue(message.hasField(f("optional_sfixed32")));
   3028       Assert.assertTrue(message.hasField(f("optional_sfixed64")));
   3029       Assert.assertTrue(message.hasField(f("optional_float"   )));
   3030       Assert.assertTrue(message.hasField(f("optional_double"  )));
   3031       Assert.assertTrue(message.hasField(f("optional_bool"    )));
   3032       Assert.assertTrue(message.hasField(f("optional_string"  )));
   3033       Assert.assertTrue(message.hasField(f("optional_bytes"   )));
   3034 
   3035       Assert.assertTrue(message.hasField(f("optionalgroup"           )));
   3036       Assert.assertTrue(message.hasField(f("optional_nested_message" )));
   3037       Assert.assertTrue(message.hasField(f("optional_foreign_message")));
   3038       Assert.assertTrue(message.hasField(f("optional_import_message" )));
   3039 
   3040       Assert.assertTrue(
   3041         ((Message)message.getField(f("optionalgroup"))).hasField(groupA));
   3042       Assert.assertTrue(
   3043         ((Message)message.getField(f("optional_nested_message")))
   3044                          .hasField(nestedB));
   3045       Assert.assertTrue(
   3046         ((Message)message.getField(f("optional_foreign_message")))
   3047                          .hasField(foreignC));
   3048       Assert.assertTrue(
   3049         ((Message)message.getField(f("optional_import_message")))
   3050                          .hasField(importD));
   3051 
   3052       Assert.assertTrue(message.hasField(f("optional_nested_enum" )));
   3053       Assert.assertTrue(message.hasField(f("optional_foreign_enum")));
   3054       Assert.assertTrue(message.hasField(f("optional_import_enum" )));
   3055 
   3056       Assert.assertTrue(message.hasField(f("optional_string_piece")));
   3057       Assert.assertTrue(message.hasField(f("optional_cord")));
   3058 
   3059       Assert.assertEquals(101  , message.getField(f("optional_int32"   )));
   3060       Assert.assertEquals(102L , message.getField(f("optional_int64"   )));
   3061       Assert.assertEquals(103  , message.getField(f("optional_uint32"  )));
   3062       Assert.assertEquals(104L , message.getField(f("optional_uint64"  )));
   3063       Assert.assertEquals(105  , message.getField(f("optional_sint32"  )));
   3064       Assert.assertEquals(106L , message.getField(f("optional_sint64"  )));
   3065       Assert.assertEquals(107  , message.getField(f("optional_fixed32" )));
   3066       Assert.assertEquals(108L , message.getField(f("optional_fixed64" )));
   3067       Assert.assertEquals(109  , message.getField(f("optional_sfixed32")));
   3068       Assert.assertEquals(110L , message.getField(f("optional_sfixed64")));
   3069       Assert.assertEquals(111F , message.getField(f("optional_float"   )));
   3070       Assert.assertEquals(112D , message.getField(f("optional_double"  )));
   3071       Assert.assertEquals(true , message.getField(f("optional_bool"    )));
   3072       Assert.assertEquals("115", message.getField(f("optional_string"  )));
   3073       Assert.assertEquals(toBytes("116"), message.getField(f("optional_bytes")));
   3074 
   3075       Assert.assertEquals(117,
   3076         ((Message)message.getField(f("optionalgroup"))).getField(groupA));
   3077       Assert.assertEquals(118,
   3078         ((Message)message.getField(f("optional_nested_message")))
   3079                          .getField(nestedB));
   3080       Assert.assertEquals(119,
   3081         ((Message)message.getField(f("optional_foreign_message")))
   3082                          .getField(foreignC));
   3083       Assert.assertEquals(120,
   3084         ((Message)message.getField(f("optional_import_message")))
   3085                          .getField(importD));
   3086 
   3087       Assert.assertEquals( nestedBaz, message.getField(f("optional_nested_enum" )));
   3088       Assert.assertEquals(foreignBaz, message.getField(f("optional_foreign_enum")));
   3089       Assert.assertEquals( importBaz, message.getField(f("optional_import_enum" )));
   3090 
   3091       Assert.assertEquals("124", message.getField(f("optional_string_piece")));
   3092       Assert.assertEquals("125", message.getField(f("optional_cord")));
   3093 
   3094       // -----------------------------------------------------------------
   3095 
   3096       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int32"   )));
   3097       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int64"   )));
   3098       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint32"  )));
   3099       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint64"  )));
   3100       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint32"  )));
   3101       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint64"  )));
   3102       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed32" )));
   3103       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed64" )));
   3104       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed32")));
   3105       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed64")));
   3106       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_float"   )));
   3107       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_double"  )));
   3108       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bool"    )));
   3109       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string"  )));
   3110       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bytes"   )));
   3111 
   3112       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeatedgroup"           )));
   3113       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_message" )));
   3114       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_message")));
   3115       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_message" )));
   3116       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_enum"    )));
   3117       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_enum"   )));
   3118       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_enum"    )));
   3119 
   3120       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string_piece")));
   3121       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_cord")));
   3122 
   3123       Assert.assertEquals(201  , message.getRepeatedField(f("repeated_int32"   ), 0));
   3124       Assert.assertEquals(202L , message.getRepeatedField(f("repeated_int64"   ), 0));
   3125       Assert.assertEquals(203  , message.getRepeatedField(f("repeated_uint32"  ), 0));
   3126       Assert.assertEquals(204L , message.getRepeatedField(f("repeated_uint64"  ), 0));
   3127       Assert.assertEquals(205  , message.getRepeatedField(f("repeated_sint32"  ), 0));
   3128       Assert.assertEquals(206L , message.getRepeatedField(f("repeated_sint64"  ), 0));
   3129       Assert.assertEquals(207  , message.getRepeatedField(f("repeated_fixed32" ), 0));
   3130       Assert.assertEquals(208L , message.getRepeatedField(f("repeated_fixed64" ), 0));
   3131       Assert.assertEquals(209  , message.getRepeatedField(f("repeated_sfixed32"), 0));
   3132       Assert.assertEquals(210L , message.getRepeatedField(f("repeated_sfixed64"), 0));
   3133       Assert.assertEquals(211F , message.getRepeatedField(f("repeated_float"   ), 0));
   3134       Assert.assertEquals(212D , message.getRepeatedField(f("repeated_double"  ), 0));
   3135       Assert.assertEquals(true , message.getRepeatedField(f("repeated_bool"    ), 0));
   3136       Assert.assertEquals("215", message.getRepeatedField(f("repeated_string"  ), 0));
   3137       Assert.assertEquals(toBytes("216"), message.getRepeatedField(f("repeated_bytes"), 0));
   3138 
   3139       Assert.assertEquals(217,
   3140         ((Message)message.getRepeatedField(f("repeatedgroup"), 0))
   3141                          .getField(repeatedGroupA));
   3142       Assert.assertEquals(218,
   3143         ((Message)message.getRepeatedField(f("repeated_nested_message"), 0))
   3144                          .getField(nestedB));
   3145       Assert.assertEquals(219,
   3146         ((Message)message.getRepeatedField(f("repeated_foreign_message"), 0))
   3147                          .getField(foreignC));
   3148       Assert.assertEquals(220,
   3149         ((Message)message.getRepeatedField(f("repeated_import_message"), 0))
   3150                          .getField(importD));
   3151 
   3152       Assert.assertEquals( nestedBar, message.getRepeatedField(f("repeated_nested_enum" ),0));
   3153       Assert.assertEquals(foreignBar, message.getRepeatedField(f("repeated_foreign_enum"),0));
   3154       Assert.assertEquals( importBar, message.getRepeatedField(f("repeated_import_enum" ),0));
   3155 
   3156       Assert.assertEquals("224", message.getRepeatedField(f("repeated_string_piece"), 0));
   3157       Assert.assertEquals("225", message.getRepeatedField(f("repeated_cord"), 0));
   3158 
   3159       Assert.assertEquals(301  , message.getRepeatedField(f("repeated_int32"   ), 1));
   3160       Assert.assertEquals(302L , message.getRepeatedField(f("repeated_int64"   ), 1));
   3161       Assert.assertEquals(303  , message.getRepeatedField(f("repeated_uint32"  ), 1));
   3162       Assert.assertEquals(304L , message.getRepeatedField(f("repeated_uint64"  ), 1));
   3163       Assert.assertEquals(305  , message.getRepeatedField(f("repeated_sint32"  ), 1));
   3164       Assert.assertEquals(306L , message.getRepeatedField(f("repeated_sint64"  ), 1));
   3165       Assert.assertEquals(307  , message.getRepeatedField(f("repeated_fixed32" ), 1));
   3166       Assert.assertEquals(308L , message.getRepeatedField(f("repeated_fixed64" ), 1));
   3167       Assert.assertEquals(309  , message.getRepeatedField(f("repeated_sfixed32"), 1));
   3168       Assert.assertEquals(310L , message.getRepeatedField(f("repeated_sfixed64"), 1));
   3169       Assert.assertEquals(311F , message.getRepeatedField(f("repeated_float"   ), 1));
   3170       Assert.assertEquals(312D , message.getRepeatedField(f("repeated_double"  ), 1));
   3171       Assert.assertEquals(false, message.getRepeatedField(f("repeated_bool"    ), 1));
   3172       Assert.assertEquals("315", message.getRepeatedField(f("repeated_string"  ), 1));
   3173       Assert.assertEquals(toBytes("316"), message.getRepeatedField(f("repeated_bytes"), 1));
   3174 
   3175       Assert.assertEquals(317,
   3176         ((Message)message.getRepeatedField(f("repeatedgroup"), 1))
   3177                          .getField(repeatedGroupA));
   3178       Assert.assertEquals(318,
   3179         ((Message)message.getRepeatedField(f("repeated_nested_message"), 1))
   3180                          .getField(nestedB));
   3181       Assert.assertEquals(319,
   3182         ((Message)message.getRepeatedField(f("repeated_foreign_message"), 1))
   3183                          .getField(foreignC));
   3184       Assert.assertEquals(320,
   3185         ((Message)message.getRepeatedField(f("repeated_import_message"), 1))
   3186                          .getField(importD));
   3187 
   3188       Assert.assertEquals( nestedBaz, message.getRepeatedField(f("repeated_nested_enum" ),1));
   3189       Assert.assertEquals(foreignBaz, message.getRepeatedField(f("repeated_foreign_enum"),1));
   3190       Assert.assertEquals( importBaz, message.getRepeatedField(f("repeated_import_enum" ),1));
   3191 
   3192       Assert.assertEquals("324", message.getRepeatedField(f("repeated_string_piece"), 1));
   3193       Assert.assertEquals("325", message.getRepeatedField(f("repeated_cord"), 1));
   3194 
   3195       // -----------------------------------------------------------------
   3196 
   3197       Assert.assertTrue(message.hasField(f("default_int32"   )));
   3198       Assert.assertTrue(message.hasField(f("default_int64"   )));
   3199       Assert.assertTrue(message.hasField(f("default_uint32"  )));
   3200       Assert.assertTrue(message.hasField(f("default_uint64"  )));
   3201       Assert.assertTrue(message.hasField(f("default_sint32"  )));
   3202       Assert.assertTrue(message.hasField(f("default_sint64"  )));
   3203       Assert.assertTrue(message.hasField(f("default_fixed32" )));
   3204       Assert.assertTrue(message.hasField(f("default_fixed64" )));
   3205       Assert.assertTrue(message.hasField(f("default_sfixed32")));
   3206       Assert.assertTrue(message.hasField(f("default_sfixed64")));
   3207       Assert.assertTrue(message.hasField(f("default_float"   )));
   3208       Assert.assertTrue(message.hasField(f("default_double"  )));
   3209       Assert.assertTrue(message.hasField(f("default_bool"    )));
   3210       Assert.assertTrue(message.hasField(f("default_string"  )));
   3211       Assert.assertTrue(message.hasField(f("default_bytes"   )));
   3212 
   3213       Assert.assertTrue(message.hasField(f("default_nested_enum" )));
   3214       Assert.assertTrue(message.hasField(f("default_foreign_enum")));
   3215       Assert.assertTrue(message.hasField(f("default_import_enum" )));
   3216 
   3217       Assert.assertTrue(message.hasField(f("default_string_piece")));
   3218       Assert.assertTrue(message.hasField(f("default_cord")));
   3219 
   3220       Assert.assertEquals(401  , message.getField(f("default_int32"   )));
   3221       Assert.assertEquals(402L , message.getField(f("default_int64"   )));
   3222       Assert.assertEquals(403  , message.getField(f("default_uint32"  )));
   3223       Assert.assertEquals(404L , message.getField(f("default_uint64"  )));
   3224       Assert.assertEquals(405  , message.getField(f("default_sint32"  )));
   3225       Assert.assertEquals(406L , message.getField(f("default_sint64"  )));
   3226       Assert.assertEquals(407  , message.getField(f("default_fixed32" )));
   3227       Assert.assertEquals(408L , message.getField(f("default_fixed64" )));
   3228       Assert.assertEquals(409  , message.getField(f("default_sfixed32")));
   3229       Assert.assertEquals(410L , message.getField(f("default_sfixed64")));
   3230       Assert.assertEquals(411F , message.getField(f("default_float"   )));
   3231       Assert.assertEquals(412D , message.getField(f("default_double"  )));
   3232       Assert.assertEquals(false, message.getField(f("default_bool"    )));
   3233       Assert.assertEquals("415", message.getField(f("default_string"  )));
   3234       Assert.assertEquals(toBytes("416"), message.getField(f("default_bytes")));
   3235 
   3236       Assert.assertEquals( nestedFoo, message.getField(f("default_nested_enum" )));
   3237       Assert.assertEquals(foreignFoo, message.getField(f("default_foreign_enum")));
   3238       Assert.assertEquals( importFoo, message.getField(f("default_import_enum" )));
   3239 
   3240       Assert.assertEquals("424", message.getField(f("default_string_piece")));
   3241       Assert.assertEquals("425", message.getField(f("default_cord")));
   3242     }
   3243 
   3244     // -------------------------------------------------------------------
   3245 
   3246     /**
   3247      * Assert (using {@code junit.framework.Assert}} that all fields of
   3248      * {@code message} are cleared, and that getting the fields returns their
   3249      * default values, using the {@link Message} reflection interface.
   3250      */
   3251     public void assertClearViaReflection(Message message) {
   3252       // has_blah() should initially be false for all optional fields.
   3253       Assert.assertFalse(message.hasField(f("optional_int32"   )));
   3254       Assert.assertFalse(message.hasField(f("optional_int64"   )));
   3255       Assert.assertFalse(message.hasField(f("optional_uint32"  )));
   3256       Assert.assertFalse(message.hasField(f("optional_uint64"  )));
   3257       Assert.assertFalse(message.hasField(f("optional_sint32"  )));
   3258       Assert.assertFalse(message.hasField(f("optional_sint64"  )));
   3259       Assert.assertFalse(message.hasField(f("optional_fixed32" )));
   3260       Assert.assertFalse(message.hasField(f("optional_fixed64" )));
   3261       Assert.assertFalse(message.hasField(f("optional_sfixed32")));
   3262       Assert.assertFalse(message.hasField(f("optional_sfixed64")));
   3263       Assert.assertFalse(message.hasField(f("optional_float"   )));
   3264       Assert.assertFalse(message.hasField(f("optional_double"  )));
   3265       Assert.assertFalse(message.hasField(f("optional_bool"    )));
   3266       Assert.assertFalse(message.hasField(f("optional_string"  )));
   3267       Assert.assertFalse(message.hasField(f("optional_bytes"   )));
   3268 
   3269       Assert.assertFalse(message.hasField(f("optionalgroup"           )));
   3270       Assert.assertFalse(message.hasField(f("optional_nested_message" )));
   3271       Assert.assertFalse(message.hasField(f("optional_foreign_message")));
   3272       Assert.assertFalse(message.hasField(f("optional_import_message" )));
   3273 
   3274       Assert.assertFalse(message.hasField(f("optional_nested_enum" )));
   3275       Assert.assertFalse(message.hasField(f("optional_foreign_enum")));
   3276       Assert.assertFalse(message.hasField(f("optional_import_enum" )));
   3277 
   3278       Assert.assertFalse(message.hasField(f("optional_string_piece")));
   3279       Assert.assertFalse(message.hasField(f("optional_cord")));
   3280 
   3281       // Optional fields without defaults are set to zero or something like it.
   3282       Assert.assertEquals(0    , message.getField(f("optional_int32"   )));
   3283       Assert.assertEquals(0L   , message.getField(f("optional_int64"   )));
   3284       Assert.assertEquals(0    , message.getField(f("optional_uint32"  )));
   3285       Assert.assertEquals(0L   , message.getField(f("optional_uint64"  )));
   3286       Assert.assertEquals(0    , message.getField(f("optional_sint32"  )));
   3287       Assert.assertEquals(0L   , message.getField(f("optional_sint64"  )));
   3288       Assert.assertEquals(0    , message.getField(f("optional_fixed32" )));
   3289       Assert.assertEquals(0L   , message.getField(f("optional_fixed64" )));
   3290       Assert.assertEquals(0    , message.getField(f("optional_sfixed32")));
   3291       Assert.assertEquals(0L   , message.getField(f("optional_sfixed64")));
   3292       Assert.assertEquals(0F   , message.getField(f("optional_float"   )));
   3293       Assert.assertEquals(0D   , message.getField(f("optional_double"  )));
   3294       Assert.assertEquals(false, message.getField(f("optional_bool"    )));
   3295       Assert.assertEquals(""   , message.getField(f("optional_string"  )));
   3296       Assert.assertEquals(ByteString.EMPTY, message.getField(f("optional_bytes")));
   3297 
   3298       // Embedded messages should also be clear.
   3299       Assert.assertFalse(
   3300         ((Message)message.getField(f("optionalgroup"))).hasField(groupA));
   3301       Assert.assertFalse(
   3302         ((Message)message.getField(f("optional_nested_message")))
   3303                          .hasField(nestedB));
   3304       Assert.assertFalse(
   3305         ((Message)message.getField(f("optional_foreign_message")))
   3306                          .hasField(foreignC));
   3307       Assert.assertFalse(
   3308         ((Message)message.getField(f("optional_import_message")))
   3309                          .hasField(importD));
   3310 
   3311       Assert.assertEquals(0,
   3312         ((Message)message.getField(f("optionalgroup"))).getField(groupA));
   3313       Assert.assertEquals(0,
   3314         ((Message)message.getField(f("optional_nested_message")))
   3315                          .getField(nestedB));
   3316       Assert.assertEquals(0,
   3317         ((Message)message.getField(f("optional_foreign_message")))
   3318                          .getField(foreignC));
   3319       Assert.assertEquals(0,
   3320         ((Message)message.getField(f("optional_import_message")))
   3321                          .getField(importD));
   3322 
   3323       // Enums without defaults are set to the first value in the enum.
   3324       Assert.assertEquals( nestedFoo, message.getField(f("optional_nested_enum" )));
   3325       Assert.assertEquals(foreignFoo, message.getField(f("optional_foreign_enum")));
   3326       Assert.assertEquals( importFoo, message.getField(f("optional_import_enum" )));
   3327 
   3328       Assert.assertEquals("", message.getField(f("optional_string_piece")));
   3329       Assert.assertEquals("", message.getField(f("optional_cord")));
   3330 
   3331       // Repeated fields are empty.
   3332       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_int32"   )));
   3333       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_int64"   )));
   3334       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_uint32"  )));
   3335       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_uint64"  )));
   3336       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sint32"  )));
   3337       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sint64"  )));
   3338       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_fixed32" )));
   3339       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_fixed64" )));
   3340       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sfixed32")));
   3341       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_sfixed64")));
   3342       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_float"   )));
   3343       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_double"  )));
   3344       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_bool"    )));
   3345       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_string"  )));
   3346       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_bytes"   )));
   3347 
   3348       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeatedgroup"           )));
   3349       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_nested_message" )));
   3350       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_foreign_message")));
   3351       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_import_message" )));
   3352       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_nested_enum"    )));
   3353       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_foreign_enum"   )));
   3354       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_import_enum"    )));
   3355 
   3356       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_string_piece")));
   3357       Assert.assertEquals(0, message.getRepeatedFieldCount(f("repeated_cord")));
   3358 
   3359       // has_blah() should also be false for all default fields.
   3360       Assert.assertFalse(message.hasField(f("default_int32"   )));
   3361       Assert.assertFalse(message.hasField(f("default_int64"   )));
   3362       Assert.assertFalse(message.hasField(f("default_uint32"  )));
   3363       Assert.assertFalse(message.hasField(f("default_uint64"  )));
   3364       Assert.assertFalse(message.hasField(f("default_sint32"  )));
   3365       Assert.assertFalse(message.hasField(f("default_sint64"  )));
   3366       Assert.assertFalse(message.hasField(f("default_fixed32" )));
   3367       Assert.assertFalse(message.hasField(f("default_fixed64" )));
   3368       Assert.assertFalse(message.hasField(f("default_sfixed32")));
   3369       Assert.assertFalse(message.hasField(f("default_sfixed64")));
   3370       Assert.assertFalse(message.hasField(f("default_float"   )));
   3371       Assert.assertFalse(message.hasField(f("default_double"  )));
   3372       Assert.assertFalse(message.hasField(f("default_bool"    )));
   3373       Assert.assertFalse(message.hasField(f("default_string"  )));
   3374       Assert.assertFalse(message.hasField(f("default_bytes"   )));
   3375 
   3376       Assert.assertFalse(message.hasField(f("default_nested_enum" )));
   3377       Assert.assertFalse(message.hasField(f("default_foreign_enum")));
   3378       Assert.assertFalse(message.hasField(f("default_import_enum" )));
   3379 
   3380       Assert.assertFalse(message.hasField(f("default_string_piece" )));
   3381       Assert.assertFalse(message.hasField(f("default_cord" )));
   3382 
   3383       // Fields with defaults have their default values (duh).
   3384       Assert.assertEquals( 41    , message.getField(f("default_int32"   )));
   3385       Assert.assertEquals( 42L   , message.getField(f("default_int64"   )));
   3386       Assert.assertEquals( 43    , message.getField(f("default_uint32"  )));
   3387       Assert.assertEquals( 44L   , message.getField(f("default_uint64"  )));
   3388       Assert.assertEquals(-45    , message.getField(f("default_sint32"  )));
   3389       Assert.assertEquals( 46L   , message.getField(f("default_sint64"  )));
   3390       Assert.assertEquals( 47    , message.getField(f("default_fixed32" )));
   3391       Assert.assertEquals( 48L   , message.getField(f("default_fixed64" )));
   3392       Assert.assertEquals( 49    , message.getField(f("default_sfixed32")));
   3393       Assert.assertEquals(-50L   , message.getField(f("default_sfixed64")));
   3394       Assert.assertEquals( 51.5F , message.getField(f("default_float"   )));
   3395       Assert.assertEquals( 52e3D , message.getField(f("default_double"  )));
   3396       Assert.assertEquals(true   , message.getField(f("default_bool"    )));
   3397       Assert.assertEquals("hello", message.getField(f("default_string"  )));
   3398       Assert.assertEquals(toBytes("world"), message.getField(f("default_bytes")));
   3399 
   3400       Assert.assertEquals( nestedBar, message.getField(f("default_nested_enum" )));
   3401       Assert.assertEquals(foreignBar, message.getField(f("default_foreign_enum")));
   3402       Assert.assertEquals( importBar, message.getField(f("default_import_enum" )));
   3403 
   3404       Assert.assertEquals("abc", message.getField(f("default_string_piece")));
   3405       Assert.assertEquals("123", message.getField(f("default_cord")));
   3406     }
   3407 
   3408     // ---------------------------------------------------------------
   3409 
   3410     public void assertRepeatedFieldsModifiedViaReflection(Message message) {
   3411       // ModifyRepeatedFields only sets the second repeated element of each
   3412       // field.  In addition to verifying this, we also verify that the first
   3413       // element and size were *not* modified.
   3414       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int32"   )));
   3415       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_int64"   )));
   3416       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint32"  )));
   3417       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_uint64"  )));
   3418       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint32"  )));
   3419       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sint64"  )));
   3420       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed32" )));
   3421       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_fixed64" )));
   3422       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed32")));
   3423       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_sfixed64")));
   3424       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_float"   )));
   3425       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_double"  )));
   3426       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bool"    )));
   3427       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string"  )));
   3428       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_bytes"   )));
   3429 
   3430       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeatedgroup"           )));
   3431       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_message" )));
   3432       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_message")));
   3433       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_message" )));
   3434       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_nested_enum"    )));
   3435       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_foreign_enum"   )));
   3436       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_import_enum"    )));
   3437 
   3438       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_string_piece")));
   3439       Assert.assertEquals(2, message.getRepeatedFieldCount(f("repeated_cord")));
   3440 
   3441       Assert.assertEquals(201  , message.getRepeatedField(f("repeated_int32"   ), 0));
   3442       Assert.assertEquals(202L , message.getRepeatedField(f("repeated_int64"   ), 0));
   3443       Assert.assertEquals(203  , message.getRepeatedField(f("repeated_uint32"  ), 0));
   3444       Assert.assertEquals(204L , message.getRepeatedField(f("repeated_uint64"  ), 0));
   3445       Assert.assertEquals(205  , message.getRepeatedField(f("repeated_sint32"  ), 0));
   3446       Assert.assertEquals(206L , message.getRepeatedField(f("repeated_sint64"  ), 0));
   3447       Assert.assertEquals(207  , message.getRepeatedField(f("repeated_fixed32" ), 0));
   3448       Assert.assertEquals(208L , message.getRepeatedField(f("repeated_fixed64" ), 0));
   3449       Assert.assertEquals(209  , message.getRepeatedField(f("repeated_sfixed32"), 0));
   3450       Assert.assertEquals(210L , message.getRepeatedField(f("repeated_sfixed64"), 0));
   3451       Assert.assertEquals(211F , message.getRepeatedField(f("repeated_float"   ), 0));
   3452       Assert.assertEquals(212D , message.getRepeatedField(f("repeated_double"  ), 0));
   3453       Assert.assertEquals(true , message.getRepeatedField(f("repeated_bool"    ), 0));
   3454       Assert.assertEquals("215", message.getRepeatedField(f("repeated_string"  ), 0));
   3455       Assert.assertEquals(toBytes("216"), message.getRepeatedField(f("repeated_bytes"), 0));
   3456 
   3457       Assert.assertEquals(217,
   3458         ((Message)message.getRepeatedField(f("repeatedgroup"), 0))
   3459                          .getField(repeatedGroupA));
   3460       Assert.assertEquals(218,
   3461         ((Message)message.getRepeatedField(f("repeated_nested_message"), 0))
   3462                          .getField(nestedB));
   3463       Assert.assertEquals(219,
   3464         ((Message)message.getRepeatedField(f("repeated_foreign_message"), 0))
   3465                          .getField(foreignC));
   3466       Assert.assertEquals(220,
   3467         ((Message)message.getRepeatedField(f("repeated_import_message"), 0))
   3468                          .getField(importD));
   3469 
   3470       Assert.assertEquals( nestedBar, message.getRepeatedField(f("repeated_nested_enum" ),0));
   3471       Assert.assertEquals(foreignBar, message.getRepeatedField(f("repeated_foreign_enum"),0));
   3472       Assert.assertEquals( importBar, message.getRepeatedField(f("repeated_import_enum" ),0));
   3473 
   3474       Assert.assertEquals("224", message.getRepeatedField(f("repeated_string_piece"), 0));
   3475       Assert.assertEquals("225", message.getRepeatedField(f("repeated_cord"), 0));
   3476 
   3477       Assert.assertEquals(501  , message.getRepeatedField(f("repeated_int32"   ), 1));
   3478       Assert.assertEquals(502L , message.getRepeatedField(f("repeated_int64"   ), 1));
   3479       Assert.assertEquals(503  , message.getRepeatedField(f("repeated_uint32"  ), 1));
   3480       Assert.assertEquals(504L , message.getRepeatedField(f("repeated_uint64"  ), 1));
   3481       Assert.assertEquals(505  , message.getRepeatedField(f("repeated_sint32"  ), 1));
   3482       Assert.assertEquals(506L , message.getRepeatedField(f("repeated_sint64"  ), 1));
   3483       Assert.assertEquals(507  , message.getRepeatedField(f("repeated_fixed32" ), 1));
   3484       Assert.assertEquals(508L , message.getRepeatedField(f("repeated_fixed64" ), 1));
   3485       Assert.assertEquals(509  , message.getRepeatedField(f("repeated_sfixed32"), 1));
   3486       Assert.assertEquals(510L , message.getRepeatedField(f("repeated_sfixed64"), 1));
   3487       Assert.assertEquals(511F , message.getRepeatedField(f("repeated_float"   ), 1));
   3488       Assert.assertEquals(512D , message.getRepeatedField(f("repeated_double"  ), 1));
   3489       Assert.assertEquals(true , message.getRepeatedField(f("repeated_bool"    ), 1));
   3490       Assert.assertEquals("515", message.getRepeatedField(f("repeated_string"  ), 1));
   3491       Assert.assertEquals(toBytes("516"), message.getRepeatedField(f("repeated_bytes"), 1));
   3492 
   3493       Assert.assertEquals(517,
   3494         ((Message)message.getRepeatedField(f("repeatedgroup"), 1))
   3495                          .getField(repeatedGroupA));
   3496       Assert.assertEquals(518,
   3497         ((Message)message.getRepeatedField(f("repeated_nested_message"), 1))
   3498                          .getField(nestedB));
   3499       Assert.assertEquals(519,
   3500         ((Message)message.getRepeatedField(f("repeated_foreign_message"), 1))
   3501                          .getField(foreignC));
   3502       Assert.assertEquals(520,
   3503         ((Message)message.getRepeatedField(f("repeated_import_message"), 1))
   3504                          .getField(importD));
   3505 
   3506       Assert.assertEquals( nestedFoo, message.getRepeatedField(f("repeated_nested_enum" ),1));
   3507       Assert.assertEquals(foreignFoo, message.getRepeatedField(f("repeated_foreign_enum"),1));
   3508       Assert.assertEquals( importFoo, message.getRepeatedField(f("repeated_import_enum" ),1));
   3509 
   3510       Assert.assertEquals("524", message.getRepeatedField(f("repeated_string_piece"), 1));
   3511       Assert.assertEquals("525", message.getRepeatedField(f("repeated_cord"), 1));
   3512     }
   3513 
   3514     public void setPackedFieldsViaReflection(Message.Builder message) {
   3515       message.addRepeatedField(f("packed_int32"   ), 601 );
   3516       message.addRepeatedField(f("packed_int64"   ), 602L);
   3517       message.addRepeatedField(f("packed_uint32"  ), 603 );
   3518       message.addRepeatedField(f("packed_uint64"  ), 604L);
   3519       message.addRepeatedField(f("packed_sint32"  ), 605 );
   3520       message.addRepeatedField(f("packed_sint64"  ), 606L);
   3521       message.addRepeatedField(f("packed_fixed32" ), 607 );
   3522       message.addRepeatedField(f("packed_fixed64" ), 608L);
   3523       message.addRepeatedField(f("packed_sfixed32"), 609 );
   3524       message.addRepeatedField(f("packed_sfixed64"), 610L);
   3525       message.addRepeatedField(f("packed_float"   ), 611F);
   3526       message.addRepeatedField(f("packed_double"  ), 612D);
   3527       message.addRepeatedField(f("packed_bool"    ), true);
   3528       message.addRepeatedField(f("packed_enum" ),  foreignBar);
   3529       // Add a second one of each field.
   3530       message.addRepeatedField(f("packed_int32"   ), 701 );
   3531       message.addRepeatedField(f("packed_int64"   ), 702L);
   3532       message.addRepeatedField(f("packed_uint32"  ), 703 );
   3533       message.addRepeatedField(f("packed_uint64"  ), 704L);
   3534       message.addRepeatedField(f("packed_sint32"  ), 705 );
   3535       message.addRepeatedField(f("packed_sint64"  ), 706L);
   3536       message.addRepeatedField(f("packed_fixed32" ), 707 );
   3537       message.addRepeatedField(f("packed_fixed64" ), 708L);
   3538       message.addRepeatedField(f("packed_sfixed32"), 709 );
   3539       message.addRepeatedField(f("packed_sfixed64"), 710L);
   3540       message.addRepeatedField(f("packed_float"   ), 711F);
   3541       message.addRepeatedField(f("packed_double"  ), 712D);
   3542       message.addRepeatedField(f("packed_bool"    ), false);
   3543       message.addRepeatedField(f("packed_enum" ),  foreignBaz);
   3544     }
   3545 
   3546     public void assertPackedFieldsSetViaReflection(Message message) {
   3547       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int32"   )));
   3548       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int64"   )));
   3549       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint32"  )));
   3550       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint64"  )));
   3551       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sint32"  )));
   3552       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sint64"  )));
   3553       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_fixed32" )));
   3554       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_fixed64" )));
   3555       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sfixed32")));
   3556       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_sfixed64")));
   3557       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_float"   )));
   3558       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_double"  )));
   3559       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_bool"    )));
   3560       Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_enum"   )));
   3561       Assert.assertEquals(601  , message.getRepeatedField(f("packed_int32"   ), 0));
   3562       Assert.assertEquals(602L , message.getRepeatedField(f("packed_int64"   ), 0));
   3563       Assert.assertEquals(603  , message.getRepeatedField(f("packed_uint32"  ), 0));
   3564       Assert.assertEquals(604L , message.getRepeatedField(f("packed_uint64"  ), 0));
   3565       Assert.assertEquals(605  , message.getRepeatedField(f("packed_sint32"  ), 0));
   3566       Assert.assertEquals(606L , message.getRepeatedField(f("packed_sint64"  ), 0));
   3567       Assert.assertEquals(607  , message.getRepeatedField(f("packed_fixed32" ), 0));
   3568       Assert.assertEquals(608L , message.getRepeatedField(f("packed_fixed64" ), 0));
   3569       Assert.assertEquals(609  , message.getRepeatedField(f("packed_sfixed32"), 0));
   3570       Assert.assertEquals(610L , message.getRepeatedField(f("packed_sfixed64"), 0));
   3571       Assert.assertEquals(611F , message.getRepeatedField(f("packed_float"   ), 0));
   3572       Assert.assertEquals(612D , message.getRepeatedField(f("packed_double"  ), 0));
   3573       Assert.assertEquals(true , message.getRepeatedField(f("packed_bool"    ), 0));
   3574       Assert.assertEquals(foreignBar, message.getRepeatedField(f("packed_enum" ),0));
   3575       Assert.assertEquals(701  , message.getRepeatedField(f("packed_int32"   ), 1));
   3576       Assert.assertEquals(702L , message.getRepeatedField(f("packed_int64"   ), 1));
   3577       Assert.assertEquals(703  , message.getRepeatedField(f("packed_uint32"  ), 1));
   3578       Assert.assertEquals(704L , message.getRepeatedField(f("packed_uint64"  ), 1));
   3579       Assert.assertEquals(705  , message.getRepeatedField(f("packed_sint32"  ), 1));
   3580       Assert.assertEquals(706L , message.getRepeatedField(f("packed_sint64"  ), 1));
   3581       Assert.assertEquals(707  , message.getRepeatedField(f("packed_fixed32" ), 1));
   3582       Assert.assertEquals(708L , message.getRepeatedField(f("packed_fixed64" ), 1));
   3583       Assert.assertEquals(709  , message.getRepeatedField(f("packed_sfixed32"), 1));
   3584       Assert.assertEquals(710L , message.getRepeatedField(f("packed_sfixed64"), 1));
   3585       Assert.assertEquals(711F , message.getRepeatedField(f("packed_float"   ), 1));
   3586       Assert.assertEquals(712D , message.getRepeatedField(f("packed_double"  ), 1));
   3587       Assert.assertEquals(false, message.getRepeatedField(f("packed_bool"    ), 1));
   3588       Assert.assertEquals(foreignBaz, message.getRepeatedField(f("packed_enum" ),1));
   3589     }
   3590 
   3591     /**
   3592      * Verifies that the reflection setters for the given.Builder object throw a
   3593      * NullPointerException if they are passed a null value.  Uses Assert to throw an
   3594      * appropriate assertion failure, if the condition is not verified.
   3595      */
   3596     public void assertReflectionSettersRejectNull(Message.Builder builder)
   3597         throws Exception {
   3598       try {
   3599         builder.setField(f("optional_string"), null);
   3600         Assert.fail("Exception was not thrown");
   3601       } catch (NullPointerException e) {
   3602         // We expect this exception.
   3603       }
   3604       try {
   3605         builder.setField(f("optional_bytes"), null);
   3606         Assert.fail("Exception was not thrown");
   3607       } catch (NullPointerException e) {
   3608         // We expect this exception.
   3609       }
   3610       try {
   3611         builder.setField(f("optional_nested_enum"), null);
   3612         Assert.fail("Exception was not thrown");
   3613       } catch (NullPointerException e) {
   3614         // We expect this exception.
   3615       }
   3616       try {
   3617         builder.setField(f("optional_nested_message"),
   3618                          (TestAllTypes.NestedMessage) null);
   3619         Assert.fail("Exception was not thrown");
   3620       } catch (NullPointerException e) {
   3621         // We expect this exception.
   3622       }
   3623       try {
   3624         builder.setField(f("optional_nested_message"),
   3625                          (TestAllTypes.NestedMessage.Builder) null);
   3626         Assert.fail("Exception was not thrown");
   3627       } catch (NullPointerException e) {
   3628         // We expect this exception.
   3629       }
   3630 
   3631       try {
   3632         builder.addRepeatedField(f("repeated_string"), null);
   3633         Assert.fail("Exception was not thrown");
   3634       } catch (NullPointerException e) {
   3635         // We expect this exception.
   3636       }
   3637       try {
   3638         builder.addRepeatedField(f("repeated_bytes"), null);
   3639         Assert.fail("Exception was not thrown");
   3640       } catch (NullPointerException e) {
   3641         // We expect this exception.
   3642       }
   3643       try {
   3644         builder.addRepeatedField(f("repeated_nested_enum"), null);
   3645         Assert.fail("Exception was not thrown");
   3646       } catch (NullPointerException e) {
   3647         // We expect this exception.
   3648       }
   3649       try {
   3650         builder.addRepeatedField(f("repeated_nested_message"), null);
   3651         Assert.fail("Exception was not thrown");
   3652       } catch (NullPointerException e) {
   3653         // We expect this exception.
   3654       }
   3655     }
   3656 
   3657     /**
   3658      * Verifies that the reflection repeated setters for the given Builder object throw a
   3659      * NullPointerException if they are passed a null value.  Uses Assert to throw an appropriate
   3660      * assertion failure, if the condition is not verified.
   3661      */
   3662     public void assertReflectionRepeatedSettersRejectNull(Message.Builder builder)
   3663         throws Exception {
   3664       builder.addRepeatedField(f("repeated_string"), "one");
   3665       try {
   3666         builder.setRepeatedField(f("repeated_string"), 0, null);
   3667         Assert.fail("Exception was not thrown");
   3668       } catch (NullPointerException e) {
   3669         // We expect this exception.
   3670       }
   3671 
   3672       builder.addRepeatedField(f("repeated_bytes"), toBytes("one"));
   3673       try {
   3674         builder.setRepeatedField(f("repeated_bytes"), 0, null);
   3675         Assert.fail("Exception was not thrown");
   3676       } catch (NullPointerException e) {
   3677         // We expect this exception.
   3678       }
   3679 
   3680       builder.addRepeatedField(f("repeated_nested_enum"), nestedBaz);
   3681       try {
   3682         builder.setRepeatedField(f("repeated_nested_enum"), 0, null);
   3683         Assert.fail("Exception was not thrown");
   3684       } catch (NullPointerException e) {
   3685         // We expect this exception.
   3686       }
   3687 
   3688       builder.addRepeatedField(
   3689           f("repeated_nested_message"),
   3690           TestAllTypes.NestedMessage.newBuilder().setBb(218).build());
   3691       try {
   3692         builder.setRepeatedField(f("repeated_nested_message"), 0, null);
   3693         Assert.fail("Exception was not thrown");
   3694       } catch (NullPointerException e) {
   3695         // We expect this exception.
   3696       }
   3697     }
   3698   }
   3699 
   3700   /**
   3701    * @param filePath The path relative to
   3702    * {@link com.google.testing.util.TestUtil#getDefaultSrcDir}.
   3703    */
   3704   public static String readTextFromFile(String filePath) {
   3705     return readBytesFromFile(filePath).toStringUtf8();
   3706   }
   3707 
   3708   private static File getTestDataDir() {
   3709     // Search each parent directory looking for "src/google/protobuf".
   3710     File ancestor = new File(".");
   3711     try {
   3712       ancestor = ancestor.getCanonicalFile();
   3713     } catch (IOException e) {
   3714       throw new RuntimeException(
   3715         "Couldn't get canonical name of working directory.", e);
   3716     }
   3717     while (ancestor != null && ancestor.exists()) {
   3718       if (new File(ancestor, "src/google/protobuf").exists()) {
   3719         return new File(ancestor, "src/google/protobuf/testdata");
   3720       }
   3721       ancestor = ancestor.getParentFile();
   3722     }
   3723 
   3724     throw new RuntimeException(
   3725       "Could not find golden files.  This test must be run from within the " +
   3726       "protobuf source package so that it can read test data files from the " +
   3727       "C++ source tree.");
   3728   }
   3729 
   3730   /**
   3731    * @param filePath The path relative to
   3732    * {@link com.google.testing.util.TestUtil#getDefaultSrcDir}.
   3733    */
   3734   public static ByteString readBytesFromFile(String filename) {
   3735     File fullPath = new File(getTestDataDir(), filename);
   3736     try {
   3737       RandomAccessFile file = new RandomAccessFile(fullPath, "r");
   3738       byte[] content = new byte[(int) file.length()];
   3739       file.readFully(content);
   3740       return ByteString.copyFrom(content);
   3741     } catch (IOException e) {
   3742       // Throw a RuntimeException here so that we can call this function from
   3743       // static initializers.
   3744       throw new IllegalArgumentException(
   3745         "Couldn't read file: " + fullPath.getPath(), e);
   3746     }
   3747   }
   3748 
   3749   /**
   3750    * Get the bytes of the "golden message".  This is a serialized TestAllTypes
   3751    * with all fields set as they would be by
   3752    * {@link setAllFields(TestAllTypes.Builder)}, but it is loaded from a file
   3753    * on disk rather than generated dynamically.  The file is actually generated
   3754    * by C++ code, so testing against it verifies compatibility with C++.
   3755    */
   3756   public static ByteString getGoldenMessage() {
   3757     if (goldenMessage == null) {
   3758       goldenMessage = readBytesFromFile("golden_message");
   3759     }
   3760     return goldenMessage;
   3761   }
   3762   private static ByteString goldenMessage = null;
   3763 
   3764   /**
   3765    * Get the bytes of the "golden packed fields message".  This is a serialized
   3766    * TestPackedTypes with all fields set as they would be by
   3767    * {@link setPackedFields(TestPackedTypes.Builder)}, but it is loaded from a
   3768    * file on disk rather than generated dynamically.  The file is actually
   3769    * generated by C++ code, so testing against it verifies compatibility with
   3770    * C++.
   3771    */
   3772   public static ByteString getGoldenPackedFieldsMessage() {
   3773     if (goldenPackedFieldsMessage == null) {
   3774       goldenPackedFieldsMessage =
   3775           readBytesFromFile("golden_packed_fields_message");
   3776     }
   3777     return goldenPackedFieldsMessage;
   3778   }
   3779   private static ByteString goldenPackedFieldsMessage = null;
   3780 }
   3781