Esempio n. 1
0
        [Test] //note: This is not a desired behavior, but this test documents this limitation
        public void Limitation_Does_not_handle_concrete_message_with_invalid_interface_property()
        {
            var message = new MessageWithInvalidInterfaceProperty
            {
                InterfaceProperty = new InvalidInterfacePropertyImplementation
                {
                    SomeProperty = "test"
                }
            };
            var serializer = SerializerFactory.Create <MessageWithInvalidInterfaceProperty>();

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

                Assert.Throws <Exception>(() => serializer.Deserialize(stream));
            }
        }
        public void CanDeserializeNullableArrayWithXsiNilAttributeSetToTrue()
        {
            var xml  = @"<?xml version=""1.0"" ?>
<MessageWithNullableArray xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
<SagaId>00000000-0000-0000-0000-000000000000</SagaId>
<SomeInts xsi:nil=""true"">
</SomeInts>
</MessageWithNullableArray>
";
            var data = Encoding.UTF8.GetBytes(xml);

            using (var stream = new MemoryStream(data))
            {
                var msgArray = SerializerFactory.Create <MessageWithNullableArray>().Deserialize(stream, new[] { typeof(MessageWithNullableArray) });
                var result   = (MessageWithNullableArray)msgArray[0];

                Assert.IsFalse(result.SomeInts.Any());
            }
        }
Esempio n. 3
0
        public static Serializer ForMessage <T>(object message, Action <XmlMessageSerializer> config = null)
        {
            using (var stream = new MemoryStream())
            {
                var serializer = SerializerFactory.Create <T>();

                if (config != null)
                {
                    config(serializer);
                }


                serializer.Serialize(new[] { message }, stream);
                stream.Position = 0;
                var result = new StreamReader(stream);

                return(new Serializer(result.ReadToEnd()));
            }
        }
Esempio n. 4
0
        public void Should_deserialize_a_batched_messages_with_typeName_passed_in_externally_even_when_not_initialized_with_type()
        {
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.WriteLine("<Messages><WhatEver><Double>23.4</Double></WhatEver><TheEmptyMessage></TheEmptyMessage></Messages>");
                writer.Flush();
                stream.Position = 0;

                var msgArray = SerializerFactory.Create()
                               .Deserialize(stream, new[]
                {
                    typeof(MessageWithDouble),
                    typeof(EmptyMessage)
                });

                Assert.AreEqual(23.4, ((MessageWithDouble)msgArray[0]).Double);
                Assert.AreEqual(typeof(EmptyMessage), msgArray[1].GetType());
            }
        }
Esempio n. 5
0
        public void CanDeserializeNullableArrayWithValueSetToNullString()
        {
            var xml  = @"<?xml version=""1.0"" ?>
<MessageWithNullableArray xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
<SagaId>00000000-0000-0000-0000-000000000000</SagaId>
<SomeInts>
<NullableOfInt32>null</NullableOfInt32>
</SomeInts>
</MessageWithNullableArray>
";
            var data = Encoding.UTF8.GetBytes(xml);

            using (var stream = new MemoryStream(data))
            {
                var msgArray = SerializerFactory.Create <MessageWithNullableArray>().Deserialize(stream, new[] { typeof(MessageWithNullableArray) });
                var result   = (MessageWithNullableArray)msgArray[0];

                Assert.AreEqual(null, result.SomeInts[0]);
            }
        }
Esempio n. 6
0
        public void CanDeserializeOriginalNullValueMessage()
        {
            var messageXml = @"<?xml version=""1.0""?>
<MessageWithNullable xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
   <FirstName>FirstName</FirstName>
   <LastName>LastName</LastName>
   <EmailAddress>EmailAddress</EmailAddress>
   <BirthDate>null</BirthDate>
</MessageWithNullable>
";

            var data = Encoding.UTF8.GetBytes(messageXml);

            using (var stream = new MemoryStream(data))
            {
                var msgArray = SerializerFactory.Create <MessageWithNullable>().Deserialize(stream, new[] { typeof(MessageWithNullable) });
                var result   = (MessageWithNullable)msgArray[0];

                Assert.AreEqual(null, result.BirthDate);
            }
        }
