From dd94f7a1dc340c9bf63d3e4adda5dfe0947638db Mon Sep 17 00:00:00 2001 From: markpollack Date: Wed, 1 Oct 2008 20:31:58 +0000 Subject: [PATCH] SPRNET-1008 - Add check for [Serializable] in SimpleMessageConverter --- .../SimpleMessageListenerContainer.cs | 1 + .../Converter/SimpleMessageConverter.cs | 8 +- .../Nms/Core/SimpleMessageConverterTests.cs | 217 ++++++++++++++++++ .../Spring.Messaging.Nms.Tests.2005.csproj | 1 + 4 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Core/SimpleMessageConverterTests.cs diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/SimpleMessageListenerContainer.cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/SimpleMessageListenerContainer.cs index 3feb135d..1d76f511 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/SimpleMessageListenerContainer.cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/SimpleMessageListenerContainer.cs @@ -35,6 +35,7 @@ namespace Spring.Messaging.Nms.Listener /// MessageConsumer.Listener method to create concurrent /// MessageConsumers for the specified listeners. /// + /// Mark Pollack public class SimpleMessageListenerContainer : AbstractMessageListenerContainer, IExceptionListener { #region Logging diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/SimpleMessageConverter.cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/SimpleMessageConverter.cs index c2bfa10a..70036a66 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/SimpleMessageConverter.cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/SimpleMessageConverter.cs @@ -68,10 +68,10 @@ namespace Spring.Messaging.Nms.Support.Converter { return CreateMessageForMap((IDictionary) objectToConvert, session); } - else if (objectToConvert is ISerializable) + else if (objectToConvert != null && objectToConvert.GetType().IsSerializable) { return - CreateMessageForSerializable(((ISerializable) objectToConvert), session); + CreateMessageForSerializable(objectToConvert, session); } else { @@ -173,7 +173,7 @@ namespace Spring.Messaging.Nms.Support.Converter /// /// NMSException if thrown by NMS methods protected virtual IObjectMessage CreateMessageForSerializable( - ISerializable objectToSend, ISession session) + object objectToSend, ISession session) { return session.CreateObjectMessage(objectToSend); } @@ -230,7 +230,7 @@ namespace Spring.Messaging.Nms.Support.Converter protected virtual object ExtractSerializableFromMessage( IObjectMessage message) { - return message.Body as ISerializable; + return message.Body; } #endregion diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Core/SimpleMessageConverterTests.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Core/SimpleMessageConverterTests.cs new file mode 100644 index 00000000..e0e78c39 --- /dev/null +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Core/SimpleMessageConverterTests.cs @@ -0,0 +1,217 @@ +#region License + +/* + * Copyright © 2002-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +#region Imports + +using System; +using System.Collections; +using System.Text; +using Apache.NMS; +using Apache.NMS.ActiveMQ.OpenWire; +using NUnit.Framework; +using Rhino.Mocks; +using Spring.Messaging.Nms.Support.Converter; +using Spring.Util; + +#endregion + +namespace Spring.Messaging.Nms.Core +{ + /// + /// This class contains tests for SimpleMessageConverer + /// + /// Mark Pollack + [TestFixture] + public class SimpleMessageConverterTests + { + private MockRepository mocks; + private SimpleMessageConverter converter; + private ISession session; + + + [SetUp] + public void Setup() + { + mocks = new MockRepository(); + session = (ISession) mocks.CreateMock(typeof (ISession)); + converter = new SimpleMessageConverter(); + } + + [Test] + public void StringConversion() + { + ITextMessage message = (ITextMessage) mocks.CreateMock(typeof (ITextMessage)); + string content = "test"; + + Expect.Call(session.CreateTextMessage(content)).Return(message).Repeat.Once(); + string txt = message.Text; + LastCall.On(message).Return(content).Repeat.Once(); + + mocks.ReplayAll(); + + + IMessage msg = converter.ToMessage(content, session); + Assert.AreEqual(content, converter.FromMessage(msg)); + + mocks.VerifyAll(); + + + } + + [Test] + public void ByteArrayConversion() + { + IBytesMessage message = (IBytesMessage) mocks.CreateMock(typeof (IBytesMessage)); + ASCIIEncoding encoding = new ASCIIEncoding(); + byte[] content = encoding.GetBytes("test"); + + Expect.Call(session.CreateBytesMessage()).Return(message); + Expect.Call(message.Content = content).Repeat.Once(); + + + Expect.Call(message.Content).Return(content).Repeat.Once(); + + mocks.ReplayAll(); + + IMessage msg = converter.ToMessage(content, session); + Assert.AreEqual(content.Length, ((byte[])converter.FromMessage(msg)).Length); + + mocks.VerifyAll(); + } + + [Test] + public void MapConversion() + { + IMapMessage message = (IMapMessage) mocks.CreateMock(typeof (IMapMessage)); + IPrimitiveMap primitiveMap = new PrimitiveMap(); + IDictionary content = new Hashtable(); + content["key1"] = "value1"; + content["key2"] = "value2"; + + Expect.Call(session.CreateMapMessage()).Return(message).Repeat.Once(); + Expect.Call(message.Body).Return(primitiveMap).Repeat.Any(); + //can't seem to mock indexer... + + mocks.ReplayAll(); + + IMessage msg = converter.ToMessage(content, session); + Assert.AreEqual(content, converter.FromMessage(msg)); + + mocks.VerifyAll(); + + + } + + [Test] + public void Serializable() + { + IObjectMessage message = (IObjectMessage)mocks.CreateMock(typeof(IObjectMessage)); + + SerializableWithAttribute content = new SerializableWithAttribute(); + + Expect.Call(session.CreateObjectMessage(content)).Return(message).Repeat.Once(); + + Expect.Call(message.Body).Return(content).Repeat.Once(); + + mocks.ReplayAll(); + + + IMessage msg = converter.ToMessage(content, session); + Assert.AreEqual(content, converter.FromMessage(message)); + + mocks.VerifyAll(); + } + + [Test] + [ExpectedException(typeof(MessageConversionException))] + public void ToMessageThrowsExceptionIfGivenNullObjectToConvert() + { + converter.ToMessage(null, null); + } + + [Test] + [ExpectedException(typeof(MessageConversionException))] + public void ToMessageThrowsExceptionIfGivenIncompatibleObjectToConvert() + { + converter.ToMessage(new Cafe(), null); + } + + [Test] + public void ToMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage() + { + IObjectMessage message = (IObjectMessage) mocks.CreateMock(typeof (IObjectMessage)); + + mocks.ReplayAll(); + + IMessage msg = converter.ToMessage(message, session); + Assert.AreSame(message, msg); + + mocks.VerifyAll(); + } + + [Test] + public void FromMessageSimplyReturnsMessageAsIsIfSuppliedWithMessage() + { + IMessage message = (IMessage)mocks.CreateMock(typeof(IMessage)); + + mocks.ReplayAll(); + + Object msg = converter.FromMessage(message); + Assert.AreSame(message, msg); + + mocks.VerifyAll(); + } + + [Test] + public void DictionaryConversionWhereMapHasNonStringTypesForKeys() + { + IMapMessage message = (IMapMessage)mocks.CreateMock(typeof(IMapMessage)); + + + Expect.Call(session.CreateMapMessage()).Return(message); + mocks.ReplayAll(); + + IDictionary content = new Hashtable(); + content.Add(new Cafe(), "value1"); + + try + { + converter.ToMessage(content, session); + Assert.Fail("Should have thrown MessageConversionException"); + } catch (MessageConversionException) + { + + } + + mocks.VerifyAll(); + } + + + [Serializable] + public class SerializableWithAttribute + { + } + + public class Cafe + { + + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj b/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj index 279ac3b6..e050b2e8 100644 --- a/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj +++ b/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2005.csproj @@ -99,6 +99,7 @@ +