2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-08-24 18:05:45 +02:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-08-24 18:05:45 +02:00
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-08-24 18:05:45 +02:00
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-08-24 18:05:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.*;
|
2004-07-12 15:36:13 +02:00
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
2003-08-24 18:05:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Test the Roster Exchange extension using the high level API
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2004-07-12 15:36:13 +02:00
|
|
|
public class RosterExchangeManagerTest extends SmackTestCase {
|
2003-10-15 16:55:19 +02:00
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
private int entriesSent;
|
|
|
|
private int entriesReceived;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for RosterExchangeManagerTest.
|
|
|
|
* @param name
|
|
|
|
*/
|
|
|
|
public RosterExchangeManagerTest(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
2018-05-09 23:06:12 +02:00
|
|
|
* This is a simple test to use with an XMPP client and check if the client receives user1's
|
2004-07-12 15:36:13 +02:00
|
|
|
* roster
|
2003-08-24 18:05:45 +02:00
|
|
|
* 1. User_1 will send his/her roster to user_2
|
|
|
|
*/
|
|
|
|
public void testSendRoster() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Send user1's roster to user2
|
2003-08-24 18:05:45 +02:00
|
|
|
try {
|
2004-07-12 15:36:13 +02:00
|
|
|
RosterExchangeManager rosterExchangeManager =
|
|
|
|
new RosterExchangeManager(getConnection(0));
|
|
|
|
rosterExchangeManager.send(getConnection(0).getRoster(), getBareJID(1));
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
fail("An error occured sending the roster");
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
2018-05-09 23:06:12 +02:00
|
|
|
* This is a simple test to use with an XMPP client and check if the client receives user1's
|
2004-07-12 15:36:13 +02:00
|
|
|
* roster groups
|
2003-08-24 18:05:45 +02:00
|
|
|
* 1. User_1 will send his/her RosterGroups to user_2
|
|
|
|
*/
|
|
|
|
public void testSendRosterGroup() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Send user1's RosterGroups to user2
|
2003-08-24 18:05:45 +02:00
|
|
|
try {
|
2006-07-17 10:39:08 +02:00
|
|
|
RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(getConnection(0));
|
|
|
|
for (RosterGroup rosterGroup : getConnection(0).getRoster().getGroups()) {
|
|
|
|
rosterExchangeManager.send(rosterGroup, getBareJID(1));
|
|
|
|
}
|
2004-07-12 15:36:13 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
fail("An error occured sending the roster");
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
|
|
|
* 1. User_1 will send his/her roster to user_2
|
|
|
|
* 2. User_2 will receive the entries and iterate over them to check if everything is fine
|
2018-05-09 23:06:12 +02:00
|
|
|
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
|
2004-07-12 15:36:13 +02:00
|
|
|
* something is wrong
|
2003-08-24 18:05:45 +02:00
|
|
|
*/
|
|
|
|
public void testSendAndReceiveRoster() {
|
2004-07-12 15:36:13 +02:00
|
|
|
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
|
|
|
|
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
// Create a RosterExchangeListener that will iterate over the received roster entries
|
|
|
|
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
|
2012-10-26 12:47:55 +02:00
|
|
|
public void entriesReceived(String from, Iterator<RemoteRosterEntry> remoteRosterEntries) {
|
2003-10-15 16:55:19 +02:00
|
|
|
int received = 0;
|
|
|
|
assertNotNull("From is null", from);
|
|
|
|
assertNotNull("rosterEntries is null", remoteRosterEntries);
|
|
|
|
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
|
|
|
|
while (remoteRosterEntries.hasNext()) {
|
|
|
|
received++;
|
2012-10-26 12:47:55 +02:00
|
|
|
RemoteRosterEntry remoteEntry = remoteRosterEntries.next();
|
2003-10-15 16:55:19 +02:00
|
|
|
System.out.println(remoteEntry);
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
entriesReceived = received;
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
};
|
|
|
|
rosterExchangeManager2.addRosterListener(rosterExchangeListener);
|
|
|
|
|
|
|
|
// Send user1's roster to user2
|
|
|
|
try {
|
2004-07-12 15:36:13 +02:00
|
|
|
entriesSent = getConnection(0).getRoster().getEntryCount();
|
2003-10-15 16:55:19 +02:00
|
|
|
entriesReceived = 0;
|
2004-07-12 15:36:13 +02:00
|
|
|
rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
|
2005-06-18 00:24:33 +02:00
|
|
|
// Wait up to 2 seconds
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(entriesSent != entriesReceived)) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2004-07-12 15:36:13 +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
|
|
|
assertEquals(
|
|
|
|
"Number of sent and received entries does not match",
|
|
|
|
entriesSent,
|
|
|
|
entriesReceived);
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
|
|
|
* 1. User_1 will send his/her roster to user_2
|
2018-05-09 23:06:12 +02:00
|
|
|
* 2. User_2 will automatically add the entries that receives to his/her roster in the
|
2004-07-12 15:36:13 +02:00
|
|
|
* corresponding group
|
2018-05-09 23:06:12 +02:00
|
|
|
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
|
2004-07-12 15:36:13 +02:00
|
|
|
* something is wrong
|
2003-08-24 18:05:45 +02:00
|
|
|
*/
|
|
|
|
public void testSendAndAcceptRoster() {
|
2004-07-12 15:36:13 +02:00
|
|
|
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
|
|
|
|
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
// Create a RosterExchangeListener that will accept all the received roster entries
|
|
|
|
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
|
2012-10-26 12:47:55 +02:00
|
|
|
public void entriesReceived(String from, Iterator<RemoteRosterEntry> remoteRosterEntries) {
|
2003-10-15 16:55:19 +02:00
|
|
|
int received = 0;
|
|
|
|
assertNotNull("From is null", from);
|
|
|
|
assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
|
|
|
|
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
|
|
|
|
while (remoteRosterEntries.hasNext()) {
|
|
|
|
received++;
|
|
|
|
try {
|
2012-10-26 12:47:55 +02:00
|
|
|
RemoteRosterEntry remoteRosterEntry = remoteRosterEntries.next();
|
2004-07-12 15:36:13 +02:00
|
|
|
getConnection(1).getRoster().createEntry(
|
2003-10-15 16:55:19 +02:00
|
|
|
remoteRosterEntry.getUser(),
|
|
|
|
remoteRosterEntry.getName(),
|
|
|
|
remoteRosterEntry.getGroupArrayNames());
|
2004-07-12 15:36:13 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
fail(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entriesReceived = received;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
rosterExchangeManager2.addRosterListener(rosterExchangeListener);
|
2003-08-24 18:05:45 +02:00
|
|
|
|
2003-10-15 16:55:19 +02:00
|
|
|
// Send user1's roster to user2
|
2003-08-24 18:05:45 +02:00
|
|
|
try {
|
2004-07-12 15:36:13 +02:00
|
|
|
entriesSent = getConnection(0).getRoster().getEntryCount();
|
2003-10-15 16:55:19 +02:00
|
|
|
entriesReceived = 0;
|
2004-07-12 15:36:13 +02:00
|
|
|
rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
|
2005-06-18 00:24:33 +02:00
|
|
|
// Wait up to 2 seconds
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(entriesSent != entriesReceived)) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2004-07-12 15:36:13 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
fail("An error occured sending the message with the roster");
|
|
|
|
}
|
|
|
|
assertEquals(
|
|
|
|
"Number of sent and received entries does not match",
|
|
|
|
entriesSent,
|
|
|
|
entriesReceived);
|
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-24 18:05:45 +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(100);
|
2003-08-24 18:05:45 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2003-10-15 16:55:19 +02:00
|
|
|
fail(e.getMessage());
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 4;
|
2003-10-15 16:55:19 +02:00
|
|
|
}
|
2003-08-24 18:05:45 +02:00
|
|
|
}
|