Beispiel #1
0
        public void When_Using_Property_WithXContainerAssignable_should_preserve_xml()
        {
            const string XmlElement  = "<SomeClass xmlns=\"http://nservicebus.com\"><SomeProperty value=\"Bar\" /></SomeClass>";
            const string XmlDocument = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + XmlElement;

            var messageWithXDocument = new MessageWithXDocument
            {
                Document = XDocument.Load(new StringReader(XmlDocument))
            };
            var messageWithXElement = new MessageWithXElement
            {
                Document = XElement.Load(new StringReader(XmlElement))
            };

            var messageMapper = new MessageMapper();
            var serializer    = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(messageWithXDocument, stream);

                stream.Position = 0;
                var json = new StreamReader(stream).ReadToEnd();
                stream.Position = 0;

                var result = serializer.Deserialize(stream, new[]
                {
                    typeof(MessageWithXDocument)
                }).Cast <MessageWithXDocument>().Single();

                Assert.AreEqual(messageWithXDocument.Document.ToString(), result.Document.ToString());
                Assert.AreEqual(XmlElement, json.Substring(13, json.Length - 15).Replace("\\", string.Empty));
            }

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(messageWithXElement, stream);

                stream.Position = 0;
                var json = new StreamReader(stream).ReadToEnd();
                stream.Position = 0;

                var result = serializer.Deserialize(stream, new[]
                {
                    typeof(MessageWithXElement)
                }).Cast <MessageWithXElement>().Single();

                Assert.AreEqual(messageWithXElement.Document.ToString(), result.Document.ToString());
                Assert.AreEqual(XmlElement, json.Substring(13, json.Length - 15).Replace("\\", string.Empty));
            }
        }
        public void Should_handle_concrete_message_with_interface_property()
        {
            var messageMapper = new MessageMapper();
            messageMapper.Initialize(new[]
            {
                typeof(MessageWithInterfaceProperty)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            var message = new MessageWithInterfaceProperty
            {
                InterfaceProperty = new InterfacePropertyImplementation
                {
                    SomeProperty = "test"
                }
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(message, stream);

                stream.Position = 0;

                var result = (MessageWithInterfaceProperty) serializer.Deserialize(stream, new[]
                {
                    typeof(MessageWithInterfaceProperty)
                })[0];

                Assert.AreEqual(message.InterfaceProperty.SomeProperty, result.InterfaceProperty.SomeProperty);
            }
        }
Beispiel #3
0
        public void Should_handle_interface_message_with_interface_property()
        {
            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(IMessageWithInterfaceProperty)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            IMessageWithInterfaceProperty message = new InterfaceMessageWithInterfacePropertyImplementation
            {
                InterfaceProperty = new InterfacePropertyImplementation
                {
                    SomeProperty = "test"
                }
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(message, stream);

                stream.Position = 0;

                var result = (IMessageWithInterfaceProperty)serializer.Deserialize(stream, new[]
                {
                    typeof(IMessageWithInterfaceProperty)
                })[0];

                Assert.AreEqual(message.InterfaceProperty.SomeProperty, result.InterfaceProperty.SomeProperty);
            }
        }
Beispiel #4
0
        public void Deserialize_message_without_concrete_implementation()
        {
            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(ISuperMessageWithoutConcreteImpl)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                var msg = messageMapper.CreateInstance <ISuperMessageWithoutConcreteImpl>();
                msg.SomeProperty = "test";

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = (ISuperMessageWithoutConcreteImpl)serializer.Deserialize(stream, new[]
                {
                    typeof(ISuperMessageWithoutConcreteImpl)
                })[0];

                Assert.AreEqual("test", result.SomeProperty);
            }
        }
Beispiel #5
0
        public void Deserialize_message_with_concrete_implementation_and_interface()
        {
            var map = new[]
            {
                typeof(SuperMessageWithConcreteImpl),
                typeof(ISuperMessageWithConcreteImpl)
            };
            var messageMapper = new MessageMapper();

            messageMapper.Initialize(map);
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                var msg = new SuperMessageWithConcreteImpl
                {
                    SomeProperty = "test"
                };

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = (ISuperMessageWithConcreteImpl)serializer.Deserialize(stream, map)[0];

                Assert.IsInstanceOf <SuperMessageWithConcreteImpl>(result);
                Assert.AreEqual("test", result.SomeProperty);
            }
        }
