blob: 89193ad23ce4b4351fc2ecf987d8ca8e823192d2 [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;
Jon Skeet25a28582009-02-18 16:06:22 +000041 private ReflectionTester packedReflectionTester;
Jon Skeet68036862008-10-22 13:30:34 +010042
43 [SetUp]
44 public void SetUp() {
45 reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
46 extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
Jon Skeet25a28582009-02-18 16:06:22 +000047 packedReflectionTester = ReflectionTester.CreateTestPackedTypesInstance();
Jon Skeet68036862008-10-22 13:30:34 +010048 }
49
50 [Test]
51 public void DynamicMessageAccessors() {
52 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
53 reflectionTester.SetAllFieldsViaReflection(builder);
54 IMessage message = builder.WeakBuild();
55 reflectionTester.AssertAllFieldsSetViaReflection(message);
56 }
57
58 [Test]
Jon Skeet642a8142009-01-27 12:25:21 +000059 public void DynamicMessageSettersRejectNull() {
60 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
61 reflectionTester.AssertReflectionSettersRejectNull(builder);
62 }
63
64 [Test]
Jon Skeet68036862008-10-22 13:30:34 +010065 public void DynamicMessageExtensionAccessors() {
Jon Skeet642a8142009-01-27 12:25:21 +000066 // We don't need to extensively test DynamicMessage's handling of
67 // extensions because, frankly, it doesn't do anything special with them.
68 // It treats them just like any other fields.
69 IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
70 extensionsReflectionTester.SetAllFieldsViaReflection(builder);
71 IMessage message = builder.WeakBuild();
72 extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
73 }
74
75 [Test]
76 public void DynamicMessageExtensionSettersRejectNull() {
77 IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
78 extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
79 }
Jon Skeet68036862008-10-22 13:30:34 +010080
81 [Test]
82 public void DynamicMessageRepeatedSetters() {
83 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
84 reflectionTester.SetAllFieldsViaReflection(builder);
85 reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
86 IMessage message = builder.WeakBuild();
87 reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
88 }
89
90 [Test]
Jon Skeet642a8142009-01-27 12:25:21 +000091 public void DynamicMessageRepeatedSettersRejectNull() {
92 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
93 reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
94 }
95
96 [Test]
Jon Skeet68036862008-10-22 13:30:34 +010097 public void DynamicMessageDefaults() {
98 reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
99 reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
100 }
101
102 [Test]
103 public void DynamicMessageSerializedSize() {
104 TestAllTypes message = TestUtil.GetAllSet();
105
106 IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
107 reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
108 IMessage dynamicMessage = dynamicBuilder.WeakBuild();
109
110 Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
111 }
112
113 [Test]
114 public void DynamicMessageSerialization() {
115 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
116 reflectionTester.SetAllFieldsViaReflection(builder);
117 IMessage message = builder.WeakBuild();
118
119 ByteString rawBytes = message.ToByteString();
120 TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
121
122 TestUtil.AssertAllFieldsSet(message2);
123
124 // In fact, the serialized forms should be exactly the same, byte-for-byte.
125 Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
126 }
127
128 [Test]
129 public void DynamicMessageParsing() {
130 TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
131 TestUtil.SetAllFields(builder);
132 TestAllTypes message = builder.Build();
133
134 ByteString rawBytes = message.ToByteString();
135
136 IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
137 reflectionTester.AssertAllFieldsSetViaReflection(message2);
138 }
139
140 [Test]
Jon Skeet25a28582009-02-18 16:06:22 +0000141 public void DynamicMessagePackedSerialization() {
142 IBuilder builder = DynamicMessage.CreateBuilder(TestPackedTypes.Descriptor);
143 packedReflectionTester.SetPackedFieldsViaReflection(builder);
144 IMessage message = builder.WeakBuild();
145
146 ByteString rawBytes = message.ToByteString();
147 TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);
148
149 TestUtil.AssertPackedFieldsSet(message2);
150
151 // In fact, the serialized forms should be exactly the same, byte-for-byte.
152 Assert.AreEqual(TestUtil.GetPackedSet().ToByteString(), rawBytes);
153 }
154
155 [Test]
156 public void testDynamicMessagePackedParsing() {
157 TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
158 TestUtil.SetPackedFields(builder);
159 TestPackedTypes message = builder.Build();
160
161 ByteString rawBytes = message.ToByteString();
162
163 IMessage message2 = DynamicMessage.ParseFrom(TestPackedTypes.Descriptor, rawBytes);
164 packedReflectionTester.AssertPackedFieldsSetViaReflection(message2);
165 }
166
167 [Test]
Jon Skeet68036862008-10-22 13:30:34 +0100168 public void DynamicMessageCopy() {
169 TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
170 TestUtil.SetAllFields(builder);
171 TestAllTypes message = builder.Build();
172
173 DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
174 reflectionTester.AssertAllFieldsSetViaReflection(copy);
175 }
176 }
177}