Ejemplo n.º 1
0
        public void TestFullMessageWithRichTypes()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetValid(true)
                                     .SetText("text")
                                     .AddTextlines("a")
                                     .AddTextlines("b")
                                     .AddTextlines("c")
                                     .SetNumber(0x1010101010)
                                     .AddNumbers(1)
                                     .AddNumbers(2)
                                     .AddNumbers(3)
                                     .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1])))
                                     .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2])))
                                     .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3])))
                                     .Build();

            StringWriter sw   = new StringWriter();
            XmlWriter    xwtr = XmlWriter.Create(sw, new XmlWriterSettings {
                Indent = true, IndentChars = "  "
            });

            XmlFormatWriter.CreateInstance(xwtr)
            .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues)
            .WriteMessage("root", message);

            string xml = sw.ToString();

            XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);

            rdr.Options = XmlReaderOptions.ReadNestedArrays;
            TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 2
0
        public void TestXmlWithWhitespace()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetText(" \t <- leading space and trailing -> \r\n\t").Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string xml = sw.ToString();

            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 3
0
        public void TestMessageWithXmlText()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetText("<text>").Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string xml = sw.ToString();

            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 4
0
        public void TestBooleanTypes()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetValid(true)
                                     .Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string          xml  = sw.ToString();
            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 5
0
        public void TestNestedMessage()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.TWO).Build())
                                     .Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string          xml  = sw.ToString();
            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 6
0
        public void TestFullMessageWithUnknownFields()
        {
            TestXmlMessage origial = TestXmlMessage.CreateBuilder()
                                     .SetValid(true)
                                     .SetText("text")
                                     .AddTextlines("a")
                                     .AddTextlines("b")
                                     .AddTextlines("c")
                                     .SetNumber(0x1010101010)
                                     .AddNumbers(1)
                                     .AddNumbers(2)
                                     .AddNumbers(3)
                                     .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1])))
                                     .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2])))
                                     .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3])))
                                     .Build();
            TestXmlNoFields message = TestXmlNoFields.CreateBuilder().MergeFrom(origial.ToByteArray()).Build();

            Assert.AreEqual(0, message.AllFields.Count);

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw)
            .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues)
            .WriteMessage("root", message);

            string xml = sw.ToString();

            using (XmlReader x = XmlReader.Create(new StringReader(xml)))
            {
                x.MoveToContent();
                Assert.AreEqual(XmlNodeType.Element, x.NodeType);
                //should always be empty
                Assert.IsTrue(x.IsEmptyElement ||
                              (x.Read() && x.NodeType == XmlNodeType.EndElement)
                              );
            }

            XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml);

            rdr.Options = XmlReaderOptions.ReadNestedArrays;
            TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build();

            Assert.AreEqual(TestXmlMessage.DefaultInstance, copy);
        }
Ejemplo n.º 7
0
        public void TestRepeatedField()
        {
            TestXmlChild message = TestXmlChild.CreateBuilder()
                                   .AddOptions(EnumOptions.ONE)
                                   .AddOptions(EnumOptions.TWO)
                                   .Build();

            //Allow the writer to write the root element
            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string          xml  = sw.ToString();
            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlChild    copy = rdr.Merge(TestXmlChild.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 8
0
        public void TestEmptyMessage()
        {
            TestXmlChild message = TestXmlChild.CreateBuilder()
                                   .Build();

            StringWriter sw = new StringWriter();
            XmlWriter    xw = XmlWriter.Create(sw);

            //When we call message.WriteTo, we are responsible for the root element
            xw.WriteStartElement("root");
            message.WriteTo(XmlFormatWriter.CreateInstance(xw));
            xw.WriteEndElement();
            xw.Flush();

            string          xml  = sw.ToString();
            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlChild    copy = rdr.Merge(TestXmlChild.CreateBuilder()).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 9
0
        public void TestXmlWithExtensionEnum()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetExtension(UnittestExtrasXmltest.ExtensionEnum, EnumOptions.ONE)
                                     .Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string xml = sw.ToString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasXmltest.RegisterAllExtensions(registry);

            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();

            Assert.AreEqual(message, copy);
        }
        public void TestXmlWithExtensionMessage()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionMessage,
                                                   new TestXmlExtension.Builder().SetNumber(42).Build()).Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string xml = sw.ToString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);

            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 11
0
        protected override TBuilder DeserializeMessage <TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
        {
            XmlFormatReader reader = XmlFormatReader.CreateInstance((string)message).SetOptions(XmlReaderOptions.ReadNestedArrays);

            return(reader.Merge("root", builder, registry));
        }