OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
14 // distribution. | 14 // distribution. |
15 // * Neither the name of Google Inc. nor the names of its | 15 // * Neither the name of Google Inc. nor the names of its |
16 // contributors may be used to endorse or promote products derived from | 16 // contributors may be used to endorse or promote products derived from |
17 // this software without specific prior written permission. | 17 // this software without specific prior written permission. |
18 // | 18 // |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf.test; |
| 32 import com.google.protobuf.*; |
32 | 33 |
33 import com.google.protobuf.DescriptorProtos.DescriptorProto; | 34 import com.google.protobuf.DescriptorProtos.DescriptorProto; |
34 import com.google.protobuf.DescriptorProtos.EnumDescriptorProto; | 35 import com.google.protobuf.DescriptorProtos.EnumDescriptorProto; |
35 import com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto; | 36 import com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto; |
36 import com.google.protobuf.DescriptorProtos.FieldDescriptorProto; | 37 import com.google.protobuf.DescriptorProtos.FieldDescriptorProto; |
37 import com.google.protobuf.DescriptorProtos.FileDescriptorProto; | 38 import com.google.protobuf.DescriptorProtos.FileDescriptorProto; |
| 39 import com.google.protobuf.Descriptors.DescriptorValidationException; |
| 40 import com.google.protobuf.Descriptors.FileDescriptor; |
38 import com.google.protobuf.Descriptors.Descriptor; | 41 import com.google.protobuf.Descriptors.Descriptor; |
39 import com.google.protobuf.Descriptors.DescriptorValidationException; | 42 import com.google.protobuf.Descriptors.FieldDescriptor; |
40 import com.google.protobuf.Descriptors.EnumDescriptor; | 43 import com.google.protobuf.Descriptors.EnumDescriptor; |
41 import com.google.protobuf.Descriptors.EnumValueDescriptor; | 44 import com.google.protobuf.Descriptors.EnumValueDescriptor; |
42 import com.google.protobuf.Descriptors.FieldDescriptor; | 45 import com.google.protobuf.Descriptors.ServiceDescriptor; |
43 import com.google.protobuf.Descriptors.FileDescriptor; | |
44 import com.google.protobuf.Descriptors.MethodDescriptor; | 46 import com.google.protobuf.Descriptors.MethodDescriptor; |
45 import com.google.protobuf.Descriptors.OneofDescriptor; | 47 |
46 import com.google.protobuf.Descriptors.ServiceDescriptor; | |
47 import com.google.protobuf.test.UnittestImport; | 48 import com.google.protobuf.test.UnittestImport; |
48 import com.google.protobuf.test.UnittestImport.ImportEnum; | 49 import com.google.protobuf.test.UnittestImport.ImportEnum; |
49 import com.google.protobuf.test.UnittestImport.ImportEnumForMap; | 50 import com.google.protobuf.test.UnittestImport.ImportMessage; |
50 import protobuf_unittest.TestCustomOptions; | |
51 import protobuf_unittest.UnittestCustomOptions; | |
52 import protobuf_unittest.UnittestProto; | 51 import protobuf_unittest.UnittestProto; |
53 import protobuf_unittest.UnittestProto.ForeignEnum; | 52 import protobuf_unittest.UnittestProto.ForeignEnum; |
54 import protobuf_unittest.UnittestProto.ForeignMessage; | 53 import protobuf_unittest.UnittestProto.ForeignMessage; |
| 54 import protobuf_unittest.UnittestProto.TestAllTypes; |
55 import protobuf_unittest.UnittestProto.TestAllExtensions; | 55 import protobuf_unittest.UnittestProto.TestAllExtensions; |
56 import protobuf_unittest.UnittestProto.TestAllTypes; | |
57 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; | 56 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; |
58 import protobuf_unittest.UnittestProto.TestMultipleExtensionRanges; | |
59 import protobuf_unittest.UnittestProto.TestRequired; | 57 import protobuf_unittest.UnittestProto.TestRequired; |
60 import protobuf_unittest.UnittestProto.TestReservedFields; | |
61 import protobuf_unittest.UnittestProto.TestService; | 58 import protobuf_unittest.UnittestProto.TestService; |
| 59 import protobuf_unittest.UnittestCustomOptions; |
| 60 |
| 61 |
62 import junit.framework.TestCase; | 62 import junit.framework.TestCase; |
63 | 63 |
64 import java.util.Arrays; | 64 import java.util.Arrays; |
65 import java.util.Collections; | 65 import java.util.Collections; |
66 import java.util.List; | 66 import java.util.List; |
67 | 67 |
68 /** | 68 /** |
69 * Unit test for {@link Descriptors}. | 69 * Unit test for {@link Descriptors}. |
70 * | 70 * |
71 * @author kenton@google.com Kenton Varda | 71 * @author kenton@google.com Kenton Varda |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes")); | 108 assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes")); |
109 for (int i = 0; i < file.getMessageTypes().size(); i++) { | 109 for (int i = 0; i < file.getMessageTypes().size(); i++) { |
110 assertEquals(i, file.getMessageTypes().get(i).getIndex()); | 110 assertEquals(i, file.getMessageTypes().get(i).getIndex()); |
111 } | 111 } |
112 | 112 |
113 EnumDescriptor enumType = ForeignEnum.getDescriptor(); | 113 EnumDescriptor enumType = ForeignEnum.getDescriptor(); |
114 assertEquals(enumType, file.getEnumTypes().get(0)); | 114 assertEquals(enumType, file.getEnumTypes().get(0)); |
115 assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); | 115 assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); |
116 assertNull(file.findEnumTypeByName("NoSuchType")); | 116 assertNull(file.findEnumTypeByName("NoSuchType")); |
117 assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); | 117 assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); |
118 assertEquals(Arrays.asList(ImportEnum.getDescriptor(), | 118 assertEquals(Arrays.asList(ImportEnum.getDescriptor()), |
119 ImportEnumForMap.getDescriptor()), | |
120 UnittestImport.getDescriptor().getEnumTypes()); | 119 UnittestImport.getDescriptor().getEnumTypes()); |
121 for (int i = 0; i < file.getEnumTypes().size(); i++) { | 120 for (int i = 0; i < file.getEnumTypes().size(); i++) { |
122 assertEquals(i, file.getEnumTypes().get(i).getIndex()); | 121 assertEquals(i, file.getEnumTypes().get(i).getIndex()); |
123 } | 122 } |
124 | 123 |
125 ServiceDescriptor service = TestService.getDescriptor(); | 124 ServiceDescriptor service = TestService.getDescriptor(); |
126 assertEquals(service, file.getServices().get(0)); | 125 assertEquals(service, file.getServices().get(0)); |
127 assertEquals(service, file.findServiceByName("TestService")); | 126 assertEquals(service, file.findServiceByName("TestService")); |
128 assertNull(file.findServiceByName("NoSuchType")); | 127 assertNull(file.findServiceByName("NoSuchType")); |
129 assertNull(file.findServiceByName("protobuf_unittest.TestService")); | 128 assertNull(file.findServiceByName("protobuf_unittest.TestService")); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 FieldDescriptor repeatedField = | 265 FieldDescriptor repeatedField = |
267 TestAllTypes.getDescriptor().findFieldByName("repeated_int32"); | 266 TestAllTypes.getDescriptor().findFieldByName("repeated_int32"); |
268 | 267 |
269 assertTrue(requiredField.isRequired()); | 268 assertTrue(requiredField.isRequired()); |
270 assertFalse(requiredField.isRepeated()); | 269 assertFalse(requiredField.isRepeated()); |
271 assertFalse(optionalField.isRequired()); | 270 assertFalse(optionalField.isRequired()); |
272 assertFalse(optionalField.isRepeated()); | 271 assertFalse(optionalField.isRepeated()); |
273 assertFalse(repeatedField.isRequired()); | 272 assertFalse(repeatedField.isRequired()); |
274 assertTrue(repeatedField.isRepeated()); | 273 assertTrue(repeatedField.isRepeated()); |
275 } | 274 } |
276 | |
277 public void testFieldDescriptorJsonName() throws Exception { | |
278 FieldDescriptor requiredField = TestRequired.getDescriptor().findFieldByName
("a"); | |
279 FieldDescriptor optionalField = TestAllTypes.getDescriptor().findFieldByName
("optional_int32"); | |
280 FieldDescriptor repeatedField = TestAllTypes.getDescriptor().findFieldByName
("repeated_int32"); | |
281 assertEquals("a", requiredField.getJsonName()); | |
282 assertEquals("optionalInt32", optionalField.getJsonName()); | |
283 assertEquals("repeatedInt32", repeatedField.getJsonName()); | |
284 } | |
285 | 275 |
286 public void testFieldDescriptorDefault() throws Exception { | 276 public void testFieldDescriptorDefault() throws Exception { |
287 Descriptor d = TestAllTypes.getDescriptor(); | 277 Descriptor d = TestAllTypes.getDescriptor(); |
288 assertFalse(d.findFieldByName("optional_int32").hasDefaultValue()); | 278 assertFalse(d.findFieldByName("optional_int32").hasDefaultValue()); |
289 assertEquals(0, d.findFieldByName("optional_int32").getDefaultValue()); | 279 assertEquals(0, d.findFieldByName("optional_int32").getDefaultValue()); |
290 assertTrue(d.findFieldByName("default_int32").hasDefaultValue()); | 280 assertTrue(d.findFieldByName("default_int32").hasDefaultValue()); |
291 assertEquals(41, d.findFieldByName("default_int32").getDefaultValue()); | 281 assertEquals(41, d.findFieldByName("default_int32").getDefaultValue()); |
292 | 282 |
293 d = TestExtremeDefaultValues.getDescriptor(); | 283 d = TestExtremeDefaultValues.getDescriptor(); |
294 assertEquals( | 284 assertEquals( |
295 ByteString.copyFrom( | 285 ByteString.copyFrom( |
296 "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.ISO_8859_1)), | 286 "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes("ISO-8859-1")), |
297 d.findFieldByName("escaped_bytes").getDefaultValue()); | 287 d.findFieldByName("escaped_bytes").getDefaultValue()); |
298 assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue()); | 288 assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue()); |
299 assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue()); | 289 assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue()); |
300 } | 290 } |
301 | 291 |
302 public void testEnumDescriptor() throws Exception { | 292 public void testEnumDescriptor() throws Exception { |
303 EnumDescriptor enumType = ForeignEnum.getDescriptor(); | 293 EnumDescriptor enumType = ForeignEnum.getDescriptor(); |
304 EnumDescriptor nestedType = TestAllTypes.NestedEnum.getDescriptor(); | 294 EnumDescriptor nestedType = TestAllTypes.NestedEnum.getDescriptor(); |
305 | 295 |
306 assertEquals("ForeignEnum", enumType.getName()); | 296 assertEquals("ForeignEnum", enumType.getName()); |
307 assertEquals("protobuf_unittest.ForeignEnum", enumType.getFullName()); | 297 assertEquals("protobuf_unittest.ForeignEnum", enumType.getFullName()); |
308 assertEquals(UnittestProto.getDescriptor(), enumType.getFile()); | 298 assertEquals(UnittestProto.getDescriptor(), enumType.getFile()); |
309 assertNull(enumType.getContainingType()); | 299 assertNull(enumType.getContainingType()); |
310 assertEquals(DescriptorProtos.EnumOptions.getDefaultInstance(), | 300 assertEquals(DescriptorProtos.EnumOptions.getDefaultInstance(), |
311 enumType.getOptions()); | 301 enumType.getOptions()); |
312 | 302 |
313 assertEquals("NestedEnum", nestedType.getName()); | 303 assertEquals("NestedEnum", nestedType.getName()); |
314 assertEquals("protobuf_unittest.TestAllTypes.NestedEnum", | 304 assertEquals("protobuf_unittest.TestAllTypes.NestedEnum", |
315 nestedType.getFullName()); | 305 nestedType.getFullName()); |
316 assertEquals(UnittestProto.getDescriptor(), nestedType.getFile()); | 306 assertEquals(UnittestProto.getDescriptor(), nestedType.getFile()); |
317 assertEquals(TestAllTypes.getDescriptor(), nestedType.getContainingType()); | 307 assertEquals(TestAllTypes.getDescriptor(), nestedType.getContainingType()); |
318 | 308 |
319 EnumValueDescriptor value = ForeignEnum.FOREIGN_FOO.getValueDescriptor(); | 309 EnumValueDescriptor value = ForeignEnum.FOREIGN_FOO.getValueDescriptor(); |
320 assertEquals(value, enumType.getValues().get(0)); | 310 assertEquals(value, enumType.getValues().get(0)); |
321 assertEquals("FOREIGN_FOO", value.getName()); | 311 assertEquals("FOREIGN_FOO", value.getName()); |
322 assertEquals("FOREIGN_FOO", value.toString()); | |
323 assertEquals(4, value.getNumber()); | 312 assertEquals(4, value.getNumber()); |
324 assertEquals(value, enumType.findValueByName("FOREIGN_FOO")); | 313 assertEquals(value, enumType.findValueByName("FOREIGN_FOO")); |
325 assertEquals(value, enumType.findValueByNumber(4)); | 314 assertEquals(value, enumType.findValueByNumber(4)); |
326 assertNull(enumType.findValueByName("NO_SUCH_VALUE")); | 315 assertNull(enumType.findValueByName("NO_SUCH_VALUE")); |
327 for (int i = 0; i < enumType.getValues().size(); i++) { | 316 for (int i = 0; i < enumType.getValues().size(); i++) { |
328 assertEquals(i, enumType.getValues().get(i).getIndex()); | 317 assertEquals(i, enumType.getValues().get(i).getIndex()); |
329 } | 318 } |
330 } | 319 } |
331 | 320 |
332 public void testServiceDescriptor() throws Exception { | 321 public void testServiceDescriptor() throws Exception { |
333 ServiceDescriptor service = TestService.getDescriptor(); | 322 ServiceDescriptor service = TestService.getDescriptor(); |
334 | 323 |
335 assertEquals("TestService", service.getName()); | 324 assertEquals("TestService", service.getName()); |
336 assertEquals("protobuf_unittest.TestService", service.getFullName()); | 325 assertEquals("protobuf_unittest.TestService", service.getFullName()); |
337 assertEquals(UnittestProto.getDescriptor(), service.getFile()); | 326 assertEquals(UnittestProto.getDescriptor(), service.getFile()); |
338 | 327 |
| 328 assertEquals(2, service.getMethods().size()); |
339 | 329 |
340 MethodDescriptor fooMethod = service.getMethods().get(0); | 330 MethodDescriptor fooMethod = service.getMethods().get(0); |
341 assertEquals("Foo", fooMethod.getName()); | 331 assertEquals("Foo", fooMethod.getName()); |
342 assertEquals(UnittestProto.FooRequest.getDescriptor(), | 332 assertEquals(UnittestProto.FooRequest.getDescriptor(), |
343 fooMethod.getInputType()); | 333 fooMethod.getInputType()); |
344 assertEquals(UnittestProto.FooResponse.getDescriptor(), | 334 assertEquals(UnittestProto.FooResponse.getDescriptor(), |
345 fooMethod.getOutputType()); | 335 fooMethod.getOutputType()); |
346 assertEquals(fooMethod, service.findMethodByName("Foo")); | 336 assertEquals(fooMethod, service.findMethodByName("Foo")); |
347 | 337 |
348 MethodDescriptor barMethod = service.getMethods().get(1); | 338 MethodDescriptor barMethod = service.getMethods().get(1); |
349 assertEquals("Bar", barMethod.getName()); | 339 assertEquals("Bar", barMethod.getName()); |
350 assertEquals(UnittestProto.BarRequest.getDescriptor(), | 340 assertEquals(UnittestProto.BarRequest.getDescriptor(), |
351 barMethod.getInputType()); | 341 barMethod.getInputType()); |
352 assertEquals(UnittestProto.BarResponse.getDescriptor(), | 342 assertEquals(UnittestProto.BarResponse.getDescriptor(), |
353 barMethod.getOutputType()); | 343 barMethod.getOutputType()); |
354 assertEquals(barMethod, service.findMethodByName("Bar")); | 344 assertEquals(barMethod, service.findMethodByName("Bar")); |
355 | 345 |
356 assertNull(service.findMethodByName("NoSuchMethod")); | 346 assertNull(service.findMethodByName("NoSuchMethod")); |
357 | 347 |
358 for (int i = 0; i < service.getMethods().size(); i++) { | 348 for (int i = 0; i < service.getMethods().size(); i++) { |
359 assertEquals(i, service.getMethods().get(i).getIndex()); | 349 assertEquals(i, service.getMethods().get(i).getIndex()); |
360 } | 350 } |
361 } | 351 } |
362 | 352 |
363 | 353 |
364 public void testCustomOptions() throws Exception { | 354 public void testCustomOptions() throws Exception { |
365 // Get the descriptor indirectly from a dependent proto class. This is to | |
366 // ensure that when a proto class is loaded, custom options defined in its | |
367 // dependencies are also properly initialized. | |
368 Descriptor descriptor = | 355 Descriptor descriptor = |
369 TestCustomOptions.TestMessageWithCustomOptionsContainer.getDescriptor() | 356 UnittestCustomOptions.TestMessageWithCustomOptions.getDescriptor(); |
370 .findFieldByName("field").getMessageType(); | |
371 | 357 |
372 assertTrue( | 358 assertTrue( |
373 descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); | 359 descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); |
374 assertEquals(Integer.valueOf(-56), | 360 assertEquals(Integer.valueOf(-56), |
375 descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); | 361 descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); |
376 | 362 |
377 FieldDescriptor field = descriptor.findFieldByName("field1"); | 363 FieldDescriptor field = descriptor.findFieldByName("field1"); |
378 assertNotNull(field); | 364 assertNotNull(field); |
379 | 365 |
380 assertTrue( | 366 assertTrue( |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 .addField(FieldDescriptorProto.newBuilder() | 483 .addField(FieldDescriptorProto.newBuilder() |
498 .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) | 484 .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) |
499 .setTypeName("Foo") | 485 .setTypeName("Foo") |
500 .setName("Foo") | 486 .setName("Foo") |
501 .setNumber(1) | 487 .setNumber(1) |
502 .build()) | 488 .build()) |
503 .build()) | 489 .build()) |
504 .build(); | 490 .build(); |
505 // translate and crosslink | 491 // translate and crosslink |
506 FileDescriptor file = | 492 FileDescriptor file = |
507 Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, | 493 Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, |
508 new FileDescriptor[0]); | 494 new FileDescriptor[0]); |
509 // verify resulting descriptors | 495 // verify resulting descriptors |
510 assertNotNull(file); | 496 assertNotNull(file); |
511 List<Descriptor> msglist = file.getMessageTypes(); | 497 List<Descriptor> msglist = file.getMessageTypes(); |
512 assertNotNull(msglist); | 498 assertNotNull(msglist); |
513 assertTrue(msglist.size() == 2); | 499 assertTrue(msglist.size() == 2); |
514 boolean barFound = false; | 500 boolean barFound = false; |
515 for (Descriptor desc : msglist) { | 501 for (Descriptor desc : msglist) { |
516 if (desc.getName().equals("Bar")) { | 502 if (desc.getName().equals("Bar")) { |
517 barFound = true; | 503 barFound = true; |
518 assertNotNull(desc.getFields()); | 504 assertNotNull(desc.getFields()); |
519 List<FieldDescriptor> fieldlist = desc.getFields(); | 505 List<FieldDescriptor> fieldlist = desc.getFields(); |
520 assertNotNull(fieldlist); | 506 assertNotNull(fieldlist); |
521 assertTrue(fieldlist.size() == 1); | 507 assertTrue(fieldlist.size() == 1); |
522 assertTrue(fieldlist.get(0).getType() == FieldDescriptor.Type.MESSAGE); | 508 assertTrue(fieldlist.get(0).getType() == FieldDescriptor.Type.MESSAGE); |
523 assertTrue(fieldlist.get(0).getMessageType().getName().equals("Foo")); | 509 assertTrue(fieldlist.get(0).getMessageType().getName().equals("Foo")); |
524 } | 510 } |
525 } | 511 } |
526 assertTrue(barFound); | 512 assertTrue(barFound); |
527 } | 513 } |
528 | 514 |
529 public void testDependencyOrder() throws Exception { | |
530 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() | |
531 .setName("foo.proto").build(); | |
532 FileDescriptorProto barProto = FileDescriptorProto.newBuilder() | |
533 .setName("bar.proto") | |
534 .addDependency("foo.proto") | |
535 .build(); | |
536 FileDescriptorProto bazProto = FileDescriptorProto.newBuilder() | |
537 .setName("baz.proto") | |
538 .addDependency("foo.proto") | |
539 .addDependency("bar.proto") | |
540 .addPublicDependency(0) | |
541 .addPublicDependency(1) | |
542 .build(); | |
543 FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, | |
544 new FileDescriptor[0]); | |
545 FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto, | |
546 new FileDescriptor[] {fooFile}); | |
547 | |
548 // Items in the FileDescriptor array can be in any order. | |
549 Descriptors.FileDescriptor.buildFrom(bazProto, | |
550 new FileDescriptor[] {fooFile, barFile}); | |
551 Descriptors.FileDescriptor.buildFrom(bazProto, | |
552 new FileDescriptor[] {barFile, fooFile}); | |
553 } | |
554 | |
555 public void testInvalidPublicDependency() throws Exception { | 515 public void testInvalidPublicDependency() throws Exception { |
556 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() | 516 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() |
557 .setName("foo.proto").build(); | 517 .setName("foo.proto") .build(); |
558 FileDescriptorProto barProto = FileDescriptorProto.newBuilder() | 518 FileDescriptorProto barProto = FileDescriptorProto.newBuilder() |
559 .setName("boo.proto") | 519 .setName("boo.proto") |
560 .addDependency("foo.proto") | 520 .addDependency("foo.proto") |
561 .addPublicDependency(1) // Error, should be 0. | 521 .addPublicDependency(1) // Error, should be 0. |
562 .build(); | 522 .build(); |
563 FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, | 523 FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, |
564 new FileDescriptor[0]); | 524 new FileDescriptor[0]); |
565 try { | 525 try { |
566 Descriptors.FileDescriptor.buildFrom(barProto, | 526 Descriptors.FileDescriptor.buildFrom(barProto, |
567 new FileDescriptor[] {fooFile}); | 527 new FileDescriptor[] {fooFile}); |
568 fail("DescriptorValidationException expected"); | 528 fail("DescriptorValidationException expected"); |
569 } catch (DescriptorValidationException e) { | 529 } catch (DescriptorValidationException e) { |
570 assertTrue( | 530 assertTrue( |
571 e.getMessage().indexOf("Invalid public dependency index.") != -1); | 531 e.getMessage().indexOf("Invalid public dependency index.") != -1); |
572 } | 532 } |
573 } | 533 } |
574 | 534 |
575 public void testUnknownFieldsDenied() throws Exception { | |
576 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() | |
577 .setName("foo.proto") | |
578 .addMessageType(DescriptorProto.newBuilder() | |
579 .setName("Foo") | |
580 .addField(FieldDescriptorProto.newBuilder() | |
581 .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) | |
582 .setTypeName("Bar") | |
583 .setName("bar") | |
584 .setNumber(1))) | |
585 .build(); | |
586 | |
587 try { | |
588 Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); | |
589 fail("DescriptorValidationException expected"); | |
590 } catch (DescriptorValidationException e) { | |
591 assertTrue(e.getMessage().indexOf("Bar") != -1); | |
592 assertTrue(e.getMessage().indexOf("is not defined") != -1); | |
593 } | |
594 } | |
595 | |
596 public void testUnknownFieldsAllowed() throws Exception { | |
597 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() | |
598 .setName("foo.proto") | |
599 .addDependency("bar.proto") | |
600 .addMessageType(DescriptorProto.newBuilder() | |
601 .setName("Foo") | |
602 .addField(FieldDescriptorProto.newBuilder() | |
603 .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) | |
604 .setTypeName("Bar") | |
605 .setName("bar") | |
606 .setNumber(1))) | |
607 .build(); | |
608 Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0], true); | |
609 } | |
610 | |
611 public void testHiddenDependency() throws Exception { | 535 public void testHiddenDependency() throws Exception { |
612 FileDescriptorProto barProto = FileDescriptorProto.newBuilder() | 536 FileDescriptorProto barProto = FileDescriptorProto.newBuilder() |
613 .setName("bar.proto") | 537 .setName("bar.proto") |
614 .addMessageType(DescriptorProto.newBuilder().setName("Bar")) | 538 .addMessageType(DescriptorProto.newBuilder().setName("Bar")) |
615 .build(); | 539 .build(); |
616 FileDescriptorProto forwardProto = FileDescriptorProto.newBuilder() | 540 FileDescriptorProto forwardProto = FileDescriptorProto.newBuilder() |
617 .setName("forward.proto") | 541 .setName("forward.proto") |
618 .addDependency("bar.proto") | 542 .addDependency("bar.proto") |
619 .build(); | 543 .build(); |
620 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() | 544 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 .setName("bar") | 588 .setName("bar") |
665 .setNumber(1))) | 589 .setNumber(1))) |
666 .build(); | 590 .build(); |
667 FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom( | 591 FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom( |
668 barProto, new FileDescriptor[0]); | 592 barProto, new FileDescriptor[0]); |
669 FileDescriptor forwardFile = Descriptors.FileDescriptor.buildFrom( | 593 FileDescriptor forwardFile = Descriptors.FileDescriptor.buildFrom( |
670 forwardProto, new FileDescriptor[]{barFile}); | 594 forwardProto, new FileDescriptor[]{barFile}); |
671 Descriptors.FileDescriptor.buildFrom( | 595 Descriptors.FileDescriptor.buildFrom( |
672 fooProto, new FileDescriptor[] {forwardFile}); | 596 fooProto, new FileDescriptor[] {forwardFile}); |
673 } | 597 } |
674 | 598 |
675 /** | 599 /** |
676 * Tests the translate/crosslink for an example with a more complex namespace | 600 * Tests the translate/crosslink for an example with a more complex namespace |
677 * referencing. | 601 * referencing. |
678 */ | 602 */ |
679 public void testComplexNamespacePublicDependency() throws Exception { | 603 public void testComplexNamespacePublicDependency() throws Exception { |
680 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() | 604 FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() |
681 .setName("bar.proto") | 605 .setName("bar.proto") |
682 .setPackage("a.b.c.d.bar.shared") | 606 .setPackage("a.b.c.d.bar.shared") |
683 .addEnumType(EnumDescriptorProto.newBuilder() | 607 .addEnumType(EnumDescriptorProto.newBuilder() |
684 .setName("MyEnum") | 608 .setName("MyEnum") |
(...skipping 28 matching lines...) Expand all Loading... |
713 assertNotNull(desc.getFields()); | 637 assertNotNull(desc.getFields()); |
714 List<FieldDescriptor> fieldlist = desc.getFields(); | 638 List<FieldDescriptor> fieldlist = desc.getFields(); |
715 assertNotNull(fieldlist); | 639 assertNotNull(fieldlist); |
716 assertTrue(fieldlist.size() == 1); | 640 assertTrue(fieldlist.size() == 1); |
717 FieldDescriptor field = fieldlist.get(0); | 641 FieldDescriptor field = fieldlist.get(0); |
718 assertTrue(field.getType() == FieldDescriptor.Type.ENUM); | 642 assertTrue(field.getType() == FieldDescriptor.Type.ENUM); |
719 assertTrue(field.getEnumType().getName().equals("MyEnum")); | 643 assertTrue(field.getEnumType().getName().equals("MyEnum")); |
720 assertTrue(field.getEnumType().getFile().getName().equals("bar.proto")); | 644 assertTrue(field.getEnumType().getFile().getName().equals("bar.proto")); |
721 assertTrue(field.getEnumType().getFile().getPackage().equals( | 645 assertTrue(field.getEnumType().getFile().getPackage().equals( |
722 "a.b.c.d.bar.shared")); | 646 "a.b.c.d.bar.shared")); |
723 } | 647 } |
724 } | |
725 | |
726 public void testOneofDescriptor() throws Exception { | |
727 Descriptor messageType = TestAllTypes.getDescriptor(); | |
728 FieldDescriptor field = | |
729 messageType.findFieldByName("oneof_nested_message"); | |
730 OneofDescriptor oneofDescriptor = field.getContainingOneof(); | |
731 assertNotNull(oneofDescriptor); | |
732 assertSame(oneofDescriptor, messageType.getOneofs().get(0)); | |
733 assertEquals("oneof_field", oneofDescriptor.getName()); | |
734 | |
735 assertEquals(4, oneofDescriptor.getFieldCount()); | |
736 assertSame(oneofDescriptor.getField(1), field); | |
737 | |
738 assertEquals(4, oneofDescriptor.getFields().size()); | |
739 assertEquals(oneofDescriptor.getFields().get(1), field); | |
740 } | |
741 | |
742 public void testMessageDescriptorExtensions() throws Exception { | |
743 assertFalse(TestAllTypes.getDescriptor().isExtendable()); | |
744 assertTrue(TestAllExtensions.getDescriptor().isExtendable()); | |
745 assertTrue(TestMultipleExtensionRanges.getDescriptor().isExtendable()); | |
746 | |
747 assertFalse(TestAllTypes.getDescriptor().isExtensionNumber(3)); | |
748 assertTrue(TestAllExtensions.getDescriptor().isExtensionNumber(3)); | |
749 assertTrue(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(42)
); | |
750 assertFalse(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(43
)); | |
751 assertFalse(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(41
42)); | |
752 assertTrue(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(414
3)); | |
753 } | |
754 | |
755 public void testReservedFields() { | |
756 Descriptor d = TestReservedFields.getDescriptor(); | |
757 assertTrue(d.isReservedNumber(2)); | |
758 assertFalse(d.isReservedNumber(8)); | |
759 assertTrue(d.isReservedNumber(9)); | |
760 assertTrue(d.isReservedNumber(10)); | |
761 assertTrue(d.isReservedNumber(11)); | |
762 assertFalse(d.isReservedNumber(12)); | |
763 assertFalse(d.isReservedName("foo")); | |
764 assertTrue(d.isReservedName("bar")); | |
765 assertTrue(d.isReservedName("baz")); | |
766 } | |
767 | |
768 public void testToString() { | |
769 assertEquals("protobuf_unittest.TestAllTypes.optional_uint64", | |
770 UnittestProto.TestAllTypes.getDescriptor().findFieldByNumber( | |
771 UnittestProto.TestAllTypes.OPTIONAL_UINT64_FIELD_NUMBER).toString())
; | |
772 } | |
773 | |
774 public void testPackedEnumField() throws Exception { | |
775 FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() | |
776 .setName("foo.proto") | |
777 .addEnumType(EnumDescriptorProto.newBuilder() | |
778 .setName("Enum") | |
779 .addValue(EnumValueDescriptorProto.newBuilder() | |
780 .setName("FOO") | |
781 .setNumber(1) | |
782 .build()) | |
783 .build()) | |
784 .addMessageType(DescriptorProto.newBuilder() | |
785 .setName("Message") | |
786 .addField(FieldDescriptorProto.newBuilder() | |
787 .setName("foo") | |
788 .setTypeName("Enum") | |
789 .setNumber(1) | |
790 .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) | |
791 .setOptions(DescriptorProtos.FieldOptions.newBuilder() | |
792 .setPacked(true) | |
793 .build()) | |
794 .build()) | |
795 .build()) | |
796 .build(); | |
797 Descriptors.FileDescriptor.buildFrom( | |
798 fileDescriptorProto, new FileDescriptor[0]); | |
799 } | 648 } |
800 } | 649 } |
OLD | NEW |