View Javadoc

1   /*
2    * $Id: XmppMessageAsyncTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.transport.xmpp;
12  
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.functional.FunctionalTestComponent;
15  import org.mule.transport.xmpp.JabberSender.Callback;
16  import org.mule.util.concurrent.Latch;
17  
18  import java.util.List;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
21  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22  
23  import org.jivesoftware.smack.packet.Message;
24  
25  public class XmppMessageAsyncTestCase extends AbstractXmppTestCase
26  {
27      protected static final long JABBER_SEND_THREAD_SLEEP_TIME = 1000;
28      private static final String RECEIVE_SERVICE_NAME = "receiveFromJabber";
29  
30      private CountDownLatch latch = new CountDownLatch(1);
31      
32      @Override
33      protected String getXmppConfigResources()
34      {
35          return "xmpp-message-async-config.xml";
36      }
37  
38      @Override
39      protected void configureJabberClient(JabberClient client)
40      {
41          client.setSynchronous(false);
42          client.setMessageLatch(latch);
43      }
44  
45      public void testDispatch() throws Exception
46      {
47          MuleClient client = new MuleClient(muleContext);
48          client.dispatch("vm://in", TEST_MESSAGE, null);
49          
50          assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
51          
52          List<Message> receivedMessages = jabberClient.getReceivedMessages();
53          assertEquals(1, receivedMessages.size());
54  
55          Message message = receivedMessages.get(0);
56          assertXmppMessage(message);
57      }
58      
59      public void testReceiveAsync() throws Exception
60      {
61          startService(RECEIVE_SERVICE_NAME);
62          
63          Latch receiveLatch = new Latch();
64          setupTestServiceComponent(receiveLatch);
65  
66          sendJabberMessageFromNewThread();
67          assertTrue(receiveLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
68      }
69  
70      private void setupTestServiceComponent(Latch receiveLatch) throws Exception
71      {   
72          Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
73          assertTrue(testComponent instanceof FunctionalTestComponent);
74          FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
75          
76          XmppCallback callback = new XmppCallback(receiveLatch, expectedXmppMessageType());
77          component.setEventCallback(callback);
78      }
79  
80      protected Message.Type expectedXmppMessageType()
81      {
82          return Message.Type.normal;
83      }
84  
85      protected void assertXmppMessage(Message message)
86      {
87          assertEquals(Message.Type.normal, message.getType());
88          assertEquals(TEST_MESSAGE, message.getBody());
89      }
90      
91      protected void sendJabberMessageFromNewThread()
92      {
93          JabberSender sender = new JabberSender(new Callback()
94          {
95              public void doit() throws Exception
96              {
97                  Thread.sleep(JABBER_SEND_THREAD_SLEEP_TIME);
98                  jabberClient.sendMessage(muleJabberUserId, TEST_MESSAGE);
99              }
100         });
101         startSendThread(sender);
102     }
103 }