View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.xmpp;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.service.Service;
11  
12  import java.util.Properties;
13  
14  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
15  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
16  import org.jivesoftware.smack.packet.Message;
17  import org.jivesoftware.smack.packet.Packet;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public abstract class AbstractXmppTestCase extends XmppEnableDisableTestCase
24  {
25  
26      private static final long STARTUP_TIMEOUT = 5000;
27  
28      private CountDownLatch jabberLatch;
29      protected JabberClient jabberClient;
30      protected String conversationPartner;
31      protected String muleJabberUserId;
32  
33      @Override
34      protected final String getConfigResources()
35      {
36          return "xmpp-connector-config.xml," + getXmppConfigResources();
37      }
38  
39      @Override
40      protected void doSetUp() throws Exception
41      {
42          super.doSetUp();
43  
44          jabberLatch = new CountDownLatch(1);
45          createAndConnectJabberClient();
46      }
47  
48      /**
49       * Subclasses implmement this method and return the name of their config file.
50       */
51      protected abstract String getXmppConfigResources();
52  
53      private void createAndConnectJabberClient() throws Exception
54      {
55          // do not hardcode host/user etc here, look it up from the registry so the only place
56          // that this info is stored is in the config
57          Properties properties = (Properties) muleContext.getRegistry().lookupObject("properties");
58          String host = properties.getProperty("host");
59          conversationPartner = properties.getProperty("conversationPartner");
60          String password = properties.getProperty("conversationPartnerPassword");
61          
62          // also save the jid that is used to connect to the jabber server
63          muleJabberUserId = properties.getProperty("user") + "@" + host;
64          
65          jabberClient = new JabberClient(host, conversationPartner, password);
66          configureJabberClient(jabberClient);
67          jabberClient.connect(jabberLatch);
68          
69          assertTrue(jabberLatch.await(STARTUP_TIMEOUT, TimeUnit.MILLISECONDS));
70      }
71  
72      protected void configureJabberClient(JabberClient client) throws Exception
73      {
74          // template method
75      }
76  
77      @Override
78      protected void doTearDown() throws Exception
79      {
80          if (jabberClient != null)
81          {
82              jabberClient.disconnect();
83          }
84          super.doTearDown();
85      }
86      
87      protected void startService(String serviceName) throws MuleException
88      {
89          Service service = muleContext.getRegistry().lookupService(serviceName);
90          assertNotNull(service);
91          
92          service.start();
93      }
94  
95      protected void startSendThread(JabberSender sender)
96      {
97          Thread sendThread = new Thread(sender);
98          sendThread.setName("Jabber send");
99          sendThread.start();
100     }
101     
102     protected void assertReceivedPacketEqualsMessageSent(Packet packet)
103     {
104         assertNotNull(packet);
105         assertTrue(packet instanceof Message);
106         Message messageFromJabber = (Message) packet;
107         assertEquals(TEST_MESSAGE, messageFromJabber.getBody());        
108     }
109 }