blob: 7179da5de6c33ce79cae5cae289f6bbb664db4c7 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001using Google.ProtocolBuffers.TestProtos;
Jon Skeet68036862008-10-22 13:30:34 +01002// Protocol Buffers - Google's data interchange format
Jon Skeet60c059b2008-10-23 21:17:56 +01003// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
Jon Skeet68036862008-10-22 13:30:34 +01006// http://code.google.com/p/protobuf/
7//
Jon Skeet60c059b2008-10-23 21:17:56 +01008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
Jon Skeet68036862008-10-22 13:30:34 +010011//
Jon Skeet60c059b2008-10-23 21:17:56 +010012// * 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.
Jon Skeet68036862008-10-22 13:30:34 +010021//
Jon Skeet60c059b2008-10-23 21:17:56 +010022// 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.
Jon Skeet68036862008-10-22 13:30:34 +010033using NUnit.Framework;
34
35namespace Google.ProtocolBuffers {
36 [TestFixture]
37 public class DynamicMessageTest {
38
39 private ReflectionTester reflectionTester;
40 private ReflectionTester extensionsReflectionTester;
41
42 [SetUp]
43 public void SetUp() {
44 reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
45 extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
46 }
47
48 [Test]
49 public void DynamicMessageAccessors() {
50 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
51 reflectionTester.SetAllFieldsViaReflection(builder);
52 IMessage message = builder.WeakBuild();
53 reflectionTester.AssertAllFieldsSetViaReflection(message);
54 }
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
67 [Test]
68 public void DynamicMessageRepeatedSetters() {
69 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
70 reflectionTester.SetAllFieldsViaReflection(builder);
71 reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
72 IMessage message = builder.WeakBuild();
73 reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
74 }
75
76 [Test]
77 public void DynamicMessageDefaults() {
78 reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
79 reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
80 }
81
82 [Test]
83 public void DynamicMessageSerializedSize() {
84 TestAllTypes message = TestUtil.GetAllSet();
85
86 IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
87 reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
88 IMessage dynamicMessage = dynamicBuilder.WeakBuild();
89
90 Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
91 }
92
93 [Test]
94 public void DynamicMessageSerialization() {
95 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
96 reflectionTester.SetAllFieldsViaReflection(builder);
97 IMessage message = builder.WeakBuild();
98
99 ByteString rawBytes = message.ToByteString();
100 TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
101
102 TestUtil.AssertAllFieldsSet(message2);
103
104 // In fact, the serialized forms should be exactly the same, byte-for-byte.
105 Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
106 }
107
108 [Test]
109 public void DynamicMessageParsing() {
110 TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
111 TestUtil.SetAllFields(builder);
112 TestAllTypes message = builder.Build();
113
114 ByteString rawBytes = message.ToByteString();
115
116 IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
117 reflectionTester.AssertAllFieldsSetViaReflection(message2);
118 }
119
120 [Test]
121 public void DynamicMessageCopy() {
122 TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
123 TestUtil.SetAllFields(builder);
124 TestAllTypes message = builder.Build();
125
126 DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
127 reflectionTester.AssertAllFieldsSetViaReflection(copy);
128 }
129 }
130}