View Javadoc

1   /*
2    * $Id: AbstractXmppTestCase.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 org.mule.api.MuleException;
14  import org.mule.api.service.Service;
15  
16  import java.util.Properties;
17  import java.util.concurrent.CountDownLatch;
18  import java.util.concurrent.TimeUnit;
19  
20  import org.jivesoftware.smack.packet.Message;
21  import org.jivesoftware.smack.packet.Packet;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public abstract class AbstractXmppTestCase extends XmppEnableDisableTestCase
28  {
29  
30      private static final long STARTUP_TIMEOUT = 5000;
31  
32      private CountDownLatch jabberLatch;
33      protected JabberClient jabberClient;
34      protected String conversationPartner;
35      protected String muleJabberUserId;
36      protected static final String COMMON_CONFIG = "xmpp-connector-config.xml";
37  
38      public AbstractXmppTestCase(ConfigVariant variant, String configResources)
39      {
40          super(variant, configResources);
41      }
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          super.doSetUp();
47  
48          jabberLatch = new CountDownLatch(1);
49          createAndConnectJabberClient();
50      }
51  
52      private void createAndConnectJabberClient() throws Exception
53      {
54          // do not hardcode host/user etc here, look it up from the registry so the
55          // 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 }