csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
| 2 |
|
| 3 | // Protocol Buffers - Google's data interchange format
|
| 4 | // Copyright 2008 Google Inc. All rights reserved.
|
| 5 | // http://github.com/jskeet/dotnet-protobufs/
|
| 6 | // Original C++/Java/Python code:
|
| 7 | // http://code.google.com/p/protobuf/
|
| 8 | //
|
| 9 | // Redistribution and use in source and binary forms, with or without
|
| 10 | // modification, are permitted provided that the following conditions are
|
| 11 | // met:
|
| 12 | //
|
| 13 | // * Redistributions of source code must retain the above copyright
|
| 14 | // notice, this list of conditions and the following disclaimer.
|
| 15 | // * Redistributions in binary form must reproduce the above
|
| 16 | // copyright notice, this list of conditions and the following disclaimer
|
| 17 | // in the documentation and/or other materials provided with the
|
| 18 | // distribution.
|
| 19 | // * Neither the name of Google Inc. nor the names of its
|
| 20 | // contributors may be used to endorse or promote products derived from
|
| 21 | // this software without specific prior written permission.
|
| 22 | //
|
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 34 |
|
| 35 | #endregion
|
| 36 |
|
| 37 | using System;
|
| 38 | using System.Collections.Generic;
|
| 39 | using Google.ProtocolBuffers.TestProtos;
|
| 40 | using NUnit.Framework;
|
| 41 |
|
| 42 | namespace Google.ProtocolBuffers
|
| 43 | {
|
| 44 | [TestFixture]
|
| 45 | public class DynamicMessageTest
|
| 46 | {
|
| 47 | private ReflectionTester reflectionTester;
|
| 48 | private ReflectionTester extensionsReflectionTester;
|
| 49 | private ReflectionTester packedReflectionTester;
|
| 50 |
|
| 51 | [SetUp]
|
| 52 | public void SetUp()
|
| 53 | {
|
| 54 | reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
|
| 55 | extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
|
| 56 | packedReflectionTester = ReflectionTester.CreateTestPackedTypesInstance();
|
| 57 | }
|
| 58 |
|
| 59 | [Test]
|
| 60 | public void DynamicMessageAccessors()
|
| 61 | {
|
| 62 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 63 | reflectionTester.SetAllFieldsViaReflection(builder);
|
| 64 | IMessage message = builder.WeakBuild();
|
| 65 | reflectionTester.AssertAllFieldsSetViaReflection(message);
|
| 66 | }
|
| 67 |
|
| 68 | [Test]
|
| 69 | public void DoubleBuildError()
|
| 70 | {
|
| 71 | DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 72 | builder.Build();
|
| 73 | try
|
| 74 | {
|
| 75 | builder.Build();
|
| 76 | Assert.Fail("Should have thrown exception.");
|
| 77 | }
|
| 78 | catch (InvalidOperationException)
|
| 79 | {
|
| 80 | // Success.
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | [Test]
|
| 85 | public void DynamicMessageSettersRejectNull()
|
| 86 | {
|
| 87 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 88 | reflectionTester.AssertReflectionSettersRejectNull(builder);
|
| 89 | }
|
| 90 |
|
| 91 | [Test]
|
| 92 | public void DynamicMessageExtensionAccessors()
|
| 93 | {
|
| 94 | // We don't need to extensively test DynamicMessage's handling of
|
| 95 | // extensions because, frankly, it doesn't do anything special with them.
|
| 96 | // It treats them just like any other fields.
|
| 97 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
|
| 98 | extensionsReflectionTester.SetAllFieldsViaReflection(builder);
|
| 99 | IMessage message = builder.WeakBuild();
|
| 100 | extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
|
| 101 | }
|
| 102 |
|
| 103 | [Test]
|
| 104 | public void DynamicMessageExtensionSettersRejectNull()
|
| 105 | {
|
| 106 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
|
| 107 | extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
|
| 108 | }
|
| 109 |
|
| 110 | [Test]
|
| 111 | public void DynamicMessageRepeatedSetters()
|
| 112 | {
|
| 113 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 114 | reflectionTester.SetAllFieldsViaReflection(builder);
|
| 115 | reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
|
| 116 | IMessage message = builder.WeakBuild();
|
| 117 | reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
|
| 118 | }
|
| 119 |
|
| 120 | [Test]
|
| 121 | public void DynamicMessageRepeatedSettersRejectNull()
|
| 122 | {
|
| 123 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 124 | reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
|
| 125 | }
|
| 126 |
|
| 127 | [Test]
|
| 128 | public void DynamicMessageDefaults()
|
| 129 | {
|
| 130 | reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
|
| 131 | reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
|
| 132 | }
|
| 133 |
|
| 134 | [Test]
|
| 135 | public void DynamicMessageSerializedSize()
|
| 136 | {
|
| 137 | TestAllTypes message = TestUtil.GetAllSet();
|
| 138 |
|
| 139 | IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 140 | reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
|
| 141 | IMessage dynamicMessage = dynamicBuilder.WeakBuild();
|
| 142 |
|
| 143 | Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
|
| 144 | }
|
| 145 |
|
| 146 | [Test]
|
| 147 | public void DynamicMessageSerialization()
|
| 148 | {
|
| 149 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 150 | reflectionTester.SetAllFieldsViaReflection(builder);
|
| 151 | IMessage message = builder.WeakBuild();
|
| 152 |
|
| 153 | ByteString rawBytes = message.ToByteString();
|
| 154 | TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
|
| 155 |
|
| 156 | TestUtil.AssertAllFieldsSet(message2);
|
| 157 |
|
| 158 | // In fact, the serialized forms should be exactly the same, byte-for-byte.
|
| 159 | Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
|
| 160 | }
|
| 161 |
|
| 162 | [Test]
|
| 163 | public void DynamicMessageParsing()
|
| 164 | {
|
| 165 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
| 166 | TestUtil.SetAllFields(builder);
|
| 167 | TestAllTypes message = builder.Build();
|
| 168 |
|
| 169 | ByteString rawBytes = message.ToByteString();
|
| 170 |
|
| 171 | IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
|
| 172 | reflectionTester.AssertAllFieldsSetViaReflection(message2);
|
| 173 | }
|
| 174 |
|
| 175 | [Test]
|
| 176 | public void DynamicMessagePackedSerialization()
|
| 177 | {
|
| 178 | IBuilder builder = DynamicMessage.CreateBuilder(TestPackedTypes.Descriptor);
|
| 179 | packedReflectionTester.SetPackedFieldsViaReflection(builder);
|
| 180 | IMessage message = builder.WeakBuild();
|
| 181 |
|
| 182 | ByteString rawBytes = message.ToByteString();
|
| 183 | TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);
|
| 184 |
|
| 185 | TestUtil.AssertPackedFieldsSet(message2);
|
| 186 |
|
| 187 | // In fact, the serialized forms should be exactly the same, byte-for-byte.
|
| 188 | Assert.AreEqual(TestUtil.GetPackedSet().ToByteString(), rawBytes);
|
| 189 | }
|
| 190 |
|
| 191 | [Test]
|
| 192 | public void testDynamicMessagePackedParsing()
|
| 193 | {
|
| 194 | TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
|
| 195 | TestUtil.SetPackedFields(builder);
|
| 196 | TestPackedTypes message = builder.Build();
|
| 197 |
|
| 198 | ByteString rawBytes = message.ToByteString();
|
| 199 |
|
| 200 | IMessage message2 = DynamicMessage.ParseFrom(TestPackedTypes.Descriptor, rawBytes);
|
| 201 | packedReflectionTester.AssertPackedFieldsSetViaReflection(message2);
|
| 202 | }
|
| 203 |
|
| 204 | [Test]
|
| 205 | public void DynamicMessageCopy()
|
| 206 | {
|
| 207 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
| 208 | TestUtil.SetAllFields(builder);
|
| 209 | TestAllTypes message = builder.Build();
|
| 210 |
|
| 211 | DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
|
| 212 | reflectionTester.AssertAllFieldsSetViaReflection(copy);
|
| 213 | }
|
| 214 |
|
| 215 | [Test]
|
| 216 | public void ToBuilder()
|
| 217 | {
|
| 218 | DynamicMessage.Builder builder =
|
| 219 | DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
|
| 220 | reflectionTester.SetAllFieldsViaReflection(builder);
|
| 221 | int unknownFieldNum = 9;
|
| 222 | ulong unknownFieldVal = 90;
|
| 223 | builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
|
| 224 | .AddField(unknownFieldNum,
|
| 225 | UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
|
| 226 | .Build());
|
| 227 | DynamicMessage message = builder.Build();
|
| 228 |
|
| 229 | DynamicMessage derived = message.ToBuilder().Build();
|
| 230 | reflectionTester.AssertAllFieldsSetViaReflection(derived);
|
| 231 |
|
| 232 | IList<ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;
|
| 233 | Assert.AreEqual(1, values.Count);
|
| 234 | Assert.AreEqual(unknownFieldVal, values[0]);
|
| 235 | }
|
| 236 | }
|
| 237 | } |