View Javadoc

1   /*
2    * $Id: QosHeadersTestCase.java 22431 2011-07-18 07:40:35Z dirk.olmes $
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.AbstractServiceAndFlowTestCase;
14  
15  import java.util.Arrays;
16  import java.util.Collection;
17  
18  import javax.jms.Connection;
19  import javax.jms.DeliveryMode;
20  import javax.jms.Destination;
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.jms.MessageConsumer;
24  import javax.jms.MessageProducer;
25  import javax.jms.Session;
26  import javax.jms.TextMessage;
27  
28  import org.apache.activemq.ActiveMQConnectionFactory;
29  import org.junit.Test;
30  import org.junit.runners.Parameterized.Parameters;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertNotNull;
34  
35  public class QosHeadersTestCase extends AbstractServiceAndFlowTestCase
36  {
37      @Parameters
38      public static Collection<Object[]> parameters()
39      {
40          return Arrays.asList(new Object[][]{
41              {ConfigVariant.SERVICE, "org/mule/test/integration/providers/jms/qosheaders-test-service.xml"},
42              {ConfigVariant.FLOW, "org/mule/test/integration/providers/jms/qosheaders-test-flow.xml"}});
43      }
44  
45      public QosHeadersTestCase(ConfigVariant variant, String configResources)
46      {
47          super(variant, configResources);
48      }
49  
50      @Test
51      public void testQosHeadersHonored() throws JMSException
52      {
53          String producerQueue = "test.in.kind";
54          String consumerQueue = "test.out.kind";
55          doSendReceiveCycle(producerQueue, consumerQueue, true);
56      }
57  
58      @Test
59      public void testQosHeadersNotHonored() throws JMSException
60      {
61          String producerQueue = "test.in.selfish";
62          String consumerQueue = "test.out.selfish";
63          doSendReceiveCycle(producerQueue, consumerQueue, false);
64      }
65  
66      /**
67       * @param honorProperties indicate which assertion path to take
68       */
69      protected void doSendReceiveCycle(final String producerQueue,
70                                        final String consumerQueue,
71                                        final boolean honorProperties) throws JMSException
72      {
73          ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
74              "vm://localhost?broker.persistent=false&broker.useJmx=false");
75          Connection producerConnection = null;
76          Connection consumerConnection = null;
77  
78          try
79          {
80              // Producer part
81              producerConnection = connectionFactory.createConnection();
82              producerConnection.start();
83  
84              Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
85              Destination producerDestination = producerSession.createQueue(producerQueue);
86              MessageProducer producer = producerSession.createProducer(producerDestination);
87  
88              // Consumer part
89              consumerConnection = connectionFactory.createConnection();
90              consumerConnection.start();
91  
92              Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
93              Destination consumerDestination = consumerSession.createQueue(consumerQueue);
94              MessageConsumer consumer = consumerSession.createConsumer(consumerDestination);
95  
96              String message = "QoS Headers Propagation Test";
97              TextMessage textMessage = producerSession.createTextMessage(message);
98              producer.setPriority(7);
99              producer.setDeliveryMode(DeliveryMode.PERSISTENT);
100             producer.send(textMessage);
101 
102             Message response = consumer.receive(10000);
103 
104             // this is ugly, but will do for this test. Man, I wish I could just pass
105             // in a closure here...
106             if (honorProperties)
107             {
108                 performHeadersHonoredAssertions(response);
109             }
110             else
111             {
112                 performHeadersNotHonoredAssertions(response);
113             }
114         }
115         finally
116         {
117             // Grrrr.....
118             try
119             {
120                 if (consumerConnection != null)
121                 {
122                     consumerConnection.close();
123                 }
124             }
125             catch (JMSException e)
126             {
127                 // don't care, just let the producer be closed as well
128             }
129 
130             try
131             {
132                 if (producerConnection != null)
133                 {
134                     producerConnection.close();
135                 }
136             }
137             catch (JMSException e)
138             {
139                 // don't care
140             }
141         }
142 
143     }
144 
145     protected void performHeadersHonoredAssertions(final Message response) throws JMSException
146     {
147         assertNotNull(response);
148         assertEquals("JMS Priority should've been honored.", 7, response.getJMSPriority());
149         assertEquals("JMS Delivery mode should've been honored", DeliveryMode.PERSISTENT,
150             response.getJMSDeliveryMode());
151     }
152 
153     protected void performHeadersNotHonoredAssertions(final Message response) throws JMSException
154     {
155         assertNotNull(response);
156         // default priority
157         assertEquals("JMS Priority should have not been honored.", 4, response.getJMSPriority());
158         assertEquals("JMS Delivery mode should have not been honored", DeliveryMode.NON_PERSISTENT,
159             response.getJMSDeliveryMode());
160     }
161 }