Esempio n. 7
0
        public void Should_handle_concrete_message_with_interface_property()
        {
            var message = new MessageWithInterfaceProperty
            {
                InterfaceProperty = new InterfacePropertyImplementation
                {
                    SomeProperty = "test"
                }
            };
            var serializer = SerializerFactory.Create <MessageWithInterfaceProperty>();

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

                stream.Position = 0;

                var result = (MessageWithInterfaceProperty)serializer.Deserialize(stream)[0];

                Assert.AreEqual(message.InterfaceProperty.SomeProperty, result.InterfaceProperty.SomeProperty);
            }
        }
Esempio n. 8
0
        public void SerializeLists()
        {
            IMessageMapper mapper     = new MessageMapper();
            var            serializer = SerializerFactory.Create <MessageWithList>();
            var            msg        = mapper.CreateInstance <MessageWithList>();

            msg.Items = new List <MessageWithListItem> {
                new MessageWithListItem {
                    Data = "Hello"
                }
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new[] { msg }, stream);
                stream.Position = 0;

                var msgArray = serializer.Deserialize(stream);
                var m        = msgArray[0] as MessageWithList;
                Assert.AreEqual("Hello", m.Items.First().Data);
            }
        }
Esempio n. 9
0
        public void SerializeClosedGenericListsInSameNamespace()
        {
            IMessageMapper mapper     = new MessageMapper();
            var            serializer = SerializerFactory.Create <MessageWithClosedList>();
            var            msg        = mapper.CreateInstance <MessageWithClosedList>();

            msg.Items = new ItemList {
                new MessageWithListItem {
                    Data = "Hello"
                }
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(msg, stream);
                stream.Position = 0;

                var msgArray = serializer.Deserialize(stream);
                var m        = (MessageWithClosedList)msgArray[0];
                Assert.AreEqual("Hello", m.Items.First().Data);
            }
        }
Esempio n. 10
0
        public void SerializeClosedGenericListsInAlternateNamespaceMultipleIListImplementations()
        {
            IMessageMapper mapper     = new MessageMapper();
            var            serializer = SerializerFactory.Create <MessageWithClosedListInAlternateNamespaceMultipleIListImplementations>();
            var            msg        = mapper.CreateInstance <MessageWithClosedListInAlternateNamespaceMultipleIListImplementations>();

            msg.Items = new AlternateNamespace.AlternateItemListMultipleIListImplementations {
                new AlternateNamespace.MessageWithListItemAlternate {
                    Data = "Hello"
                }
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(msg, stream);
                stream.Position = 0;

                var msgArray = serializer.Deserialize(stream);
                var m        = (MessageWithClosedListInAlternateNamespaceMultipleIListImplementations)msgArray[0];
                Assert.AreEqual("Hello", m.Items.First <AlternateNamespace.MessageWithListItemAlternate>().Data);
            }
        }
Esempio n. 11
0
        public void SerializeClosedGenericListsInAlternateNamespace()
        {
            IMessageMapper mapper     = new MessageMapper();
            var            serializer = SerializerFactory.Create <MessageWithClosedListInAlternateNamespace>();
            var            msg        = mapper.CreateInstance <MessageWithClosedListInAlternateNamespace>();

            msg.Items = new NServiceBus.Serializers.XML.Test.AlternateNamespace.AlternateItemList {
                new NServiceBus.Serializers.XML.Test.AlternateNamespace.MessageWithListItemAlternate {
                    Data = "Hello"
                }
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new[] { msg }, stream);
                stream.Position = 0;

                var msgArray = serializer.Deserialize(stream);
                var m        = msgArray[0] as MessageWithClosedListInAlternateNamespace;
                Assert.AreEqual("Hello", m.Items.First().Data);
            }
        }
