2003-08-02 20:46:55 +02:00
|
|
|
/*
|
|
|
|
* Created on 01/08/2003
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.packet;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.*;
|
|
|
|
import org.jivesoftware.smack.filter.*;
|
|
|
|
import org.jivesoftware.smack.packet.*;
|
2004-07-12 15:36:13 +02:00
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
2003-08-26 18:05:57 +02:00
|
|
|
import org.jivesoftware.smackx.*;
|
2003-08-02 20:46:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* Test the Roster Exchange extension using the low level API
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2004-07-12 15:36:13 +02:00
|
|
|
public class RosterExchangeTest extends SmackTestCase {
|
2003-10-15 16:55:19 +02:00
|
|
|
|
2003-08-02 20:46:55 +02:00
|
|
|
public RosterExchangeTest(String arg0) {
|
|
|
|
super(arg0);
|
|
|
|
}
|
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
* Low level API test.
|
|
|
|
* This is a simple test to use with a XMPP client and check if the client receives the message
|
|
|
|
* 1. User_1 will send his/her roster entries to user_2
|
|
|
|
*/
|
|
|
|
public void testSendRosterEntries() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Create a chat for each connection
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
// Create the message to send with the roster
|
2006-11-22 23:55:37 +01:00
|
|
|
Message msg = new Message();
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.setSubject("Any subject you want");
|
|
|
|
msg.setBody("This message contains roster items.");
|
|
|
|
// Create a RosterExchange Package and add it to the message
|
2004-07-12 15:36:13 +02:00
|
|
|
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
|
|
|
|
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.addExtension(rosterExchange);
|
|
|
|
|
|
|
|
// Send the message that contains the roster
|
2003-08-24 18:05:45 +02:00
|
|
|
try {
|
2003-10-15 16:55:19 +02:00
|
|
|
chat1.sendMessage(msg);
|
2003-08-24 18:05:45 +02:00
|
|
|
} catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
fail("An error occured sending the message with the roster");
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-15 16:55:19 +02:00
|
|
|
/**
|
|
|
|
* Low level API test.
|
|
|
|
* 1. User_1 will send his/her roster entries to user_2
|
|
|
|
* 2. User_2 will receive the entries and iterate over them to check if everything is fine
|
|
|
|
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
|
|
|
|
*/
|
2003-08-02 20:46:55 +02:00
|
|
|
public void testSendAndReceiveRosterEntries() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Create a chat for each connection
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
|
|
|
|
final PacketCollector chat2 = getConnection(1).createPacketCollector(
|
|
|
|
new ThreadFilter(chat1.getThreadID()));
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
// Create the message to send with the roster
|
2006-11-22 23:55:37 +01:00
|
|
|
Message msg = new Message();
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.setSubject("Any subject you want");
|
|
|
|
msg.setBody("This message contains roster items.");
|
|
|
|
// Create a RosterExchange Package and add it to the message
|
2004-07-12 15:36:13 +02:00
|
|
|
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
|
|
|
|
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.addExtension(rosterExchange);
|
|
|
|
|
|
|
|
// Send the message that contains the roster
|
|
|
|
try {
|
|
|
|
chat1.sendMessage(msg);
|
2003-08-24 18:05:45 +02:00
|
|
|
} catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
fail("An error occured sending the message with the roster");
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
// Wait for 2 seconds for a reply
|
2006-11-22 23:55:37 +01:00
|
|
|
Packet packet = chat2.nextResult(2000);
|
|
|
|
Message message = (Message) packet;
|
|
|
|
assertNotNull("Body is null", message.getBody());
|
|
|
|
try {
|
|
|
|
rosterExchange =
|
|
|
|
(RosterExchange) message.getExtension("x", "jabber:x:roster");
|
|
|
|
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
|
|
|
|
assertTrue(
|
|
|
|
"Roster without entries",
|
|
|
|
rosterExchange.getRosterEntries().hasNext());
|
|
|
|
}
|
|
|
|
catch (ClassCastException e) {
|
|
|
|
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
|
|
|
|
}
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
* Low level API test.
|
|
|
|
* 1. User_1 will send his/her roster entries to user_2
|
|
|
|
* 2. User_2 will automatically add the entries that receives to his/her roster in the corresponding group
|
|
|
|
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
|
|
|
|
*/
|
2003-08-02 20:46:55 +02:00
|
|
|
public void testSendAndAcceptRosterEntries() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Create a chat for each connection
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
|
|
|
|
final PacketCollector chat2 = getConnection(1).createPacketCollector(
|
|
|
|
new ThreadFilter(chat1.getThreadID()));
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
// Create the message to send with the roster
|
2006-11-22 23:55:37 +01:00
|
|
|
Message msg = new Message();
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.setSubject("Any subject you want");
|
|
|
|
msg.setBody("This message contains roster items.");
|
|
|
|
// Create a RosterExchange Package and add it to the message
|
2004-07-12 15:36:13 +02:00
|
|
|
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
|
|
|
|
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.addExtension(rosterExchange);
|
|
|
|
|
|
|
|
// Send the message that contains the roster
|
|
|
|
try {
|
|
|
|
chat1.sendMessage(msg);
|
|
|
|
} catch (Exception e) {
|
|
|
|
fail("An error occured sending the message with the roster");
|
|
|
|
}
|
|
|
|
// Wait for 10 seconds for a reply
|
2006-11-22 23:55:37 +01:00
|
|
|
Packet packet = chat2.nextResult(5000);
|
|
|
|
Message message = (Message) packet;
|
|
|
|
assertNotNull("Body is null", message.getBody());
|
2004-07-12 15:36:13 +02:00
|
|
|
try {
|
2006-11-22 23:55:37 +01:00
|
|
|
rosterExchange =
|
|
|
|
(RosterExchange) message.getExtension("x", "jabber:x:roster");
|
|
|
|
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
|
|
|
|
assertTrue(
|
|
|
|
"Roster without entries",
|
|
|
|
rosterExchange.getRosterEntries().hasNext());
|
|
|
|
// Add the roster entries to user2's roster
|
2012-10-26 12:47:55 +02:00
|
|
|
for (Iterator<RemoteRosterEntry> it = rosterExchange.getRosterEntries(); it.hasNext();) {
|
|
|
|
RemoteRosterEntry remoteRosterEntry = it.next();
|
2006-11-22 23:55:37 +01:00
|
|
|
getConnection(1).getRoster().createEntry(
|
|
|
|
remoteRosterEntry.getUser(),
|
|
|
|
remoteRosterEntry.getName(),
|
|
|
|
remoteRosterEntry.getGroupArrayNames());
|
|
|
|
}
|
2004-07-12 15:36:13 +02:00
|
|
|
}
|
2006-11-22 23:55:37 +01:00
|
|
|
catch (ClassCastException e) {
|
|
|
|
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
fail(e.toString());
|
|
|
|
}
|
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
|
2003-10-15 16:55:19 +02:00
|
|
|
}
|
2003-08-02 20:46:55 +02:00
|
|
|
|
2003-10-15 16:55:19 +02:00
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
try {
|
2004-07-12 15:36:13 +02:00
|
|
|
getConnection(0).getRoster().createEntry(
|
|
|
|
getBareJID(2),
|
2003-10-15 16:55:19 +02:00
|
|
|
"gato5",
|
|
|
|
new String[] { "Friends, Coworker" });
|
2004-07-12 15:36:13 +02:00
|
|
|
getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
|
2003-10-15 16:55:19 +02:00
|
|
|
Thread.sleep(300);
|
2003-08-02 20:46:55 +02:00
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
} catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
fail(e.getMessage());
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
}
|
2003-08-02 20:46:55 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 4;
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|