Repository: qpid-jms Updated Branches: refs/heads/master 616f0e773 -> ee5042c14
Test the AmqpJmsMapMessageFacade Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/ee5042c1 Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/ee5042c1 Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/ee5042c1 Branch: refs/heads/master Commit: ee5042c14cb50326318c07963947ea3667ac838b Parents: 616f0e7 Author: Timothy Bish <[email protected]> Authored: Wed Oct 8 16:38:21 2014 -0400 Committer: Timothy Bish <[email protected]> Committed: Wed Oct 8 16:38:21 2014 -0400 ---------------------------------------------------------------------- .../message/AmqpJmsMapMessageFacadeTest.java | 234 +++++++++++++++++++ 1 file changed, 234 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/ee5042c1/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/amqp/message/AmqpJmsMapMessageFacadeTest.java ---------------------------------------------------------------------- diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/amqp/message/AmqpJmsMapMessageFacadeTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/amqp/message/AmqpJmsMapMessageFacadeTest.java new file mode 100644 index 0000000..3475dcf --- /dev/null +++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/provider/amqp/message/AmqpJmsMapMessageFacadeTest.java @@ -0,0 +1,234 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.qpid.jms.provider.amqp.message; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import org.apache.qpid.jms.JmsDestination; +import org.apache.qpid.jms.JmsTopic; +import org.apache.qpid.jms.provider.amqp.AmqpConnection; +import org.apache.qpid.jms.provider.amqp.AmqpConsumer; +import org.apache.qpid.jms.test.QpidJmsTestCase; +import org.apache.qpid.proton.amqp.Symbol; +import org.apache.qpid.proton.amqp.messaging.AmqpSequence; +import org.apache.qpid.proton.amqp.messaging.AmqpValue; +import org.apache.qpid.proton.amqp.messaging.MessageAnnotations; +import org.apache.qpid.proton.message.Message; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * Test for the AmqpJmsMapMessageFacade class + */ +public class AmqpJmsMapMessageFacadeTest extends QpidJmsTestCase { + + private JmsDestination consumerDestination; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + consumerDestination = new JmsTopic("TestTopic"); + }; + + //---------- Test initial state of newly created message -----------------// + + @Test + public void testNewMessageToSendContainsMessageTypeAnnotation() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + + Message protonMessage = amqpMapMessageFacade.getAmqpMessage(); + MessageAnnotations annotations = protonMessage.getMessageAnnotations(); + Map<Symbol, Object> annotationsMap = annotations.getValue(); + + assertNotNull("MessageAnnotations section was not present", annotations); + assertNotNull("MessageAnnotations section value was not present", annotationsMap); + + assertTrue("expected message type annotation to be present", annotationsMap.containsKey(AmqpMessageSupport.getSymbol(AmqpMessageSupport.JMS_MSG_TYPE))); + assertEquals("unexpected value for message type annotation value", AmqpMessageSupport.JMS_MAP_MESSAGE, annotationsMap.get(AmqpMessageSupport.getSymbol(AmqpMessageSupport.JMS_MSG_TYPE))); + } + + @Test + public void testNewMessageToSendClearBodyDoesNotFail() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + amqpMapMessageFacade.clearBody(); + } + + @Test + public void testNewMessageToSendReportsIsEmpty() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + assertTrue(amqpMapMessageFacade.isEmpty()); + } + + @Test + public void testNewMessageToSendItemExists() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + assertFalse(amqpMapMessageFacade.itemExists("entry")); + } + + @Test + public void testNewMessageToSendGetReturnsNull() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + assertNull(amqpMapMessageFacade.get("entry")); + } + + @Test + public void testNewMessageToSendRemoveReturnsNull() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + assertNull(amqpMapMessageFacade.remove("entry")); + } + + @Test + public void testNewMessageToSendReturnsEmptyMapNamesEnumeration() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + assertNotNull(amqpMapMessageFacade.getMapNames()); + + Enumeration<String> names = amqpMapMessageFacade.getMapNames(); + assertFalse(names.hasMoreElements()); + } + + // ---------- test for normal message operations -------------------------// + + @Test + public void testMessageClearBodyWorks() throws Exception { + AmqpJmsMapMessageFacade amqpMapMessageFacade = createNewMapMessageFacade(); + assertTrue(amqpMapMessageFacade.isEmpty()); + amqpMapMessageFacade.put("entry", "value"); + assertFalse(amqpMapMessageFacade.isEmpty()); + amqpMapMessageFacade.clearBody(); + assertTrue(amqpMapMessageFacade.isEmpty()); + } + + // ---------- test handling of received messages -------------------------// + + @Test + public void testCreateWithEmptyMap() throws Exception { + Message message = Message.Factory.create(); + message.setBody(new AmqpValue(new HashMap<String, Object>())); + + AmqpJmsMapMessageFacade amqpMapMessageFacade = createReceivedMapMessageFacade(createMockAmqpConsumer(), message); + + // Should be able to use the message, e.g clearing it and adding to it. + amqpMapMessageFacade.clearBody(); + amqpMapMessageFacade.put("entry", "value"); + } + + @Test + public void testCreateWithPopulatedMap() throws Exception { + Message message = Message.Factory.create(); + Map<String, Object> bodyMap = new HashMap<String, Object>(); + bodyMap.put("entry1", Boolean.TRUE); + bodyMap.put("entry2", Boolean.FALSE); + + message.setBody(new AmqpValue(bodyMap)); + + AmqpJmsMapMessageFacade amqpMapMessageFacade = createReceivedMapMessageFacade(createMockAmqpConsumer(), message); + + // Data should be preserved + assertFalse(amqpMapMessageFacade.isEmpty()); + Object result = amqpMapMessageFacade.get("entry1"); + assertNotNull(result); + assertTrue(result instanceof Boolean); + + // Should be able to use the message, e.g clearing it and adding to it. + amqpMapMessageFacade.clearBody(); + amqpMapMessageFacade.put("entry", "value"); + } + + @Test + public void testCreateWithAmqpSequenceBodySectionThrowsISE() throws Exception { + Message message = Message.Factory.create(); + message.setBody(new AmqpSequence(null) ); + + try { + createReceivedMapMessageFacade(createMockAmqpConsumer(), message); + fail("expected exception to be thrown"); + } catch (IllegalStateException ise) { + // expected + } + } + + @Test + public void testCreateWithAmqpValueBodySectionContainingUnexpectedValueThrowsISE() throws Exception { + Message message = Message.Factory.create(); + message.setBody(new AmqpValue("not-a-map")); + + try { + createReceivedMapMessageFacade(createMockAmqpConsumer(), message); + fail("expected exception to be thrown"); + } catch (IllegalStateException ise) { + // expected + } + } + + @Test + public void testCreateWithNullBodySection() throws Exception { + Message message = Message.Factory.create(); + message.setBody(null); + + AmqpJmsMapMessageFacade amqpMapMessageFacade = createReceivedMapMessageFacade(createMockAmqpConsumer(), message); + + // Should be able to use the message, e.g clearing it and adding to it. + amqpMapMessageFacade.clearBody(); + amqpMapMessageFacade.put("entry", "value"); + assertFalse(amqpMapMessageFacade.isEmpty()); + } + + @Test + public void testCreateWithEmptyAmqpValueBodySection() throws Exception { + Message message = Message.Factory.create(); + message.setBody(new AmqpValue(null)); + + AmqpJmsMapMessageFacade amqpMapMessageFacade = createReceivedMapMessageFacade(createMockAmqpConsumer(), message); + + // Should be able to use the message, e.g clearing it and adding to it. + amqpMapMessageFacade.clearBody(); + amqpMapMessageFacade.put("entry", "value"); + assertFalse(amqpMapMessageFacade.isEmpty()); + } + + //---------- Test Support Methods ----------------------------------------// + + private AmqpJmsMapMessageFacade createNewMapMessageFacade() { + return new AmqpJmsMapMessageFacade(createMockAmqpConnection()); + } + + private AmqpJmsMapMessageFacade createReceivedMapMessageFacade(AmqpConsumer amqpConsumer, Message message) { + return new AmqpJmsMapMessageFacade(amqpConsumer, message); + } + + private AmqpConsumer createMockAmqpConsumer() { + AmqpConsumer consumer = Mockito.mock(AmqpConsumer.class); + Mockito.when(consumer.getConnection()).thenReturn(createMockAmqpConnection()); + Mockito.when(consumer.getDestination()).thenReturn(consumerDestination); + return consumer; + } + + private AmqpConnection createMockAmqpConnection() { + return Mockito.mock(AmqpConnection.class); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