Esempio n. 12
0
        public void SerializeInvalidCharacters()
        {
            IMessageMapper mapper     = new MessageMapper();
            var            serializer = SerializerFactory.Create <MessageWithInvalidCharacter>();
            var            msg        = mapper.CreateInstance <MessageWithInvalidCharacter>();

            var sb = new StringBuilder();

            sb.Append("Hello");
            sb.Append((char)0x1C);
            sb.Append("John");
            msg.Special = sb.ToString();

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(msg, stream);
                stream.Position = 0;

                var msgArray = serializer.Deserialize(stream);
                var m        = (MessageWithInvalidCharacter)msgArray[0];
                Assert.AreEqual(sb.ToString(), m.Special);
            }
        }
Esempio n. 13
0
        public void Deserialize_private_message_with_two_unrelated_interface_without_wrapping()
        {
            var serializer   = SerializerFactory.Create(typeof(CompositeMessage), typeof(IMyEventA), typeof(IMyEventB));
            var deserializer = SerializerFactory.Create(typeof(IMyEventA), typeof(IMyEventB));

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

                serializer.Serialize(msg, stream);

                stream.Position = 0;

                var result = deserializer.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);
            }
        }
Esempio n. 14
0
        public void Should_be_able_to_deserialize_many_messages_of_same_type()
        {
            var xml = @"<?xml version=""1.0"" ?>
<Messages xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
    <EmptyMessage>
    </EmptyMessage>
    <EmptyMessage>
    </EmptyMessage>
    <EmptyMessage>
    </EmptyMessage>
</Messages>
";

            using (var stream = new MemoryStream())
            {
                var streamWriter = new StreamWriter(stream);
                streamWriter.Write(xml);
                streamWriter.Flush();
                stream.Position = 0;
                var serializer = SerializerFactory.Create <EmptyMessage>();
                var msgArray   = serializer.Deserialize(stream, new[] { typeof(EmptyMessage) });
                Assert.AreEqual(3, msgArray.Length);
            }
        }
Esempio n. 15
0
        public void Culture()
        {
            var serializer = SerializerFactory.Create <MessageWithDouble>();
            var val        = 65.36;
            var msg        = new MessageWithDouble {
                Double = val
            };

            Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

            var stream = new MemoryStream();

            serializer.Serialize(msg, stream);

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            stream.Position = 0;
            var msgArray = serializer.Deserialize(stream);
            var m        = (MessageWithDouble)msgArray[0];

            Assert.AreEqual(val, m.Double);

            stream.Dispose();
        }
Esempio n. 16
0
        public void Should_be_able_to_serialize_single_message_with_default_namespaces()
        {
            var serializer = SerializerFactory.Create <EmptyMessage>();

            serializer.SkipWrappingElementForSingleMessages = true;
            var msg = new EmptyMessage();

            var expected = @"<EmptyMessage xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">";

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new[] { msg }, stream);
                stream.Position = 0;

                string result;
                using (var reader = new StreamReader(stream))
                {
                    reader.ReadLine();
                    result = reader.ReadLine();
                }

                Assert.AreEqual(expected, result);
            }
        }
