View Javadoc

1   /*
2    * $Id: XmppMessageSyncTestCase.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.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.functional.FunctionalTestComponent;
16  import org.mule.transport.NullPayload;
17  import org.mule.transport.xmpp.JabberSender.Callback;
18  import org.mule.util.concurrent.Latch;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
21  
22  import org.jivesoftware.smack.packet.Message;
23  import org.jivesoftware.smack.packet.Packet;
24  
25  public class XmppMessageSyncTestCase 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      @Override
31      protected String getXmppConfigResources()
32      {
33          return "xmpp-message-sync-config.xml";
34      }
35  
36      public void testSendSync() throws Exception
37      {   
38          MuleClient client = new MuleClient(muleContext);
39          MuleMessage reply = client.send("vm://in", TEST_MESSAGE, null);
40          assertNotNull(reply);
41          assertEquals(NullPayload.getInstance(), reply.getPayload());
42          
43          Packet packet = jabberClient.receive(RECEIVE_TIMEOUT);
44          assertReceivedPacketEqualsMessageSent(packet);
45      }
46      
47      public void testReceiveSync() throws Exception
48      {
49          startService(RECEIVE_SERVICE_NAME);
50          
51          Latch receiveLatch = new Latch();
52          setupTestServiceComponent(receiveLatch);
53          
54          sendJabberMessageFromNewThread();
55          assertTrue(receiveLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
56      }
57  
58      private void setupTestServiceComponent(Latch receiveLatch) throws Exception
59      {   
60          Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
61          assertTrue(testComponent instanceof FunctionalTestComponent);
62          FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
63          
64          XmppCallback callback = new XmppCallback(receiveLatch, expectedXmppMessageType());
65          component.setEventCallback(callback);
66      }
67  
68      public void testRequestSync() throws Exception
69      {
70          doTestRequest("xmpp://MESSAGE/mule2@localhost?exchangePattern=request-response");
71      }
72      
73      protected void doTestRequest(String url) throws Exception
74      {
75          sendJabberMessageFromNewThread();
76  
77          MuleClient client = new MuleClient(muleContext);
78          MuleMessage muleMessage = client.request(url, RECEIVE_TIMEOUT);
79          assertNotNull(muleMessage);
80  
81          Message xmppMessage = (Message) muleMessage.getPayload();
82          assertEquals(expectedXmppMessageType(), xmppMessage.getType());
83          assertEquals(TEST_MESSAGE, xmppMessage.getBody());
84      }
85  
86      protected Message.Type expectedXmppMessageType()
87      {
88          return Message.Type.normal;
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 }