Exemple #1
0
        public void TestRecoverMissingMessage()
        {
            TestMissingFieldsA.Types.SubA suba =
                TestMissingFieldsA.Types.SubA.CreateBuilder().SetCount(3).AddValues("a").AddValues("b").AddValues("c").
                Build();
            TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
                                      .SetId(1001)
                                      .SetName("Name")
                                      .SetTestA(suba)
                                      .Build();

            //serialize to type B and verify all fields exist
            TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());

            Assert.AreEqual(1001, msgb.Id);
            Assert.AreEqual("Name", msgb.Name);
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual(suba.ToString(),
                            TestMissingFieldsA.Types.SubA.ParseFrom(
                                msgb.UnknownFields[TestMissingFieldsA.TestAFieldNumber].LengthDelimitedList[0]).ToString
                                ());

            //serializes exactly the same (at least for this simple example)
            Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
            Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));

            //now re-create an exact copy of A from serialized B
            TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());

            Assert.AreEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual(suba, copya.TestA);

            //Now we modify B... and try again
            TestMissingFieldsB.Types.SubB subb =
                TestMissingFieldsB.Types.SubB.CreateBuilder().AddValues("test-b").Build();
            msgb = msgb.ToBuilder().SetTestB(subb).Build();
            //Does B still have the missing field?
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);

            //Convert back to A and see if all fields are there?
            copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
            Assert.AreNotEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual(suba, copya.TestA);
            Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual(subb.ToByteArray(),
                            copya.UnknownFields[TestMissingFieldsB.TestBFieldNumber].LengthDelimitedList[0].ToByteArray());

            //Lastly we can even still trip back to type B and see all fields:
            TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());

            Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
            Assert.AreEqual(1001, copyb.Id);
            Assert.AreEqual("Name", copyb.Name);
            Assert.AreEqual(subb, copyb.TestB);
            Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
        }
Exemple #2
0
        public void TestRecoverMissingFields()
        {
            TestMissingFieldsA msga = TestMissingFieldsA.CreateBuilder()
                                      .SetId(1001)
                                      .SetName("Name")
                                      .SetEmail("*****@*****.**")
                                      .Build();

            //serialize to type B and verify all fields exist
            TestMissingFieldsB msgb = TestMissingFieldsB.ParseFrom(msga.ToByteArray());

            Assert.AreEqual(1001, msgb.Id);
            Assert.AreEqual("Name", msgb.Name);
            Assert.IsFalse(msgb.HasWebsite);
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual("*****@*****.**",
                            msgb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8());

            //serializes exactly the same (at least for this simple example)
            Assert.AreEqual(msga.ToByteArray(), msgb.ToByteArray());
            Assert.AreEqual(msga, TestMissingFieldsA.ParseFrom(msgb.ToByteArray()));

            //now re-create an exact copy of A from serialized B
            TestMissingFieldsA copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());

            Assert.AreEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual("*****@*****.**", copya.Email);

            //Now we modify B... and try again
            msgb = msgb.ToBuilder().SetWebsite("http://new.missing.field").Build();
            //Does B still have the missing field?
            Assert.AreEqual(1, msgb.UnknownFields.FieldDictionary.Count);

            //Convert back to A and see if all fields are there?
            copya = TestMissingFieldsA.ParseFrom(msgb.ToByteArray());
            Assert.AreNotEqual(msga, copya);
            Assert.AreEqual(1001, copya.Id);
            Assert.AreEqual("Name", copya.Name);
            Assert.AreEqual("*****@*****.**", copya.Email);
            Assert.AreEqual(1, copya.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual("http://new.missing.field",
                            copya.UnknownFields[TestMissingFieldsB.WebsiteFieldNumber].LengthDelimitedList[0].
                            ToStringUtf8());

            //Lastly we can even still trip back to type B and see all fields:
            TestMissingFieldsB copyb = TestMissingFieldsB.ParseFrom(copya.ToByteArray());

            Assert.AreEqual(copya.ToByteArray().Length, copyb.ToByteArray().Length); //not exact order.
            Assert.AreEqual(1001, copyb.Id);
            Assert.AreEqual("Name", copyb.Name);
            Assert.AreEqual("http://new.missing.field", copyb.Website);
            Assert.AreEqual(1, copyb.UnknownFields.FieldDictionary.Count);
            Assert.AreEqual("*****@*****.**",
                            copyb.UnknownFields[TestMissingFieldsA.EmailFieldNumber].LengthDelimitedList[0].ToStringUtf8
                                ());
        }