You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by gs...@apache.org on 2014/11/14 12:03:20 UTC
[11/50] [abbrv] qpid-proton git commit: PROTON-711: add tests for the
existing destination type annotation processing done by the transformers
PROTON-711: add tests for the existing destination type annotation processing done by the transformers
git-svn-id: https://svn.apache.org/repos/asf/qpid/proton/trunk@1631793 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/c87fc8a2
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/c87fc8a2
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/c87fc8a2
Branch: refs/heads/examples
Commit: c87fc8a2944ad8d68dec4e66c78768a071da35c0
Parents: 12c6d30
Author: Robert Gemmell <ro...@apache.org>
Authored: Tue Oct 14 15:33:15 2014 +0000
Committer: Robert Gemmell <ro...@apache.org>
Committed: Tue Oct 14 15:33:15 2014 +0000
----------------------------------------------------------------------
.../jms/JMSMappingInboundTransformerTest.java | 214 ++++++++++++++++++
.../jms/JMSMappingOutboundTransformerTest.java | 226 +++++++++++++++++++
2 files changed, 440 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c87fc8a2/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
----------------------------------------------------------------------
diff --git a/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java b/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
new file mode 100644
index 0000000..42a99ca
--- /dev/null
+++ b/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
@@ -0,0 +1,214 @@
+/*
+ * 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.proton.jms;
+
+import static org.junit.Assert.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jms.Destination;
+import javax.jms.Queue;
+import javax.jms.TemporaryQueue;
+import javax.jms.TemporaryTopic;
+import javax.jms.TextMessage;
+import javax.jms.Topic;
+
+import org.apache.qpid.proton.amqp.Symbol;
+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.Test;
+import org.mockito.Mockito;
+
+public class JMSMappingInboundTransformerTest
+{
+ @Test
+ public void testTransformMessageWithAmqpValueStringCreatesTextMessage() throws Exception
+ {
+ TextMessage mockTextMessage = createMockTextMessage();
+ JMSVendor mockVendor = createMockVendor(mockTextMessage);
+ JMSMappingInboundTransformer transformer = new JMSMappingInboundTransformer(mockVendor);
+
+ String contentString = "myTextMessageContent";
+ Message amqp = Message.Factory.create();
+ amqp.setBody(new AmqpValue(contentString));
+
+ EncodedMessage em = encodeMessage(amqp);
+
+ javax.jms.Message jmsMessage = transformer.transform(em);
+
+ assertTrue("Expected TextMessage", jmsMessage instanceof TextMessage);
+ Mockito.verify(mockTextMessage).setText(contentString);
+ assertSame("Expected provided mock message, got a different one", mockTextMessage, jmsMessage);
+ }
+
+ // ======= JMSDestination Handling =========
+ // =========================================
+
+ @Test
+ public void testTransformWithNoToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithToTypeDestinationTypeAnnotationTestImpl(null, Destination.class);
+ }
+
+ @Test
+ public void testTransformWithQueueStringToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithToTypeDestinationTypeAnnotationTestImpl("queue", Queue.class);
+ }
+
+ @Test
+ public void testTransformWithTemporaryQueueStringToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithToTypeDestinationTypeAnnotationTestImpl("queue,temporary", TemporaryQueue.class);
+ }
+
+ @Test
+ public void testTransformWithTopicStringToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithToTypeDestinationTypeAnnotationTestImpl("topic", Topic.class);
+ }
+
+ @Test
+ public void testTransformWithTemporaryTopicStringToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithToTypeDestinationTypeAnnotationTestImpl("topic,temporary", TemporaryTopic.class);
+ }
+
+ private void doTransformWithToTypeDestinationTypeAnnotationTestImpl(Object toTypeAnnotationValue, Class<? extends Destination> expectedClass) throws Exception
+ {
+ TextMessage mockTextMessage = createMockTextMessage();
+ JMSVendor mockVendor = createMockVendor(mockTextMessage);
+ JMSMappingInboundTransformer transformer = new JMSMappingInboundTransformer(mockVendor);
+
+ String toAddress = "toAddress";
+ Message amqp = Message.Factory.create();
+ amqp.setBody(new AmqpValue("myTextMessageContent"));
+ amqp.setAddress(toAddress);
+ if(toTypeAnnotationValue != null)
+ {
+ Map<Symbol, Object> map = new HashMap<Symbol, Object>();
+ map.put(Symbol.valueOf("x-opt-to-type"), toTypeAnnotationValue);
+ MessageAnnotations ma = new MessageAnnotations(map);
+ amqp.setMessageAnnotations(ma);
+ }
+
+ EncodedMessage em = encodeMessage(amqp);
+
+ javax.jms.Message jmsMessage = transformer.transform(em);
+ assertTrue("Expected TextMessage", jmsMessage instanceof TextMessage);
+
+ // Verify that createDestination was called with the provided 'to' address and 'Destination' class
+ Mockito.verify(mockVendor).createDestination(toAddress, expectedClass);
+ }
+
+ // ======= JMSReplyTo Handling =========
+ // =====================================
+
+ @Test
+ public void testTransformWithNoReplyToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithReplyToTypeDestinationTypeAnnotationTestImpl(null,Destination.class);
+ }
+
+ @Test
+ public void testTransformWithQueueStringReplyToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithReplyToTypeDestinationTypeAnnotationTestImpl("queue", Queue.class);
+ }
+
+ @Test
+ public void testTransformWithTemporaryQueueStringReplyToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithReplyToTypeDestinationTypeAnnotationTestImpl("queue,temporary", TemporaryQueue.class);
+ }
+
+ @Test
+ public void testTransformWithTopicStringReplyToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithReplyToTypeDestinationTypeAnnotationTestImpl("topic", Topic.class);
+ }
+
+ @Test
+ public void testTransformWithTemporaryTopicStringReplyToTypeDestinationTypeAnnotation() throws Exception
+ {
+ doTransformWithReplyToTypeDestinationTypeAnnotationTestImpl("topic,temporary", TemporaryTopic.class);
+ }
+
+ private void doTransformWithReplyToTypeDestinationTypeAnnotationTestImpl(Object replyToTypeAnnotationValue, Class<? extends Destination> expectedClass) throws Exception
+ {
+ TextMessage mockTextMessage = createMockTextMessage();
+ JMSVendor mockVendor = createMockVendor(mockTextMessage);
+ JMSMappingInboundTransformer transformer = new JMSMappingInboundTransformer(mockVendor);
+
+ String replyToAddress = "replyToAddress";
+ Message amqp = Message.Factory.create();
+ amqp.setBody(new AmqpValue("myTextMessageContent"));
+ amqp.setReplyTo(replyToAddress);
+ if(replyToTypeAnnotationValue != null)
+ {
+ Map<Symbol, Object> map = new HashMap<Symbol, Object>();
+ map.put(Symbol.valueOf("x-opt-reply-type"), replyToTypeAnnotationValue);
+ MessageAnnotations ma = new MessageAnnotations(map);
+ amqp.setMessageAnnotations(ma);
+ }
+
+ EncodedMessage em = encodeMessage(amqp);
+
+ javax.jms.Message jmsMessage = transformer.transform(em);
+ assertTrue("Expected TextMessage", jmsMessage instanceof TextMessage);
+
+ // Verify that createDestination was called with the provided 'replyTo' address and 'Destination' class
+ Mockito.verify(mockVendor).createDestination(replyToAddress,expectedClass);
+ }
+
+ // ======= Utility Methods =========
+ // =================================
+
+ private TextMessage createMockTextMessage()
+ {
+ TextMessage mockTextMessage = Mockito.mock(TextMessage.class);
+
+ return mockTextMessage;
+ }
+
+ private JMSVendor createMockVendor(TextMessage mockTextMessage)
+ {
+ JMSVendor mockVendor = Mockito.mock(JMSVendor.class);
+ Mockito.when(mockVendor.createTextMessage()).thenReturn(mockTextMessage);
+
+ return mockVendor;
+ }
+
+ private EncodedMessage encodeMessage(Message message)
+ {
+ byte[] encodeBuffer = new byte[1024 * 8];
+ int encodedSize;
+ while (true) {
+ try {
+ encodedSize = message.encode(encodeBuffer, 0, encodeBuffer.length);
+ break;
+ } catch (java.nio.BufferOverflowException e) {
+ encodeBuffer = new byte[encodeBuffer.length * 2];
+ }
+ }
+
+ long messageFormat = 0;
+ return new EncodedMessage(messageFormat, encodeBuffer, 0, encodedSize);
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/c87fc8a2/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
----------------------------------------------------------------------
diff --git a/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java b/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
new file mode 100644
index 0000000..1b14627
--- /dev/null
+++ b/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
@@ -0,0 +1,226 @@
+/*
+ * 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.proton.jms;
+
+import static org.junit.Assert.*;
+
+import java.util.Collections;
+import java.util.Map;
+
+import javax.jms.Destination;
+import javax.jms.Queue;
+import javax.jms.TemporaryQueue;
+import javax.jms.TemporaryTopic;
+import javax.jms.TextMessage;
+import javax.jms.Topic;
+
+import org.apache.qpid.proton.amqp.Symbol;
+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.Test;
+import org.mockito.Mockito;
+
+public class JMSMappingOutboundTransformerTest
+{
+ @Test
+ public void testConvertMessageWithTextMessageCreatesAmqpValueStringBody() throws Exception
+ {
+ String contentString = "myTextMessageContent";
+ TextMessage mockTextMessage = createMockTextMessage();
+ Mockito.when(mockTextMessage.getText()).thenReturn(contentString);
+ JMSVendor mockVendor = createMockVendor();
+
+ JMSMappingOutboundTransformer transformer = new JMSMappingOutboundTransformer(mockVendor);
+
+ Message amqp = transformer.convert(mockTextMessage);
+
+ assertNotNull(amqp.getBody());
+ assertTrue(amqp.getBody() instanceof AmqpValue);
+ assertEquals(contentString, ((AmqpValue) amqp.getBody()).getValue());
+ }
+
+ // ======= JMSDestination Handling =========
+ // =========================================
+
+ @Test
+ public void testConvertMessageWithJMSDestinationNull() throws Exception
+ {
+ doTestConvertMessageWithJMSDestination(null, null);
+ }
+
+ @Test
+ public void testConvertMessageWithJMSDestinationQueue() throws Exception
+ {
+ Queue mockDest = Mockito.mock(Queue.class);
+
+ doTestConvertMessageWithJMSDestination(mockDest, "queue");
+ }
+
+ @Test
+ public void testConvertMessageWithJMSDestinationTemporaryQueue() throws Exception
+ {
+ TemporaryQueue mockDest = Mockito.mock(TemporaryQueue.class);
+
+ doTestConvertMessageWithJMSDestination(mockDest, "temporary,queue");
+ }
+
+ @Test
+ public void testConvertMessageWithJMSDestinationTopic() throws Exception
+ {
+ Topic mockDest = Mockito.mock(Topic.class);
+
+ doTestConvertMessageWithJMSDestination(mockDest, "topic");
+ }
+
+ @Test
+ public void testConvertMessageWithJMSDestinationTemporaryTopic() throws Exception
+ {
+ TemporaryTopic mockDest = Mockito.mock(TemporaryTopic.class);
+
+ doTestConvertMessageWithJMSDestination(mockDest, "temporary,topic");
+ }
+
+ private void doTestConvertMessageWithJMSDestination(Destination jmsDestination, Object expectedAnnotationValue) throws Exception
+ {
+ TextMessage mockTextMessage = createMockTextMessage();
+ Mockito.when(mockTextMessage.getText()).thenReturn("myTextMessageContent");
+ Mockito.when(mockTextMessage.getJMSDestination()).thenReturn(jmsDestination);
+ JMSVendor mockVendor = createMockVendor();
+ String toAddress = "someToAddress";
+ if(jmsDestination != null)
+ {
+ Mockito.when(mockVendor.toAddress(Mockito.any(Destination.class)))
+ .thenReturn(toAddress);
+ }
+
+ JMSMappingOutboundTransformer transformer = new JMSMappingOutboundTransformer(mockVendor);
+
+ Message amqp = transformer.convert(mockTextMessage);
+
+ MessageAnnotations ma = amqp.getMessageAnnotations();
+ Map<Symbol, Object> maMap = ma == null ? null : ma.getValue();
+ if(maMap != null)
+ {
+ Object actualValue = maMap.get(Symbol.valueOf("x-opt-to-type"));
+ assertEquals("Unexpected annotation value", expectedAnnotationValue, actualValue);
+ }
+ else if (expectedAnnotationValue != null)
+ {
+ fail("Expected annotation value, but there were no annotations");
+ }
+
+ if(jmsDestination != null)
+ {
+ assertEquals("Unexpected 'to' address", toAddress, amqp.getAddress());
+ }
+ }
+
+ // ======= JMSReplyTo Handling =========
+ // =====================================
+
+ @Test
+ public void testConvertMessageWithJMSReplyToNull() throws Exception
+ {
+ doTestConvertMessageWithJMSReplyTo(null, null);
+ }
+
+ @Test
+ public void testConvertMessageWithJMSReplyToQueue() throws Exception
+ {
+ Queue mockDest = Mockito.mock(Queue.class);
+
+ doTestConvertMessageWithJMSReplyTo(mockDest, "queue");
+ }
+
+ @Test
+ public void testConvertMessageWithJMSReplyToTemporaryQueue() throws Exception
+ {
+ TemporaryQueue mockDest = Mockito.mock(TemporaryQueue.class);
+
+ doTestConvertMessageWithJMSReplyTo(mockDest, "temporary,queue");
+ }
+
+ @Test
+ public void testConvertMessageWithJMSReplyToTopic() throws Exception
+ {
+ Topic mockDest = Mockito.mock(Topic.class);
+
+ doTestConvertMessageWithJMSReplyTo(mockDest, "topic");
+ }
+
+ @Test
+ public void testConvertMessageWithJMSReplyToTemporaryTopic() throws Exception
+ {
+ TemporaryTopic mockDest = Mockito.mock(TemporaryTopic.class);
+
+ doTestConvertMessageWithJMSReplyTo(mockDest, "temporary,topic");
+ }
+
+ private void doTestConvertMessageWithJMSReplyTo(Destination jmsReplyTo, Object expectedAnnotationValue) throws Exception
+ {
+ TextMessage mockTextMessage = createMockTextMessage();
+ Mockito.when(mockTextMessage.getText()).thenReturn("myTextMessageContent");
+ Mockito.when(mockTextMessage.getJMSReplyTo()).thenReturn(jmsReplyTo);
+ JMSVendor mockVendor = createMockVendor();
+ String replyToAddress = "someReplyToAddress";
+ if(jmsReplyTo != null)
+ {
+ Mockito.when(mockVendor.toAddress(Mockito.any(Destination.class)))
+ .thenReturn(replyToAddress);
+ }
+
+ JMSMappingOutboundTransformer transformer = new JMSMappingOutboundTransformer(mockVendor);
+
+ Message amqp = transformer.convert(mockTextMessage);
+
+ MessageAnnotations ma = amqp.getMessageAnnotations();
+ Map<Symbol, Object> maMap = ma == null ? null : ma.getValue();
+ if(maMap != null)
+ {
+ Object actualValue = maMap.get(Symbol.valueOf("x-opt-reply-type"));
+ assertEquals("Unexpected annotation value", expectedAnnotationValue, actualValue);
+ }
+ else if (expectedAnnotationValue != null)
+ {
+ fail("Expected annotation value, but there were no annotations");
+ }
+
+ if(jmsReplyTo != null)
+ {
+ assertEquals("Unexpected 'reply-to' address", replyToAddress, amqp.getReplyTo());
+ }
+ }
+
+ // ======= Utility Methods =========
+ // =================================
+
+ private TextMessage createMockTextMessage() throws Exception
+ {
+ TextMessage mockTextMessage = Mockito.mock(TextMessage.class);
+ Mockito.when(mockTextMessage.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
+
+ return mockTextMessage;
+ }
+
+ private JMSVendor createMockVendor()
+ {
+ JMSVendor mockVendor = Mockito.mock(JMSVendor.class);
+
+ return mockVendor;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org