diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
commit | 01c8c35ca3d3424a9473dd22ea2456933b3e8d36 (patch) | |
tree | de582cb9ef3e449f24015dec09f80e998806b26a /csharp/src/ProtocolBuffers.Test/SerializableTest.cs | |
parent | 813d6d652d8091e9365bfbd01efb5f2ee8bdfbce (diff) | |
parent | 1eab56aee8dddcb656db67d9fb87a8cc6a2ac112 (diff) | |
download | protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.gz protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.bz2 protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.zip |
Merge pull request #2 from jtattermusch/csharp_protobuf_imported
Pulling protobuf-csharp-port into csharp/ directory
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test/SerializableTest.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/SerializableTest.cs | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/SerializableTest.cs b/csharp/src/ProtocolBuffers.Test/SerializableTest.cs new file mode 100644 index 00000000..90406140 --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/SerializableTest.cs @@ -0,0 +1,179 @@ +#if !NOSERIALIZABLE
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Text;
+using Google.ProtocolBuffers.TestProtos;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Google.ProtocolBuffers
+{
+ [TestClass]
+ public class SerializableTest
+ {
+ /// <summary>
+ /// Just keep it from even compiling if we these objects don't implement the expected interface.
+ /// </summary>
+ public static readonly ISerializable CompileTimeCheckSerializableMessage = TestXmlMessage.DefaultInstance;
+ public static readonly ISerializable CompileTimeCheckSerializableBuilder = new TestXmlMessage.Builder();
+
+ [TestMethod]
+ public void TestPlainMessage()
+ {
+ 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();
+
+ MemoryStream ms = new MemoryStream();
+ new BinaryFormatter().Serialize(ms, message);
+
+ ms.Position = 0;
+ TestXmlMessage copy = (TestXmlMessage)new BinaryFormatter().Deserialize(ms);
+
+ Assert.AreEqual(message, copy);
+ }
+
+ [TestMethod]
+ public void TestMessageWithExtensions()
+ {
+ 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])))
+ .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionText, " extension text value ! ")
+ .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionMessage, new TestXmlExtension.Builder().SetNumber(42).Build())
+ .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 100)
+ .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 101)
+ .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 102)
+ .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionEnum, EnumOptions.ONE)
+ .Build();
+
+ ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
+ UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
+
+ MemoryStream ms = new MemoryStream();
+ new BinaryFormatter().Serialize(ms, message);
+
+ ms.Position = 0;
+ //you need to provide the extension registry as context to the serializer
+ BinaryFormatter bff = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All, registry));
+ TestXmlMessage copy = (TestXmlMessage)bff.Deserialize(ms);
+
+ // And all extensions will be defined.
+ Assert.AreEqual(message, copy);
+ }
+
+ [TestMethod]
+ public void TestPlainBuilder()
+ {
+ TestXmlMessage.Builder builder = 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])))
+ ;
+
+ MemoryStream ms = new MemoryStream();
+ new BinaryFormatter().Serialize(ms, builder);
+
+ ms.Position = 0;
+ TestXmlMessage.Builder copy = (TestXmlMessage.Builder)new BinaryFormatter().Deserialize(ms);
+
+ Assert.AreEqual(builder.Build(), copy.Build());
+ }
+
+ [TestMethod]
+ public void TestBuilderWithExtensions()
+ {
+ TestXmlMessage.Builder builder = 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])))
+ .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionText, " extension text value ! ")
+ .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionMessage, new TestXmlExtension.Builder().SetNumber(42).Build())
+ .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 100)
+ .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 101)
+ .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 102)
+ .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionEnum, EnumOptions.ONE)
+ ;
+
+ ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
+ UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);
+
+ MemoryStream ms = new MemoryStream();
+ new BinaryFormatter().Serialize(ms, builder);
+
+ ms.Position = 0;
+ //you need to provide the extension registry as context to the serializer
+ BinaryFormatter bff = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All, registry));
+ TestXmlMessage.Builder copy = (TestXmlMessage.Builder)bff.Deserialize(ms);
+
+ // And all extensions will be defined.
+ Assert.AreEqual(builder.Build(), copy.Build());
+ }
+ }
+}
+#endif
\ No newline at end of file |