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