2003-09-24 20:15:59 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
|
|
|
* ====================================================================
|
|
|
|
* The Jive Software License (based on Apache Software License, Version 1.1)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by
|
|
|
|
* Jive Software (http://www.jivesoftware.com)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
*
|
|
|
|
* 4. The names "Smack" and "Jive Software" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please
|
|
|
|
* contact webmaster@jivesoftware.com.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "Smack",
|
|
|
|
* nor may "Smack" appear in their name, without prior written
|
|
|
|
* permission of Jive Software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2003-11-28 22:47:48 +01:00
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
2004-07-12 15:36:13 +02:00
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
2003-11-28 22:47:48 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
|
|
2006-07-06 20:55:29 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2006-07-17 10:39:08 +02:00
|
|
|
import java.util.List;
|
2006-07-06 20:55:29 +02:00
|
|
|
|
2003-09-24 20:15:59 +02:00
|
|
|
/**
|
|
|
|
* Tests the Roster functionality by creating and removing roster entries.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2010-08-15 11:00:49 +02:00
|
|
|
public class RosterSmackTest extends SmackTestCase {
|
2003-09-24 20:15:59 +02:00
|
|
|
|
|
|
|
/**
|
2010-08-15 11:00:49 +02:00
|
|
|
* Constructor for RosterSmackTest.
|
2003-09-24 20:15:59 +02:00
|
|
|
* @param name
|
|
|
|
*/
|
2010-08-15 11:00:49 +02:00
|
|
|
public RosterSmackTest(String name) {
|
2003-09-24 20:15:59 +02:00
|
|
|
super(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-10-05 17:23:18 +02:00
|
|
|
* 1. Create entries in roster groups
|
2003-09-24 20:15:59 +02:00
|
|
|
* 2. Iterate on the groups and remove the entry from each group
|
2003-10-05 17:23:18 +02:00
|
|
|
* 3. Check that the entries are kept as unfiled entries
|
2003-09-24 20:15:59 +02:00
|
|
|
*/
|
|
|
|
public void testDeleteAllRosterGroupEntries() {
|
|
|
|
try {
|
|
|
|
// Add a new roster entry
|
2004-07-12 15:36:13 +02:00
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends", "Family" });
|
|
|
|
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2004-01-18 15:13:43 +01:00
|
|
|
// Wait until the server confirms the new entries
|
2006-07-17 21:38:37 +02:00
|
|
|
long initial = System.currentTimeMillis();
|
2006-07-18 07:14:33 +02:00
|
|
|
while (System.currentTimeMillis() - initial < 2000 && (
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(1)).isAvailable() ||
|
|
|
|
!roster.getPresence(getBareJID(2)).isAvailable())) {
|
2006-07-18 07:14:33 +02:00
|
|
|
Thread.sleep(100);
|
2004-01-18 15:13:43 +01:00
|
|
|
}
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
|
|
|
for (RosterGroup rosterGroup : entry.getGroups()) {
|
2003-09-24 20:15:59 +02:00
|
|
|
rosterGroup.removeEntry(entry);
|
|
|
|
}
|
|
|
|
}
|
2005-06-18 00:24:33 +02:00
|
|
|
// Wait up to 2 seconds
|
2006-07-17 21:38:37 +02:00
|
|
|
initial = System.currentTimeMillis();
|
2005-06-18 00:24:33 +02:00
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(roster.getGroupCount() != 0 &&
|
|
|
|
getConnection(2).getRoster().getEntryCount() != 2)) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2003-10-05 17:23:18 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
assertEquals(
|
|
|
|
"The number of entries in connection 1 should be 1",
|
|
|
|
1,
|
|
|
|
getConnection(1).getRoster().getEntryCount());
|
|
|
|
assertEquals(
|
|
|
|
"The number of groups in connection 1 should be 0",
|
|
|
|
0,
|
|
|
|
getConnection(1).getRoster().getGroupCount());
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"The number of entries in connection 2 should be 1",
|
|
|
|
1,
|
|
|
|
getConnection(2).getRoster().getEntryCount());
|
|
|
|
assertEquals(
|
|
|
|
"The number of groups in connection 2 should be 0",
|
|
|
|
0,
|
|
|
|
getConnection(2).getRoster().getGroupCount());
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"The number of entries in connection 0 should be 2",
|
|
|
|
2,
|
|
|
|
roster.getEntryCount());
|
|
|
|
assertEquals(
|
|
|
|
"The number of groups in connection 0 should be 0",
|
|
|
|
0,
|
|
|
|
roster.getGroupCount());
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2007-02-21 01:52:57 +01:00
|
|
|
finally {
|
|
|
|
cleanUpRoster();
|
|
|
|
}
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
2003-10-05 17:23:18 +02:00
|
|
|
|
2003-09-24 20:15:59 +02:00
|
|
|
/**
|
2003-10-05 17:23:18 +02:00
|
|
|
* 1. Create entries in roster groups
|
2003-09-24 20:15:59 +02:00
|
|
|
* 2. Iterate on all the entries and remove them from the roster
|
2003-10-05 17:23:18 +02:00
|
|
|
* 3. Check that the number of entries and groups is zero
|
2003-09-24 20:15:59 +02:00
|
|
|
*/
|
2007-07-23 04:06:30 +02:00
|
|
|
public void testDeleteAllRosterEntries() throws Exception {
|
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
|
|
|
|
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2007-07-23 04:06:30 +02:00
|
|
|
// Wait up to 2 seconds to receive new roster contacts
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 2) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2007-07-23 04:06:30 +02:00
|
|
|
assertEquals("Wrong number of entries in connection 0", 2, roster.getEntryCount());
|
2005-04-15 23:46:53 +02:00
|
|
|
|
2007-07-23 04:06:30 +02:00
|
|
|
// Wait up to 2 seconds to receive presences of the new roster contacts
|
|
|
|
initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 5000 &&
|
|
|
|
(!roster.getPresence(getBareJID(1)).isAvailable() ||
|
|
|
|
!roster.getPresence(getBareJID(2)).isAvailable()))
|
|
|
|
{
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
assertTrue("Presence not received", roster.getPresence(getBareJID(1)).isAvailable());
|
|
|
|
assertTrue("Presence not received", roster.getPresence(getBareJID(2)).isAvailable());
|
2004-07-12 15:36:13 +02:00
|
|
|
|
2007-07-23 04:06:30 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
|
|
|
roster.removeEntry(entry);
|
|
|
|
Thread.sleep(250);
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
2007-07-23 04:06:30 +02:00
|
|
|
|
|
|
|
// Wait up to 2 seconds to receive roster removal notifications
|
|
|
|
initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) {
|
|
|
|
Thread.sleep(100);
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
2007-07-23 04:06:30 +02:00
|
|
|
|
|
|
|
assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
|
|
|
|
assertEquals("Wrong number of groups in connection 0", 0, roster.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());
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Create unfiled entries
|
|
|
|
* 2. Iterate on all the entries and remove them from the roster
|
2003-10-05 17:23:18 +02:00
|
|
|
* 3. Check that the number of entries and groups is zero
|
2003-09-24 20:15:59 +02:00
|
|
|
*/
|
|
|
|
public void testDeleteAllUnfiledRosterEntries() {
|
|
|
|
try {
|
|
|
|
// Add a new roster entry
|
2004-07-12 15:36:13 +02:00
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", null);
|
|
|
|
roster.createEntry(getBareJID(2), "gato12", null);
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
// Wait up to 2 seconds to let the server process presence subscriptions
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && (
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(1)).isAvailable() ||
|
|
|
|
!roster.getPresence(getBareJID(2)).isAvailable())) {
|
2006-07-18 07:14:33 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
|
2003-10-05 17:23:18 +02:00
|
|
|
Thread.sleep(200);
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
2004-07-12 15:36:13 +02:00
|
|
|
roster.removeEntry(entry);
|
2006-07-18 07:14:33 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait up to 2 seconds to receive roster removal notifications
|
|
|
|
initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) {
|
|
|
|
Thread.sleep(100);
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
|
|
|
|
assertEquals("Wrong number of groups in connection 0", 0, roster.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());
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Create an unfiled entry
|
|
|
|
* 2. Change its name
|
|
|
|
* 3. Check that the name has been modified
|
|
|
|
* 4. Reload the whole roster
|
|
|
|
* 5. Check that the name has been modified
|
|
|
|
*/
|
|
|
|
public void testChangeNameToUnfiledEntry() {
|
|
|
|
try {
|
|
|
|
// Add a new roster entry
|
2004-07-12 15:36:13 +02:00
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), null, null);
|
2003-09-24 20:15:59 +02:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
// Wait up to 2 seconds to let the server process presence subscriptions
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(1)).isAvailable())
|
|
|
|
{
|
2006-07-18 07:14:33 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2003-09-24 20:15:59 +02:00
|
|
|
|
|
|
|
// Change the roster entry name and check if the change was made
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
2003-09-24 20:15:59 +02:00
|
|
|
entry.setName("gato11");
|
|
|
|
assertEquals("gato11", entry.getName());
|
|
|
|
}
|
2003-10-05 17:23:18 +02:00
|
|
|
// Reload the roster and check the name again
|
2004-07-12 15:36:13 +02:00
|
|
|
roster.reload();
|
2003-10-05 17:23:18 +02:00
|
|
|
Thread.sleep(2000);
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
2003-09-24 20:15:59 +02:00
|
|
|
assertEquals("gato11", entry.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2007-02-21 01:52:57 +01:00
|
|
|
finally {
|
|
|
|
cleanUpRoster();
|
|
|
|
}
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
|
|
|
|
2004-07-15 04:35:54 +02:00
|
|
|
/**
|
|
|
|
* 1. Create an unfiled entry with no name
|
|
|
|
* 2. Check that the the entry does not belong to any group
|
|
|
|
* 3. Change its name and add it to a group
|
|
|
|
* 4. Check that the name has been modified and that the entry belongs to a group
|
|
|
|
*/
|
|
|
|
public void testChangeGroupAndNameToUnfiledEntry() {
|
|
|
|
try {
|
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), null, null);
|
|
|
|
|
2005-04-15 23:46:53 +02:00
|
|
|
Thread.sleep(500);
|
2004-07-15 04:35:54 +02:00
|
|
|
|
|
|
|
getConnection(1).getRoster().createEntry(getBareJID(0), null, null);
|
|
|
|
|
2005-04-15 23:46:53 +02:00
|
|
|
// Wait up to 5 seconds to receive presences of the new roster contacts
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 5000 &&
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(0)).isAvailable()) {
|
2005-04-15 23:46:53 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
//assertNotNull("Presence not received", roster.getPresence(getBareJID(0)));
|
2004-07-15 04:35:54 +02:00
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
|
|
|
assertFalse("The roster entry belongs to a group", !entry.getGroups().isEmpty());
|
2004-07-15 04:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change the roster entry name and check if the change was made
|
|
|
|
roster.createEntry(getBareJID(1), "NewName", new String[] { "Friends" });
|
|
|
|
|
|
|
|
// Reload the roster and check the name again
|
|
|
|
Thread.sleep(200);
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
2004-07-15 04:35:54 +02:00
|
|
|
assertEquals("Name of roster entry is wrong", "NewName", entry.getName());
|
2006-07-17 10:39:08 +02:00
|
|
|
assertTrue("The roster entry does not belong to any group", !entry.getGroups().isEmpty());
|
2004-07-15 04:35:54 +02:00
|
|
|
}
|
2005-04-15 23:46:53 +02:00
|
|
|
// Wait up to 5 seconds to receive presences of the new roster contacts
|
|
|
|
initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 5000 &&
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(1)).isAvailable()) {
|
2005-04-15 23:46:53 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2007-02-21 01:52:57 +01:00
|
|
|
assertTrue("Presence not received", roster.getPresence(getBareJID(1)).isAvailable());
|
2004-07-15 04:35:54 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2007-02-21 01:52:57 +01:00
|
|
|
finally {
|
|
|
|
cleanUpRoster();
|
|
|
|
}
|
2004-07-15 04:35:54 +02:00
|
|
|
}
|
|
|
|
|
2005-10-14 21:46:31 +02:00
|
|
|
/**
|
|
|
|
* Tests that adding an existing roster entry that belongs to a group to another group
|
|
|
|
* works fine.
|
|
|
|
*/
|
|
|
|
public void testAddEntryToNewGroup() {
|
|
|
|
try {
|
2005-10-25 03:27:35 +02:00
|
|
|
Thread.sleep(500);
|
|
|
|
|
2005-10-14 21:46:31 +02:00
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
|
|
|
|
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
|
|
|
|
|
|
|
// Wait up to 2 seconds to receive new roster contacts
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 2) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals("Wrong number of entries in connection 0", 2, roster.getEntryCount());
|
|
|
|
|
|
|
|
// Add "gato11" to a new group called NewGroup
|
|
|
|
roster.createGroup("NewGroup").addEntry(roster.getEntry(getBareJID(1)));
|
|
|
|
|
|
|
|
|
|
|
|
// Log in from another resource so we can test the roster
|
2007-01-05 00:00:58 +01:00
|
|
|
XMPPConnection con2 = createConnection();
|
2006-09-14 21:16:40 +02:00
|
|
|
con2.connect();
|
2005-10-14 21:46:31 +02:00
|
|
|
con2.login(getUsername(0), getUsername(0), "MyNewResource");
|
|
|
|
|
|
|
|
Roster roster2 = con2.getRoster();
|
|
|
|
|
|
|
|
assertEquals("Wrong number of entries in new connection", 2, roster2.getEntryCount());
|
|
|
|
assertEquals("Wrong number of groups in new connection", 3, roster2.getGroupCount());
|
|
|
|
|
|
|
|
|
|
|
|
RosterEntry entry = roster2.getEntry(getBareJID(1));
|
|
|
|
assertNotNull("No entry for user 1 was found", entry);
|
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
List<String> groupNames = new ArrayList<String>();
|
|
|
|
for (RosterGroup rosterGroup : entry.getGroups()) {
|
|
|
|
groupNames.add(rosterGroup.getName());
|
|
|
|
}
|
|
|
|
assertTrue("Friends group was not found", groupNames.contains("Friends"));
|
|
|
|
assertTrue("NewGroup group was not found", groupNames.contains("NewGroup"));
|
2005-10-14 21:46:31 +02:00
|
|
|
|
|
|
|
// Close the new connection
|
2006-09-14 21:16:40 +02:00
|
|
|
con2.disconnect();
|
2005-10-25 03:27:35 +02:00
|
|
|
Thread.sleep(500);
|
2005-10-14 21:46:31 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2007-02-21 01:52:57 +01:00
|
|
|
finally {
|
|
|
|
cleanUpRoster();
|
|
|
|
}
|
2005-10-14 21:46:31 +02:00
|
|
|
}
|
|
|
|
|
2004-01-18 15:13:43 +01:00
|
|
|
/**
|
|
|
|
* Test if renaming a roster group works fine.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void testRenameRosterGroup() {
|
|
|
|
try {
|
2005-10-25 03:27:35 +02:00
|
|
|
Thread.sleep(200);
|
|
|
|
|
2004-01-18 15:13:43 +01:00
|
|
|
// Add a new roster entry
|
2004-07-12 15:36:13 +02:00
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
|
|
|
|
roster.createEntry(getBareJID(2), "gato12", new String[] { "Friends" });
|
2004-01-18 15:13:43 +01:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
// Wait up to 2 seconds to let the server process presence subscriptions
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && (
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(1)).isAvailable() ||
|
|
|
|
!roster.getPresence(getBareJID(2)).isAvailable())) {
|
2006-07-18 07:14:33 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2004-01-18 15:13:43 +01:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
roster.getGroup("Friends").setName("Amigos");
|
2005-04-15 23:46:53 +02:00
|
|
|
|
2005-06-18 00:24:33 +02:00
|
|
|
// Wait up to 2 seconds
|
2006-07-18 07:14:33 +02:00
|
|
|
initial = System.currentTimeMillis();
|
2005-06-18 00:24:33 +02:00
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(roster.getGroup("Friends") != null)) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2005-04-15 23:46:53 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
assertNull("The group Friends still exists", roster.getGroup("Friends"));
|
|
|
|
assertNotNull("The group Amigos does not exist", roster.getGroup("Amigos"));
|
|
|
|
assertEquals(
|
|
|
|
"Wrong number of entries in the group Amigos",
|
|
|
|
2,
|
|
|
|
roster.getGroup("Amigos").getEntryCount());
|
|
|
|
|
2005-12-08 22:05:12 +01:00
|
|
|
// Setting the name to empty is like removing this group
|
2004-07-12 15:36:13 +02:00
|
|
|
roster.getGroup("Amigos").setName("");
|
2005-04-15 23:46:53 +02:00
|
|
|
|
2005-12-01 05:29:08 +01:00
|
|
|
// Wait up to 2 seconds for the group to change its name
|
|
|
|
initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(roster.getGroup("Amigos") != null)) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2005-04-15 23:46:53 +02:00
|
|
|
|
2006-01-24 19:16:29 +01:00
|
|
|
assertNull("The group Amigos still exists", roster.getGroup("Amigos"));
|
|
|
|
assertNotNull("The group with no name does not exist", roster.getGroup(""));
|
|
|
|
assertEquals(
|
|
|
|
"Wrong number of entries in the group with no name",
|
|
|
|
2,
|
|
|
|
roster.getGroup("").getEntryCount());
|
|
|
|
|
|
|
|
/*assertEquals("There are still groups in the roster", 0, roster.getGroupCount());
|
2004-07-12 15:36:13 +02:00
|
|
|
assertEquals(
|
|
|
|
"Wrong number of entries in the group \"\" ",
|
|
|
|
2,
|
2006-01-24 19:16:29 +01:00
|
|
|
roster.getUnfiledEntryCount());*/
|
2004-01-18 15:13:43 +01:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
|
2004-01-18 15:13:43 +01:00
|
|
|
Thread.sleep(200);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2007-02-21 01:52:57 +01:00
|
|
|
finally {
|
|
|
|
cleanUpRoster();
|
|
|
|
}
|
2004-01-18 15:13:43 +01:00
|
|
|
}
|
|
|
|
|
2003-11-28 22:47:48 +01:00
|
|
|
/**
|
2006-07-18 23:24:00 +02:00
|
|
|
* Test presence management.<p>
|
|
|
|
*
|
|
|
|
* 1. Log in user0 from a client and user1 from 2 clients
|
|
|
|
* 2. Create presence subscription of type BOTH between 2 users
|
|
|
|
* 3. Check that presence is correctly delivered to both users
|
|
|
|
* 4. User1 logs out from a client
|
|
|
|
* 5. Check that presence for each connected resource is correct
|
2003-11-28 22:47:48 +01:00
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public void testRosterPresences() throws Exception {
|
|
|
|
Thread.sleep(200);
|
2007-02-21 01:52:57 +01:00
|
|
|
try {
|
|
|
|
Presence presence;
|
2005-10-25 03:27:35 +02:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Create another connection for the same user of connection 1
|
2007-07-23 04:06:30 +02:00
|
|
|
ConnectionConfiguration connectionConfiguration =
|
|
|
|
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
|
|
|
|
XMPPConnection conn4 = new XMPPConnection(connectionConfiguration);
|
2007-02-21 01:52:57 +01:00
|
|
|
conn4.connect();
|
|
|
|
conn4.login(getUsername(1), getUsername(1), "Home");
|
2003-11-28 22:47:48 +01:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", null);
|
2004-07-12 15:36:13 +02:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Wait up to 2 seconds
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(roster.getPresence(getBareJID(1)).getType() == Presence.Type.unavailable)) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2003-11-28 22:47:48 +01:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Check that a presence is returned for a user
|
|
|
|
presence = roster.getPresence(getBareJID(1));
|
|
|
|
assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
|
|
|
|
|
|
|
|
// Check that the right presence is returned for a user+resource
|
|
|
|
presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home");
|
|
|
|
assertEquals("Returned the wrong Presence", "Home",
|
|
|
|
StringUtils.parseResource(presence.getFrom()));
|
|
|
|
|
|
|
|
// Check that the right presence is returned for a user+resource
|
|
|
|
presence = roster.getPresenceResource(getFullJID(1));
|
|
|
|
assertTrue("Presence not found for user " + getFullJID(1), presence.isAvailable());
|
|
|
|
assertEquals("Returned the wrong Presence", "Smack",
|
|
|
|
StringUtils.parseResource(presence.getFrom()));
|
|
|
|
|
|
|
|
// Check the returned presence for a non-existent user+resource
|
|
|
|
presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack");
|
|
|
|
assertFalse("Available presence was returned for a non-existing user", presence.isAvailable());
|
|
|
|
assertEquals("Returned Presence for a non-existing user has the incorrect type",
|
|
|
|
Presence.Type.unavailable, presence.getType());
|
|
|
|
|
|
|
|
// Check that the returned presences are correct
|
2008-11-21 05:54:59 +01:00
|
|
|
Iterator<Presence> presences = roster.getPresences(getBareJID(1));
|
2007-02-21 01:52:57 +01:00
|
|
|
int count = 0;
|
|
|
|
while (presences.hasNext()) {
|
|
|
|
count++;
|
|
|
|
presences.next();
|
|
|
|
}
|
|
|
|
assertEquals("Wrong number of returned presences", count, 2);
|
2006-07-18 07:14:33 +02:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Close the connection so one presence must go
|
|
|
|
conn4.disconnect();
|
2003-11-28 22:47:48 +01:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Check that the returned presences are correct
|
|
|
|
presences = roster.getPresences(getBareJID(1));
|
|
|
|
count = 0;
|
|
|
|
while (presences.hasNext()) {
|
|
|
|
count++;
|
|
|
|
presences.next();
|
|
|
|
}
|
|
|
|
assertEquals("Wrong number of returned presences", count, 1);
|
2004-01-18 15:13:43 +01:00
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
Thread.sleep(200);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
cleanUpRoster();
|
2003-12-02 02:58:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-06 20:55:29 +02:00
|
|
|
/**
|
|
|
|
* User1 is connected from 2 resources. User1 adds User0 to his roster. Ensure
|
|
|
|
* that both resources of user1 get the available presence of User0. Remove User0
|
|
|
|
* from User1's roster and check presences again.
|
|
|
|
*/
|
|
|
|
public void testMultipleResources() throws Exception {
|
|
|
|
// Create another connection for the same user of connection 1
|
2007-07-23 04:06:30 +02:00
|
|
|
ConnectionConfiguration connectionConfiguration =
|
|
|
|
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
|
|
|
|
XMPPConnection conn4 = new XMPPConnection(connectionConfiguration);
|
2006-09-14 21:16:40 +02:00
|
|
|
conn4.connect();
|
2006-07-06 20:55:29 +02:00
|
|
|
conn4.login(getUsername(1), getUsername(1), "Home");
|
|
|
|
|
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = conn4.getRoster();
|
|
|
|
roster.createEntry(getBareJID(0), "gato11", null);
|
|
|
|
|
|
|
|
// Wait up to 2 seconds
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && (
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(0)).isAvailable() ||
|
|
|
|
!getConnection(1).getRoster().getPresence(getBareJID(0)).isAvailable())) {
|
2006-07-06 20:55:29 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that a presence is returned for the new contact
|
|
|
|
Presence presence = roster.getPresence(getBareJID(0));
|
2007-02-21 01:52:57 +01:00
|
|
|
assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
|
2006-07-06 20:55:29 +02:00
|
|
|
|
|
|
|
// Check that a presence is returned for the new contact
|
|
|
|
presence = getConnection(1).getRoster().getPresence(getBareJID(0));
|
2007-02-21 01:52:57 +01:00
|
|
|
assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
|
2006-07-06 20:55:29 +02:00
|
|
|
|
|
|
|
// Delete user from roster
|
|
|
|
roster.removeEntry(roster.getEntry(getBareJID(0)));
|
|
|
|
|
|
|
|
// Wait up to 2 seconds
|
|
|
|
initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && (
|
2007-02-16 01:13:18 +01:00
|
|
|
roster.getPresence(getBareJID(0)).getType() != Presence.Type.unavailable ||
|
|
|
|
getConnection(1).getRoster().getPresence(getBareJID(0)).getType() !=
|
|
|
|
Presence.Type.unavailable)) {
|
2006-07-06 20:55:29 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that no presence is returned for the removed contact
|
|
|
|
presence = roster.getPresence(getBareJID(0));
|
2007-02-21 01:52:57 +01:00
|
|
|
assertFalse("Available presence was returned for removed contact", presence.isAvailable());
|
2007-02-16 01:13:18 +01:00
|
|
|
assertEquals("Returned Presence for removed contact has incorrect type",
|
|
|
|
Presence.Type.unavailable, presence.getType());
|
2006-07-06 20:55:29 +02:00
|
|
|
|
|
|
|
// Check that no presence is returned for the removed contact
|
|
|
|
presence = getConnection(1).getRoster().getPresence(getBareJID(0));
|
2007-02-21 01:52:57 +01:00
|
|
|
assertFalse("Available presence was returned for removed contact", presence.isAvailable());
|
2007-02-16 01:13:18 +01:00
|
|
|
assertEquals("Returned Presence for removed contact has incorrect type",
|
|
|
|
Presence.Type.unavailable, presence.getType());
|
2006-07-06 20:55:29 +02:00
|
|
|
}
|
|
|
|
|
2006-08-10 23:35:08 +02:00
|
|
|
/**
|
|
|
|
* Tests that the server and Smack are able to handle nicknames that include
|
|
|
|
* < > characters.
|
|
|
|
*/
|
|
|
|
public void testNotCommonNickname() throws Exception {
|
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "Thiago <12001200>", null);
|
|
|
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
assertEquals("Created entry was never received", 1, roster.getEntryCount());
|
|
|
|
|
|
|
|
// Create another connection for the same user of connection 0
|
2007-07-23 04:06:30 +02:00
|
|
|
ConnectionConfiguration connectionConfiguration =
|
|
|
|
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
|
|
|
|
XMPPConnection conn2 = new XMPPConnection(connectionConfiguration);
|
2006-09-14 21:16:40 +02:00
|
|
|
conn2.connect();
|
2006-08-10 23:35:08 +02:00
|
|
|
conn2.login(getUsername(0), getUsername(0), "Home");
|
|
|
|
|
|
|
|
// Retrieve roster and verify that new contact is there and nickname is correct
|
|
|
|
Roster roster2 = conn2.getRoster();
|
|
|
|
assertEquals("Created entry was never received", 1, roster2.getEntryCount());
|
|
|
|
RosterEntry entry = roster2.getEntry(getBareJID(1));
|
|
|
|
assertNotNull("New entry was not returned from the server", entry);
|
|
|
|
assertEquals("Roster item name is incorrect", "Thiago <12001200>", entry.getName());
|
|
|
|
}
|
|
|
|
|
2003-09-24 20:15:59 +02:00
|
|
|
/**
|
|
|
|
* Clean up all the entries in the roster
|
|
|
|
*/
|
|
|
|
private void cleanUpRoster() {
|
2005-12-01 05:29:08 +01:00
|
|
|
for (int i=0; i<getMaxConnections(); i++) {
|
|
|
|
// Delete all the entries from the roster
|
2007-02-21 01:52:57 +01:00
|
|
|
Roster roster = getConnection(i).getRoster();
|
|
|
|
for (RosterEntry entry : roster.getEntries()) {
|
2005-12-01 05:29:08 +01:00
|
|
|
try {
|
2007-02-21 01:52:57 +01:00
|
|
|
roster.removeEntry(entry);
|
2006-07-17 10:39:08 +02:00
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
2005-12-01 05:29:08 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2005-03-30 04:01:21 +02:00
|
|
|
}
|
2005-10-25 19:14:52 +02:00
|
|
|
|
2005-12-01 05:29:08 +01:00
|
|
|
try {
|
|
|
|
Thread.sleep(700);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2005-10-25 19:14:52 +02:00
|
|
|
}
|
2006-07-18 07:14:33 +02:00
|
|
|
// Wait up to 6 seconds to receive roster removal notifications
|
2005-04-15 23:46:53 +02:00
|
|
|
long initial = System.currentTimeMillis();
|
2006-07-18 07:14:33 +02:00
|
|
|
while (System.currentTimeMillis() - initial < 6000 && (
|
|
|
|
getConnection(0).getRoster().getEntryCount() != 0 ||
|
|
|
|
getConnection(1).getRoster().getEntryCount() != 0 ||
|
|
|
|
getConnection(2).getRoster().getEntryCount() != 0)) {
|
2005-04-15 23:46:53 +02:00
|
|
|
try {
|
|
|
|
Thread.sleep(100);
|
|
|
|
} catch (InterruptedException e) {}
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
2005-04-15 23:46:53 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
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());
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"Wrong number of entries in connection 2",
|
|
|
|
0,
|
|
|
|
getConnection(2).getRoster().getEntryCount());
|
|
|
|
assertEquals(
|
|
|
|
"Wrong number of groups in connection 2",
|
|
|
|
0,
|
|
|
|
getConnection(2).getRoster().getGroupCount());
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
/**
|
|
|
|
* Tests the creation of a roster and then simulates abrupt termination. Cached presences
|
|
|
|
* must go offline. At reconnection, presences must go back to online.
|
|
|
|
* <ol>
|
|
|
|
* <li> Create some entries
|
|
|
|
* <li> Breack the connection
|
|
|
|
* <li> Check offline presences
|
|
|
|
* <li> Whait for automatic reconnection
|
|
|
|
* <li> Check online presences
|
|
|
|
* </ol>
|
|
|
|
*/
|
|
|
|
public void testOfflinePresencesAfterDisconnection() throws Exception {
|
|
|
|
// Add a new roster entry
|
|
|
|
Roster roster = getConnection(0).getRoster();
|
|
|
|
roster.createEntry(getBareJID(1), "gato11", null);
|
|
|
|
roster.createEntry(getBareJID(2), "gato12", null);
|
|
|
|
|
|
|
|
// Wait up to 2 seconds to let the server process presence subscriptions
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 && (
|
2007-02-21 01:52:57 +01:00
|
|
|
!roster.getPresence(getBareJID(1)).isAvailable() ||
|
|
|
|
!roster.getPresence(getBareJID(2)).isAvailable())) {
|
2006-09-14 21:16:40 +02:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.sleep(200);
|
|
|
|
|
2007-02-21 01:52:57 +01:00
|
|
|
// Break the connection
|
2006-09-14 21:16:40 +02:00
|
|
|
getConnection(0).packetReader.notifyConnectionError(new Exception("Simulated Error"));
|
|
|
|
|
|
|
|
Presence presence = roster.getPresence(getBareJID(1));
|
2007-02-21 01:52:57 +01:00
|
|
|
assertFalse("Unavailable presence not found for offline user", presence.isAvailable());
|
2007-02-16 01:13:18 +01:00
|
|
|
assertEquals("Unavailable presence not found for offline user", Presence.Type.unavailable,
|
|
|
|
presence.getType());
|
2006-09-14 21:16:40 +02:00
|
|
|
// Reconnection should occur in 10 seconds
|
|
|
|
Thread.sleep(12200);
|
|
|
|
presence = roster.getPresence(getBareJID(1));
|
2007-02-21 01:52:57 +01:00
|
|
|
assertTrue("Presence not found for user", presence.isAvailable());
|
2006-09-14 21:16:40 +02:00
|
|
|
assertEquals("Presence should be online after a connection reconnection",
|
|
|
|
Presence.Type.available, presence.getType());
|
|
|
|
}
|
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 3;
|
2003-09-24 20:15:59 +02:00
|
|
|
}
|
2005-04-15 23:46:53 +02:00
|
|
|
|
|
|
|
protected void setUp() throws Exception {
|
2005-06-18 00:24:33 +02:00
|
|
|
//XMPPConnection.DEBUG_ENABLED = false;
|
2005-10-25 03:27:35 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
}
|
2005-10-25 19:14:52 +02:00
|
|
|
catch (InterruptedException e) {
|
2005-10-25 03:27:35 +02:00
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
|
2005-04-15 23:46:53 +02:00
|
|
|
super.setUp();
|
|
|
|
}
|
2003-10-05 06:45:05 +02:00
|
|
|
}
|