| OLD | NEW |
| (Empty) |
| 1 // Protocol Buffers - Google's data interchange format | |
| 2 // Copyright 2008 Google Inc. All rights reserved. | |
| 3 // http://code.google.com/p/protobuf/ | |
| 4 // | |
| 5 // Redistribution and use in source and binary forms, with or without | |
| 6 // modification, are permitted provided that the following conditions are | |
| 7 // met: | |
| 8 // | |
| 9 // * Redistributions of source code must retain the above copyright | |
| 10 // notice, this list of conditions and the following disclaimer. | |
| 11 // * Redistributions in binary form must reproduce the above | |
| 12 // copyright notice, this list of conditions and the following disclaimer | |
| 13 // in the documentation and/or other materials provided with the | |
| 14 // distribution. | |
| 15 // * Neither the name of Google Inc. nor the names of its | |
| 16 // contributors may be used to endorse or promote products derived from | |
| 17 // this software without specific prior written permission. | |
| 18 // | |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 | |
| 31 package com.google.protobuf.test; | |
| 32 import com.google.protobuf.*; | |
| 33 | |
| 34 import com.google.protobuf.Descriptors.Descriptor; | |
| 35 import com.google.protobuf.Descriptors.FieldDescriptor; | |
| 36 import com.google.protobuf.test.UnittestImport; | |
| 37 import protobuf_unittest.EnumWithNoOuter; | |
| 38 import protobuf_unittest.MessageWithNoOuter; | |
| 39 import protobuf_unittest.MultipleFilesTestProto; | |
| 40 import protobuf_unittest.NestedExtension.MyNestedExtension; | |
| 41 import protobuf_unittest.NonNestedExtension; | |
| 42 import protobuf_unittest.NonNestedExtension.MessageToBeExtended; | |
| 43 import protobuf_unittest.NonNestedExtension.MyNonNestedExtension; | |
| 44 import protobuf_unittest.ServiceWithNoOuter; | |
| 45 import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize; | |
| 46 import protobuf_unittest.UnittestOptimizeFor.TestOptionalOptimizedForSize; | |
| 47 import protobuf_unittest.UnittestOptimizeFor.TestRequiredOptimizedForSize; | |
| 48 import protobuf_unittest.UnittestProto; | |
| 49 import protobuf_unittest.UnittestProto.ForeignEnum; | |
| 50 import protobuf_unittest.UnittestProto.ForeignMessage; | |
| 51 import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder; | |
| 52 import protobuf_unittest.UnittestProto.TestAllExtensions; | |
| 53 import protobuf_unittest.UnittestProto.TestAllTypes; | |
| 54 import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; | |
| 55 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; | |
| 56 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; | |
| 57 import protobuf_unittest.UnittestProto.TestPackedTypes; | |
| 58 import protobuf_unittest.UnittestProto.TestUnpackedTypes; | |
| 59 | |
| 60 import junit.framework.TestCase; | |
| 61 | |
| 62 import java.io.ByteArrayInputStream; | |
| 63 import java.io.ByteArrayOutputStream; | |
| 64 import java.io.ObjectInputStream; | |
| 65 import java.io.ObjectOutputStream; | |
| 66 import java.util.Arrays; | |
| 67 import java.util.Collections; | |
| 68 import java.util.List; | |
| 69 | |
| 70 /** | |
| 71 * Unit test for generated messages and generated code. See also | |
| 72 * {@link MessageTest}, which tests some generated message functionality. | |
| 73 * | |
| 74 * @author kenton@google.com Kenton Varda | |
| 75 */ | |
| 76 public class GeneratedMessageTest extends TestCase { | |
| 77 TestUtil.ReflectionTester reflectionTester = | |
| 78 new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); | |
| 79 | |
| 80 public void testDefaultInstance() throws Exception { | |
| 81 assertSame(TestAllTypes.getDefaultInstance(), | |
| 82 TestAllTypes.getDefaultInstance().getDefaultInstanceForType()); | |
| 83 assertSame(TestAllTypes.getDefaultInstance(), | |
| 84 TestAllTypes.newBuilder().getDefaultInstanceForType()); | |
| 85 } | |
| 86 | |
| 87 public void testMessageOrBuilder() throws Exception { | |
| 88 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 89 TestUtil.setAllFields(builder); | |
| 90 TestAllTypes message = builder.build(); | |
| 91 TestUtil.assertAllFieldsSet(message); | |
| 92 } | |
| 93 | |
| 94 public void testUsingBuilderMultipleTimes() throws Exception { | |
| 95 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 96 // primitive field scalar and repeated | |
| 97 builder.setOptionalSfixed64(100); | |
| 98 builder.addRepeatedInt32(100); | |
| 99 // enum field scalar and repeated | |
| 100 builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); | |
| 101 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); | |
| 102 // proto field scalar and repeated | |
| 103 builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(1)); | |
| 104 builder.addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(1)); | |
| 105 | |
| 106 TestAllTypes value1 = builder.build(); | |
| 107 | |
| 108 assertEquals(100, value1.getOptionalSfixed64()); | |
| 109 assertEquals(100, value1.getRepeatedInt32(0)); | |
| 110 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, | |
| 111 value1.getOptionalImportEnum()); | |
| 112 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, | |
| 113 value1.getRepeatedImportEnum(0)); | |
| 114 assertEquals(1, value1.getOptionalForeignMessage().getC()); | |
| 115 assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); | |
| 116 | |
| 117 // Make sure that builder didn't update previously created values | |
| 118 builder.setOptionalSfixed64(200); | |
| 119 builder.setRepeatedInt32(0, 200); | |
| 120 builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_FOO); | |
| 121 builder.setRepeatedImportEnum(0, UnittestImport.ImportEnum.IMPORT_FOO); | |
| 122 builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(2)); | |
| 123 builder.setRepeatedForeignMessage(0, ForeignMessage.newBuilder().setC(2)); | |
| 124 | |
| 125 TestAllTypes value2 = builder.build(); | |
| 126 | |
| 127 // Make sure value1 didn't change. | |
| 128 assertEquals(100, value1.getOptionalSfixed64()); | |
| 129 assertEquals(100, value1.getRepeatedInt32(0)); | |
| 130 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, | |
| 131 value1.getOptionalImportEnum()); | |
| 132 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, | |
| 133 value1.getRepeatedImportEnum(0)); | |
| 134 assertEquals(1, value1.getOptionalForeignMessage().getC()); | |
| 135 assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); | |
| 136 | |
| 137 // Make sure value2 is correct | |
| 138 assertEquals(200, value2.getOptionalSfixed64()); | |
| 139 assertEquals(200, value2.getRepeatedInt32(0)); | |
| 140 assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, | |
| 141 value2.getOptionalImportEnum()); | |
| 142 assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, | |
| 143 value2.getRepeatedImportEnum(0)); | |
| 144 assertEquals(2, value2.getOptionalForeignMessage().getC()); | |
| 145 assertEquals(2, value2.getRepeatedForeignMessage(0).getC()); | |
| 146 } | |
| 147 | |
| 148 public void testRepeatedArraysAreImmutable() throws Exception { | |
| 149 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 150 builder.addRepeatedInt32(100); | |
| 151 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); | |
| 152 builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()); | |
| 153 assertIsUnmodifiable(builder.getRepeatedInt32List()); | |
| 154 assertIsUnmodifiable(builder.getRepeatedImportEnumList()); | |
| 155 assertIsUnmodifiable(builder.getRepeatedForeignMessageList()); | |
| 156 assertIsUnmodifiable(builder.getRepeatedFloatList()); | |
| 157 | |
| 158 | |
| 159 TestAllTypes value = builder.build(); | |
| 160 assertIsUnmodifiable(value.getRepeatedInt32List()); | |
| 161 assertIsUnmodifiable(value.getRepeatedImportEnumList()); | |
| 162 assertIsUnmodifiable(value.getRepeatedForeignMessageList()); | |
| 163 assertIsUnmodifiable(value.getRepeatedFloatList()); | |
| 164 } | |
| 165 | |
| 166 public void testParsedMessagesAreImmutable() throws Exception { | |
| 167 TestAllTypes value = TestAllTypes.PARSER.parseFrom( | |
| 168 TestUtil.getAllSet().toByteString()); | |
| 169 assertIsUnmodifiable(value.getRepeatedInt32List()); | |
| 170 assertIsUnmodifiable(value.getRepeatedInt64List()); | |
| 171 assertIsUnmodifiable(value.getRepeatedUint32List()); | |
| 172 assertIsUnmodifiable(value.getRepeatedUint64List()); | |
| 173 assertIsUnmodifiable(value.getRepeatedSint32List()); | |
| 174 assertIsUnmodifiable(value.getRepeatedSint64List()); | |
| 175 assertIsUnmodifiable(value.getRepeatedFixed32List()); | |
| 176 assertIsUnmodifiable(value.getRepeatedFixed64List()); | |
| 177 assertIsUnmodifiable(value.getRepeatedSfixed32List()); | |
| 178 assertIsUnmodifiable(value.getRepeatedSfixed64List()); | |
| 179 assertIsUnmodifiable(value.getRepeatedFloatList()); | |
| 180 assertIsUnmodifiable(value.getRepeatedDoubleList()); | |
| 181 assertIsUnmodifiable(value.getRepeatedBoolList()); | |
| 182 assertIsUnmodifiable(value.getRepeatedStringList()); | |
| 183 assertIsUnmodifiable(value.getRepeatedBytesList()); | |
| 184 assertIsUnmodifiable(value.getRepeatedGroupList()); | |
| 185 assertIsUnmodifiable(value.getRepeatedNestedMessageList()); | |
| 186 assertIsUnmodifiable(value.getRepeatedForeignMessageList()); | |
| 187 assertIsUnmodifiable(value.getRepeatedImportMessageList()); | |
| 188 assertIsUnmodifiable(value.getRepeatedNestedEnumList()); | |
| 189 assertIsUnmodifiable(value.getRepeatedForeignEnumList()); | |
| 190 assertIsUnmodifiable(value.getRepeatedImportEnumList()); | |
| 191 } | |
| 192 | |
| 193 private void assertIsUnmodifiable(List<?> list) { | |
| 194 if (list == Collections.emptyList()) { | |
| 195 // OKAY -- Need to check this b/c EmptyList allows you to call clear. | |
| 196 } else { | |
| 197 try { | |
| 198 list.clear(); | |
| 199 fail("List wasn't immutable"); | |
| 200 } catch (UnsupportedOperationException e) { | |
| 201 // good | |
| 202 } | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 public void testSettersRejectNull() throws Exception { | |
| 207 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 208 try { | |
| 209 builder.setOptionalString(null); | |
| 210 fail("Exception was not thrown"); | |
| 211 } catch (NullPointerException e) { | |
| 212 // We expect this exception. | |
| 213 } | |
| 214 try { | |
| 215 builder.setOptionalBytes(null); | |
| 216 fail("Exception was not thrown"); | |
| 217 } catch (NullPointerException e) { | |
| 218 // We expect this exception. | |
| 219 } | |
| 220 try { | |
| 221 builder.setOptionalNestedMessage((TestAllTypes.NestedMessage) null); | |
| 222 fail("Exception was not thrown"); | |
| 223 } catch (NullPointerException e) { | |
| 224 // We expect this exception. | |
| 225 } | |
| 226 try { | |
| 227 builder.setOptionalNestedMessage( | |
| 228 (TestAllTypes.NestedMessage.Builder) null); | |
| 229 fail("Exception was not thrown"); | |
| 230 } catch (NullPointerException e) { | |
| 231 // We expect this exception. | |
| 232 } | |
| 233 try { | |
| 234 builder.setOptionalNestedEnum(null); | |
| 235 fail("Exception was not thrown"); | |
| 236 } catch (NullPointerException e) { | |
| 237 // We expect this exception. | |
| 238 } | |
| 239 try { | |
| 240 builder.addRepeatedString(null); | |
| 241 fail("Exception was not thrown"); | |
| 242 } catch (NullPointerException e) { | |
| 243 // We expect this exception. | |
| 244 } | |
| 245 try { | |
| 246 builder.addRepeatedBytes(null); | |
| 247 fail("Exception was not thrown"); | |
| 248 } catch (NullPointerException e) { | |
| 249 // We expect this exception. | |
| 250 } | |
| 251 try { | |
| 252 builder.addRepeatedNestedMessage((TestAllTypes.NestedMessage) null); | |
| 253 fail("Exception was not thrown"); | |
| 254 } catch (NullPointerException e) { | |
| 255 // We expect this exception. | |
| 256 } | |
| 257 try { | |
| 258 builder.addRepeatedNestedMessage( | |
| 259 (TestAllTypes.NestedMessage.Builder) null); | |
| 260 fail("Exception was not thrown"); | |
| 261 } catch (NullPointerException e) { | |
| 262 // We expect this exception. | |
| 263 } | |
| 264 try { | |
| 265 builder.addRepeatedNestedEnum(null); | |
| 266 fail("Exception was not thrown"); | |
| 267 } catch (NullPointerException e) { | |
| 268 // We expect this exception. | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 public void testRepeatedSetters() throws Exception { | |
| 273 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 274 TestUtil.setAllFields(builder); | |
| 275 TestUtil.modifyRepeatedFields(builder); | |
| 276 TestAllTypes message = builder.build(); | |
| 277 TestUtil.assertRepeatedFieldsModified(message); | |
| 278 } | |
| 279 | |
| 280 public void testRepeatedSettersRejectNull() throws Exception { | |
| 281 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 282 | |
| 283 builder.addRepeatedString("one"); | |
| 284 builder.addRepeatedString("two"); | |
| 285 try { | |
| 286 builder.setRepeatedString(1, null); | |
| 287 fail("Exception was not thrown"); | |
| 288 } catch (NullPointerException e) { | |
| 289 // We expect this exception. | |
| 290 } | |
| 291 | |
| 292 builder.addRepeatedBytes(TestUtil.toBytes("one")); | |
| 293 builder.addRepeatedBytes(TestUtil.toBytes("two")); | |
| 294 try { | |
| 295 builder.setRepeatedBytes(1, null); | |
| 296 fail("Exception was not thrown"); | |
| 297 } catch (NullPointerException e) { | |
| 298 // We expect this exception. | |
| 299 } | |
| 300 | |
| 301 builder.addRepeatedNestedMessage( | |
| 302 TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); | |
| 303 builder.addRepeatedNestedMessage( | |
| 304 TestAllTypes.NestedMessage.newBuilder().setBb(456).build()); | |
| 305 try { | |
| 306 builder.setRepeatedNestedMessage(1, (TestAllTypes.NestedMessage) null); | |
| 307 fail("Exception was not thrown"); | |
| 308 } catch (NullPointerException e) { | |
| 309 // We expect this exception. | |
| 310 } | |
| 311 try { | |
| 312 builder.setRepeatedNestedMessage( | |
| 313 1, (TestAllTypes.NestedMessage.Builder) null); | |
| 314 fail("Exception was not thrown"); | |
| 315 } catch (NullPointerException e) { | |
| 316 // We expect this exception. | |
| 317 } | |
| 318 | |
| 319 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.FOO); | |
| 320 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); | |
| 321 try { | |
| 322 builder.setRepeatedNestedEnum(1, null); | |
| 323 fail("Exception was not thrown"); | |
| 324 } catch (NullPointerException e) { | |
| 325 // We expect this exception. | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 public void testRepeatedAppend() throws Exception { | |
| 330 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 331 | |
| 332 builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4)); | |
| 333 builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ)); | |
| 334 | |
| 335 ForeignMessage foreignMessage = | |
| 336 ForeignMessage.newBuilder().setC(12).build(); | |
| 337 builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage)); | |
| 338 | |
| 339 TestAllTypes message = builder.build(); | |
| 340 assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4)); | |
| 341 assertEquals(message.getRepeatedForeignEnumList(), | |
| 342 Arrays.asList(ForeignEnum.FOREIGN_BAZ)); | |
| 343 assertEquals(1, message.getRepeatedForeignMessageCount()); | |
| 344 assertEquals(12, message.getRepeatedForeignMessage(0).getC()); | |
| 345 } | |
| 346 | |
| 347 public void testRepeatedAppendRejectsNull() throws Exception { | |
| 348 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 349 | |
| 350 ForeignMessage foreignMessage = | |
| 351 ForeignMessage.newBuilder().setC(12).build(); | |
| 352 try { | |
| 353 builder.addAllRepeatedForeignMessage( | |
| 354 Arrays.asList(foreignMessage, (ForeignMessage) null)); | |
| 355 fail("Exception was not thrown"); | |
| 356 } catch (NullPointerException e) { | |
| 357 // We expect this exception. | |
| 358 } | |
| 359 | |
| 360 try { | |
| 361 builder.addAllRepeatedForeignEnum( | |
| 362 Arrays.asList(ForeignEnum.FOREIGN_BAZ, null)); | |
| 363 fail("Exception was not thrown"); | |
| 364 } catch (NullPointerException e) { | |
| 365 // We expect this exception. | |
| 366 } | |
| 367 | |
| 368 try { | |
| 369 builder.addAllRepeatedString(Arrays.asList("one", null)); | |
| 370 fail("Exception was not thrown"); | |
| 371 } catch (NullPointerException e) { | |
| 372 // We expect this exception. | |
| 373 } | |
| 374 | |
| 375 try { | |
| 376 builder.addAllRepeatedBytes(Arrays.asList(TestUtil.toBytes("one"), null)); | |
| 377 fail("Exception was not thrown"); | |
| 378 } catch (NullPointerException e) { | |
| 379 // We expect this exception. | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 public void testSettingForeignMessageUsingBuilder() throws Exception { | |
| 384 TestAllTypes message = TestAllTypes.newBuilder() | |
| 385 // Pass builder for foreign message instance. | |
| 386 .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123)) | |
| 387 .build(); | |
| 388 TestAllTypes expectedMessage = TestAllTypes.newBuilder() | |
| 389 // Create expected version passing foreign message instance explicitly. | |
| 390 .setOptionalForeignMessage( | |
| 391 ForeignMessage.newBuilder().setC(123).build()) | |
| 392 .build(); | |
| 393 // TODO(ngd): Upgrade to using real #equals method once implemented | |
| 394 assertEquals(expectedMessage.toString(), message.toString()); | |
| 395 } | |
| 396 | |
| 397 public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception { | |
| 398 TestAllTypes message = TestAllTypes.newBuilder() | |
| 399 // Pass builder for foreign message instance. | |
| 400 .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456)) | |
| 401 .build(); | |
| 402 TestAllTypes expectedMessage = TestAllTypes.newBuilder() | |
| 403 // Create expected version passing foreign message instance explicitly. | |
| 404 .addRepeatedForeignMessage( | |
| 405 ForeignMessage.newBuilder().setC(456).build()) | |
| 406 .build(); | |
| 407 assertEquals(expectedMessage.toString(), message.toString()); | |
| 408 } | |
| 409 | |
| 410 public void testDefaults() throws Exception { | |
| 411 TestUtil.assertClear(TestAllTypes.getDefaultInstance()); | |
| 412 TestUtil.assertClear(TestAllTypes.newBuilder().build()); | |
| 413 | |
| 414 TestExtremeDefaultValues message = | |
| 415 TestExtremeDefaultValues.getDefaultInstance(); | |
| 416 assertEquals("\u1234", message.getUtf8String()); | |
| 417 assertEquals(Double.POSITIVE_INFINITY, message.getInfDouble()); | |
| 418 assertEquals(Double.NEGATIVE_INFINITY, message.getNegInfDouble()); | |
| 419 assertTrue(Double.isNaN(message.getNanDouble())); | |
| 420 assertEquals(Float.POSITIVE_INFINITY, message.getInfFloat()); | |
| 421 assertEquals(Float.NEGATIVE_INFINITY, message.getNegInfFloat()); | |
| 422 assertTrue(Float.isNaN(message.getNanFloat())); | |
| 423 assertEquals("? ? ?? ?? ??? ??/ ??-", message.getCppTrigraph()); | |
| 424 } | |
| 425 | |
| 426 public void testClear() throws Exception { | |
| 427 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 428 TestUtil.assertClear(builder); | |
| 429 TestUtil.setAllFields(builder); | |
| 430 builder.clear(); | |
| 431 TestUtil.assertClear(builder); | |
| 432 } | |
| 433 | |
| 434 public void testReflectionGetters() throws Exception { | |
| 435 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 436 TestUtil.setAllFields(builder); | |
| 437 reflectionTester.assertAllFieldsSetViaReflection(builder); | |
| 438 | |
| 439 TestAllTypes message = builder.build(); | |
| 440 reflectionTester.assertAllFieldsSetViaReflection(message); | |
| 441 } | |
| 442 | |
| 443 public void testReflectionSetters() throws Exception { | |
| 444 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 445 reflectionTester.setAllFieldsViaReflection(builder); | |
| 446 TestUtil.assertAllFieldsSet(builder); | |
| 447 | |
| 448 TestAllTypes message = builder.build(); | |
| 449 TestUtil.assertAllFieldsSet(message); | |
| 450 } | |
| 451 | |
| 452 public void testReflectionSettersRejectNull() throws Exception { | |
| 453 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 454 reflectionTester.assertReflectionSettersRejectNull(builder); | |
| 455 } | |
| 456 | |
| 457 public void testReflectionRepeatedSetters() throws Exception { | |
| 458 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 459 reflectionTester.setAllFieldsViaReflection(builder); | |
| 460 reflectionTester.modifyRepeatedFieldsViaReflection(builder); | |
| 461 TestUtil.assertRepeatedFieldsModified(builder); | |
| 462 | |
| 463 TestAllTypes message = builder.build(); | |
| 464 TestUtil.assertRepeatedFieldsModified(message); | |
| 465 } | |
| 466 | |
| 467 public void testReflectionRepeatedSettersRejectNull() throws Exception { | |
| 468 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 469 reflectionTester.assertReflectionRepeatedSettersRejectNull(builder); | |
| 470 } | |
| 471 | |
| 472 public void testReflectionDefaults() throws Exception { | |
| 473 reflectionTester.assertClearViaReflection( | |
| 474 TestAllTypes.getDefaultInstance()); | |
| 475 reflectionTester.assertClearViaReflection( | |
| 476 TestAllTypes.newBuilder().build()); | |
| 477 } | |
| 478 | |
| 479 public void testEnumInterface() throws Exception { | |
| 480 assertTrue(TestAllTypes.getDefaultInstance().getDefaultNestedEnum() | |
| 481 instanceof ProtocolMessageEnum); | |
| 482 } | |
| 483 | |
| 484 public void testEnumMap() throws Exception { | |
| 485 Internal.EnumLiteMap<ForeignEnum> map = ForeignEnum.internalGetValueMap(); | |
| 486 | |
| 487 for (ForeignEnum value : ForeignEnum.values()) { | |
| 488 assertEquals(value, map.findValueByNumber(value.getNumber())); | |
| 489 } | |
| 490 | |
| 491 assertTrue(map.findValueByNumber(12345) == null); | |
| 492 } | |
| 493 | |
| 494 public void testParsePackedToUnpacked() throws Exception { | |
| 495 TestUnpackedTypes.Builder builder = TestUnpackedTypes.newBuilder(); | |
| 496 TestUnpackedTypes message = | |
| 497 builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); | |
| 498 TestUtil.assertUnpackedFieldsSet(message); | |
| 499 } | |
| 500 | |
| 501 public void testParseUnpackedToPacked() throws Exception { | |
| 502 TestPackedTypes.Builder builder = TestPackedTypes.newBuilder(); | |
| 503 TestPackedTypes message = | |
| 504 builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); | |
| 505 TestUtil.assertPackedFieldsSet(message); | |
| 506 } | |
| 507 | |
| 508 // ================================================================= | |
| 509 // Extensions. | |
| 510 | |
| 511 TestUtil.ReflectionTester extensionsReflectionTester = | |
| 512 new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(), | |
| 513 TestUtil.getExtensionRegistry()); | |
| 514 | |
| 515 public void testExtensionMessageOrBuilder() throws Exception { | |
| 516 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 517 TestUtil.setAllExtensions(builder); | |
| 518 TestAllExtensions message = builder.build(); | |
| 519 TestUtil.assertAllExtensionsSet(message); | |
| 520 } | |
| 521 | |
| 522 public void testExtensionRepeatedSetters() throws Exception { | |
| 523 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 524 TestUtil.setAllExtensions(builder); | |
| 525 TestUtil.modifyRepeatedExtensions(builder); | |
| 526 TestAllExtensions message = builder.build(); | |
| 527 TestUtil.assertRepeatedExtensionsModified(message); | |
| 528 } | |
| 529 | |
| 530 public void testExtensionDefaults() throws Exception { | |
| 531 TestUtil.assertExtensionsClear(TestAllExtensions.getDefaultInstance()); | |
| 532 TestUtil.assertExtensionsClear(TestAllExtensions.newBuilder().build()); | |
| 533 } | |
| 534 | |
| 535 public void testExtensionReflectionGetters() throws Exception { | |
| 536 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 537 TestUtil.setAllExtensions(builder); | |
| 538 extensionsReflectionTester.assertAllFieldsSetViaReflection(builder); | |
| 539 | |
| 540 TestAllExtensions message = builder.build(); | |
| 541 extensionsReflectionTester.assertAllFieldsSetViaReflection(message); | |
| 542 } | |
| 543 | |
| 544 public void testExtensionReflectionSetters() throws Exception { | |
| 545 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 546 extensionsReflectionTester.setAllFieldsViaReflection(builder); | |
| 547 TestUtil.assertAllExtensionsSet(builder); | |
| 548 | |
| 549 TestAllExtensions message = builder.build(); | |
| 550 TestUtil.assertAllExtensionsSet(message); | |
| 551 } | |
| 552 | |
| 553 public void testExtensionReflectionSettersRejectNull() throws Exception { | |
| 554 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 555 extensionsReflectionTester.assertReflectionSettersRejectNull(builder); | |
| 556 } | |
| 557 | |
| 558 public void testExtensionReflectionRepeatedSetters() throws Exception { | |
| 559 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 560 extensionsReflectionTester.setAllFieldsViaReflection(builder); | |
| 561 extensionsReflectionTester.modifyRepeatedFieldsViaReflection(builder); | |
| 562 TestUtil.assertRepeatedExtensionsModified(builder); | |
| 563 | |
| 564 TestAllExtensions message = builder.build(); | |
| 565 TestUtil.assertRepeatedExtensionsModified(message); | |
| 566 } | |
| 567 | |
| 568 public void testExtensionReflectionRepeatedSettersRejectNull() | |
| 569 throws Exception { | |
| 570 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
| 571 extensionsReflectionTester.assertReflectionRepeatedSettersRejectNull( | |
| 572 builder); | |
| 573 } | |
| 574 | |
| 575 public void testExtensionReflectionDefaults() throws Exception { | |
| 576 extensionsReflectionTester.assertClearViaReflection( | |
| 577 TestAllExtensions.getDefaultInstance()); | |
| 578 extensionsReflectionTester.assertClearViaReflection( | |
| 579 TestAllExtensions.newBuilder().build()); | |
| 580 } | |
| 581 | |
| 582 public void testClearExtension() throws Exception { | |
| 583 // clearExtension() is not actually used in TestUtil, so try it manually. | |
| 584 assertFalse( | |
| 585 TestAllExtensions.newBuilder() | |
| 586 .setExtension(UnittestProto.optionalInt32Extension, 1) | |
| 587 .clearExtension(UnittestProto.optionalInt32Extension) | |
| 588 .hasExtension(UnittestProto.optionalInt32Extension)); | |
| 589 assertEquals(0, | |
| 590 TestAllExtensions.newBuilder() | |
| 591 .addExtension(UnittestProto.repeatedInt32Extension, 1) | |
| 592 .clearExtension(UnittestProto.repeatedInt32Extension) | |
| 593 .getExtensionCount(UnittestProto.repeatedInt32Extension)); | |
| 594 } | |
| 595 | |
| 596 public void testExtensionCopy() throws Exception { | |
| 597 TestAllExtensions original = TestUtil.getAllExtensionsSet(); | |
| 598 TestAllExtensions copy = TestAllExtensions.newBuilder(original).build(); | |
| 599 TestUtil.assertAllExtensionsSet(copy); | |
| 600 } | |
| 601 | |
| 602 public void testExtensionMergeFrom() throws Exception { | |
| 603 TestAllExtensions original = | |
| 604 TestAllExtensions.newBuilder() | |
| 605 .setExtension(UnittestProto.optionalInt32Extension, 1).build(); | |
| 606 TestAllExtensions merged = | |
| 607 TestAllExtensions.newBuilder().mergeFrom(original).build(); | |
| 608 assertTrue(merged.hasExtension(UnittestProto.optionalInt32Extension)); | |
| 609 assertEquals( | |
| 610 1, (int) merged.getExtension(UnittestProto.optionalInt32Extension)); | |
| 611 } | |
| 612 | |
| 613 // ================================================================= | |
| 614 // multiple_files_test | |
| 615 | |
| 616 public void testMultipleFilesOption() throws Exception { | |
| 617 // We mostly just want to check that things compile. | |
| 618 MessageWithNoOuter message = | |
| 619 MessageWithNoOuter.newBuilder() | |
| 620 .setNested(MessageWithNoOuter.NestedMessage.newBuilder().setI(1)) | |
| 621 .addForeign(TestAllTypes.newBuilder().setOptionalInt32(1)) | |
| 622 .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ) | |
| 623 .setForeignEnum(EnumWithNoOuter.BAR) | |
| 624 .build(); | |
| 625 assertEquals(message, MessageWithNoOuter.parseFrom(message.toByteString())); | |
| 626 | |
| 627 assertEquals(MultipleFilesTestProto.getDescriptor(), | |
| 628 MessageWithNoOuter.getDescriptor().getFile()); | |
| 629 | |
| 630 Descriptors.FieldDescriptor field = | |
| 631 MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum"); | |
| 632 assertEquals(EnumWithNoOuter.BAR.getValueDescriptor(), | |
| 633 message.getField(field)); | |
| 634 | |
| 635 assertEquals(MultipleFilesTestProto.getDescriptor(), | |
| 636 ServiceWithNoOuter.getDescriptor().getFile()); | |
| 637 | |
| 638 assertFalse( | |
| 639 TestAllExtensions.getDefaultInstance().hasExtension( | |
| 640 MultipleFilesTestProto.extensionWithOuter)); | |
| 641 } | |
| 642 | |
| 643 public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize() | |
| 644 throws Exception { | |
| 645 TestOptionalOptimizedForSize message = | |
| 646 TestOptionalOptimizedForSize.getDefaultInstance(); | |
| 647 assertTrue(message.isInitialized()); | |
| 648 | |
| 649 message = TestOptionalOptimizedForSize.newBuilder().setO( | |
| 650 TestRequiredOptimizedForSize.newBuilder().buildPartial() | |
| 651 ).buildPartial(); | |
| 652 assertFalse(message.isInitialized()); | |
| 653 | |
| 654 message = TestOptionalOptimizedForSize.newBuilder().setO( | |
| 655 TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial() | |
| 656 ).buildPartial(); | |
| 657 assertTrue(message.isInitialized()); | |
| 658 } | |
| 659 | |
| 660 public void testUninitializedExtensionInOptimizedForSize() | |
| 661 throws Exception { | |
| 662 TestOptimizedForSize.Builder builder = TestOptimizedForSize.newBuilder(); | |
| 663 builder.setExtension(TestOptimizedForSize.testExtension2, | |
| 664 TestRequiredOptimizedForSize.newBuilder().buildPartial()); | |
| 665 assertFalse(builder.isInitialized()); | |
| 666 assertFalse(builder.buildPartial().isInitialized()); | |
| 667 | |
| 668 builder = TestOptimizedForSize.newBuilder(); | |
| 669 builder.setExtension(TestOptimizedForSize.testExtension2, | |
| 670 TestRequiredOptimizedForSize.newBuilder().setX(10).buildPartial()); | |
| 671 assertTrue(builder.isInitialized()); | |
| 672 assertTrue(builder.buildPartial().isInitialized()); | |
| 673 } | |
| 674 | |
| 675 public void testToBuilder() throws Exception { | |
| 676 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 677 TestUtil.setAllFields(builder); | |
| 678 TestAllTypes message = builder.build(); | |
| 679 TestUtil.assertAllFieldsSet(message); | |
| 680 TestUtil.assertAllFieldsSet(message.toBuilder().build()); | |
| 681 } | |
| 682 | |
| 683 public void testFieldConstantValues() throws Exception { | |
| 684 assertEquals(TestAllTypes.NestedMessage.BB_FIELD_NUMBER, 1); | |
| 685 assertEquals(TestAllTypes.OPTIONAL_INT32_FIELD_NUMBER, 1); | |
| 686 assertEquals(TestAllTypes.OPTIONALGROUP_FIELD_NUMBER, 16); | |
| 687 assertEquals(TestAllTypes.OPTIONAL_NESTED_MESSAGE_FIELD_NUMBER, 18); | |
| 688 assertEquals(TestAllTypes.OPTIONAL_NESTED_ENUM_FIELD_NUMBER, 21); | |
| 689 assertEquals(TestAllTypes.REPEATED_INT32_FIELD_NUMBER, 31); | |
| 690 assertEquals(TestAllTypes.REPEATEDGROUP_FIELD_NUMBER, 46); | |
| 691 assertEquals(TestAllTypes.REPEATED_NESTED_MESSAGE_FIELD_NUMBER, 48); | |
| 692 assertEquals(TestAllTypes.REPEATED_NESTED_ENUM_FIELD_NUMBER, 51); | |
| 693 } | |
| 694 | |
| 695 public void testExtensionConstantValues() throws Exception { | |
| 696 assertEquals(UnittestProto.TestRequired.SINGLE_FIELD_NUMBER, 1000); | |
| 697 assertEquals(UnittestProto.TestRequired.MULTI_FIELD_NUMBER, 1001); | |
| 698 assertEquals(UnittestProto.OPTIONAL_INT32_EXTENSION_FIELD_NUMBER, 1); | |
| 699 assertEquals(UnittestProto.OPTIONALGROUP_EXTENSION_FIELD_NUMBER, 16); | |
| 700 assertEquals( | |
| 701 UnittestProto.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18); | |
| 702 assertEquals(UnittestProto.OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 21); | |
| 703 assertEquals(UnittestProto.REPEATED_INT32_EXTENSION_FIELD_NUMBER, 31); | |
| 704 assertEquals(UnittestProto.REPEATEDGROUP_EXTENSION_FIELD_NUMBER, 46); | |
| 705 assertEquals( | |
| 706 UnittestProto.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48); | |
| 707 assertEquals(UnittestProto.REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 51); | |
| 708 } | |
| 709 | |
| 710 public void testRecursiveMessageDefaultInstance() throws Exception { | |
| 711 UnittestProto.TestRecursiveMessage message = | |
| 712 UnittestProto.TestRecursiveMessage.getDefaultInstance(); | |
| 713 assertTrue(message != null); | |
| 714 assertTrue(message.getA() != null); | |
| 715 assertTrue(message.getA() == message); | |
| 716 } | |
| 717 | |
| 718 public void testSerialize() throws Exception { | |
| 719 ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| 720 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 721 TestUtil.setAllFields(builder); | |
| 722 TestAllTypes expected = builder.build(); | |
| 723 ObjectOutputStream out = new ObjectOutputStream(baos); | |
| 724 try { | |
| 725 out.writeObject(expected); | |
| 726 } finally { | |
| 727 out.close(); | |
| 728 } | |
| 729 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | |
| 730 ObjectInputStream in = new ObjectInputStream(bais); | |
| 731 TestAllTypes actual = (TestAllTypes) in.readObject(); | |
| 732 assertEquals(expected, actual); | |
| 733 } | |
| 734 | |
| 735 public void testSerializePartial() throws Exception { | |
| 736 ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| 737 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 738 TestAllTypes expected = builder.buildPartial(); | |
| 739 ObjectOutputStream out = new ObjectOutputStream(baos); | |
| 740 try { | |
| 741 out.writeObject(expected); | |
| 742 } finally { | |
| 743 out.close(); | |
| 744 } | |
| 745 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | |
| 746 ObjectInputStream in = new ObjectInputStream(bais); | |
| 747 TestAllTypes actual = (TestAllTypes) in.readObject(); | |
| 748 assertEquals(expected, actual); | |
| 749 } | |
| 750 | |
| 751 public void testEnumValues() { | |
| 752 assertEquals( | |
| 753 TestAllTypes.NestedEnum.BAR.getNumber(), | |
| 754 TestAllTypes.NestedEnum.BAR_VALUE); | |
| 755 assertEquals( | |
| 756 TestAllTypes.NestedEnum.BAZ.getNumber(), | |
| 757 TestAllTypes.NestedEnum.BAZ_VALUE); | |
| 758 assertEquals( | |
| 759 TestAllTypes.NestedEnum.FOO.getNumber(), | |
| 760 TestAllTypes.NestedEnum.FOO_VALUE); | |
| 761 } | |
| 762 | |
| 763 public void testNonNestedExtensionInitialization() { | |
| 764 assertTrue(NonNestedExtension.nonNestedExtension | |
| 765 .getMessageDefaultInstance() instanceof MyNonNestedExtension); | |
| 766 assertEquals("nonNestedExtension", | |
| 767 NonNestedExtension.nonNestedExtension.getDescriptor().getName()
); | |
| 768 } | |
| 769 | |
| 770 public void testNestedExtensionInitialization() { | |
| 771 assertTrue(MyNestedExtension.recursiveExtension.getMessageDefaultInstance() | |
| 772 instanceof MessageToBeExtended); | |
| 773 assertEquals("recursiveExtension", | |
| 774 MyNestedExtension.recursiveExtension.getDescriptor().getName())
; | |
| 775 } | |
| 776 | |
| 777 | |
| 778 public void testBaseMessageOrBuilder() { | |
| 779 // Mostly just makes sure the base interface exists and has some methods. | |
| 780 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 781 TestAllTypes message = builder.buildPartial(); | |
| 782 TestAllTypesOrBuilder builderAsInterface = (TestAllTypesOrBuilder) builder; | |
| 783 TestAllTypesOrBuilder messageAsInterface = (TestAllTypesOrBuilder) message; | |
| 784 | |
| 785 assertEquals( | |
| 786 messageAsInterface.getDefaultBool(), | |
| 787 messageAsInterface.getDefaultBool()); | |
| 788 assertEquals( | |
| 789 messageAsInterface.getOptionalDouble(), | |
| 790 messageAsInterface.getOptionalDouble()); | |
| 791 } | |
| 792 | |
| 793 public void testMessageOrBuilderGetters() { | |
| 794 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 795 | |
| 796 // single fields | |
| 797 assertSame(ForeignMessage.getDefaultInstance(), | |
| 798 builder.getOptionalForeignMessageOrBuilder()); | |
| 799 ForeignMessage.Builder subBuilder = | |
| 800 builder.getOptionalForeignMessageBuilder(); | |
| 801 assertSame(subBuilder, builder.getOptionalForeignMessageOrBuilder()); | |
| 802 | |
| 803 // repeated fields | |
| 804 ForeignMessage m0 = ForeignMessage.newBuilder().buildPartial(); | |
| 805 ForeignMessage m1 = ForeignMessage.newBuilder().buildPartial(); | |
| 806 ForeignMessage m2 = ForeignMessage.newBuilder().buildPartial(); | |
| 807 builder.addRepeatedForeignMessage(m0); | |
| 808 builder.addRepeatedForeignMessage(m1); | |
| 809 builder.addRepeatedForeignMessage(m2); | |
| 810 assertSame(m0, builder.getRepeatedForeignMessageOrBuilder(0)); | |
| 811 assertSame(m1, builder.getRepeatedForeignMessageOrBuilder(1)); | |
| 812 assertSame(m2, builder.getRepeatedForeignMessageOrBuilder(2)); | |
| 813 ForeignMessage.Builder b0 = builder.getRepeatedForeignMessageBuilder(0); | |
| 814 ForeignMessage.Builder b1 = builder.getRepeatedForeignMessageBuilder(1); | |
| 815 assertSame(b0, builder.getRepeatedForeignMessageOrBuilder(0)); | |
| 816 assertSame(b1, builder.getRepeatedForeignMessageOrBuilder(1)); | |
| 817 assertSame(m2, builder.getRepeatedForeignMessageOrBuilder(2)); | |
| 818 | |
| 819 List<? extends ForeignMessageOrBuilder> messageOrBuilderList = | |
| 820 builder.getRepeatedForeignMessageOrBuilderList(); | |
| 821 assertSame(b0, messageOrBuilderList.get(0)); | |
| 822 assertSame(b1, messageOrBuilderList.get(1)); | |
| 823 assertSame(m2, messageOrBuilderList.get(2)); | |
| 824 } | |
| 825 | |
| 826 public void testGetFieldBuilder() { | |
| 827 Descriptor descriptor = TestAllTypes.getDescriptor(); | |
| 828 | |
| 829 FieldDescriptor fieldDescriptor = | |
| 830 descriptor.findFieldByName("optional_nested_message"); | |
| 831 FieldDescriptor foreignFieldDescriptor = | |
| 832 descriptor.findFieldByName("optional_foreign_message"); | |
| 833 FieldDescriptor importFieldDescriptor = | |
| 834 descriptor.findFieldByName("optional_import_message"); | |
| 835 | |
| 836 // Mutate the message with new field builder | |
| 837 // Mutate nested message | |
| 838 TestAllTypes.Builder builder1 = TestAllTypes.newBuilder(); | |
| 839 Message.Builder fieldBuilder1 = builder1.newBuilderForField(fieldDescriptor) | |
| 840 .mergeFrom((Message) builder1.getField(fieldDescriptor)); | |
| 841 FieldDescriptor subFieldDescriptor1 = | |
| 842 fieldBuilder1.getDescriptorForType().findFieldByName("bb"); | |
| 843 fieldBuilder1.setField(subFieldDescriptor1, 1); | |
| 844 builder1.setField(fieldDescriptor, fieldBuilder1.build()); | |
| 845 | |
| 846 // Mutate foreign message | |
| 847 Message.Builder foreignFieldBuilder1 = builder1.newBuilderForField( | |
| 848 foreignFieldDescriptor) | |
| 849 .mergeFrom((Message) builder1.getField(foreignFieldDescriptor)); | |
| 850 FieldDescriptor subForeignFieldDescriptor1 = | |
| 851 foreignFieldBuilder1.getDescriptorForType().findFieldByName("c"); | |
| 852 foreignFieldBuilder1.setField(subForeignFieldDescriptor1, 2); | |
| 853 builder1.setField(foreignFieldDescriptor, foreignFieldBuilder1.build()); | |
| 854 | |
| 855 // Mutate import message | |
| 856 Message.Builder importFieldBuilder1 = builder1.newBuilderForField( | |
| 857 importFieldDescriptor) | |
| 858 .mergeFrom((Message) builder1.getField(importFieldDescriptor)); | |
| 859 FieldDescriptor subImportFieldDescriptor1 = | |
| 860 importFieldBuilder1.getDescriptorForType().findFieldByName("d"); | |
| 861 importFieldBuilder1.setField(subImportFieldDescriptor1, 3); | |
| 862 builder1.setField(importFieldDescriptor, importFieldBuilder1.build()); | |
| 863 | |
| 864 Message newMessage1 = builder1.build(); | |
| 865 | |
| 866 // Mutate the message with existing field builder | |
| 867 // Mutate nested message | |
| 868 TestAllTypes.Builder builder2 = TestAllTypes.newBuilder(); | |
| 869 Message.Builder fieldBuilder2 = builder2.getFieldBuilder(fieldDescriptor); | |
| 870 FieldDescriptor subFieldDescriptor2 = | |
| 871 fieldBuilder2.getDescriptorForType().findFieldByName("bb"); | |
| 872 fieldBuilder2.setField(subFieldDescriptor2, 1); | |
| 873 builder2.setField(fieldDescriptor, fieldBuilder2.build()); | |
| 874 | |
| 875 // Mutate foreign message | |
| 876 Message.Builder foreignFieldBuilder2 = builder2.newBuilderForField( | |
| 877 foreignFieldDescriptor) | |
| 878 .mergeFrom((Message) builder2.getField(foreignFieldDescriptor)); | |
| 879 FieldDescriptor subForeignFieldDescriptor2 = | |
| 880 foreignFieldBuilder2.getDescriptorForType().findFieldByName("c"); | |
| 881 foreignFieldBuilder2.setField(subForeignFieldDescriptor2, 2); | |
| 882 builder2.setField(foreignFieldDescriptor, foreignFieldBuilder2.build()); | |
| 883 | |
| 884 // Mutate import message | |
| 885 Message.Builder importFieldBuilder2 = builder2.newBuilderForField( | |
| 886 importFieldDescriptor) | |
| 887 .mergeFrom((Message) builder2.getField(importFieldDescriptor)); | |
| 888 FieldDescriptor subImportFieldDescriptor2 = | |
| 889 importFieldBuilder2.getDescriptorForType().findFieldByName("d"); | |
| 890 importFieldBuilder2.setField(subImportFieldDescriptor2, 3); | |
| 891 builder2.setField(importFieldDescriptor, importFieldBuilder2.build()); | |
| 892 | |
| 893 Message newMessage2 = builder2.build(); | |
| 894 | |
| 895 // These two messages should be equal. | |
| 896 assertEquals(newMessage1, newMessage2); | |
| 897 } | |
| 898 | |
| 899 public void testGetFieldBuilderWithInitializedValue() { | |
| 900 Descriptor descriptor = TestAllTypes.getDescriptor(); | |
| 901 FieldDescriptor fieldDescriptor = | |
| 902 descriptor.findFieldByName("optional_nested_message"); | |
| 903 | |
| 904 // Before setting field, builder is initialized by default value. | |
| 905 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 906 NestedMessage.Builder fieldBuilder = | |
| 907 (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor); | |
| 908 assertEquals(0, fieldBuilder.getBb()); | |
| 909 | |
| 910 // Setting field value with new field builder instance. | |
| 911 builder = TestAllTypes.newBuilder(); | |
| 912 NestedMessage.Builder newFieldBuilder = | |
| 913 builder.getOptionalNestedMessageBuilder(); | |
| 914 newFieldBuilder.setBb(2); | |
| 915 // Then get the field builder instance by getFieldBuilder(). | |
| 916 fieldBuilder = | |
| 917 (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor); | |
| 918 // It should contain new value. | |
| 919 assertEquals(2, fieldBuilder.getBb()); | |
| 920 // These two builder should be equal. | |
| 921 assertSame(fieldBuilder, newFieldBuilder); | |
| 922 } | |
| 923 | |
| 924 public void testGetFieldBuilderNotSupportedException() { | |
| 925 Descriptor descriptor = TestAllTypes.getDescriptor(); | |
| 926 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
| 927 try { | |
| 928 builder.getFieldBuilder(descriptor.findFieldByName("optional_int32")); | |
| 929 fail("Exception was not thrown"); | |
| 930 } catch (UnsupportedOperationException e) { | |
| 931 // We expect this exception. | |
| 932 } | |
| 933 try { | |
| 934 builder.getFieldBuilder( | |
| 935 descriptor.findFieldByName("optional_nested_enum")); | |
| 936 fail("Exception was not thrown"); | |
| 937 } catch (UnsupportedOperationException e) { | |
| 938 // We expect this exception. | |
| 939 } | |
| 940 try { | |
| 941 builder.getFieldBuilder(descriptor.findFieldByName("repeated_int32")); | |
| 942 fail("Exception was not thrown"); | |
| 943 } catch (UnsupportedOperationException e) { | |
| 944 // We expect this exception. | |
| 945 } | |
| 946 try { | |
| 947 builder.getFieldBuilder( | |
| 948 descriptor.findFieldByName("repeated_nested_enum")); | |
| 949 fail("Exception was not thrown"); | |
| 950 } catch (UnsupportedOperationException e) { | |
| 951 // We expect this exception. | |
| 952 } | |
| 953 try { | |
| 954 builder.getFieldBuilder( | |
| 955 descriptor.findFieldByName("repeated_nested_message")); | |
| 956 fail("Exception was not thrown"); | |
| 957 } catch (UnsupportedOperationException e) { | |
| 958 // We expect this exception. | |
| 959 } | |
| 960 } | |
| 961 } | |
| OLD | NEW |