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