/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/test/java/com/google/protobuf/DescriptorsTest.java

http://github.com/tomahawk-player/tomahawk · Java · 460 lines · 345 code · 64 blank · 51 comment · 16 complexity · 6c9aecc21a8c0d8f5577c6e5d6abce14 MD5 · raw file

  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. package com.google.protobuf;
  31. import com.google.protobuf.DescriptorProtos.DescriptorProto;
  32. import com.google.protobuf.DescriptorProtos.FieldDescriptorProto;
  33. import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
  34. import com.google.protobuf.Descriptors.DescriptorValidationException;
  35. import com.google.protobuf.Descriptors.FileDescriptor;
  36. import com.google.protobuf.Descriptors.Descriptor;
  37. import com.google.protobuf.Descriptors.FieldDescriptor;
  38. import com.google.protobuf.Descriptors.EnumDescriptor;
  39. import com.google.protobuf.Descriptors.EnumValueDescriptor;
  40. import com.google.protobuf.Descriptors.ServiceDescriptor;
  41. import com.google.protobuf.Descriptors.MethodDescriptor;
  42. import com.google.protobuf.test.UnittestImport;
  43. import com.google.protobuf.test.UnittestImport.ImportEnum;
  44. import com.google.protobuf.test.UnittestImport.ImportMessage;
  45. import protobuf_unittest.UnittestProto;
  46. import protobuf_unittest.UnittestProto.ForeignEnum;
  47. import protobuf_unittest.UnittestProto.ForeignMessage;
  48. import protobuf_unittest.UnittestProto.TestAllTypes;
  49. import protobuf_unittest.UnittestProto.TestAllExtensions;
  50. import protobuf_unittest.UnittestProto.TestExtremeDefaultValues;
  51. import protobuf_unittest.UnittestProto.TestRequired;
  52. import protobuf_unittest.UnittestProto.TestService;
  53. import protobuf_unittest.UnittestCustomOptions;
  54. import junit.framework.TestCase;
  55. import java.util.Arrays;
  56. import java.util.Collections;
  57. /**
  58. * Unit test for {@link Descriptors}.
  59. *
  60. * @author kenton@google.com Kenton Varda
  61. */
  62. public class DescriptorsTest extends TestCase {
  63. // Regression test for bug where referencing a FieldDescriptor.Type value
  64. // before a FieldDescriptorProto.Type value would yield a
  65. // ExceptionInInitializerError.
  66. @SuppressWarnings("unused")
  67. private static final Object STATIC_INIT_TEST = FieldDescriptor.Type.BOOL;
  68. public void testFieldTypeEnumMapping() throws Exception {
  69. assertEquals(FieldDescriptor.Type.values().length,
  70. FieldDescriptorProto.Type.values().length);
  71. for (FieldDescriptor.Type type : FieldDescriptor.Type.values()) {
  72. FieldDescriptorProto.Type protoType = type.toProto();
  73. assertEquals("TYPE_" + type.name(), protoType.name());
  74. assertEquals(type, FieldDescriptor.Type.valueOf(protoType));
  75. }
  76. }
  77. public void testFileDescriptor() throws Exception {
  78. FileDescriptor file = UnittestProto.getDescriptor();
  79. assertEquals("google/protobuf/unittest.proto", file.getName());
  80. assertEquals("protobuf_unittest", file.getPackage());
  81. assertEquals("UnittestProto", file.getOptions().getJavaOuterClassname());
  82. assertEquals("google/protobuf/unittest.proto",
  83. file.toProto().getName());
  84. assertEquals(Arrays.asList(UnittestImport.getDescriptor()),
  85. file.getDependencies());
  86. Descriptor messageType = TestAllTypes.getDescriptor();
  87. assertEquals(messageType, file.getMessageTypes().get(0));
  88. assertEquals(messageType, file.findMessageTypeByName("TestAllTypes"));
  89. assertNull(file.findMessageTypeByName("NoSuchType"));
  90. assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes"));
  91. for (int i = 0; i < file.getMessageTypes().size(); i++) {
  92. assertEquals(i, file.getMessageTypes().get(i).getIndex());
  93. }
  94. EnumDescriptor enumType = ForeignEnum.getDescriptor();
  95. assertEquals(enumType, file.getEnumTypes().get(0));
  96. assertEquals(enumType, file.findEnumTypeByName("ForeignEnum"));
  97. assertNull(file.findEnumTypeByName("NoSuchType"));
  98. assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum"));
  99. assertEquals(Arrays.asList(ImportEnum.getDescriptor()),
  100. UnittestImport.getDescriptor().getEnumTypes());
  101. for (int i = 0; i < file.getEnumTypes().size(); i++) {
  102. assertEquals(i, file.getEnumTypes().get(i).getIndex());
  103. }
  104. ServiceDescriptor service = TestService.getDescriptor();
  105. assertEquals(service, file.getServices().get(0));
  106. assertEquals(service, file.findServiceByName("TestService"));
  107. assertNull(file.findServiceByName("NoSuchType"));
  108. assertNull(file.findServiceByName("protobuf_unittest.TestService"));
  109. assertEquals(Collections.emptyList(),
  110. UnittestImport.getDescriptor().getServices());
  111. for (int i = 0; i < file.getServices().size(); i++) {
  112. assertEquals(i, file.getServices().get(i).getIndex());
  113. }
  114. FieldDescriptor extension =
  115. UnittestProto.optionalInt32Extension.getDescriptor();
  116. assertEquals(extension, file.getExtensions().get(0));
  117. assertEquals(extension,
  118. file.findExtensionByName("optional_int32_extension"));
  119. assertNull(file.findExtensionByName("no_such_ext"));
  120. assertNull(file.findExtensionByName(
  121. "protobuf_unittest.optional_int32_extension"));
  122. assertEquals(Collections.emptyList(),
  123. UnittestImport.getDescriptor().getExtensions());
  124. for (int i = 0; i < file.getExtensions().size(); i++) {
  125. assertEquals(i, file.getExtensions().get(i).getIndex());
  126. }
  127. }
  128. public void testDescriptor() throws Exception {
  129. Descriptor messageType = TestAllTypes.getDescriptor();
  130. Descriptor nestedType = TestAllTypes.NestedMessage.getDescriptor();
  131. assertEquals("TestAllTypes", messageType.getName());
  132. assertEquals("protobuf_unittest.TestAllTypes", messageType.getFullName());
  133. assertEquals(UnittestProto.getDescriptor(), messageType.getFile());
  134. assertNull(messageType.getContainingType());
  135. assertEquals(DescriptorProtos.MessageOptions.getDefaultInstance(),
  136. messageType.getOptions());
  137. assertEquals("TestAllTypes", messageType.toProto().getName());
  138. assertEquals("NestedMessage", nestedType.getName());
  139. assertEquals("protobuf_unittest.TestAllTypes.NestedMessage",
  140. nestedType.getFullName());
  141. assertEquals(UnittestProto.getDescriptor(), nestedType.getFile());
  142. assertEquals(messageType, nestedType.getContainingType());
  143. FieldDescriptor field = messageType.getFields().get(0);
  144. assertEquals("optional_int32", field.getName());
  145. assertEquals(field, messageType.findFieldByName("optional_int32"));
  146. assertNull(messageType.findFieldByName("no_such_field"));
  147. assertEquals(field, messageType.findFieldByNumber(1));
  148. assertNull(messageType.findFieldByNumber(571283));
  149. for (int i = 0; i < messageType.getFields().size(); i++) {
  150. assertEquals(i, messageType.getFields().get(i).getIndex());
  151. }
  152. assertEquals(nestedType, messageType.getNestedTypes().get(0));
  153. assertEquals(nestedType, messageType.findNestedTypeByName("NestedMessage"));
  154. assertNull(messageType.findNestedTypeByName("NoSuchType"));
  155. for (int i = 0; i < messageType.getNestedTypes().size(); i++) {
  156. assertEquals(i, messageType.getNestedTypes().get(i).getIndex());
  157. }
  158. EnumDescriptor enumType = TestAllTypes.NestedEnum.getDescriptor();
  159. assertEquals(enumType, messageType.getEnumTypes().get(0));
  160. assertEquals(enumType, messageType.findEnumTypeByName("NestedEnum"));
  161. assertNull(messageType.findEnumTypeByName("NoSuchType"));
  162. for (int i = 0; i < messageType.getEnumTypes().size(); i++) {
  163. assertEquals(i, messageType.getEnumTypes().get(i).getIndex());
  164. }
  165. }
  166. public void testFieldDescriptor() throws Exception {
  167. Descriptor messageType = TestAllTypes.getDescriptor();
  168. FieldDescriptor primitiveField =
  169. messageType.findFieldByName("optional_int32");
  170. FieldDescriptor enumField =
  171. messageType.findFieldByName("optional_nested_enum");
  172. FieldDescriptor messageField =
  173. messageType.findFieldByName("optional_foreign_message");
  174. FieldDescriptor cordField =
  175. messageType.findFieldByName("optional_cord");
  176. FieldDescriptor extension =
  177. UnittestProto.optionalInt32Extension.getDescriptor();
  178. FieldDescriptor nestedExtension = TestRequired.single.getDescriptor();
  179. assertEquals("optional_int32", primitiveField.getName());
  180. assertEquals("protobuf_unittest.TestAllTypes.optional_int32",
  181. primitiveField.getFullName());
  182. assertEquals(1, primitiveField.getNumber());
  183. assertEquals(messageType, primitiveField.getContainingType());
  184. assertEquals(UnittestProto.getDescriptor(), primitiveField.getFile());
  185. assertEquals(FieldDescriptor.Type.INT32, primitiveField.getType());
  186. assertEquals(FieldDescriptor.JavaType.INT, primitiveField.getJavaType());
  187. assertEquals(DescriptorProtos.FieldOptions.getDefaultInstance(),
  188. primitiveField.getOptions());
  189. assertFalse(primitiveField.isExtension());
  190. assertEquals("optional_int32", primitiveField.toProto().getName());
  191. assertEquals("optional_nested_enum", enumField.getName());
  192. assertEquals(FieldDescriptor.Type.ENUM, enumField.getType());
  193. assertEquals(FieldDescriptor.JavaType.ENUM, enumField.getJavaType());
  194. assertEquals(TestAllTypes.NestedEnum.getDescriptor(),
  195. enumField.getEnumType());
  196. assertEquals("optional_foreign_message", messageField.getName());
  197. assertEquals(FieldDescriptor.Type.MESSAGE, messageField.getType());
  198. assertEquals(FieldDescriptor.JavaType.MESSAGE, messageField.getJavaType());
  199. assertEquals(ForeignMessage.getDescriptor(), messageField.getMessageType());
  200. assertEquals("optional_cord", cordField.getName());
  201. assertEquals(FieldDescriptor.Type.STRING, cordField.getType());
  202. assertEquals(FieldDescriptor.JavaType.STRING, cordField.getJavaType());
  203. assertEquals(DescriptorProtos.FieldOptions.CType.CORD,
  204. cordField.getOptions().getCtype());
  205. assertEquals("optional_int32_extension", extension.getName());
  206. assertEquals("protobuf_unittest.optional_int32_extension",
  207. extension.getFullName());
  208. assertEquals(1, extension.getNumber());
  209. assertEquals(TestAllExtensions.getDescriptor(),
  210. extension.getContainingType());
  211. assertEquals(UnittestProto.getDescriptor(), extension.getFile());
  212. assertEquals(FieldDescriptor.Type.INT32, extension.getType());
  213. assertEquals(FieldDescriptor.JavaType.INT, extension.getJavaType());
  214. assertEquals(DescriptorProtos.FieldOptions.getDefaultInstance(),
  215. extension.getOptions());
  216. assertTrue(extension.isExtension());
  217. assertEquals(null, extension.getExtensionScope());
  218. assertEquals("optional_int32_extension", extension.toProto().getName());
  219. assertEquals("single", nestedExtension.getName());
  220. assertEquals("protobuf_unittest.TestRequired.single",
  221. nestedExtension.getFullName());
  222. assertEquals(TestRequired.getDescriptor(),
  223. nestedExtension.getExtensionScope());
  224. }
  225. public void testFieldDescriptorLabel() throws Exception {
  226. FieldDescriptor requiredField =
  227. TestRequired.getDescriptor().findFieldByName("a");
  228. FieldDescriptor optionalField =
  229. TestAllTypes.getDescriptor().findFieldByName("optional_int32");
  230. FieldDescriptor repeatedField =
  231. TestAllTypes.getDescriptor().findFieldByName("repeated_int32");
  232. assertTrue(requiredField.isRequired());
  233. assertFalse(requiredField.isRepeated());
  234. assertFalse(optionalField.isRequired());
  235. assertFalse(optionalField.isRepeated());
  236. assertFalse(repeatedField.isRequired());
  237. assertTrue(repeatedField.isRepeated());
  238. }
  239. public void testFieldDescriptorDefault() throws Exception {
  240. Descriptor d = TestAllTypes.getDescriptor();
  241. assertFalse(d.findFieldByName("optional_int32").hasDefaultValue());
  242. assertEquals(0, d.findFieldByName("optional_int32").getDefaultValue());
  243. assertTrue(d.findFieldByName("default_int32").hasDefaultValue());
  244. assertEquals(41, d.findFieldByName("default_int32").getDefaultValue());
  245. d = TestExtremeDefaultValues.getDescriptor();
  246. assertEquals(
  247. ByteString.copyFrom(
  248. "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes("ISO-8859-1")),
  249. d.findFieldByName("escaped_bytes").getDefaultValue());
  250. assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue());
  251. assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue());
  252. }
  253. public void testEnumDescriptor() throws Exception {
  254. EnumDescriptor enumType = ForeignEnum.getDescriptor();
  255. EnumDescriptor nestedType = TestAllTypes.NestedEnum.getDescriptor();
  256. assertEquals("ForeignEnum", enumType.getName());
  257. assertEquals("protobuf_unittest.ForeignEnum", enumType.getFullName());
  258. assertEquals(UnittestProto.getDescriptor(), enumType.getFile());
  259. assertNull(enumType.getContainingType());
  260. assertEquals(DescriptorProtos.EnumOptions.getDefaultInstance(),
  261. enumType.getOptions());
  262. assertEquals("NestedEnum", nestedType.getName());
  263. assertEquals("protobuf_unittest.TestAllTypes.NestedEnum",
  264. nestedType.getFullName());
  265. assertEquals(UnittestProto.getDescriptor(), nestedType.getFile());
  266. assertEquals(TestAllTypes.getDescriptor(), nestedType.getContainingType());
  267. EnumValueDescriptor value = ForeignEnum.FOREIGN_FOO.getValueDescriptor();
  268. assertEquals(value, enumType.getValues().get(0));
  269. assertEquals("FOREIGN_FOO", value.getName());
  270. assertEquals(4, value.getNumber());
  271. assertEquals(value, enumType.findValueByName("FOREIGN_FOO"));
  272. assertEquals(value, enumType.findValueByNumber(4));
  273. assertNull(enumType.findValueByName("NO_SUCH_VALUE"));
  274. for (int i = 0; i < enumType.getValues().size(); i++) {
  275. assertEquals(i, enumType.getValues().get(i).getIndex());
  276. }
  277. }
  278. public void testServiceDescriptor() throws Exception {
  279. ServiceDescriptor service = TestService.getDescriptor();
  280. assertEquals("TestService", service.getName());
  281. assertEquals("protobuf_unittest.TestService", service.getFullName());
  282. assertEquals(UnittestProto.getDescriptor(), service.getFile());
  283. assertEquals(2, service.getMethods().size());
  284. MethodDescriptor fooMethod = service.getMethods().get(0);
  285. assertEquals("Foo", fooMethod.getName());
  286. assertEquals(UnittestProto.FooRequest.getDescriptor(),
  287. fooMethod.getInputType());
  288. assertEquals(UnittestProto.FooResponse.getDescriptor(),
  289. fooMethod.getOutputType());
  290. assertEquals(fooMethod, service.findMethodByName("Foo"));
  291. MethodDescriptor barMethod = service.getMethods().get(1);
  292. assertEquals("Bar", barMethod.getName());
  293. assertEquals(UnittestProto.BarRequest.getDescriptor(),
  294. barMethod.getInputType());
  295. assertEquals(UnittestProto.BarResponse.getDescriptor(),
  296. barMethod.getOutputType());
  297. assertEquals(barMethod, service.findMethodByName("Bar"));
  298. assertNull(service.findMethodByName("NoSuchMethod"));
  299. for (int i = 0; i < service.getMethods().size(); i++) {
  300. assertEquals(i, service.getMethods().get(i).getIndex());
  301. }
  302. }
  303. public void testCustomOptions() throws Exception {
  304. Descriptor descriptor =
  305. UnittestCustomOptions.TestMessageWithCustomOptions.getDescriptor();
  306. assertTrue(
  307. descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1));
  308. assertEquals(Integer.valueOf(-56),
  309. descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1));
  310. FieldDescriptor field = descriptor.findFieldByName("field1");
  311. assertNotNull(field);
  312. assertTrue(
  313. field.getOptions().hasExtension(UnittestCustomOptions.fieldOpt1));
  314. assertEquals(Long.valueOf(8765432109L),
  315. field.getOptions().getExtension(UnittestCustomOptions.fieldOpt1));
  316. EnumDescriptor enumType =
  317. UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor();
  318. assertTrue(
  319. enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1));
  320. assertEquals(Integer.valueOf(-789),
  321. enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1));
  322. ServiceDescriptor service =
  323. UnittestCustomOptions.TestServiceWithCustomOptions.getDescriptor();
  324. assertTrue(
  325. service.getOptions().hasExtension(UnittestCustomOptions.serviceOpt1));
  326. assertEquals(Long.valueOf(-9876543210L),
  327. service.getOptions().getExtension(UnittestCustomOptions.serviceOpt1));
  328. MethodDescriptor method = service.findMethodByName("Foo");
  329. assertNotNull(method);
  330. assertTrue(
  331. method.getOptions().hasExtension(UnittestCustomOptions.methodOpt1));
  332. assertEquals(UnittestCustomOptions.MethodOpt1.METHODOPT1_VAL2,
  333. method.getOptions().getExtension(UnittestCustomOptions.methodOpt1));
  334. }
  335. /**
  336. * Test that the FieldDescriptor.Type enum is the same as the
  337. * WireFormat.FieldType enum.
  338. */
  339. public void testFieldTypeTablesMatch() throws Exception {
  340. FieldDescriptor.Type[] values1 = FieldDescriptor.Type.values();
  341. WireFormat.FieldType[] values2 = WireFormat.FieldType.values();
  342. assertEquals(values1.length, values2.length);
  343. for (int i = 0; i < values1.length; i++) {
  344. assertEquals(values1[i].toString(), values2[i].toString());
  345. }
  346. }
  347. /**
  348. * Test that the FieldDescriptor.JavaType enum is the same as the
  349. * WireFormat.JavaType enum.
  350. */
  351. public void testJavaTypeTablesMatch() throws Exception {
  352. FieldDescriptor.JavaType[] values1 = FieldDescriptor.JavaType.values();
  353. WireFormat.JavaType[] values2 = WireFormat.JavaType.values();
  354. assertEquals(values1.length, values2.length);
  355. for (int i = 0; i < values1.length; i++) {
  356. assertEquals(values1[i].toString(), values2[i].toString());
  357. }
  358. }
  359. public void testEnormousDescriptor() throws Exception {
  360. // The descriptor for this file is larger than 64k, yet it did not cause
  361. // a compiler error due to an over-long string literal.
  362. assertTrue(
  363. UnittestEnormousDescriptor.getDescriptor()
  364. .toProto().getSerializedSize() > 65536);
  365. }
  366. /**
  367. * Tests that the DescriptorValidationException works as intended.
  368. */
  369. public void testDescriptorValidatorException() throws Exception {
  370. FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder()
  371. .setName("foo.proto")
  372. .addMessageType(DescriptorProto.newBuilder()
  373. .setName("Foo")
  374. .addField(FieldDescriptorProto.newBuilder()
  375. .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
  376. .setType(FieldDescriptorProto.Type.TYPE_INT32)
  377. .setName("foo")
  378. .setNumber(1)
  379. .setDefaultValue("invalid")
  380. .build())
  381. .build())
  382. .build();
  383. try {
  384. Descriptors.FileDescriptor.buildFrom(fileDescriptorProto,
  385. new FileDescriptor[0]);
  386. fail("DescriptorValidationException expected");
  387. } catch (DescriptorValidationException e) {
  388. // Expected; check that the error message contains some useful hints
  389. assertTrue(e.getMessage().indexOf("foo") != -1);
  390. assertTrue(e.getMessage().indexOf("Foo") != -1);
  391. assertTrue(e.getMessage().indexOf("invalid") != -1);
  392. assertTrue(e.getCause() instanceof NumberFormatException);
  393. assertTrue(e.getCause().getMessage().indexOf("invalid") != -1);
  394. }
  395. }
  396. }