DynamicMessageTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Google.ProtocolBuffers.TestProtos;
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. using NUnit.Framework;
  34. namespace Google.ProtocolBuffers {
  35. [TestFixture]
  36. public class DynamicMessageTest {
  37. private ReflectionTester reflectionTester;
  38. private ReflectionTester extensionsReflectionTester;
  39. [SetUp]
  40. public void SetUp() {
  41. reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
  42. extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
  43. }
  44. [Test]
  45. public void DynamicMessageAccessors() {
  46. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  47. reflectionTester.SetAllFieldsViaReflection(builder);
  48. IMessage message = builder.WeakBuild();
  49. reflectionTester.AssertAllFieldsSetViaReflection(message);
  50. }
  51. [Test]
  52. public void DynamicMessageSettersRejectNull() {
  53. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  54. reflectionTester.AssertReflectionSettersRejectNull(builder);
  55. }
  56. [Test]
  57. public void DynamicMessageExtensionAccessors() {
  58. // We don't need to extensively test DynamicMessage's handling of
  59. // extensions because, frankly, it doesn't do anything special with them.
  60. // It treats them just like any other fields.
  61. IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
  62. extensionsReflectionTester.SetAllFieldsViaReflection(builder);
  63. IMessage message = builder.WeakBuild();
  64. extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
  65. }
  66. [Test]
  67. public void DynamicMessageExtensionSettersRejectNull() {
  68. IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
  69. extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
  70. }
  71. [Test]
  72. public void DynamicMessageRepeatedSetters() {
  73. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  74. reflectionTester.SetAllFieldsViaReflection(builder);
  75. reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
  76. IMessage message = builder.WeakBuild();
  77. reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
  78. }
  79. [Test]
  80. public void DynamicMessageRepeatedSettersRejectNull() {
  81. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  82. reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
  83. }
  84. [Test]
  85. public void DynamicMessageDefaults() {
  86. reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
  87. reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
  88. }
  89. [Test]
  90. public void DynamicMessageSerializedSize() {
  91. TestAllTypes message = TestUtil.GetAllSet();
  92. IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  93. reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
  94. IMessage dynamicMessage = dynamicBuilder.WeakBuild();
  95. Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
  96. }
  97. [Test]
  98. public void DynamicMessageSerialization() {
  99. IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
  100. reflectionTester.SetAllFieldsViaReflection(builder);
  101. IMessage message = builder.WeakBuild();
  102. ByteString rawBytes = message.ToByteString();
  103. TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
  104. TestUtil.AssertAllFieldsSet(message2);
  105. // In fact, the serialized forms should be exactly the same, byte-for-byte.
  106. Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
  107. }
  108. [Test]
  109. public void DynamicMessageParsing() {
  110. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  111. TestUtil.SetAllFields(builder);
  112. TestAllTypes message = builder.Build();
  113. ByteString rawBytes = message.ToByteString();
  114. IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
  115. reflectionTester.AssertAllFieldsSetViaReflection(message2);
  116. }
  117. [Test]
  118. public void DynamicMessageCopy() {
  119. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  120. TestUtil.SetAllFields(builder);
  121. TestAllTypes message = builder.Build();
  122. DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
  123. reflectionTester.AssertAllFieldsSetViaReflection(copy);
  124. }
  125. }
  126. }