View Javadoc

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