csharptest | d965c66 | 2011-05-20 13:57:07 -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.IO;
|
| 38 | using NUnit.Framework;
|
| 39 | using System.Collections.Generic;
|
| 40 | using Google.ProtocolBuffers.TestProtos;
|
| 41 |
|
| 42 | namespace Google.ProtocolBuffers {
|
| 43 | [TestFixture]
|
| 44 | public class MissingFieldAndExtensionTest {
|
| 45 | [Test]
|
| 46 | public void TestRecoverMissingExtensions() {
|
| 47 | const int optionalInt32 = 12345678;
|
| 48 | TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
|
| 49 | builder.SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32);
|
| 50 | builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.1);
|
| 51 | builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.2);
|
| 52 | builder.AddExtension(UnitTestProtoFile.RepeatedDoubleExtension, 1.3);
|
| 53 | TestAllExtensions msg = builder.Build();
|
| 54 |
|
| 55 | Assert.IsTrue(msg.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
|
| 56 | Assert.AreEqual(3, msg.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
|
| 57 |
|
| 58 | byte[] bits = msg.ToByteArray();
|
| 59 | TestAllExtensions copy = TestAllExtensions.ParseFrom(bits);
|
| 60 | Assert.IsFalse(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
|
| 61 | Assert.AreEqual(0, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
|
| 62 | Assert.AreNotEqual(msg, copy);
|
| 63 |
|
| 64 | //Even though copy does not understand the typees they serialize correctly
|
| 65 | byte[] copybits = copy.ToByteArray();
|
| 66 | Assert.AreEqual(bits, copybits);
|
| 67 |
|
| 68 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 69 | UnitTestProtoFile.RegisterAllExtensions(registry);
|
| 70 |
|
| 71 | //Now we can take those copy bits and restore the full message with extensions
|
| 72 | copy = TestAllExtensions.ParseFrom(copybits, registry);
|
| 73 | Assert.IsTrue(copy.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
|
| 74 | Assert.AreEqual(3, copy.GetExtensionCount(UnitTestProtoFile.RepeatedDoubleExtension));
|
| 75 |
|
| 76 | Assert.AreEqual(msg, copy);
|
| 77 | Assert.AreEqual(bits, copy.ToByteArray());
|
| 78 |
|
| 79 | //If we modify the object this should all continue to work as before
|
| 80 | copybits = copy.ToBuilder().Build().ToByteArray();
|
| 81 | Assert.AreEqual(bits, copybits);
|
| 82 |
|
| 83 | //If we replace extension the object this should all continue to work as before
|
| 84 | copybits = copy.ToBuilder()
|
| 85 | .SetExtension(UnitTestProtoFile.OptionalInt32Extension, optionalInt32)
|
| 86 | .Build().ToByteArray();
|
| 87 | Assert.AreEqual(bits, copybits);
|
| 88 | }
|
| 89 |
|
| 90 | [Test]
|
| 91 | public void TestRecoverMissingFields() {
|
| 92 | TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
|
| 93 | .SetId(1001)
|
| 94 | .SetName("Name")
|
| 95 | .SetEmail("missing@field.value")
|
| 96 | .Build();
|
| 97 |
|
| 98 | //serialize to type B and verify all fields exist
|
| 99 | TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());
|
| 100 | Assert.AreEqual(1001, msgb.Id);
|
| 101 | Assert.AreEqual("Name", msgb.Name);
|
| 102 | Assert.IsFalse(msgb.HasWebsite);
|
| 103 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
| 104 | Assert.AreEqual("missing@field.value", msgb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());
|
| 105 |
|
| 106 | //serializes exactly the same (at least for this simple example)
|
| 107 | Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
|
| 108 | Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));
|
| 109 |
|
| 110 | //now re-create an exact copy of A from serialized B
|
| 111 | TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 112 | Assert.AreEqual(msga, copya);
|
| 113 | Assert.AreEqual(1001, copya.Id);
|
| 114 | Assert.AreEqual("Name", copya.Name);
|
| 115 | Assert.AreEqual("missing@field.value", copya.Email);
|
| 116 |
|
| 117 | //Now we modify B... and try again
|
| 118 | msgb = msgb.ToBuilder().SetWebsite("http://new.missing.field").Build();
|
| 119 | //Does B still have the missing field?
|
| 120 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
| 121 |
|
| 122 | //Convert back to A and see if all fields are there?
|
| 123 | copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 124 | Assert.AreNotEqual(msga, copya);
|
| 125 | Assert.AreEqual(1001, copya.Id);
|
| 126 | Assert.AreEqual("Name", copya.Name);
|
| 127 | Assert.AreEqual("missing@field.value", copya.Email);
|
| 128 | Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
|
| 129 | Assert.AreEqual("http://new.missing.field", copya.UnknownFields[TestMissingFieldsB.WebsiteFieldNumber].LengthDelimitedList[0].ToStringUtf8());
|
| 130 |
|
| 131 | //Lastly we can even still trip back to type B and see all fields:
|
| 132 | TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());
|
| 133 | Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
|
| 134 | Assert.AreEqual(1001, copyb.Id);
|
| 135 | Assert.AreEqual("Name", copyb.Name);
|
| 136 | Assert.AreEqual("http://new.missing.field", copyb.Website);
|
| 137 | Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
|
| 138 | Assert.AreEqual("missing@field.value", copyb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());
|
| 139 | }
|
| 140 |
|
| 141 | [Test]
|
| 142 | public void TestRecoverMissingMessage() {
|
| 143 | TestMissingFieldsA.Types.SubA suba = TestMissingFieldsA.Types.SubA.CreateBuilder().SetCount(3).AddValues("a").AddValues("b").AddValues("c").Build();
|
| 144 | TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
|
| 145 | .SetId(1001)
|
| 146 | .SetName("Name")
|
| 147 | .SetTestA(suba)
|
| 148 | .Build();
|
| 149 |
|
| 150 | //serialize to type B and verify all fields exist
|
| 151 | TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());
|
| 152 | Assert.AreEqual(1001, msgb.Id);
|
| 153 | Assert.AreEqual("Name", msgb.Name);
|
| 154 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
| 155 | Assert.AreEqual(suba.ToString(), TestMissingFieldsA.Types.SubA.ParseFrom(msgb.UnknownFields[TestMissingFieldsA.TestAFieldNumber].LengthDelimitedList[0]).ToString());
|
| 156 |
|
| 157 | //serializes exactly the same (at least for this simple example)
|
| 158 | Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
|
| 159 | Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));
|
| 160 |
|
| 161 | //now re-create an exact copy of A from serialized B
|
| 162 | TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 163 | Assert.AreEqual(msga, copya);
|
| 164 | Assert.AreEqual(1001, copya.Id);
|
| 165 | Assert.AreEqual("Name", copya.Name);
|
| 166 | Assert.AreEqual(suba, copya.TestA);
|
| 167 |
|
| 168 | //Now we modify B... and try again
|
| 169 | TestMissingFieldsB.Types.SubB subb = TestMissingFieldsB.Types.SubB.CreateBuilder().AddValues("test-b").Build();
|
| 170 | msgb = msgb.ToBuilder().SetTestB(subb).Build();
|
| 171 | //Does B still have the missing field?
|
| 172 | Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
|
| 173 |
|
| 174 | //Convert back to A and see if all fields are there?
|
| 175 | copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
|
| 176 | Assert.AreNotEqual(msga, copya);
|
| 177 | Assert.AreEqual(1001, copya.Id);
|
| 178 | Assert.AreEqual("Name", copya.Name);
|
| 179 | Assert.AreEqual(suba, copya.TestA);
|
| 180 | Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
|
| 181 | Assert.AreEqual(subb.ToByteArray(), copya.UnknownFields[TestMissingFieldsB.TestBFieldNumber].LengthDelimitedList[0].ToByteArray());
|
| 182 |
|
| 183 | //Lastly we can even still trip back to type B and see all fields:
|
| 184 | TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());
|
| 185 | Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
|
| 186 | Assert.AreEqual(1001, copyb.Id);
|
| 187 | Assert.AreEqual("Name", copyb.Name);
|
| 188 | Assert.AreEqual(subb, copyb.TestB);
|
| 189 | Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
|
| 190 | }
|
| 191 |
|
| 192 | [Test]
|
| 193 | public void TestRestoreFromOtherType() {
|
| 194 | TestInteropPerson person = TestInteropPerson.CreateBuilder()
|
| 195 | .SetId(123)
|
| 196 | .SetName("abc")
|
| 197 | .SetEmail("abc@123.com")
|
| 198 | .AddRangeCodes(new[] {1, 2, 3})
|
| 199 | .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
|
| 200 | .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
|
| 201 | .AddAddresses(TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").SetState("NA").SetZip(12345).Build())
|
| 202 | .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId, TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
|
| 203 | .Build();
|
| 204 | Assert.IsTrue(person.IsInitialized);
|
| 205 |
|
| 206 | TestEmptyMessage temp = TestEmptyMessage.ParseFrom(person.ToByteArray());
|
| 207 | Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
|
| 208 | temp = temp.ToBuilder().Build();
|
| 209 | Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
|
| 210 |
|
| 211 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 212 | UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
|
| 213 |
|
| 214 | TestInteropPerson copy = TestInteropPerson.ParseFrom(temp.ToByteArray(), registry);
|
| 215 | Assert.AreEqual(person, copy);
|
| 216 | Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
|
| 217 | }
|
| 218 | }
|
csharptest | cb7fc65 | 2010-11-19 10:59:50 -0600 | [diff] [blame] | 219 | } |