1   /*
2    * $Id: AbstractJmsQueueFunctionalTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.jms;
12  
13  import org.mule.MuleManager;
14  import org.mule.providers.jms.JmsMessageReceiver;
15  import org.mule.tck.functional.EventCallback;
16  import org.mule.test.integration.providers.jms.tools.JmsTestUtils;
17  import org.mule.umo.UMOComponent;
18  import org.mule.umo.UMOEventContext;
19  import org.mule.umo.endpoint.UMOEndpoint;
20  import org.mule.umo.lifecycle.InitialisationException;
21  import org.mule.umo.provider.UMOConnector;
22  
23  import javax.jms.Message;
24  import javax.jms.MessageConsumer;
25  import javax.jms.MessageListener;
26  import javax.jms.QueueConnection;
27  import javax.jms.Session;
28  import javax.jms.TextMessage;
29  import javax.jms.TopicConnection;
30  
31  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
32  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
33  
34  public abstract class AbstractJmsQueueFunctionalTestCase extends AbstractJmsFunctionalTestCase
35  {
36      protected static CountDownLatch receiverIsUp;
37  
38      public void testSend() throws Exception
39      {
40          final CountDownLatch countDown = new CountDownLatch(2);
41          receiverIsUp = new CountDownLatch(1);
42  
43          EventCallback callback = new EventCallback()
44          {
45              public synchronized void eventReceived(UMOEventContext context, Object component)
46              {
47                  callbackCalled = true;
48                  assertNull(context.getCurrentTransaction());
49                  countDown.countDown();
50              }
51          };
52  
53          initialiseComponent(callback);
54          // Start the server
55          MuleManager.getInstance().start();
56  
57          MessageConsumer mc;
58          // check replyTo
59          if (useTopics())
60          {
61              mc = JmsTestUtils.getTopicSubscriber((TopicConnection)cnn, getOutDest().getAddress());
62          }
63          else
64          {
65              mc = JmsTestUtils.getQueueReceiver((QueueConnection)cnn, getOutDest().getAddress());
66          }
67          mc.setMessageListener(new MessageListener()
68          {
69              public void onMessage(Message message)
70              {
71                  currentMsg = message;
72                  countDown.countDown();
73              }
74          });
75  
76          logger.debug("Waiting for coutdown isReceiverUp");
77          assertTrue(receiverIsUp.await(LOCK_WAIT, TimeUnit.MILLISECONDS));
78          receiverIsUp = null;
79  
80          send(DEFAULT_MESSAGE, false, Session.AUTO_ACKNOWLEDGE, null);
81          assertTrue(countDown.await(LOCK_WAIT, TimeUnit.MILLISECONDS));
82  
83          assertNotNull(currentMsg);
84          assertTrue(currentMsg instanceof TextMessage);
85          assertEquals(DEFAULT_MESSAGE + " Received", ((TextMessage)currentMsg).getText());
86  
87          assertTrue(callbackCalled);
88      }
89  
90      public void testSendWithReplyTo() throws Exception
91      {
92          final CountDownLatch countDown = new CountDownLatch(2);
93          receiverIsUp = new CountDownLatch(1);
94  
95          EventCallback callback = new EventCallback()
96          {
97              public synchronized void eventReceived(UMOEventContext context, Object component)
98              {
99                  callbackCalled = true;
100                 assertNull(context.getCurrentTransaction());
101                 countDown.countDown();
102             }
103         };
104 
105         initialiseComponent(callback);
106         // Start the server
107         MuleManager.getInstance().start();
108 
109         MessageConsumer mc;
110         // check replyTo
111         if (useTopics())
112         {
113             mc = JmsTestUtils.getTopicSubscriber((TopicConnection)cnn, "replyto");
114         }
115         else
116         {
117             mc = JmsTestUtils.getQueueReceiver((QueueConnection)cnn, "replyto");
118         }
119         mc.setMessageListener(new MessageListener()
120         {
121             public void onMessage(Message message)
122             {
123                 currentMsg = message;
124                 countDown.countDown();
125             }
126         });
127 
128         logger.debug("Waiting for coutdown isReceiverUp");
129         assertTrue(receiverIsUp.await(LOCK_WAIT, TimeUnit.MILLISECONDS));
130         receiverIsUp = null;
131 
132         send(DEFAULT_MESSAGE, false, Session.AUTO_ACKNOWLEDGE, "replyto");
133 
134         assertTrue(countDown.await(LOCK_WAIT, TimeUnit.MILLISECONDS));
135 
136         assertNotNull(currentMsg);
137         assertTrue(currentMsg instanceof TextMessage);
138         assertEquals(DEFAULT_MESSAGE + " Received", ((TextMessage)currentMsg).getText());
139         assertTrue(callbackCalled);
140     }
141 
142     public boolean useTopics()
143     {
144         return false;
145     }
146 
147     protected static class JmsMessageReceiverSynchronous extends JmsMessageReceiver
148     {
149         public JmsMessageReceiverSynchronous(UMOConnector connector,
150                                              UMOComponent component,
151                                              UMOEndpoint endpoint) throws InitialisationException
152         {
153             super(connector, component, endpoint);
154         }
155 
156         protected void doConnect() throws Exception
157         {
158             super.doConnect();
159             if (receiverIsUp != null)
160             {
161                 logger.debug("Releasing coutdown isReceiverUp");
162                 receiverIsUp.countDown();
163             }
164         }
165     }
166 }