Esempio n. 17
0
        public void NullableTypeSerializeToValueWhenNotNull()
        {
            var message = new MessageWithNullable
            {
                FirstName    = "FirstName",
                LastName     = "LastName",
                EmailAddress = "EmailAddress",
                BirthDate    = new DateTime(1950, 04, 25)
            };

            using (var stream = new MemoryStream())
            {
                SerializerFactory.Create <MessageWithNullable>().Serialize(message, stream);
                stream.Position = 0;
                var reader = new StreamReader(stream);
                var xml    = reader.ReadToEnd();

#if NET452
                var birthDate = "1950-04-25T00:00:00";
#else
                var birthDate = "1950-04-25T00:00:00.0000000";
#endif

                var expected = XDocument.Parse($@"<?xml version=""1.0""?>
<MessageWithNullable xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://tempuri.net/NServiceBus.Serializers.XML.Test"">
   <FirstName>FirstName</FirstName>
   <LastName>LastName</LastName>
   <EmailAddress>EmailAddress</EmailAddress>
   <BirthDate>{birthDate}</BirthDate>
</MessageWithNullable>
");
                var actual   = XDocument.Parse(xml);

                Assert.AreEqual(expected.ToString(), actual.ToString());
            }
        }
Esempio n. 18
0
        public void TestInterfaces()
        {
            var mapper     = new MessageMapper();
            var serializer = SerializerFactory.Create <ISecondSerializableMessage>(mapper);


            var o = mapper.CreateInstance <ISecondSerializableMessage>();

            o.Id      = Guid.NewGuid();
            o.Age     = 10;
            o.Address = Guid.NewGuid().ToString();
            o.Int     = 7;
            o.Name    = "udi";
            o.Uri     = new Uri("http://www.UdiDahan.com/");
            o.Risk    = new Risk
            {
                Percent  = 0.15D,
                Annum    = true,
                Accuracy = 0.314M
            };
            o.Some         = SomeEnum.B;
            o.Start        = DateTime.Now;
            o.Duration     = TimeSpan.Parse("-01:15:27.123");
            o.Offset       = DateTimeOffset.Now;
            o.Lookup       = new MyDictionary();
            o.Lookup["1"]  = "1";
            o.Foos         = new Dictionary <string, List <Foo> >();
            o.Foos["foo1"] = new List <Foo>(new[]
            {
                new Foo
                {
                    Name  = "1",
                    Title = "1"
                },
                new Foo
                {
                    Name  = "2",
                    Title = "2"
                }
            });
            o.Data = new byte[]
            {
                1,
                2,
                3,
                4,
                5,
                4,
                3,
                2,
                1
            };
            o.SomeStrings = new List <string>
            {
                "a",
                "b",
                "c"
            };

            o.ArrayFoos = new[]
            {
                new Foo
                {
                    Name  = "FooArray1",
                    Title = "Mr."
                },
                new Foo
                {
                    Name  = "FooAray2",
                    Title = "Mrs"
                }
            };
            o.Bars = new[]
            {
                new Bar
                {
                    Name   = "Bar1",
                    Length = 1
                },
                new Bar
                {
                    Name   = "BAr2",
                    Length = 5
                }
            };
            o.NaturalNumbers = new HashSet <int>(new[]
            {
                0,
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9
            });
            o.Developers = new HashSet <string>(new[]
            {
                "Udi Dahan",
                "Andreas Ohlund",
                "Matt Burton",
                "Jonathan Oliver et al"
            });

            o.Parent         = mapper.CreateInstance <IFirstSerializableMessage>();
            o.Parent.Name    = "udi";
            o.Parent.Age     = 10;
            o.Parent.Address = Guid.NewGuid().ToString();
            o.Parent.Int     = 7;
            o.Parent.Name    = "-1";
            o.Parent.Risk    = new Risk
            {
                Percent  = 0.15D,
                Annum    = true,
                Accuracy = 0.314M
            };

            o.Names = new List <IFirstSerializableMessage>();
            for (var i = 0; i < number; i++)
            {
                var firstMessage = mapper.CreateInstance <IFirstSerializableMessage>();
                o.Names.Add(firstMessage);
                firstMessage.Age     = 10;
                firstMessage.Address = Guid.NewGuid().ToString();
                firstMessage.Int     = 7;
                firstMessage.Name    = i.ToString();
                firstMessage.Risk    = new Risk
                {
                    Percent  = 0.15D,
                    Annum    = true,
                    Accuracy = 0.314M
                };
            }

            o.MoreNames = o.Names.ToArray();

            Time(o, serializer);
        }
Esempio n. 19
0
 public void When_Using_A_Dictionary_With_An_Object_As_Value_should_throw()
 {
     Assert.Throws <NotSupportedException>(() => SerializerFactory.Create <MessageWithDictionaryWithAnObjectAsValue>());
 }