blob: 045f5c4f5c5806ca1827026b7515cb8b7a61ece0 [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]
Jon Skeet642a8142009-01-27 12:25:21 +000057 public void DynamicMessageSettersRejectNull() {
58 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
59 reflectionTester.AssertReflectionSettersRejectNull(builder);
60 }
61
62 [Test]
Jon Skeet68036862008-10-22 13:30:34 +010063 public void DynamicMessageExtensionAccessors() {
Jon Skeet642a8142009-01-27 12:25:21 +000064 // We don't need to extensively test DynamicMessage's handling of
65 // extensions because, frankly, it doesn't do anything special with them.
66 // It treats them just like any other fields.
67 IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
68 extensionsReflectionTester.SetAllFieldsViaReflection(builder);
69 IMessage message = builder.WeakBuild();
70 extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
71 }
72
73 [Test]
74 public void DynamicMessageExtensionSettersRejectNull() {
75 IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
76 extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
77 }
Jon Skeet68036862008-10-22 13:30:34 +010078
79 [Test]
80 public void DynamicMessageRepeatedSetters() {
81 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
82 reflectionTester.SetAllFieldsViaReflection(builder);
83 reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
84 IMessage message = builder.WeakBuild();
85 reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
86 }
87
88 [Test]
Jon Skeet642a8142009-01-27 12:25:21 +000089 public void DynamicMessageRepeatedSettersRejectNull() {
90 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
91 reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
92 }
93
94 [Test]
Jon Skeet68036862008-10-22 13:30:34 +010095 public void DynamicMessageDefaults() {
96 reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
97 reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
98 }
99
100 [Test]
101 public void DynamicMessageSerializedSize() {
102 TestAllTypes message = TestUtil.GetAllSet();
103
104 IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
105 reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
106 IMessage dynamicMessage = dynamicBuilder.WeakBuild();
107
108 Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
109 }
110
111 [Test]
112 public void DynamicMessageSerialization() {
113 IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
114 reflectionTester.SetAllFieldsViaReflection(builder);
115 IMessage message = builder.WeakBuild();
116
117 ByteString rawBytes = message.ToByteString();
118 TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
119
120 TestUtil.AssertAllFieldsSet(message2);
121
122 // In fact, the serialized forms should be exactly the same, byte-for-byte.
123 Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
124 }
125
126 [Test]
127 public void DynamicMessageParsing() {
128 TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
129 TestUtil.SetAllFields(builder);
130 TestAllTypes message = builder.Build();
131
132 ByteString rawBytes = message.ToByteString();
133
134 IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
135 reflectionTester.AssertAllFieldsSetViaReflection(message2);
136 }
137
138 [Test]
139 public void DynamicMessageCopy() {
140 TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
141 TestUtil.SetAllFields(builder);
142 TestAllTypes message = builder.Build();
143
144 DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
145 reflectionTester.AssertAllFieldsSetViaReflection(copy);
146 }
147 }
148}