1
2
3
4
5
6
7
8
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
50
51 protected abstract String getXmppConfigResources();
52
53 private void createAndConnectJabberClient() throws Exception
54 {
55
56
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
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
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 }