1
2
3
4
5
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
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 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 }