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

http://github.com/tomahawk-player/tomahawk · Java · 313 lines · 218 code · 47 blank · 48 comment · 0 complexity · b5f1be7342994b34f8adb6b9f7bc8582 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 protobuf_unittest.UnittestProto.TestAllTypes;
  32. import protobuf_unittest.UnittestProto.TestAllExtensions;
  33. import protobuf_unittest.UnittestProto.TestRequired;
  34. import protobuf_unittest.UnittestProto.TestRequiredForeign;
  35. import protobuf_unittest.UnittestProto.ForeignMessage;
  36. import junit.framework.TestCase;
  37. /**
  38. * Misc. unit tests for message operations that apply to both generated
  39. * and dynamic messages.
  40. *
  41. * @author kenton@google.com Kenton Varda
  42. */
  43. public class MessageTest extends TestCase {
  44. // =================================================================
  45. // Message-merging tests.
  46. static final TestAllTypes MERGE_SOURCE =
  47. TestAllTypes.newBuilder()
  48. .setOptionalInt32(1)
  49. .setOptionalString("foo")
  50. .setOptionalForeignMessage(ForeignMessage.getDefaultInstance())
  51. .addRepeatedString("bar")
  52. .build();
  53. static final TestAllTypes MERGE_DEST =
  54. TestAllTypes.newBuilder()
  55. .setOptionalInt64(2)
  56. .setOptionalString("baz")
  57. .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(3).build())
  58. .addRepeatedString("qux")
  59. .build();
  60. static final String MERGE_RESULT_TEXT =
  61. "optional_int32: 1\n" +
  62. "optional_int64: 2\n" +
  63. "optional_string: \"foo\"\n" +
  64. "optional_foreign_message {\n" +
  65. " c: 3\n" +
  66. "}\n" +
  67. "repeated_string: \"qux\"\n" +
  68. "repeated_string: \"bar\"\n";
  69. public void testMergeFrom() throws Exception {
  70. TestAllTypes result =
  71. TestAllTypes.newBuilder(MERGE_DEST)
  72. .mergeFrom(MERGE_SOURCE).build();
  73. assertEquals(MERGE_RESULT_TEXT, result.toString());
  74. }
  75. /**
  76. * Test merging a DynamicMessage into a GeneratedMessage. As long as they
  77. * have the same descriptor, this should work, but it is an entirely different
  78. * code path.
  79. */
  80. public void testMergeFromDynamic() throws Exception {
  81. TestAllTypes result =
  82. TestAllTypes.newBuilder(MERGE_DEST)
  83. .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build())
  84. .build();
  85. assertEquals(MERGE_RESULT_TEXT, result.toString());
  86. }
  87. /** Test merging two DynamicMessages. */
  88. public void testDynamicMergeFrom() throws Exception {
  89. DynamicMessage result =
  90. DynamicMessage.newBuilder(MERGE_DEST)
  91. .mergeFrom(DynamicMessage.newBuilder(MERGE_SOURCE).build())
  92. .build();
  93. assertEquals(MERGE_RESULT_TEXT, result.toString());
  94. }
  95. // =================================================================
  96. // Required-field-related tests.
  97. private static final TestRequired TEST_REQUIRED_UNINITIALIZED =
  98. TestRequired.getDefaultInstance();
  99. private static final TestRequired TEST_REQUIRED_INITIALIZED =
  100. TestRequired.newBuilder().setA(1).setB(2).setC(3).build();
  101. public void testRequired() throws Exception {
  102. TestRequired.Builder builder = TestRequired.newBuilder();
  103. assertFalse(builder.isInitialized());
  104. builder.setA(1);
  105. assertFalse(builder.isInitialized());
  106. builder.setB(1);
  107. assertFalse(builder.isInitialized());
  108. builder.setC(1);
  109. assertTrue(builder.isInitialized());
  110. }
  111. public void testRequiredForeign() throws Exception {
  112. TestRequiredForeign.Builder builder = TestRequiredForeign.newBuilder();
  113. assertTrue(builder.isInitialized());
  114. builder.setOptionalMessage(TEST_REQUIRED_UNINITIALIZED);
  115. assertFalse(builder.isInitialized());
  116. builder.setOptionalMessage(TEST_REQUIRED_INITIALIZED);
  117. assertTrue(builder.isInitialized());
  118. builder.addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED);
  119. assertFalse(builder.isInitialized());
  120. builder.setRepeatedMessage(0, TEST_REQUIRED_INITIALIZED);
  121. assertTrue(builder.isInitialized());
  122. }
  123. public void testRequiredExtension() throws Exception {
  124. TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
  125. assertTrue(builder.isInitialized());
  126. builder.setExtension(TestRequired.single, TEST_REQUIRED_UNINITIALIZED);
  127. assertFalse(builder.isInitialized());
  128. builder.setExtension(TestRequired.single, TEST_REQUIRED_INITIALIZED);
  129. assertTrue(builder.isInitialized());
  130. builder.addExtension(TestRequired.multi, TEST_REQUIRED_UNINITIALIZED);
  131. assertFalse(builder.isInitialized());
  132. builder.setExtension(TestRequired.multi, 0, TEST_REQUIRED_INITIALIZED);
  133. assertTrue(builder.isInitialized());
  134. }
  135. public void testRequiredDynamic() throws Exception {
  136. Descriptors.Descriptor descriptor = TestRequired.getDescriptor();
  137. DynamicMessage.Builder builder = DynamicMessage.newBuilder(descriptor);
  138. assertFalse(builder.isInitialized());
  139. builder.setField(descriptor.findFieldByName("a"), 1);
  140. assertFalse(builder.isInitialized());
  141. builder.setField(descriptor.findFieldByName("b"), 1);
  142. assertFalse(builder.isInitialized());
  143. builder.setField(descriptor.findFieldByName("c"), 1);
  144. assertTrue(builder.isInitialized());
  145. }
  146. public void testRequiredDynamicForeign() throws Exception {
  147. Descriptors.Descriptor descriptor = TestRequiredForeign.getDescriptor();
  148. DynamicMessage.Builder builder = DynamicMessage.newBuilder(descriptor);
  149. assertTrue(builder.isInitialized());
  150. builder.setField(descriptor.findFieldByName("optional_message"),
  151. TEST_REQUIRED_UNINITIALIZED);
  152. assertFalse(builder.isInitialized());
  153. builder.setField(descriptor.findFieldByName("optional_message"),
  154. TEST_REQUIRED_INITIALIZED);
  155. assertTrue(builder.isInitialized());
  156. builder.addRepeatedField(descriptor.findFieldByName("repeated_message"),
  157. TEST_REQUIRED_UNINITIALIZED);
  158. assertFalse(builder.isInitialized());
  159. builder.setRepeatedField(descriptor.findFieldByName("repeated_message"), 0,
  160. TEST_REQUIRED_INITIALIZED);
  161. assertTrue(builder.isInitialized());
  162. }
  163. public void testUninitializedException() throws Exception {
  164. try {
  165. TestRequired.newBuilder().build();
  166. fail("Should have thrown an exception.");
  167. } catch (UninitializedMessageException e) {
  168. assertEquals("Message missing required fields: a, b, c", e.getMessage());
  169. }
  170. }
  171. public void testBuildPartial() throws Exception {
  172. // We're mostly testing that no exception is thrown.
  173. TestRequired message = TestRequired.newBuilder().buildPartial();
  174. assertFalse(message.isInitialized());
  175. }
  176. public void testNestedUninitializedException() throws Exception {
  177. try {
  178. TestRequiredForeign.newBuilder()
  179. .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED)
  180. .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED)
  181. .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED)
  182. .build();
  183. fail("Should have thrown an exception.");
  184. } catch (UninitializedMessageException e) {
  185. assertEquals(
  186. "Message missing required fields: " +
  187. "optional_message.a, " +
  188. "optional_message.b, " +
  189. "optional_message.c, " +
  190. "repeated_message[0].a, " +
  191. "repeated_message[0].b, " +
  192. "repeated_message[0].c, " +
  193. "repeated_message[1].a, " +
  194. "repeated_message[1].b, " +
  195. "repeated_message[1].c",
  196. e.getMessage());
  197. }
  198. }
  199. public void testBuildNestedPartial() throws Exception {
  200. // We're mostly testing that no exception is thrown.
  201. TestRequiredForeign message =
  202. TestRequiredForeign.newBuilder()
  203. .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED)
  204. .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED)
  205. .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED)
  206. .buildPartial();
  207. assertFalse(message.isInitialized());
  208. }
  209. public void testParseUnititialized() throws Exception {
  210. try {
  211. TestRequired.parseFrom(ByteString.EMPTY);
  212. fail("Should have thrown an exception.");
  213. } catch (InvalidProtocolBufferException e) {
  214. assertEquals("Message missing required fields: a, b, c", e.getMessage());
  215. }
  216. }
  217. public void testParseNestedUnititialized() throws Exception {
  218. ByteString data =
  219. TestRequiredForeign.newBuilder()
  220. .setOptionalMessage(TEST_REQUIRED_UNINITIALIZED)
  221. .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED)
  222. .addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED)
  223. .buildPartial().toByteString();
  224. try {
  225. TestRequiredForeign.parseFrom(data);
  226. fail("Should have thrown an exception.");
  227. } catch (InvalidProtocolBufferException e) {
  228. assertEquals(
  229. "Message missing required fields: " +
  230. "optional_message.a, " +
  231. "optional_message.b, " +
  232. "optional_message.c, " +
  233. "repeated_message[0].a, " +
  234. "repeated_message[0].b, " +
  235. "repeated_message[0].c, " +
  236. "repeated_message[1].a, " +
  237. "repeated_message[1].b, " +
  238. "repeated_message[1].c",
  239. e.getMessage());
  240. }
  241. }
  242. public void testDynamicUninitializedException() throws Exception {
  243. try {
  244. DynamicMessage.newBuilder(TestRequired.getDescriptor()).build();
  245. fail("Should have thrown an exception.");
  246. } catch (UninitializedMessageException e) {
  247. assertEquals("Message missing required fields: a, b, c", e.getMessage());
  248. }
  249. }
  250. public void testDynamicBuildPartial() throws Exception {
  251. // We're mostly testing that no exception is thrown.
  252. DynamicMessage message =
  253. DynamicMessage.newBuilder(TestRequired.getDescriptor())
  254. .buildPartial();
  255. assertFalse(message.isInitialized());
  256. }
  257. public void testDynamicParseUnititialized() throws Exception {
  258. try {
  259. Descriptors.Descriptor descriptor = TestRequired.getDescriptor();
  260. DynamicMessage.parseFrom(descriptor, ByteString.EMPTY);
  261. fail("Should have thrown an exception.");
  262. } catch (InvalidProtocolBufferException e) {
  263. assertEquals("Message missing required fields: a, b, c", e.getMessage());
  264. }
  265. }
  266. }