mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 18:59:41 +02:00
add the ability to register for roster events before logging in (SMACK-156)
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11826 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
7a3818783b
commit
a5693609b2
9 changed files with 469 additions and 88 deletions
|
@ -0,0 +1,34 @@
|
|||
package org.jivesoftware.smack;
|
||||
|
||||
/**
|
||||
* Run all tests defined in RosterTest but initialize the roster before connection is logged in and
|
||||
* authenticated.
|
||||
*
|
||||
* @author Henning Staib
|
||||
*/
|
||||
public class RosterInitializedBeforeConnectTest extends RosterSmackTest {
|
||||
|
||||
public RosterInitializedBeforeConnectTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected boolean createOfflineConnections() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
// initialize all rosters before login
|
||||
for (int i = 0; i < getMaxConnections(); i++) {
|
||||
XMPPConnection connection = getConnection(i);
|
||||
assertFalse(connection.isConnected());
|
||||
|
||||
Roster roster = connection.getRoster();
|
||||
assertNotNull(roster);
|
||||
|
||||
connectAndLogin(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
193
test/org/jivesoftware/smack/RosterListenerTest.java
Normal file
193
test/org/jivesoftware/smack/RosterListenerTest.java
Normal file
|
@ -0,0 +1,193 @@
|
|||
package org.jivesoftware.smack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.test.SmackTestCase;
|
||||
|
||||
/**
|
||||
* Test cases for adding the {@link RosterListener} in different connection states.
|
||||
*
|
||||
* @author Henning Staib
|
||||
*/
|
||||
public class RosterListenerTest extends SmackTestCase {
|
||||
|
||||
public RosterListenerTest(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public void testAddingRosterListenerBeforeConnect() throws Exception {
|
||||
int inviterIndex = 0;
|
||||
int inviteeIndex = 1;
|
||||
XMPPConnection inviterConnection = getConnection(inviterIndex);
|
||||
connectAndLogin(inviterIndex);
|
||||
|
||||
assertTrue("Inviter is not online", inviterConnection.isConnected());
|
||||
|
||||
Roster inviterRoster = inviterConnection.getRoster();
|
||||
|
||||
// add user1 to roster to create roster events stored at XMPP server
|
||||
inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
|
||||
|
||||
Thread.sleep(500); // wait for XMPP server
|
||||
|
||||
XMPPConnection inviteeConnection = getConnection(inviteeIndex);
|
||||
assertFalse("Invitee is already online", inviteeConnection.isConnected());
|
||||
|
||||
// collector for added entries
|
||||
final List<String> addedEntries = new ArrayList<String>();
|
||||
|
||||
// register roster listener before login
|
||||
Roster inviteeRoster = inviteeConnection.getRoster();
|
||||
inviteeRoster.addRosterListener(new RosterListener() {
|
||||
|
||||
public void presenceChanged(Presence presence) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public void entriesUpdated(Collection<String> addresses) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public void entriesDeleted(Collection<String> addresses) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public void entriesAdded(Collection<String> addresses) {
|
||||
addedEntries.addAll(addresses);
|
||||
}
|
||||
});
|
||||
|
||||
// connect after adding the listener
|
||||
connectAndLogin(inviteeIndex);
|
||||
|
||||
Thread.sleep(500); // wait for packets to be processed
|
||||
|
||||
assertNotNull("inviter is not in roster", inviteeRoster.getEntry(getBareJID(inviterIndex)));
|
||||
|
||||
assertTrue("got no event for adding inviter",
|
||||
addedEntries.contains(getBareJID(inviterIndex)));
|
||||
|
||||
}
|
||||
|
||||
public void testAddingRosterListenerAfterConnect() throws Exception {
|
||||
int inviterIndex = 0;
|
||||
int inviteeIndex = 1;
|
||||
XMPPConnection inviterConnection = getConnection(inviterIndex);
|
||||
connectAndLogin(inviterIndex);
|
||||
assertTrue("Inviter is not online", inviterConnection.isConnected());
|
||||
|
||||
Roster inviterRoster = inviterConnection.getRoster();
|
||||
|
||||
// add user1 to roster to create roster events stored at XMPP server
|
||||
inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
|
||||
|
||||
Thread.sleep(500); // wait for XMPP server
|
||||
|
||||
XMPPConnection inviteeConnection = getConnection(inviteeIndex);
|
||||
connectAndLogin(inviteeIndex);
|
||||
assertTrue("Invitee is not online", inviteeConnection.isConnected());
|
||||
|
||||
// collector for added entries
|
||||
final List<String> addedEntries = new ArrayList<String>();
|
||||
|
||||
// wait to simulate concurrency before adding listener
|
||||
Thread.sleep(200);
|
||||
|
||||
// register roster listener after login
|
||||
Roster inviteeRoster = inviteeConnection.getRoster();
|
||||
inviteeRoster.addRosterListener(new RosterListener() {
|
||||
|
||||
public void presenceChanged(Presence presence) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public void entriesUpdated(Collection<String> addresses) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public void entriesDeleted(Collection<String> addresses) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public void entriesAdded(Collection<String> addresses) {
|
||||
addedEntries.addAll(addresses);
|
||||
}
|
||||
});
|
||||
|
||||
Thread.sleep(500); // wait for packets to be processed
|
||||
|
||||
assertNotNull("Inviter is not in roster", inviteeRoster.getEntry(getBareJID(inviterIndex)));
|
||||
|
||||
assertFalse("got event for adding inviter", addedEntries.contains(getBareJID(inviterIndex)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
cleanUpRoster();
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
protected int getMaxConnections() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
protected boolean createOfflineConnections() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up all the entries in the roster
|
||||
*/
|
||||
private void cleanUpRoster() {
|
||||
for (int i = 0; i < getMaxConnections(); i++) {
|
||||
// Delete all the entries from the roster
|
||||
Roster roster = getConnection(i).getRoster();
|
||||
for (RosterEntry entry : roster.getEntries()) {
|
||||
try {
|
||||
roster.removeEntry(entry);
|
||||
Thread.sleep(100);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(700);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
// Wait up to 6 seconds to receive roster removal notifications
|
||||
long initial = System.currentTimeMillis();
|
||||
while (System.currentTimeMillis() - initial < 6000
|
||||
&& (getConnection(0).getRoster().getEntryCount() != 0 || getConnection(1).getRoster().getEntryCount() != 0)) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals("Wrong number of entries in connection 0", 0,
|
||||
getConnection(0).getRoster().getEntryCount());
|
||||
assertEquals("Wrong number of groups in connection 0", 0,
|
||||
getConnection(0).getRoster().getGroupCount());
|
||||
|
||||
assertEquals("Wrong number of entries in connection 1", 0,
|
||||
getConnection(1).getRoster().getEntryCount());
|
||||
assertEquals("Wrong number of groups in connection 1", 0,
|
||||
getConnection(1).getRoster().getGroupCount());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -446,18 +446,12 @@ public class RosterSmackTest extends SmackTestCase {
|
|||
}
|
||||
|
||||
assertNull("The group Amigos still exists", roster.getGroup("Amigos"));
|
||||
assertNotNull("The group with no name does not exist", roster.getGroup(""));
|
||||
assertNull("The group with no name does exist", roster.getGroup(""));
|
||||
assertEquals("There are still groups in the roster", 0, roster.getGroupCount());
|
||||
assertEquals(
|
||||
"Wrong number of entries in the group with no name",
|
||||
"Wrong number of unfiled entries",
|
||||
2,
|
||||
roster.getGroup("").getEntryCount());
|
||||
|
||||
/*assertEquals("There are still groups in the roster", 0, roster.getGroupCount());
|
||||
assertEquals(
|
||||
"Wrong number of entries in the group \"\" ",
|
||||
2,
|
||||
roster.getUnfiledEntryCount());*/
|
||||
|
||||
roster.getUnfiledEntryCount());
|
||||
|
||||
Thread.sleep(200);
|
||||
}
|
||||
|
|
|
@ -23,8 +23,6 @@ import junit.framework.TestCase;
|
|||
import org.jivesoftware.smack.ConnectionConfiguration;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Type;
|
||||
import org.xmlpull.mxp1.MXParser;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
|
@ -96,6 +94,23 @@ public abstract class SmackTestCase extends TestCase {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>false</code> if the connections initialized by the test case will be
|
||||
* automatically connected to the XMPP server.
|
||||
* Returns <code>true</code> if the connections initialized by the test case will
|
||||
* NOT be connected to the XMPP server. To connect the connections invoke
|
||||
* {@link #connectAndLogin(int)}.
|
||||
* <p>
|
||||
* Connections are connected by default.
|
||||
* Overwrite this method if the test case needs unconnected connections.
|
||||
*
|
||||
* @return <code>true</code> if connections should NOT be connected automatically,
|
||||
* <code>false</code> if connections should be connected automatically.
|
||||
*/
|
||||
protected boolean createOfflineConnections() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the XMPPConnection located at the requested position. Each test case holds a
|
||||
* pool of connections which is initialized while setting up the test case. The maximum
|
||||
|
@ -209,7 +224,8 @@ public abstract class SmackTestCase extends TestCase {
|
|||
// Connect to the server
|
||||
for (int i = 0; i < getMaxConnections(); i++) {
|
||||
connections[i] = createConnection();
|
||||
connections[i].connect();
|
||||
if (!createOfflineConnections())
|
||||
connections[i].connect();
|
||||
}
|
||||
// Use the host name that the server reports. This is a good idea in most
|
||||
// cases, but could fail if the user set a hostname in their XMPP server
|
||||
|
@ -217,41 +233,37 @@ public abstract class SmackTestCase extends TestCase {
|
|||
host = connections[0].getHost();
|
||||
serviceName = connections[0].getServiceName();
|
||||
|
||||
for (int i = 0; i < getMaxConnections(); i++) {
|
||||
String password = usernamePrefix + (i+1);
|
||||
String currentUser = password;
|
||||
|
||||
if (passwordPrefix != null)
|
||||
password = (samePassword ? passwordPrefix : passwordPrefix + (i+1));
|
||||
|
||||
try
|
||||
{
|
||||
getConnection(i).login(currentUser, password, "Smack");
|
||||
}
|
||||
catch (XMPPException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
|
||||
// Create the test accounts
|
||||
if (!getConnection(0).getAccountManager().supportsAccountCreation())
|
||||
fail("Server does not support account creation");
|
||||
|
||||
// Create the account and try logging in again as the
|
||||
// same user.
|
||||
try
|
||||
{
|
||||
createAccount(i, currentUser, password);
|
||||
}
|
||||
catch (Exception e1)
|
||||
{
|
||||
e1.printStackTrace();
|
||||
fail("Could not create user: " + currentUser);
|
||||
}
|
||||
i--;
|
||||
}
|
||||
if (!createOfflineConnections()) {
|
||||
for (int i = 0; i < getMaxConnections(); i++) {
|
||||
String password = usernamePrefix + (i+1);
|
||||
String currentUser = password;
|
||||
|
||||
if (passwordPrefix != null)
|
||||
password = (samePassword ? passwordPrefix : passwordPrefix + (i+1));
|
||||
|
||||
try {
|
||||
getConnection(i).login(currentUser, password, "Smack");
|
||||
} catch (XMPPException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
// Create the test accounts
|
||||
if (!getConnection(0).getAccountManager().supportsAccountCreation())
|
||||
fail("Server does not support account creation");
|
||||
|
||||
// Create the account and try logging in again as the
|
||||
// same user.
|
||||
try {
|
||||
createAccount(i, currentUser, password);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
fail("Could not create user: " + currentUser);
|
||||
}
|
||||
i--;
|
||||
}
|
||||
}
|
||||
// Let the server process the available presences
|
||||
Thread.sleep(150);
|
||||
}
|
||||
// Let the server process the available presences
|
||||
Thread.sleep(150);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -261,16 +273,21 @@ public abstract class SmackTestCase extends TestCase {
|
|||
|
||||
protected void connectAndLogin(int connectionIndex) throws XMPPException
|
||||
{
|
||||
String password = usernamePrefix + connectionIndex;
|
||||
String password = usernamePrefix + (connectionIndex + 1);
|
||||
|
||||
if (passwordPrefix != null)
|
||||
password = (samePassword ? passwordPrefix : passwordPrefix + connectionIndex);
|
||||
password = (samePassword ? passwordPrefix : passwordPrefix + (connectionIndex + 1));
|
||||
|
||||
XMPPConnection con = getConnection(connectionIndex);
|
||||
|
||||
if (!con.isConnected())
|
||||
con.connect();
|
||||
con.login(usernamePrefix + connectionIndex, password, "Smack");
|
||||
try {
|
||||
con.login(usernamePrefix + (connectionIndex + 1), password, "Smack");
|
||||
} catch (XMPPException e) {
|
||||
createAccount(connectionIndex, usernamePrefix + (connectionIndex + 1), password);
|
||||
con.login(usernamePrefix + (connectionIndex + 1), password, "Smack");
|
||||
}
|
||||
}
|
||||
|
||||
protected void disconnect(int connectionIndex) throws XMPPException
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue