public void The_xml_message_serializer_should_rock_the_house()
        {
            byte[] serializedMessageData;

            var serializer = new XmlMessageSerializer();

            OutboundMessage.Set(x =>
            {
                x.SetSourceAddress("msmq://localhost/queue_name");
                x.SetDestinationAddress("msmq://remotehost/queue_name");
                x.SetResponseAddress("msmq://localhost/response_queue");
                x.SetFaultAddress("msmq://localhost/fault_queue");
                x.SetRetryCount(7);
            });

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, _message);

                serializedMessageData = output.ToArray();

                Trace.WriteLine(Encoding.UTF8.GetString(serializedMessageData));
            }

            using (MemoryStream input = new MemoryStream(serializedMessageData))
            {
                SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage;

                Assert.AreEqual(_message, receivedMessage);
            }
        }
예제 #2
0
		private void VerifyMessageHeaderIsPassed(Action<IOutboundMessage> setHeaderAction, Action<IInboundMessageHeaders> checkHeaderAction)
		{
			byte[] data;
			var serializer = new XmlMessageSerializer();

			OutboundMessage.Set(setHeaderAction);

			var message = new PingMessage();

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

				data = output.ToArray();
			}

			Trace.WriteLine(OutboundMessage.Headers.MessageType);

			Trace.WriteLine(Encoding.UTF8.GetString(data));

			using (MemoryStream input = new MemoryStream(data))
			{
				serializer.Deserialize(input);

				checkHeaderAction(CurrentMessage.Headers);
			}
		}
예제 #3
0
        public void Can_read_subscription_from_queue()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                      new CastleServiceLocator(new WindsorContainer()));

            var msg = new Message();

            serializer.Serialize(new object[] { new AddSubscription
                                                {
                                                    Endpoint = TransactionalTestQueueUri,
                                                    Type     = typeof(TestMessage).FullName,
                                                } }, msg.BodyStream);

            msg.Extension = Guid.NewGuid().ToByteArray();
            queue.Send(msg, MessageQueueTransactionType.None);
            msg = queue.Peek(TimeSpan.FromSeconds(30));
            queue.MoveToSubQueue("subscriptions", msg);


            var subscriptionStorage = new MsmqSubscriptionStorage(new DefaultReflection(),
                                                                  serializer,
                                                                  TestQueueUri.Uri,
                                                                  new EndpointRouter(),
                                                                  new SubQueueStrategy());

            subscriptionStorage.Initialize();

            var uri = subscriptionStorage
                      .GetSubscriptionsFor(typeof(TestMessage))
                      .Single();

            Assert.Equal(TransactionalTestQueueUri.Uri, uri);
        }
예제 #4
0
        public void Adding_then_removing_will_result_in_no_subscriptions()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var msg        = new Message();

            serializer.Serialize(new object[] { new AddSubscription
                                                {
                                                    Endpoint = TransactionalTestQueueUri,
                                                    Type     = typeof(TestMessage).FullName,
                                                } }, msg.BodyStream);
            msg.Extension = Guid.NewGuid().ToByteArray();

            queue.Send(msg, MessageQueueTransactionType.None);
            msg = queue.Peek(TimeSpan.FromSeconds(30));
            queue.MoveToSubQueue("subscriptions", msg);

            var subscriptionStorage = new MsmqSubscriptionStorage(new DefaultReflection(),
                                                                  serializer,
                                                                  TestQueueUri.Uri,
                                                                  new EndpointRouter(),
                                                                  new SubQueueStrategy());

            subscriptionStorage.Initialize();
            subscriptionStorage.RemoveSubscription(typeof(TestMessage).FullName, TransactionalTestQueueUri.Uri.ToString());

            var uris = subscriptionStorage
                       .GetSubscriptionsFor(typeof(TestMessage));

            Assert.Equal(0, uris.Count());
        }
예제 #5
0
        public void Trying_to_send_more_than_256_objects_will_fail()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                      new CastleServiceLocator(new WindsorContainer()));

            Assert.Throws <UnboundedResultSetException>(() => serializer.Serialize(new object[257], new MemoryStream()));
        }
예제 #6
0
        private void VerifyMessageHeaderIsPassed(Action <IOutboundMessage> setHeaderAction, Action <IInboundMessageHeaders> checkHeaderAction)
        {
            byte[] data;
            var    serializer = new XmlMessageSerializer();

            OutboundMessage.Set(setHeaderAction);

            var message = new PingMessage();

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

                data = output.ToArray();
            }

            Trace.WriteLine(OutboundMessage.Headers.MessageType);

            Trace.WriteLine(Encoding.UTF8.GetString(data));

            using (MemoryStream input = new MemoryStream(data))
            {
                serializer.Deserialize(input);

                checkHeaderAction(CurrentMessage.Headers);
            }
        }
예제 #7
0
        public void Can_roundtrip_with_datetime_on_non_english_culture(string cultureName)
        {
            var oldCulture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);

            try
            {
                var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                          new CastleServiceLocator(new WindsorContainer()));

                var stream = new MemoryStream();
                var date   = DateTime.Now;
                serializer.Serialize(new object[] { new Bar {
                                                        Date = date
                                                    } }, stream);

                stream.Position = 0;

                var bar = (Bar)serializer.Deserialize(stream)[0];
                Assert.Equal(date, bar.Date);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = oldCulture;
            }
        }
예제 #8
0
        public void Can_handle_dictionaries_where_values_are_objects()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                      new CastleServiceLocator(new WindsorContainer()));

            var stream = new MemoryStream();

            serializer.Serialize(new object[] { new ItemWithObjectDictionary()
                                                {
                                                    Name = "abc", Arguments = new Dictionary <string, object>
                                                    {
                                                        { "abc", "cdef" },
                                                        { "def", 1 },
                                                        { "123", new Dog {
                                                              Name = "Oscar"
                                                          } }
                                                    }
                                                } }, stream);

            stream.Position = 0;

            stream.Position = 0;


            var foo = (ItemWithObjectDictionary)serializer.Deserialize(stream)[0];

            Assert.Equal("abc", foo.Name);
            Assert.Equal("cdef", foo.Arguments["abc"]);
            Assert.Equal(1, (int)foo.Arguments["def"]);
            Assert.Equal("Oscar", ((Dog)foo.Arguments["123"]).Name);
        }
예제 #9
0
        void VerifyMessageHeaderIsPassed(Action <ISendContext <PingMessage> > setHeaderAction,
                                         Action <IConsumeContext> checkHeaderAction)
        {
            byte[] data;
            var    serializer = new XmlMessageSerializer();

            var message = new PingMessage();

            using (var output = new MemoryStream())
            {
                var sendContext = new SendContext <PingMessage>(message);
                setHeaderAction(sendContext);

                serializer.Serialize(output, sendContext);

                data = output.ToArray();
            }

            //Trace.WriteLine(Encoding.UTF8.GetString(data));

            using (var input = new MemoryStream(data))
            {
                var receiveContext = ReceiveContext.FromBodyStream(input);
                serializer.Deserialize(receiveContext);

                checkHeaderAction(receiveContext);
            }
        }
        public void Can_read_subscription_from_queue()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                      new CastleServiceLocator(new WindsorContainer()));

            var msg = new Message();

            serializer.Serialize(new object[] { new AddSubscription
                                                {
                                                    Endpoint = transactionalTestQueueEndpoint,
                                                    Type     = typeof(TestMessage).FullName,
                                                } }, msg.BodyStream);
            msg.Extension = Guid.NewGuid().ToByteArray();
            queue.OpenSiblngQueue(SubQueue.Subscriptions, QueueAccessMode.Send).Send(msg);


            var subscriptionStorage = new MsmqSubscriptionStorage(new DefaultReflection(),
                                                                  serializer,
                                                                  testQueueEndPoint.Uri,
                                                                  new EndpointRouter(),
                                                                  new FlatQueueStrategy(new EndpointRouter(), testQueueEndPoint.Uri));

            subscriptionStorage.Initialize();

            var uri = subscriptionStorage
                      .GetSubscriptionsFor(typeof(TestMessage))
                      .Single();

            Assert.Equal(transactionalTestQueueEndpoint.Uri, uri);
        }
        public void Adding_then_removing_will_result_in_no_subscriptions()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var msg        = new Message();

            serializer.Serialize(new object[] { new AddSubscription
                                                {
                                                    Endpoint = transactionalTestQueueEndpoint,
                                                    Type     = typeof(TestMessage).FullName,
                                                } }, msg.BodyStream);

            msg.Extension = Guid.NewGuid().ToByteArray();
            queue.OpenSiblngQueue(SubQueue.Subscriptions, QueueAccessMode.Send).Send(msg);

            var subscriptionStorage = new MsmqSubscriptionStorage(new DefaultReflection(),
                                                                  serializer,
                                                                  testQueueEndPoint.Uri,
                                                                  new EndpointRouter(),
                                                                  new FlatQueueStrategy(new EndpointRouter(), testQueueEndPoint.Uri));

            subscriptionStorage.Initialize();
            subscriptionStorage.RemoveSubscription(typeof(TestMessage).FullName, transactionalTestQueueEndpoint.Uri.ToString());

            var uris = subscriptionStorage
                       .GetSubscriptionsFor(typeof(TestMessage));

            Assert.Equal(0, uris.Count());
        }
예제 #12
0
        public void Can_serialize_and_deserialize_byte_array()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { new byte[] { 1, 2, 3, 4 } }, stream);
            stream.Position = 0;
            var actual = (byte[])serializer.Deserialize(stream)[0];

            Assert.Equal(new byte[] { 1, 2, 3, 4 }, actual);
        }
예제 #13
0
        public void Can_serialize_and_deserialize_byte_array()
        {
            var serializer = new XmlMessageSerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { new byte[] { 1, 2, 3, 4 } }, stream);
            stream.Position = 0;
            var actual = (byte[])serializer.Deserialize(stream).As <byte[]>();

            new byte[] { 1, 2, 3, 4 }.ShouldEqual(actual);
        }
예제 #14
0
        public void Can_serialize_and_deserialize_primitive()
        {
            long ticks      = DateTime.Now.Ticks;
            var  serializer = new XmlMessageSerializer();
            var  stream     = new MemoryStream();

            serializer.Serialize(new object[] { ticks }, stream);
            stream.Position = 0;
            var actual = (long)serializer.Deserialize(stream).As <long>();

            ticks.ShouldEqual(actual);
        }
예제 #15
0
        public void Can_serialize_and_deserialize_float()
        {
            float aFloat     = 1.12f;
            var   serializer = new XmlMessageSerializer();
            var   stream     = new MemoryStream();

            serializer.Serialize(new object[] { aFloat }, stream);
            stream.Position = 0;
            var actual = (float)serializer.Deserialize(stream).As <float>();

            aFloat.ShouldEqual(actual);
        }
예제 #16
0
        public void Can_serialize_and_deserialize_double()
        {
            double aDouble    = 1.12;
            var    serializer = new XmlMessageSerializer();
            var    stream     = new MemoryStream();

            serializer.Serialize(new object[] { aDouble }, stream);
            stream.Position = 0;
            var actual = (double)serializer.Deserialize(stream).As <double>();

            aDouble.ShouldEqual(actual);
        }
예제 #17
0
        public void Can_serialize_and_deserialize_float()
        {
            float aFloat     = 1.12f;
            var   serializer = new XmlMessageSerializer(new DefaultReflection(), new CastleServiceLocator(new WindsorContainer()));
            var   stream     = new MemoryStream();

            serializer.Serialize(new object[] { aFloat }, stream);
            stream.Position = 0;
            var actual = (float)serializer.Deserialize(stream)[0];

            Assert.Equal(aFloat, actual);
        }
예제 #18
0
        public void Can_serialize_and_deserialize_DateTimeOffset()
        {
            var value      = DateTimeOffset.Now;
            var serializer = new XmlMessageSerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { value }, stream);
            stream.Position = 0;
            var actual = (DateTimeOffset)serializer.Deserialize(stream).As <DateTimeOffset>();

            value.ShouldEqual(actual);
        }
예제 #19
0
        public void Can_serialize_and_deserialize_double()
        {
            double aDouble    = 1.12;
            var    serializer = new XmlMessageSerializer(new DefaultReflection(), new CastleServiceLocator(new WindsorContainer()));
            var    stream     = new MemoryStream();

            serializer.Serialize(new object[] { aDouble }, stream);
            stream.Position = 0;
            var actual = (double)serializer.Deserialize(stream)[0];

            Assert.Equal(aDouble, actual);
        }
예제 #20
0
        public void can_serialize_and_deserialize_icpc_monitor()
        {
            var    ms         = new MemoryStream();
            var    serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var    submission = S("testuser A AC 11");
            var    builder    = new IcpcMonitorBuilder(contest);
            object monitor    = builder.BuildMonitor(new[] { submission });

            serializer.Serialize(new[] { monitor }, ms);
            ms.Position = 0;
            serializer.Deserialize(ms);
        }
예제 #21
0
        public void Can_serialize_and_deserialize_DateTimeOffset()
        {
            var value      = DateTimeOffset.Now;
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { value }, stream);
            stream.Position = 0;
            var actual = (DateTimeOffset)serializer.Deserialize(stream)[0];

            Assert.Equal(value, actual);
        }
예제 #22
0
        private string GenerateXml(object message)
        {
            using (var stream = new MemoryStream())
            {
                _tibcoMessageSerializer.Serialize(new object[] { message }, stream); //TODO: handle for json, and binary, objectMessage???
                stream.Position = 0;

                var doc = new XmlDocument();
                doc.Load(stream);
                return(doc.LastChild.InnerXml);
            }
        }
예제 #23
0
        public void Can_serialize_and_deserialize_primitive()
        {
            long ticks      = DateTime.Now.Ticks;
            var  serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var  stream     = new MemoryStream();

            serializer.Serialize(new object[] { ticks }, stream);
            stream.Position = 0;
            var actual = (long)serializer.Deserialize(stream)[0];

            Assert.Equal(ticks, actual);
        }
예제 #24
0
        public void ShouldDeserializeFromXml()
        {
            IMessageSerializer serializer = new XmlMessageSerializer();
            IMessageDeserializer <TestMessageProto> deserializer = new XmlMessageDeserializer <TestMessageProto>();

            var message = new TestMessageProto {
                Id = 123, Name = "SomeName"
            };
            var serialize   = serializer.Serialize(message);
            var deserialize = deserializer.Deserialize(serialize);

            Assert.Equal(message, deserialize);
        }
예제 #25
0
        public void ShouldSerializeToXml()
        {
            IMessageSerializer serializer = new XmlMessageSerializer();

            var message = new TestMessageProto {
                Id = 123, Name = "SomeName"
            };
            var serialize = XmlSerializerDefaults.Encoding.GetString(serializer.Serialize(message));

            Assert.NotNull(serialize);
            Assert.Contains(message.Id.ToString(), serialize);
            Assert.Contains(message.Name, serialize);
        }
예제 #26
0
        public void can_round_trip_single_message()
        {
            var serializer = new XmlMessageSerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(sample, stream);

            stream.Position = 0;

            var actual = serializer.Deserialize(stream).ShouldBeOfType <Order>();

            actual.OrderId.ShouldEqual(sample.OrderId);
        }
        public void ShouldDeserializeFromXml()
        {
            IMessageSerializer serializer = new XmlMessageSerializer();
            IMessageDeserializer <TestMessage> deserializer = new XmlMessageDeserializer <TestMessage>();

            var message = new TestMessage {
                Id = 123, Name = "SomeName"
            };
            var serialize   = serializer.Serialize(message);
            var deserialize = deserializer.Deserialize(serialize);

            Assert.That(message, Is.EqualTo(deserialize));
        }
예제 #28
0
        public void ShouldDeserializeFromXmlAscii()
        {
            IMessageSerializer serializer = new XmlMessageSerializer();
            IMessageDeserializer <TestMessageProto> deserializer = new XmlMessageDeserializer <TestMessageProto>();

            var message = new TestMessageProto {
                Id = 123, Name = "SomeName"
            };
            var serialize   = serializer.Serialize(message);
            var ascii       = Encoding.Convert(XmlSerializerDefaults.Encoding, Encoding.ASCII, serialize);
            var deserialize = deserializer.Deserialize(ascii);

            Assert.Equal(message, deserialize);
        }
        public void ShouldSerializeToXml()
        {
            IMessageSerializer serializer = new XmlMessageSerializer();

            var message = new TestMessage {
                Id = 123, Name = "SomeName"
            };
            var serialize = XmlSerializerDefaults.Encoding.GetString(serializer.Serialize(message));

            Assert.That(serialize, Is.Not.Null);
            Assert.That(serialize, Is.Not.Empty);
            Assert.That(serialize, Contains.Substring(message.Id.ToString()));
            Assert.That(serialize, Contains.Substring(message.Name));
        }
