View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.integration.transport.jms;
8   
9   import org.mule.tck.junit4.FunctionalTestCase;
10  
11  import javax.jms.Connection;
12  import javax.jms.DeliveryMode;
13  import javax.jms.Destination;
14  import javax.jms.JMSException;
15  import javax.jms.Message;
16  import javax.jms.MessageConsumer;
17  import javax.jms.MessageProducer;
18  import javax.jms.Session;
19  import javax.jms.TextMessage;
20  
21  import org.apache.activemq.ActiveMQConnectionFactory;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  
27  public class QosHeadersTestCase extends FunctionalTestCase
28  {
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "org/mule/test/integration/providers/jms/qosheaders-test.xml";
34      }
35      
36      @Test
37      public void testQosHeadersHonored() throws JMSException
38      {
39          String producerQueue = "test.in.kind";
40          String consumerQueue = "test.out.kind";
41          doSendReceiveCycle(producerQueue, consumerQueue, true);
42      }
43  
44      @Test
45      public void testQosHeadersNotHonored() throws JMSException
46      {
47          String producerQueue = "test.in.selfish";
48          String consumerQueue = "test.out.selfish";
49          doSendReceiveCycle(producerQueue, consumerQueue, false);
50      }
51  
52      /**
53       * @param honorProperties indicate which assertion path to take
54       */
55      protected void doSendReceiveCycle(final String producerQueue, final String consumerQueue, final boolean honorProperties)
56              throws JMSException
57      {
58          ActiveMQConnectionFactory connectionFactory =
59                  new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
60          Connection producerConnection = null;
61          Connection consumerConnection = null;
62  
63          try
64          {
65              // Producer part
66              producerConnection = connectionFactory.createConnection();
67              producerConnection.start();
68  
69              Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
70              Destination producerDestination = producerSession.createQueue(producerQueue);
71              MessageProducer producer = producerSession.createProducer(producerDestination);
72  
73              // Consumer part
74              consumerConnection = connectionFactory.createConnection();
75              consumerConnection.start();
76  
77              Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
78              Destination consumerDestination = consumerSession.createQueue(consumerQueue);
79              MessageConsumer consumer = consumerSession.createConsumer(consumerDestination);
80  
81              String message = "QoS Headers Propagation Test";
82              TextMessage textMessage = producerSession.createTextMessage(message);
83              producer.setPriority(7);
84              producer.setDeliveryMode(DeliveryMode.PERSISTENT);
85              producer.send(textMessage);
86  
87              Message response = consumer.receive(10000);
88  
89              // this is ugly, but will do for this test. Man, I wish I could just pass in a closure here...
90              if (honorProperties)
91              {
92                  performHeadersHonoredAssertions(response);
93              }
94              else
95              {
96                  performHeadersNotHonoredAssertions(response);
97              }
98          }
99          finally
100         {
101             // Grrrr.....
102             try
103             {
104                 if (consumerConnection != null)
105                 {
106                     consumerConnection.close();
107                 }
108             }
109             catch (JMSException e)
110             {
111                 // don't care, just let the producer be closed as well
112             }
113 
114             try
115             {
116                 if (producerConnection != null)
117                 {
118                     producerConnection.close();
119                 }
120             }
121             catch (JMSException e)
122             {
123                 // don't care
124             }
125         }
126 
127     }
128 
129     protected void performHeadersHonoredAssertions(final Message response)
130             throws JMSException
131     {
132         assertNotNull(response);
133         assertEquals("JMS Priority should've been honored.", 7, response.getJMSPriority());
134         assertEquals("JMS Delivery mode should've been honored",
135                      DeliveryMode.PERSISTENT, response.getJMSDeliveryMode());
136     }
137 
138     protected void performHeadersNotHonoredAssertions(final Message response)
139             throws JMSException
140     {
141         assertNotNull(response);
142         // default priority
143         assertEquals("JMS Priority should have not been honored.", 4, response.getJMSPriority());
144         assertEquals("JMS Delivery mode should have not been honored",
145                      DeliveryMode.NON_PERSISTENT, response.getJMSDeliveryMode());
146     }
147 }
148 
149