Beispiel #6
0
        public void Deserialize_private_message_with_two_unrelated_interface_without_wrapping()
        {
            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(IMyEventA),
                typeof(IMyEventB)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                var msg = new CompositeMessage
                {
                    IntValue    = 42,
                    StringValue = "Answer"
                };

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = serializer.Deserialize(stream, new[]
                {
                    typeof(IMyEventA),
                    typeof(IMyEventB)
                });
                var a = (IMyEventA)result[0];
                var b = (IMyEventB)result[1];
                Assert.AreEqual(42, b.IntValue);
                Assert.AreEqual("Answer", a.StringValue);
            }
        }
Beispiel #7
0
        public void Serialize_message_without_typeInfo()
        {
            var messageMapper = new MessageMapper();
            var serializer    = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new SimpleMessage(), stream);

                stream.Position = 0;
                var result = new StreamReader(stream).ReadToEnd();

                Assert.That(!result.Contains("$type"), result);
            }
        }
Beispiel #8
0
        public void Should_preserve_timezones()
        {
            var expectedDateTime            = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Unspecified);
            var expectedDateTimeLocal       = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Local);
            var expectedDateTimeUtc         = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Utc);
            var expectedDateTimeOffset      = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.FromHours(6));
            var expectedDateTimeOffsetLocal = DateTimeOffset.Now;
            var expectedDateTimeOffsetUtc   = DateTimeOffset.UtcNow;

            using (var stream = new MemoryStream())
            {
                var serializer = new JsonMessageSerializer(new MessageMapper());
                serializer.Serialize(new DateTimeMessage
                {
                    DateTime            = expectedDateTime,
                    DateTimeLocal       = expectedDateTimeLocal,
                    DateTimeUtc         = expectedDateTimeUtc,
                    DateTimeOffset      = expectedDateTimeOffset,
                    DateTimeOffsetLocal = expectedDateTimeOffsetLocal,
                    DateTimeOffsetUtc   = expectedDateTimeOffsetUtc
                }, stream);
                stream.Position = 0;

                var result = serializer.Deserialize(stream, new List <Type>
                {
                    typeof(DateTimeMessage)
                }).Cast <DateTimeMessage>().Single();

                Assert.AreEqual(expectedDateTime.Kind, result.DateTime.Kind);
                Assert.AreEqual(expectedDateTime, result.DateTime);
                Assert.AreEqual(expectedDateTimeLocal.Kind, result.DateTimeLocal.Kind);
                Assert.AreEqual(expectedDateTimeLocal, result.DateTimeLocal);
                Assert.AreEqual(expectedDateTimeUtc.Kind, result.DateTimeUtc.Kind);
                Assert.AreEqual(expectedDateTimeUtc, result.DateTimeUtc);

                Assert.AreEqual(expectedDateTimeOffset, result.DateTimeOffset);
                Assert.AreEqual(expectedDateTimeOffset.Offset, result.DateTimeOffset.Offset);
                Assert.AreEqual(expectedDateTimeOffsetLocal, result.DateTimeOffsetLocal);
                Assert.AreEqual(expectedDateTimeOffsetLocal.Offset, result.DateTimeOffsetLocal.Offset);
                Assert.AreEqual(expectedDateTimeOffsetUtc, result.DateTimeOffsetUtc);
                Assert.AreEqual(expectedDateTimeOffsetUtc.Offset, result.DateTimeOffsetUtc.Offset);
            }
        }
Beispiel #9
0
        public void Deserialize_message_without_wrapping()
        {
            var messageMapper = new MessageMapper();
            var serializer    = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new SimpleMessage
                {
                    SomeProperty = "test"
                }, stream);

                stream.Position = 0;
                var result = (SimpleMessage)serializer.Deserialize(stream, new[]
                {
                    typeof(SimpleMessage)
                })[0];

                Assert.AreEqual("test", result.SomeProperty);
            }
        }
Beispiel #10
0
        public void Serialize_message_without_concrete_implementation()
        {
            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(ISuperMessageWithoutConcreteImpl)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(messageMapper.CreateInstance <ISuperMessageWithoutConcreteImpl>(), stream);

                stream.Position = 0;
                var result = new StreamReader(stream).ReadToEnd();

                Assert.That(!result.Contains("$type"), result);
                Assert.That(result.Contains("SomeProperty"), result);
            }
        }
