View Javadoc

1   /*
2    * $Id: XmppMucSyncTestCase.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.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.functional.EventCallback;
17  import org.mule.tck.functional.FunctionalTestComponent;
18  import org.mule.transport.NullPayload;
19  import org.mule.transport.xmpp.JabberSender.Callback;
20  import org.mule.util.UUID;
21  import org.mule.util.concurrent.Latch;
22  
23  import java.util.Properties;
24  
25  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
26  
27  import org.jivesoftware.smack.packet.Message;
28  import org.jivesoftware.smack.packet.Packet;
29  
30  public class XmppMucSyncTestCase extends AbstractXmppTestCase
31  {
32      protected static final long JABBER_SEND_THREAD_SLEEP_TIME = 1000;
33      private static final String RECEIVE_SERVICE_NAME = "receiveFromJabber";
34      private static final long SHORT_RETRIEVE_TIMEOUT = 100;
35      
36      private final String testMessage = UUID.getUUID().toString();
37      
38      @Override
39      protected void doSetUp() throws Exception
40      {
41          super.doSetUp();
42  
43          Properties properties = (Properties) muleContext.getRegistry().lookupObject("properties");
44          String chatroom = properties.getProperty("chatroom");
45          assertNotNull(chatroom);
46          
47          jabberClient.joinGroupchat(chatroom);
48      }
49  
50      @Override
51      protected String getXmppConfigResources()
52      {
53          return "xmpp-muc-sync-config.xml";
54      }
55  
56      public void testSendSync() throws Exception
57      {           
58          MuleClient client = new MuleClient(muleContext);
59          MuleMessage reply = client.send("vm://in", testMessage, null);
60          assertNotNull(reply);
61          
62          assertEquals(NullPayload.getInstance(), reply.getPayload());
63          
64          Packet packet = jabberClient.receive(RECEIVE_TIMEOUT);
65          // The groupchat may have a backlog of messages whis is sent before our input is transmitted.
66          // Poll the entire groupchat history
67          boolean inputSeen = false;
68          packet = jabberClient.receive(SHORT_RETRIEVE_TIMEOUT);
69          while (packet != null)
70          {
71              String payload = ((Message) packet).getBody();
72              if (payload.equals(testMessage))
73              {
74                  inputSeen = true;
75                  break;
76              }
77  
78              packet = jabberClient.receive(SHORT_RETRIEVE_TIMEOUT);
79          }
80          assertTrue(inputSeen);
81      }
82      
83      public void testReceiveSync() throws Exception
84      {
85          startService(RECEIVE_SERVICE_NAME);
86  
87          Latch receiveLatch = new Latch();
88          setupTestServiceComponent(receiveLatch);
89          
90          sendJabberMessageFromNewThread();
91          assertTrue(receiveLatch.await(60, TimeUnit.SECONDS));
92      }
93      
94      private void setupTestServiceComponent(Latch receiveLatch) throws Exception
95      {   
96          Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
97          assertTrue(testComponent instanceof FunctionalTestComponent);
98          FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
99          
100         XmppGroupchatCallback callback = new XmppGroupchatCallback(receiveLatch);
101         component.setEventCallback(callback);
102     }
103     
104     protected Message.Type expectedXmppMessageType()
105     {
106         return Message.Type.groupchat;
107     }
108     
109     protected void sendJabberMessageFromNewThread()
110     {
111         JabberSender sender = new JabberSender(new Callback()
112         {
113             public void doit() throws Exception
114             {
115                 Thread.sleep(JABBER_SEND_THREAD_SLEEP_TIME);
116                 jabberClient.sendGroupchatMessage(testMessage);
117             }
118         });
119         startSendThread(sender);
120     }
121     
122     private class XmppGroupchatCallback implements EventCallback
123     {
124         private Latch latch;
125         
126         public XmppGroupchatCallback(Latch latch)
127         {
128             super();
129             this.latch = latch;
130         }
131         
132         public void eventReceived(MuleEventContext context, Object component) throws Exception
133         {
134             MuleMessage muleMessage = context.getMessage();
135             Object payload = muleMessage.getPayload();
136             assertTrue(payload instanceof Message);
137             
138             Message jabberMessage = (Message) payload;
139             if (jabberMessage.getBody().equals(testMessage))
140             {
141                 latch.countDown();
142             }
143         }
144     }
145 }