1 | #region Copyright notice and license
|
---|
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 | #endregion
|
---|
34 |
|
---|
35 | using System;
|
---|
36 | using System.Collections.Generic;
|
---|
37 | using Google.ProtocolBuffers.TestProtos;
|
---|
38 | using NUnit.Framework;
|
---|
39 |
|
---|
40 | namespace Google.ProtocolBuffers {
|
---|
41 | [TestFixture]
|
---|
42 | public class DynamicMessageTest {
|
---|
43 |
|
---|
44 | private ReflectionTester reflectionTester;
|
---|
45 | private ReflectionTester extensionsReflectionTester;
|
---|
46 | private ReflectionTester packedReflectionTester;
|
---|
47 |
|
---|
48 | [SetUp]
|
---|
49 | public void SetUp() {
|
---|
50 | reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
|
---|
51 | extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
|
---|
52 | packedReflectionTester = ReflectionTester.CreateTestPackedTypesInstance();
|
---|
53 | }
|
---|
54 |
|
---|
55 | [Test]
|
---|
56 | public void DynamicMessageAccessors() {
|
---|
57 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
58 | reflectionTester.SetAllFieldsViaReflection(builder);
|
---|
59 | IMessage message = builder.WeakBuild();
|
---|
60 | reflectionTester.AssertAllFieldsSetViaReflection(message);
|
---|
61 | }
|
---|
62 |
|
---|
63 | [Test]
|
---|
64 | public void DoubleBuildError() {
|
---|
65 | DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
66 | builder.Build();
|
---|
67 | try {
|
---|
68 | builder.Build();
|
---|
69 | Assert.Fail("Should have thrown exception.");
|
---|
70 | } catch (InvalidOperationException) {
|
---|
71 | // Success.
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | [Test]
|
---|
76 | public void DynamicMessageSettersRejectNull() {
|
---|
77 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
78 | reflectionTester.AssertReflectionSettersRejectNull(builder);
|
---|
79 | }
|
---|
80 |
|
---|
81 | [Test]
|
---|
82 | public void DynamicMessageExtensionAccessors() {
|
---|
83 | // We don't need to extensively test DynamicMessage's handling of
|
---|
84 | // extensions because, frankly, it doesn't do anything special with them.
|
---|
85 | // It treats them just like any other fields.
|
---|
86 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
|
---|
87 | extensionsReflectionTester.SetAllFieldsViaReflection(builder);
|
---|
88 | IMessage message = builder.WeakBuild();
|
---|
89 | extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
|
---|
90 | }
|
---|
91 |
|
---|
92 | [Test]
|
---|
93 | public void DynamicMessageExtensionSettersRejectNull() {
|
---|
94 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
|
---|
95 | extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
|
---|
96 | }
|
---|
97 |
|
---|
98 | [Test]
|
---|
99 | public void DynamicMessageRepeatedSetters() {
|
---|
100 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
101 | reflectionTester.SetAllFieldsViaReflection(builder);
|
---|
102 | reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
|
---|
103 | IMessage message = builder.WeakBuild();
|
---|
104 | reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
|
---|
105 | }
|
---|
106 |
|
---|
107 | [Test]
|
---|
108 | public void DynamicMessageRepeatedSettersRejectNull() {
|
---|
109 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
110 | reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
|
---|
111 | }
|
---|
112 |
|
---|
113 | [Test]
|
---|
114 | public void DynamicMessageDefaults() {
|
---|
115 | reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
|
---|
116 | reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
|
---|
117 | }
|
---|
118 |
|
---|
119 | [Test]
|
---|
120 | public void DynamicMessageSerializedSize() {
|
---|
121 | TestAllTypes message = TestUtil.GetAllSet();
|
---|
122 |
|
---|
123 | IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
124 | reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
|
---|
125 | IMessage dynamicMessage = dynamicBuilder.WeakBuild();
|
---|
126 |
|
---|
127 | Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
|
---|
128 | }
|
---|
129 |
|
---|
130 | [Test]
|
---|
131 | public void DynamicMessageSerialization() {
|
---|
132 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
133 | reflectionTester.SetAllFieldsViaReflection(builder);
|
---|
134 | IMessage message = builder.WeakBuild();
|
---|
135 |
|
---|
136 | ByteString rawBytes = message.ToByteString();
|
---|
137 | TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
|
---|
138 |
|
---|
139 | TestUtil.AssertAllFieldsSet(message2);
|
---|
140 |
|
---|
141 | // In fact, the serialized forms should be exactly the same, byte-for-byte.
|
---|
142 | Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
|
---|
143 | }
|
---|
144 |
|
---|
145 | [Test]
|
---|
146 | public void DynamicMessageParsing() {
|
---|
147 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
148 | TestUtil.SetAllFields(builder);
|
---|
149 | TestAllTypes message = builder.Build();
|
---|
150 |
|
---|
151 | ByteString rawBytes = message.ToByteString();
|
---|
152 |
|
---|
153 | IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
|
---|
154 | reflectionTester.AssertAllFieldsSetViaReflection(message2);
|
---|
155 | }
|
---|
156 |
|
---|
157 | [Test]
|
---|
158 | public void DynamicMessagePackedSerialization() {
|
---|
159 | IBuilder builder = DynamicMessage.CreateBuilder(TestPackedTypes.Descriptor);
|
---|
160 | packedReflectionTester.SetPackedFieldsViaReflection(builder);
|
---|
161 | IMessage message = builder.WeakBuild();
|
---|
162 |
|
---|
163 | ByteString rawBytes = message.ToByteString();
|
---|
164 | TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);
|
---|
165 |
|
---|
166 | TestUtil.AssertPackedFieldsSet(message2);
|
---|
167 |
|
---|
168 | // In fact, the serialized forms should be exactly the same, byte-for-byte.
|
---|
169 | Assert.AreEqual(TestUtil.GetPackedSet().ToByteString(), rawBytes);
|
---|
170 | }
|
---|
171 |
|
---|
172 | [Test]
|
---|
173 | public void testDynamicMessagePackedParsing() {
|
---|
174 | TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
|
---|
175 | TestUtil.SetPackedFields(builder);
|
---|
176 | TestPackedTypes message = builder.Build();
|
---|
177 |
|
---|
178 | ByteString rawBytes = message.ToByteString();
|
---|
179 |
|
---|
180 | IMessage message2 = DynamicMessage.ParseFrom(TestPackedTypes.Descriptor, rawBytes);
|
---|
181 | packedReflectionTester.AssertPackedFieldsSetViaReflection(message2);
|
---|
182 | }
|
---|
183 |
|
---|
184 | [Test]
|
---|
185 | public void DynamicMessageCopy() {
|
---|
186 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
187 | TestUtil.SetAllFields(builder);
|
---|
188 | TestAllTypes message = builder.Build();
|
---|
189 |
|
---|
190 | DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
|
---|
191 | reflectionTester.AssertAllFieldsSetViaReflection(copy);
|
---|
192 | }
|
---|
193 |
|
---|
194 | [Test]
|
---|
195 | public void ToBuilder() {
|
---|
196 | DynamicMessage.Builder builder =
|
---|
197 | DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
---|
198 | reflectionTester.SetAllFieldsViaReflection(builder);
|
---|
199 | int unknownFieldNum = 9;
|
---|
200 | ulong unknownFieldVal = 90;
|
---|
201 | builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
|
---|
202 | .AddField(unknownFieldNum,
|
---|
203 | UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
|
---|
204 | .Build());
|
---|
205 | DynamicMessage message = builder.Build();
|
---|
206 |
|
---|
207 | DynamicMessage derived = message.ToBuilder().Build();
|
---|
208 | reflectionTester.AssertAllFieldsSetViaReflection(derived);
|
---|
209 |
|
---|
210 | IList<ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;
|
---|
211 | Assert.AreEqual(1, values.Count);
|
---|
212 | Assert.AreEqual(unknownFieldVal, values[0]);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|