View Javadoc

1   /*
2    * $Id: AbstractXmppTestCase.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.MuleException;
14  import org.mule.api.service.Service;
15  
16  import java.util.Properties;
17  
18  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
19  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20  
21  import org.jivesoftware.smack.packet.Message;
22  import org.jivesoftware.smack.packet.Packet;
23  
24  public abstract class AbstractXmppTestCase extends XmppEnableDisableTestCase
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 void doSetUp() throws Exception
35      {
36          super.doSetUp();
37          
38          jabberLatch = new CountDownLatch(1);
39          createAndConnectJabberClient();
40      }
41      
42      @Override
43      protected final String getConfigResources()
44      {
45          return "xmpp-connector-config.xml," + getXmppConfigResources();
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          jabberClient.disconnect();
81          super.doTearDown();
82      }
83      
84      protected void startService(String serviceName) throws MuleException
85      {
86          Service service = muleContext.getRegistry().lookupService(serviceName);
87          assertNotNull(service);
88          
89          service.start();
90      }
91  
92      protected void startSendThread(JabberSender sender)
93      {
94          Thread sendThread = new Thread(sender);
95          sendThread.setName("Jabber send");
96          sendThread.start();
97      }
98      
99      protected void assertReceivedPacketEqualsMessageSent(Packet packet)
100     {
101         assertNotNull(packet);
102         assertTrue(packet instanceof Message);
103         Message messageFromJabber = (Message) packet;
104         assertEquals(TEST_MESSAGE, messageFromJabber.getBody());        
105     }
106 }