diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/Adapter/MessageListenerAdapter.cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/Adapter/MessageListenerAdapter.cs index 63f99ac2..6ee27630 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/Adapter/MessageListenerAdapter.cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Listener/Adapter/MessageListenerAdapter.cs @@ -100,7 +100,6 @@ namespace Spring.Messaging.Nms.Listener.Adapter { InitDefaultStrategies(); handlerObject = this; - processingExpression = Expression.Parse(defaultHandlerMethod + "(#convertedObject)"); } /// @@ -141,6 +140,7 @@ namespace Spring.Messaging.Nms.Listener.Adapter set { defaultHandlerMethod = value; + processingExpression = Expression.Parse(defaultHandlerMethod + "(#convertedObject)"); } } @@ -286,14 +286,14 @@ namespace Spring.Messaging.Nms.Listener.Adapter // Regular case: find a handler method reflectively. object convertedMessage = ExtractMessage(message); - + IDictionary vars = new Hashtable(); vars["convertedObject"] = convertedMessage; //Need to parse each time since have overloaded methods and //expression processor caches target of first invocation. //TODO - check JIRA as I believe this has been fixed, otherwise, use regular reflection. -MLP - processingExpression = Expression.Parse(defaultHandlerMethod + "(#convertedObject)"); + //processingExpression = Expression.Parse(defaultHandlerMethod + "(#convertedObject)"); //Invoke message handler method and get result. object result; @@ -340,6 +340,7 @@ namespace Spring.Messaging.Nms.Listener.Adapter protected virtual void InitDefaultStrategies() { MessageConverter = new SimpleMessageConverter(); + processingExpression = Expression.Parse(defaultHandlerMethod + "(#convertedObject)"); } /// diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/IMessageContentsHandler.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/IMessageContentsHandler.cs new file mode 100644 index 00000000..a8f2f3f0 --- /dev/null +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/IMessageContentsHandler.cs @@ -0,0 +1,43 @@ + + +#region License + +/* + * Copyright 2002-2008 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 + +using System.Collections; + +namespace Spring.Messaging.Nms.Listener.Adapter +{ + /// + /// Used in MessageListenerAdapterTests + /// + /// Mark Pollack + public interface IMessageContentsHandler + { + void HandleMessage(IDictionary message); + + void HandleMessage(byte[] message); + + void HandleMessage(int message); + + void HandleMessage(object message); + + void HandleMessage(string message); + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/MessageContentsHandler.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/MessageContentsHandler.cs new file mode 100644 index 00000000..1b894d9b --- /dev/null +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/MessageContentsHandler.cs @@ -0,0 +1,63 @@ +#region License + +/* + * Copyright 2002-2008 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 + +using System.Collections; + +namespace Spring.Messaging.Nms.Listener.Adapter +{ + /// + /// + /// + /// + /// + /// + /// Mark Pollack + public class MessageContentsHandler : IMessageContentsHandler + { + public int HandledStringCount; + + public int HandledByteArrayCount; + + public void HandleMessage(IDictionary message) + { + throw new System.NotImplementedException(); + } + + public void HandleMessage(byte[] message) + { + HandledByteArrayCount++; + } + + public void HandleMessage(int message) + { + throw new System.NotImplementedException(); + } + + public void HandleMessage(object message) + { + throw new System.NotImplementedException(); + } + + public void HandleMessage(string message) + { + HandledStringCount++; + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/MessageListenerAdapterTests.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/MessageListenerAdapterTests.cs new file mode 100644 index 00000000..28d41e7e --- /dev/null +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Listener/Adapter/MessageListenerAdapterTests.cs @@ -0,0 +1,132 @@ +#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.Text; +using Apache.NMS; +using NUnit.Framework; +using Rhino.Mocks; + +#endregion + +namespace Spring.Messaging.Nms.Listener.Adapter +{ + /// + /// This class contains tests for MessageListenerAdapter + /// + /// Mark Pollack + /// $Id:$ + [TestFixture] + public class MessageListenerAdapterTests + { + private MockRepository mocks; + + private static string TEXT = "I fancy a good cuppa right now"; + + private static int NUMBER = 1; + + private static SerializableObject OBJECT = new SerializableObject(); + + private static string CORRELATION_ID = "100"; + + private static string RESPONSE_TEXT = "... wi' some full fat creamy milk. Top banana."; + + [SetUp] + public void Setup() + { + mocks = new MockRepository(); + } + + [Test] + public void MessageContentsHandlerForTextMessage() + { + int numIterations = 10; + ITextMessage message = (ITextMessage)mocks.CreateMock(typeof(ITextMessage)); + Expect.Call(message.Text).Return(TEXT).Repeat.Times(numIterations); + MessageContentsHandler handler = new MessageContentsHandler(); + mocks.ReplayAll(); + + MessageListenerAdapter adapter = new MessageListenerAdapter(handler); + for (int i = 0; i < numIterations; i++) + { + adapter.OnMessage(message); + } + Assert.AreEqual(numIterations, handler.HandledStringCount); + + mocks.VerifyAll(); + } + + [Test] + public void MessageContentsHandlerForBytesMessage() + { + int numIterations = 10; + IBytesMessage message = (IBytesMessage)mocks.CreateMock(typeof(IBytesMessage)); + ASCIIEncoding encoding = new ASCIIEncoding(); + byte[] content = encoding.GetBytes("test"); + Expect.Call(message.Content).Return(content).Repeat.Times(numIterations); + MessageContentsHandler handler = new MessageContentsHandler(); + mocks.ReplayAll(); + + MessageListenerAdapter adapter = new MessageListenerAdapter(handler); + for (int i = 0; i < numIterations; i++) + { + adapter.OnMessage(message); + } + Assert.AreEqual(numIterations, handler.HandledByteArrayCount); + + mocks.VerifyAll(); + } + + [Test] + public void MessageContentsHandlerOverloadCalls() + { + int numIterations = 10; + IBytesMessage bytesMessage = (IBytesMessage)mocks.CreateMock(typeof(IBytesMessage)); + ASCIIEncoding encoding = new ASCIIEncoding(); + byte[] content = encoding.GetBytes("test"); + Expect.Call(bytesMessage.Content).Return(content).Repeat.Times(numIterations / 2); + + ITextMessage textMessage = (ITextMessage)mocks.CreateMock(typeof(ITextMessage)); + Expect.Call(textMessage.Text).Return(TEXT).Repeat.Times(numIterations/2); + + MessageContentsHandler handler = new MessageContentsHandler(); + mocks.ReplayAll(); + + MessageListenerAdapter adapter = new MessageListenerAdapter(handler); + for (int i = 0; i < numIterations/2; i++) + { + adapter.OnMessage(textMessage); + adapter.OnMessage(bytesMessage); + } + Assert.AreEqual(numIterations / 2, handler.HandledByteArrayCount); + Assert.AreEqual(numIterations / 2, handler.HandledStringCount); + + mocks.VerifyAll(); + } + } + + [Serializable] + public class SerializableObject + { + + } +} \ 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 e050b2e8..894e6d76 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 @@ -105,6 +105,9 @@ + + +