Beispiel #11
0
        public void Deserialize_message_with_interface_without_wrapping()
        {
            var messageMapper = new MessageMapper();
            var serializer    = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new SuperMessage
                {
                    SomeProperty = "John"
                }, stream);

                stream.Position = 0;

                var result = (SuperMessage)serializer.Deserialize(stream, new[]
                {
                    typeof(SuperMessage),
                    typeof(IMyEvent)
                })[0];

                Assert.AreEqual("John", result.SomeProperty);
            }
        }
        public void Deserialize_private_message_with_two_unrelated_interface_without_wrapping()
        {
            var messageMapper = new MessageMapper();
            messageMapper.Initialize(new[]
            {
                typeof(IMyEventA),
                typeof(IMyEventB)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                var msg = new CompositeMessage
                {
                    IntValue = 42,
                    StringValue = "Answer"
                };

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = serializer.Deserialize(stream, new[]
                {
                    typeof(IMyEventA),
                    typeof(IMyEventB)
                });
                var a = (IMyEventA) result[0];
                var b = (IMyEventB) result[1];
                Assert.AreEqual(42, b.IntValue);
                Assert.AreEqual("Answer", a.StringValue);
            }
        }
Beispiel #13
0
        public void Test()
        {
            var expectedDate      = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Unspecified);
            var expectedDateLocal = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Local);
            var expectedDateUtc   = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Utc);
            var expectedGuid      = Guid.NewGuid();

            var obj = new A
            {
                AGuid = expectedGuid,
                Data  = new byte[32],
                I     = 23,
                S     = "Foo",
                Ints  = new List <int>
                {
                    12,
                    42
                },
                Bs = new List <B>
                {
                    new B
                    {
                        BString = "aaa",
                        C       = new C
                        {
                            Cstr = "ccc"
                        }
                    },
                    new BB
                    {
                        BString = "bbbb",
                        C       = new C
                        {
                            Cstr = "dddd"
                        },
                        BBString = "BBStr"
                    }
                },
                DateTime      = expectedDate,
                DateTimeLocal = expectedDateLocal,
                DateTimeUtc   = expectedDateUtc
            };

            new Random().NextBytes(obj.Data);

            var output = new MemoryStream();

            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(IA),
                typeof(A)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            serializer.Serialize(obj, output);

            output.Position = 0;

            var result = serializer.Deserialize(output, new[]
            {
                typeof(A)
            });

            Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open");

            Assert.That(result[0], Is.TypeOf(typeof(A)));
            var a = (A)result[0];

            Assert.AreEqual(obj.Data, a.Data);
            Assert.AreEqual(23, a.I);
            Assert.AreEqual("Foo", a.S);
            Assert.AreEqual(expectedDate.Kind, a.DateTime.Kind);
            Assert.AreEqual(expectedDate, a.DateTime);
            Assert.AreEqual(expectedDateLocal.Kind, a.DateTimeLocal.Kind);
            Assert.AreEqual(expectedDateLocal, a.DateTimeLocal);
            Assert.AreEqual(expectedDateUtc.Kind, a.DateTimeUtc.Kind);
            Assert.AreEqual(expectedDateUtc, a.DateTimeUtc);
            Assert.AreEqual("ccc", ((C)a.Bs[0].C).Cstr);
            Assert.AreEqual(expectedGuid, a.AGuid);

            Assert.IsInstanceOf <B>(a.Bs[0]);
            Assert.IsInstanceOf <BB>(a.Bs[1]);
        }
