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

http://github.com/tomahawk-player/tomahawk · Java · 226 lines · 149 code · 35 blank · 42 comment · 0 complexity · 7060d5ff4a76e895c1b4546fae4e80ee 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.TestPackedTypes;
  34. import junit.framework.TestCase;
  35. import java.util.Arrays;
  36. /**
  37. * Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which
  38. * tests some {@link DynamicMessage} functionality.
  39. *
  40. * @author kenton@google.com Kenton Varda
  41. */
  42. public class DynamicMessageTest extends TestCase {
  43. TestUtil.ReflectionTester reflectionTester =
  44. new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null);
  45. TestUtil.ReflectionTester extensionsReflectionTester =
  46. new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(),
  47. TestUtil.getExtensionRegistry());
  48. TestUtil.ReflectionTester packedReflectionTester =
  49. new TestUtil.ReflectionTester(TestPackedTypes.getDescriptor(), null);
  50. public void testDynamicMessageAccessors() throws Exception {
  51. Message.Builder builder =
  52. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  53. reflectionTester.setAllFieldsViaReflection(builder);
  54. Message message = builder.build();
  55. reflectionTester.assertAllFieldsSetViaReflection(message);
  56. }
  57. public void testDoubleBuildError() throws Exception {
  58. Message.Builder builder =
  59. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  60. builder.build();
  61. try {
  62. builder.build();
  63. fail("Should have thrown exception.");
  64. } catch (IllegalStateException e) {
  65. // Success.
  66. }
  67. }
  68. public void testClearAfterBuildError() throws Exception {
  69. Message.Builder builder =
  70. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  71. builder.build();
  72. try {
  73. builder.clear();
  74. fail("Should have thrown exception.");
  75. } catch (IllegalStateException e) {
  76. // Success.
  77. }
  78. }
  79. public void testDynamicMessageSettersRejectNull() throws Exception {
  80. Message.Builder builder =
  81. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  82. reflectionTester.assertReflectionSettersRejectNull(builder);
  83. }
  84. public void testDynamicMessageExtensionAccessors() throws Exception {
  85. // We don't need to extensively test DynamicMessage's handling of
  86. // extensions because, frankly, it doesn't do anything special with them.
  87. // It treats them just like any other fields.
  88. Message.Builder builder =
  89. DynamicMessage.newBuilder(TestAllExtensions.getDescriptor());
  90. extensionsReflectionTester.setAllFieldsViaReflection(builder);
  91. Message message = builder.build();
  92. extensionsReflectionTester.assertAllFieldsSetViaReflection(message);
  93. }
  94. public void testDynamicMessageExtensionSettersRejectNull() throws Exception {
  95. Message.Builder builder =
  96. DynamicMessage.newBuilder(TestAllExtensions.getDescriptor());
  97. extensionsReflectionTester.assertReflectionSettersRejectNull(builder);
  98. }
  99. public void testDynamicMessageRepeatedSetters() throws Exception {
  100. Message.Builder builder =
  101. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  102. reflectionTester.setAllFieldsViaReflection(builder);
  103. reflectionTester.modifyRepeatedFieldsViaReflection(builder);
  104. Message message = builder.build();
  105. reflectionTester.assertRepeatedFieldsModifiedViaReflection(message);
  106. }
  107. public void testDynamicMessageRepeatedSettersRejectNull() throws Exception {
  108. Message.Builder builder =
  109. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  110. reflectionTester.assertReflectionRepeatedSettersRejectNull(builder);
  111. }
  112. public void testDynamicMessageDefaults() throws Exception {
  113. reflectionTester.assertClearViaReflection(
  114. DynamicMessage.getDefaultInstance(TestAllTypes.getDescriptor()));
  115. reflectionTester.assertClearViaReflection(
  116. DynamicMessage.newBuilder(TestAllTypes.getDescriptor()).build());
  117. }
  118. public void testDynamicMessageSerializedSize() throws Exception {
  119. TestAllTypes message = TestUtil.getAllSet();
  120. Message.Builder dynamicBuilder =
  121. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  122. reflectionTester.setAllFieldsViaReflection(dynamicBuilder);
  123. Message dynamicMessage = dynamicBuilder.build();
  124. assertEquals(message.getSerializedSize(),
  125. dynamicMessage.getSerializedSize());
  126. }
  127. public void testDynamicMessageSerialization() throws Exception {
  128. Message.Builder builder =
  129. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  130. reflectionTester.setAllFieldsViaReflection(builder);
  131. Message message = builder.build();
  132. ByteString rawBytes = message.toByteString();
  133. TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes);
  134. TestUtil.assertAllFieldsSet(message2);
  135. // In fact, the serialized forms should be exactly the same, byte-for-byte.
  136. assertEquals(TestUtil.getAllSet().toByteString(), rawBytes);
  137. }
  138. public void testDynamicMessageParsing() throws Exception {
  139. TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  140. TestUtil.setAllFields(builder);
  141. TestAllTypes message = builder.build();
  142. ByteString rawBytes = message.toByteString();
  143. Message message2 =
  144. DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), rawBytes);
  145. reflectionTester.assertAllFieldsSetViaReflection(message2);
  146. }
  147. public void testDynamicMessagePackedSerialization() throws Exception {
  148. Message.Builder builder =
  149. DynamicMessage.newBuilder(TestPackedTypes.getDescriptor());
  150. packedReflectionTester.setPackedFieldsViaReflection(builder);
  151. Message message = builder.build();
  152. ByteString rawBytes = message.toByteString();
  153. TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes);
  154. TestUtil.assertPackedFieldsSet(message2);
  155. // In fact, the serialized forms should be exactly the same, byte-for-byte.
  156. assertEquals(TestUtil.getPackedSet().toByteString(), rawBytes);
  157. }
  158. public void testDynamicMessagePackedParsing() throws Exception {
  159. TestPackedTypes.Builder builder = TestPackedTypes.newBuilder();
  160. TestUtil.setPackedFields(builder);
  161. TestPackedTypes message = builder.build();
  162. ByteString rawBytes = message.toByteString();
  163. Message message2 =
  164. DynamicMessage.parseFrom(TestPackedTypes.getDescriptor(), rawBytes);
  165. packedReflectionTester.assertPackedFieldsSetViaReflection(message2);
  166. }
  167. public void testDynamicMessageCopy() throws Exception {
  168. TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  169. TestUtil.setAllFields(builder);
  170. TestAllTypes message = builder.build();
  171. DynamicMessage copy = DynamicMessage.newBuilder(message).build();
  172. reflectionTester.assertAllFieldsSetViaReflection(copy);
  173. }
  174. public void testToBuilder() throws Exception {
  175. DynamicMessage.Builder builder =
  176. DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
  177. reflectionTester.setAllFieldsViaReflection(builder);
  178. int unknownFieldNum = 9;
  179. long unknownFieldVal = 90;
  180. builder.setUnknownFields(UnknownFieldSet.newBuilder()
  181. .addField(unknownFieldNum,
  182. UnknownFieldSet.Field.newBuilder()
  183. .addVarint(unknownFieldVal).build())
  184. .build());
  185. DynamicMessage message = builder.build();
  186. DynamicMessage derived = message.toBuilder().build();
  187. reflectionTester.assertAllFieldsSetViaReflection(derived);
  188. assertEquals(Arrays.asList(unknownFieldVal),
  189. derived.getUnknownFields().getField(unknownFieldNum).getVarintList());
  190. }
  191. }