Author: robbie
Date: Tue Oct 14 15:33:15 2014
New Revision: 1631793
URL: http://svn.apache.org/r1631793
Log:
PROTON-711: add tests for the existing destination type annotation processing
done by the transformers
Added:
qpid/proton/trunk/contrib/proton-jms/src/test/
qpid/proton/trunk/contrib/proton-jms/src/test/java/
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
Added:
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
URL:
http://svn.apache.org/viewvc/qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java?rev=1631793&view=auto
==============================================================================
---
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
(added)
+++
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingInboundTransformerTest.java
Tue Oct 14 15:33:15 2014
@@ -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);
+ }
+}
Added:
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
URL:
http://svn.apache.org/viewvc/qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java?rev=1631793&view=auto
==============================================================================
---
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
(added)
+++
qpid/proton/trunk/contrib/proton-jms/src/test/java/org/apache/qpid/proton/jms/JMSMappingOutboundTransformerTest.java
Tue Oct 14 15:33:15 2014
@@ -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: [email protected]
For additional commands, e-mail: [email protected]