Beispiel #14
0
        public void TestInterfaces()
        {
            var output = new MemoryStream();

            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(IA),
                typeof(IAImpl)
            });
            var obj = messageMapper.CreateInstance <IA>(
                x =>
            {
                x.S    = "kalle";
                x.I    = 42;
                x.Data = new byte[23];
                x.B    = new B
                {
                    BString = "BOO",
                    C       = new C
                    {
                        Cstr = "COO"
                    }
                };
            }
                );

            new Random().NextBytes(obj.Data);

            var serializer = new JsonMessageSerializer(messageMapper);

            serializer.Serialize(obj, output);

            output.Position = 0;

            var filename = $"{GetType().Name}.{MethodBase.GetCurrentMethod().Name}.txt";

            File.WriteAllBytes(filename, output.ToArray());

            output.Position = 0;

            var result = serializer.Deserialize(output, new[]
            {
                typeof(IAImpl)
            });

            Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open");

            Assert.IsNotEmpty(result);
            Assert.That(result, Has.Length.EqualTo(1));

            Assert.That(result[0], Is.AssignableTo(typeof(IA)));
            var a = (IA)result[0];

            Assert.AreEqual(a.Data, obj.Data);
            Assert.AreEqual(42, a.I);
            Assert.AreEqual("kalle", a.S);
            Assert.IsNotNull(a.B);
            Assert.AreEqual("BOO", a.B.BString);
            Assert.AreEqual("COO", ((C)a.B.C).Cstr);
        }
        public void Serialize_message_without_typeInfo()
        {
            var messageMapper = new MessageMapper();
            var serializer = new JsonMessageSerializer(messageMapper);
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new SimpleMessage(), stream);

                stream.Position = 0;
                var result = new StreamReader(stream).ReadToEnd();

                Assert.That(!result.Contains("$type"), result);
            }
        }
        public void Deserialize_message_with_interface_without_wrapping()
        {
            var messageMapper = new MessageMapper();
            var serializer = new JsonMessageSerializer(messageMapper);
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new SuperMessage
                {
                    SomeProperty = "John"
                }, stream);

                stream.Position = 0;

                var result = (SuperMessage) serializer.Deserialize(stream, new[]
                {
                    typeof(SuperMessage),
                    typeof(IMyEvent)
                })[0];

                Assert.AreEqual("John", result.SomeProperty);
            }
        }
        public void Deserialize_message_without_concrete_implementation()
        {
            var messageMapper = new MessageMapper();
            messageMapper.Initialize(new[]
            {
                typeof(ISuperMessageWithoutConcreteImpl)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                var msg = messageMapper.CreateInstance<ISuperMessageWithoutConcreteImpl>();
                msg.SomeProperty = "test";

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = (ISuperMessageWithoutConcreteImpl) serializer.Deserialize(stream, new[]
                {
                    typeof(ISuperMessageWithoutConcreteImpl)
                })[0];

                Assert.AreEqual("test", result.SomeProperty);
            }
        }
        public void Serialize_message_without_concrete_implementation()
        {
            var messageMapper = new MessageMapper();
            messageMapper.Initialize(new[]
            {
                typeof(ISuperMessageWithoutConcreteImpl)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(messageMapper.CreateInstance<ISuperMessageWithoutConcreteImpl>(), stream);

                stream.Position = 0;
                var result = new StreamReader(stream).ReadToEnd();

                Assert.That(!result.Contains("$type"), result);
                Assert.That(result.Contains("SomeProperty"), result);
            }
        }
        public void When_Using_Property_WithXContainerAssignable_should_preserve_xml()
        {
            const string XmlElement = "<SomeClass xmlns=\"http://nservicebus.com\"><SomeProperty value=\"Bar\" /></SomeClass>";
            const string XmlDocument = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + XmlElement;

            var messageWithXDocument = new MessageWithXDocument
            {
                Document = XDocument.Load(new StringReader(XmlDocument))
            };
            var messageWithXElement = new MessageWithXElement
            {
                Document = XElement.Load(new StringReader(XmlElement))
            };

            var messageMapper = new MessageMapper();
            var serializer = new JsonMessageSerializer(messageMapper);
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(messageWithXDocument, stream);

                stream.Position = 0;
                var json = new StreamReader(stream).ReadToEnd();
                stream.Position = 0;

                var result = serializer.Deserialize(stream, new[]
                {
                    typeof(MessageWithXDocument)
                }).Cast<MessageWithXDocument>().Single();

                Assert.AreEqual(messageWithXDocument.Document.ToString(), result.Document.ToString());
                Assert.AreEqual(XmlElement, json.Substring(13, json.Length - 15).Replace("\\", string.Empty));
            }

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(messageWithXElement, stream);

                stream.Position = 0;
                var json = new StreamReader(stream).ReadToEnd();
                stream.Position = 0;

                var result = serializer.Deserialize(stream, new[]
                {
                    typeof(MessageWithXElement)
                }).Cast<MessageWithXElement>().Single();

                Assert.AreEqual(messageWithXElement.Document.ToString(), result.Document.ToString());
                Assert.AreEqual(XmlElement, json.Substring(13, json.Length - 15).Replace("\\", string.Empty));
            }
        }
        public void Deserialize_message_with_concrete_implementation_and_interface()
        {
            var map = new[]
            {
                typeof(SuperMessageWithConcreteImpl),
                typeof(ISuperMessageWithConcreteImpl)
            };
            var messageMapper = new MessageMapper();
            messageMapper.Initialize(map);
            var serializer = new JsonMessageSerializer(messageMapper);

            using (var stream = new MemoryStream())
            {
                var msg = new SuperMessageWithConcreteImpl
                {
                    SomeProperty = "test"
                };

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = (ISuperMessageWithConcreteImpl) serializer.Deserialize(stream, map)[0];

                Assert.IsInstanceOf<SuperMessageWithConcreteImpl>(result);
                Assert.AreEqual("test", result.SomeProperty);
            }
        }
        public void Should_preserve_timezones()
        {
            var expectedDateTime = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Unspecified);
            var expectedDateTimeLocal = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Local);
            var expectedDateTimeUtc = new DateTime(2010, 10, 13, 12, 32, 42, DateTimeKind.Utc);
            var expectedDateTimeOffset = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.FromHours(6));
            var expectedDateTimeOffsetLocal = DateTimeOffset.Now;
            var expectedDateTimeOffsetUtc = DateTimeOffset.UtcNow;

            using (var stream = new MemoryStream())
            {
                var serializer = new JsonMessageSerializer(new MessageMapper());
                serializer.Serialize(new DateTimeMessage
                {
                    DateTime = expectedDateTime,
                    DateTimeLocal = expectedDateTimeLocal,
                    DateTimeUtc = expectedDateTimeUtc,
                    DateTimeOffset = expectedDateTimeOffset,
                    DateTimeOffsetLocal = expectedDateTimeOffsetLocal,
                    DateTimeOffsetUtc = expectedDateTimeOffsetUtc
                }, stream);
                stream.Position = 0;

                var result = serializer.Deserialize(stream, new List<Type>
                {
                    typeof(DateTimeMessage)
                }).Cast<DateTimeMessage>().Single();

                Assert.AreEqual(expectedDateTime.Kind, result.DateTime.Kind);
                Assert.AreEqual(expectedDateTime, result.DateTime);
                Assert.AreEqual(expectedDateTimeLocal.Kind, result.DateTimeLocal.Kind);
                Assert.AreEqual(expectedDateTimeLocal, result.DateTimeLocal);
                Assert.AreEqual(expectedDateTimeUtc.Kind, result.DateTimeUtc.Kind);
                Assert.AreEqual(expectedDateTimeUtc, result.DateTimeUtc);

                Assert.AreEqual(expectedDateTimeOffset, result.DateTimeOffset);
                Assert.AreEqual(expectedDateTimeOffset.Offset, result.DateTimeOffset.Offset);
                Assert.AreEqual(expectedDateTimeOffsetLocal, result.DateTimeOffsetLocal);
                Assert.AreEqual(expectedDateTimeOffsetLocal.Offset, result.DateTimeOffsetLocal.Offset);
                Assert.AreEqual(expectedDateTimeOffsetUtc, result.DateTimeOffsetUtc);
                Assert.AreEqual(expectedDateTimeOffsetUtc.Offset, result.DateTimeOffsetUtc.Offset);
            }

        }
        public void Deserialize_message_without_wrapping()
        {
            var messageMapper = new MessageMapper();
            var serializer = new JsonMessageSerializer(messageMapper);
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new SimpleMessage
                {
                    SomeProperty = "test"
                }, stream);

                stream.Position = 0;
                var result = (SimpleMessage) serializer.Deserialize(stream, new[]
                {
                    typeof(SimpleMessage)
                })[0];

                Assert.AreEqual("test", result.SomeProperty);
            }
        }
        public void TestInterfaces()
        {
            var output = new MemoryStream();

            var messageMapper = new MessageMapper();
            messageMapper.Initialize(new[]
            {
                typeof(IA),
                typeof(IAImpl)
            });
            var obj = messageMapper.CreateInstance<IA>(
                x =>
                {
                    x.S = "kalle";
                    x.I = 42;
                    x.Data = new byte[23];
                    x.B = new B
                    {
                        BString = "BOO",
                        C = new C
                        {
                            Cstr = "COO"
                        }
                    };
                }
                );

            new Random().NextBytes(obj.Data);

            var serializer = new JsonMessageSerializer(messageMapper);

            serializer.Serialize(obj, output);

            output.Position = 0;

            var filename = $"{GetType().Name}.{MethodBase.GetCurrentMethod().Name}.txt";

            File.WriteAllBytes(filename, output.ToArray());

            output.Position = 0;

            var result = serializer.Deserialize(output, new[]
            {
                typeof(IAImpl)
            });

            Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open");

            Assert.IsNotEmpty(result);
            Assert.That(result, Has.Length.EqualTo(1));

            Assert.That(result[0], Is.AssignableTo(typeof(IA)));
            var a = (IA) result[0];

            Assert.AreEqual(a.Data, obj.Data);
            Assert.AreEqual(42, a.I);
            Assert.AreEqual("kalle", a.S);
            Assert.IsNotNull(a.B);
            Assert.AreEqual("BOO", a.B.BString);
            Assert.AreEqual("COO", ((C) a.B.C).Cstr);
        }