예제 #30
0
        public void Serialize_CorrectText_TextMessageCreated()
        {
            var serializer = new XmlMessageSerializer <TextMessage>();

            var result = serializer.Serialize(new TextMessage {
                Body = "testMessage"
            });

            var message = (TextMessage)result;
            var m       = message.Body.Replace("\r", "")
                          .Replace("\n", "")
                          .Replace(">  <", "><");

            Assert.That(m == MessageText);
        }
예제 #31
0
        public static TransportMessage Serialize <T>(T message)
        {
            var s = new XmlMessageSerializer(new MessageMapper());

            s.Initialize(new [] { typeof(T) });

            var m = EmptyTransportMessage();

            using (var stream = new MemoryStream())
            {
                s.Serialize(new object[] { message }, stream);
                m.Body = stream.ToArray();
            }

            return(m);
        }
예제 #32
0
        public void The_xml_message_serializer_should_rock_the_house()
        {
            byte[] serializedMessageData;

            var serializer = new XmlMessageSerializer();

			OutboundMessage.Set(x =>
				{
					x.SetSourceAddress("msmq://localhost/queue_name");
					x.SetDestinationAddress("msmq://remotehost/queue_name");
					x.SetResponseAddress("msmq://localhost/response_queue");
					x.SetFaultAddress("msmq://localhost/fault_queue");
					x.SetRetryCount(7);
				});

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, _message);

                serializedMessageData = output.ToArray();

                Trace.WriteLine(Encoding.UTF8.GetString(serializedMessageData));
            }

            using (MemoryStream input = new MemoryStream(serializedMessageData))
            {
                SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage;

                Assert.AreEqual(_message, receivedMessage);
            }
        }
예제 #33
0
		public void Just_how_fast_is_the_custom_xml_serializer()
		{
			var message = new SerializationTestMessage
				{
					DecimalValue = 123.45m,
					LongValue = 098123213,
					BoolValue = true,
					ByteValue = 127,
					IntValue = 123,
					DateTimeValue = new DateTime(2008, 9, 8, 7, 6, 5, 4),
					TimeSpanValue = 30.Seconds(),
					GuidValue = Guid.NewGuid(),
					StringValue = "Chris's Sample Code",
					DoubleValue = 1823.172,
				};

//			var message = new PingMessage
//				{
//					CorrelationId = CombGuid.Generate()
//				};

			var serializer = new XmlMessageSerializer();

			for (int i = 0; i < 10; i++)
			{
				byte[] data;
				using (MemoryStream output = new MemoryStream())
				{
					serializer.Serialize(output, message);
					data = output.ToArray();
				}
				using (MemoryStream input = new MemoryStream(data))
				{
					//		serializer.Deserialize(input);
				}
			}

			Stopwatch timer = Stopwatch.StartNew();

			const int iterations = 50000;

			for (int i = 0; i < iterations; i++)
			{
				using (MemoryStream output = new MemoryStream())
				{
					serializer.Serialize(output, message);
				}
			}

			timer.Stop();

			long perSecond = iterations*1000/timer.ElapsedMilliseconds;

			var msg = string.Format("Serialize: {0}ms, Rate: {1} m/s", timer.ElapsedMilliseconds, perSecond);
			Trace.WriteLine(msg);

			byte[] sample;
			using (MemoryStream output = new MemoryStream())
			{
				serializer.Serialize(output, message);
				sample = output.ToArray();
			}

			timer = Stopwatch.StartNew();

			for (int i = 0; i < 50000; i++)
			{
				using (MemoryStream input = new MemoryStream(sample))
				{
					serializer.Deserialize(input);
				}
			}

			timer.Stop();

			perSecond = iterations*1000/timer.ElapsedMilliseconds;

			msg = string.Format("Deserialize: {0}ms, Rate: {1} m/s", timer.ElapsedMilliseconds, perSecond);
			Trace.WriteLine(msg);
		}