View Javadoc

1   /*
2    * $Id: XmppMessageAsyncTestCase.java 22449 2011-07-19 07:40:43Z justin.calleja $
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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.Arrays;
17  import java.util.Collection;
18  import java.util.List;
19  import java.util.concurrent.CountDownLatch;
20  import java.util.concurrent.TimeUnit;
21  
22  import org.jivesoftware.smack.packet.Message;
23  import org.junit.Test;
24  import org.junit.runners.Parameterized.Parameters;
25  import org.mule.module.client.MuleClient;
26  import org.mule.tck.functional.FunctionalTestComponent;
27  import org.mule.transport.xmpp.JabberSender.Callback;
28  import org.mule.util.concurrent.Latch;
29  
30  public class XmppMessageAsyncTestCase extends AbstractXmppTestCase
31  {
32      protected static final long JABBER_SEND_THREAD_SLEEP_TIME = 1000;
33      private static final String RECEIVE_SERVICE_NAME = "receiveFromJabber";
34  
35      private CountDownLatch latch = new CountDownLatch(1);
36  
37      public XmppMessageAsyncTestCase(ConfigVariant variant, String configResources)
38      {
39          super(variant, configResources);
40      }
41  
42      @Parameters
43      public static Collection<Object[]> parameters()
44      {
45          return Arrays.asList(new Object[][]{
46              {ConfigVariant.SERVICE,
47                  AbstractXmppTestCase.COMMON_CONFIG + "," + "xmpp-message-async-config-service.xml"},
48              {ConfigVariant.FLOW,
49                  AbstractXmppTestCase.COMMON_CONFIG + "," + "xmpp-message-async-config-flow.xml"}});
50      }
51  
52      @Override
53      protected void configureJabberClient(JabberClient client)
54      {
55          client.setSynchronous(false);
56          client.setMessageLatch(latch);
57      }
58  
59      @Test
60      public void testDispatch() throws Exception
61      {
62          MuleClient client = new MuleClient(muleContext);
63          client.dispatch("vm://in", TEST_MESSAGE, null);
64  
65          assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
66  
67          List<Message> receivedMessages = jabberClient.getReceivedMessages();
68          assertEquals(1, receivedMessages.size());
69  
70          Message message = receivedMessages.get(0);
71          assertXmppMessage(message);
72      }
73  
74      @Test
75      public void testReceiveAsync() throws Exception
76      {
77          startService(RECEIVE_SERVICE_NAME);
78  
79          Latch receiveLatch = new Latch();
80          setupTestServiceComponent(receiveLatch);
81  
82          sendJabberMessageFromNewThread();
83          assertTrue(receiveLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
84      }
85  
86      private void setupTestServiceComponent(Latch receiveLatch) throws Exception
87      {
88          Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
89          assertTrue(testComponent instanceof FunctionalTestComponent);
90          FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
91  
92          XmppCallback callback = new XmppCallback(receiveLatch, expectedXmppMessageType());
93          component.setEventCallback(callback);
94      }
95  
96      protected Message.Type expectedXmppMessageType()
97      {
98          return Message.Type.normal;
99      }
100 
101     protected void assertXmppMessage(Message message)
102     {
103         assertEquals(Message.Type.normal, message.getType());
104         assertEquals(TEST_MESSAGE, message.getBody());
105     }
106 
107     protected void sendJabberMessageFromNewThread()
108     {
109         JabberSender sender = new JabberSender(new Callback()
110         {
111             @Override
112             public void doit() throws Exception
113             {
114                 Thread.sleep(JABBER_SEND_THREAD_SLEEP_TIME);
115                 jabberClient.sendMessage(muleJabberUserId, TEST_MESSAGE);
116             }
117         });
118         startSendThread(sender);
119     }
120 }