Beispiel #24
0
        public void Should_handle_types_correctly()
        {
            var expectedGuid = Guid.NewGuid();

            var obj = new A
            {
                AGuid = expectedGuid,
                Data  = new byte[32],
                I     = 23,
                S     = "Foo",
                Ints  = new List <int>
                {
                    12,
                    42
                },
                Bs = new List <B>
                {
                    new B
                    {
                        BString = "aaa",
                        C       = new C
                        {
                            Cstr = "ccc"
                        }
                    },
                    new BB
                    {
                        BString = "bbbb",
                        C       = new C
                        {
                            Cstr = "dddd"
                        },
                        BBString = "BBStr"
                    }
                }
            };

            new Random().NextBytes(obj.Data);

            var output = new MemoryStream();

            var messageMapper = new MessageMapper();

            messageMapper.Initialize(new[]
            {
                typeof(IA),
                typeof(A)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            serializer.Serialize(obj, output);

            output.Position = 0;

            var result = serializer.Deserialize(output, new[]
            {
                typeof(A)
            });

            Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open");

            Assert.That(result[0], Is.TypeOf(typeof(A)));
            var a = (A)result[0];

            Assert.AreEqual(obj.Data, a.Data);
            Assert.AreEqual(23, a.I);
            Assert.AreEqual("Foo", a.S);
            Assert.AreEqual("ccc", ((C)a.Bs[0].C).Cstr);
            Assert.AreEqual(expectedGuid, a.AGuid);

            Assert.IsInstanceOf <B>(a.Bs[0]);
            Assert.IsInstanceOf <BB>(a.Bs[1]);
        }
        public void Should_handle_types_correctly()
        {
            var expectedGuid = Guid.NewGuid();

            var obj = new A
            {
                AGuid = expectedGuid,
                Data = new byte[32],
                I = 23,
                S = "Foo",
                Ints = new List<int>
                {
                    12,
                    42
                },
                Bs = new List<B>
                {
                    new B
                    {
                        BString = "aaa",
                        C = new C
                        {
                            Cstr = "ccc"
                        }
                    },
                    new BB
                    {
                        BString = "bbbb",
                        C = new C
                        {
                            Cstr = "dddd"
                        },
                        BBString = "BBStr"
                    }
                }
            };

            new Random().NextBytes(obj.Data);

            var output = new MemoryStream();

            var messageMapper = new MessageMapper();
            messageMapper.Initialize(new[]
            {
                typeof(IA),
                typeof(A)
            });
            var serializer = new JsonMessageSerializer(messageMapper);

            serializer.Serialize(obj, output);

            output.Position = 0;

            var result = serializer.Deserialize(output, new[]
            {
                typeof(A)
            });

            Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open");

            Assert.That(result[0], Is.TypeOf(typeof(A)));
            var a = (A) result[0];

            Assert.AreEqual(obj.Data, a.Data);
            Assert.AreEqual(23, a.I);
            Assert.AreEqual("Foo", a.S);
            Assert.AreEqual("ccc", ((C) a.Bs[0].C).Cstr);
            Assert.AreEqual(expectedGuid, a.AGuid);

            Assert.IsInstanceOf<B>(a.Bs[0]);
            Assert.IsInstanceOf<BB>(a.Bs[1]);
        }