1 52 53 package org.jivesoftware.smack; 54 55 import java.util.Iterator ; 56 57 import org.jivesoftware.smack.packet.Presence; 58 import org.jivesoftware.smack.test.SmackTestCase; 59 import org.jivesoftware.smack.util.StringUtils; 60 61 66 public class RosterTest extends SmackTestCase { 67 68 72 public RosterTest(String name) { 73 super(name); 74 } 75 76 81 public void testDeleteAllRosterGroupEntries() { 82 try { 83 Roster roster = getConnection(0).getRoster(); 85 roster.createEntry(getBareJID(1), "gato11", new String [] { "Friends", "Family" }); 86 roster.createEntry(getBareJID(2), "gato12", new String [] { "Family" }); 87 88 while (roster.getEntryCount() != 2) { 90 Thread.sleep(50); 91 } 92 93 Iterator it = roster.getEntries(); 94 while (it.hasNext()) { 95 RosterEntry entry = (RosterEntry) it.next(); 96 Iterator groups = entry.getGroups(); 97 while (groups.hasNext()) { 98 RosterGroup rosterGroup = (RosterGroup) groups.next(); 99 rosterGroup.removeEntry(entry); 100 } 101 } 102 long initial = System.currentTimeMillis(); 104 while (System.currentTimeMillis() - initial < 2000 && 105 (roster.getGroupCount() != 0 && 106 getConnection(2).getRoster().getEntryCount() != 2)) { 107 Thread.sleep(100); 108 } 109 110 assertEquals( 111 "The number of entries in connection 1 should be 1", 112 1, 113 getConnection(1).getRoster().getEntryCount()); 114 assertEquals( 115 "The number of groups in connection 1 should be 0", 116 0, 117 getConnection(1).getRoster().getGroupCount()); 118 119 assertEquals( 120 "The number of entries in connection 2 should be 1", 121 1, 122 getConnection(2).getRoster().getEntryCount()); 123 assertEquals( 124 "The number of groups in connection 2 should be 0", 125 0, 126 getConnection(2).getRoster().getGroupCount()); 127 128 assertEquals( 129 "The number of entries in connection 0 should be 2", 130 2, 131 roster.getEntryCount()); 132 assertEquals( 133 "The number of groups in connection 0 should be 0", 134 0, 135 roster.getGroupCount()); 136 137 cleanUpRoster(); 138 } 139 catch (Exception e) { 140 fail(e.getMessage()); 141 } 142 } 143 144 149 public void testDeleteAllRosterEntries() { 150 try { 151 Roster roster = getConnection(0).getRoster(); 153 roster.createEntry(getBareJID(1), "gato11", new String [] { "Friends" }); 154 roster.createEntry(getBareJID(2), "gato12", new String [] { "Family" }); 155 156 long initial = System.currentTimeMillis(); 158 while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 2) { 159 Thread.sleep(100); 160 } 161 162 assertEquals("Wrong number of entries in connection 0", 2, roster.getEntryCount()); 163 164 initial = System.currentTimeMillis(); 166 while (System.currentTimeMillis() - initial < 5000 && 167 (roster.getPresence(getBareJID(1)) == null || 168 roster.getPresence(getBareJID(2)) == null)) { 169 Thread.sleep(100); 170 } 171 assertNotNull("Presence not received", roster.getPresence(getBareJID(1))); 172 assertNotNull("Presence not received", roster.getPresence(getBareJID(2))); 173 174 Iterator it = roster.getEntries(); 175 while (it.hasNext()) { 176 RosterEntry entry = (RosterEntry) it.next(); 177 roster.removeEntry(entry); 178 Thread.sleep(250); 179 } 180 181 initial = System.currentTimeMillis(); 183 while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) { 184 Thread.sleep(100); 185 } 186 187 assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount()); 188 assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount()); 189 190 assertEquals( 191 "Wrong number of entries in connection 1", 192 0, 193 getConnection(1).getRoster().getEntryCount()); 194 assertEquals( 195 "Wrong number of groups in connection 1", 196 0, 197 getConnection(1).getRoster().getGroupCount()); 198 } 199 catch (Exception e) { 200 fail(e.getMessage()); 201 } 202 } 203 204 209 public void testDeleteAllUnfiledRosterEntries() { 210 try { 211 Roster roster = getConnection(0).getRoster(); 213 roster.createEntry(getBareJID(1), "gato11", null); 214 roster.createEntry(getBareJID(2), "gato12", null); 215 216 Thread.sleep(200); 217 218 Iterator it = roster.getEntries(); 219 while (it.hasNext()) { 220 RosterEntry entry = (RosterEntry) it.next(); 221 roster.removeEntry(entry); 222 Thread.sleep(250); 223 } 224 225 assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount()); 226 assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount()); 227 228 assertEquals( 229 "Wrong number of entries in connection 1", 230 0, 231 getConnection(1).getRoster().getEntryCount()); 232 assertEquals( 233 "Wrong number of groups in connection 1", 234 0, 235 getConnection(1).getRoster().getGroupCount()); 236 } 237 catch (Exception e) { 238 fail(e.getMessage()); 239 } 240 } 241 242 249 public void testChangeNameToUnfiledEntry() { 250 try { 251 Roster roster = getConnection(0).getRoster(); 253 roster.createEntry(getBareJID(1), null, null); 254 255 Thread.sleep(200); 256 257 Iterator it = roster.getEntries(); 259 while (it.hasNext()) { 260 RosterEntry entry = (RosterEntry) it.next(); 261 entry.setName("gato11"); 262 assertEquals("gato11", entry.getName()); 263 } 264 roster.reload(); 266 Thread.sleep(2000); 267 it = roster.getEntries(); 268 while (it.hasNext()) { 269 RosterEntry entry = (RosterEntry) it.next(); 270 assertEquals("gato11", entry.getName()); 271 } 272 273 cleanUpRoster(); 274 } 275 catch (Exception e) { 276 fail(e.getMessage()); 277 } 278 } 279 280 286 public void testChangeGroupAndNameToUnfiledEntry() { 287 try { 288 Roster roster = getConnection(0).getRoster(); 290 roster.createEntry(getBareJID(1), null, null); 291 292 Thread.sleep(500); 293 294 getConnection(1).getRoster().createEntry(getBareJID(0), null, null); 295 296 long initial = System.currentTimeMillis(); 298 while (System.currentTimeMillis() - initial < 5000 && 299 roster.getPresence(getBareJID(0)) == null) { 300 Thread.sleep(100); 301 } 302 304 Iterator it = roster.getEntries(); 305 while (it.hasNext()) { 306 RosterEntry entry = (RosterEntry) it.next(); 307 assertFalse("The roster entry belongs to a group", entry.getGroups().hasNext()); 308 } 309 310 roster.createEntry(getBareJID(1), "NewName", new String [] { "Friends" }); 312 313 Thread.sleep(200); 315 it = roster.getEntries(); 316 while (it.hasNext()) { 317 RosterEntry entry = (RosterEntry) it.next(); 318 assertEquals("Name of roster entry is wrong", "NewName", entry.getName()); 319 assertTrue("The roster entry does not belong to any group", entry.getGroups() 320 .hasNext()); 321 } 322 initial = System.currentTimeMillis(); 324 while (System.currentTimeMillis() - initial < 5000 && 325 roster.getPresence(getBareJID(1)) == null) { 326 Thread.sleep(100); 327 } 328 assertNotNull("Presence not received", roster.getPresence(getBareJID(1))); 329 330 cleanUpRoster(); 331 } catch (Exception e) { 332 fail(e.getMessage()); 333 } 334 } 335 336 340 public void testRenameRosterGroup() { 341 try { 342 Roster roster = getConnection(0).getRoster(); 344 roster.createEntry(getBareJID(1), "gato11", new String [] { "Friends" }); 345 roster.createEntry(getBareJID(2), "gato12", new String [] { "Friends" }); 346 347 Thread.sleep(200); 348 349 roster.getGroup("Friends").setName("Amigos"); 350 351 long initial = System.currentTimeMillis(); 353 while (System.currentTimeMillis() - initial < 2000 && 354 (roster.getGroup("Friends") != null)) { 355 Thread.sleep(100); 356 } 357 358 assertNull("The group Friends still exists", roster.getGroup("Friends")); 359 assertNotNull("The group Amigos does not exist", roster.getGroup("Amigos")); 360 assertEquals( 361 "Wrong number of entries in the group Amigos", 362 2, 363 roster.getGroup("Amigos").getEntryCount()); 364 365 roster.getGroup("Amigos").setName(""); 366 367 Thread.sleep(500); 368 369 assertNull("The group Amigos still exists", roster.getGroup("Amigos")); 370 assertNotNull("The group with no name does not exist", roster.getGroup("")); 371 assertEquals( 372 "Wrong number of entries in the group \"\" ", 373 2, 374 roster.getGroup("").getEntryCount()); 375 376 cleanUpRoster(); 377 Thread.sleep(200); 378 } 379 catch (Exception e) { 380 fail(e.getMessage()); 381 } 382 } 383 384 387 public void testRosterPresences() { 388 try { 389 Presence presence = null; 390 391 XMPPConnection conn4 = new XMPPConnection(getServiceName()); 393 conn4.login(getUsername(1), getUsername(1), "Home"); 394 395 Roster roster = getConnection(0).getRoster(); 397 roster.createEntry(getBareJID(1), "gato11", null); 398 399 long initial = System.currentTimeMillis(); 401 while (System.currentTimeMillis() - initial < 2000 && 402 (roster.getPresence(getBareJID(1)) == null)) { 403 Thread.sleep(100); 404 } 405 406 presence = roster.getPresence(getBareJID(1)); 408 assertNotNull("Returned a null Presence for an existing user", presence); 409 410 presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home"); 412 assertEquals( 413 "Returned the wrong Presence", 414 StringUtils.parseResource(presence.getFrom()), 415 "Home"); 416 417 presence = roster.getPresenceResource(getFullJID(1)); 419 assertEquals( 420 "Returned the wrong Presence", 421 StringUtils.parseResource(presence.getFrom()), 422 "Smack"); 423 424 presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack"); 426 assertNull("Returned a Presence for a non-existing user", presence); 427 428 Iterator presences = roster.getPresences(getBareJID(1)); 430 int count = 0; 431 while (presences.hasNext()) { 432 count++; 433 presences.next(); 434 } 435 assertEquals("Wrong number of returned presences", count, 2); 436 437 conn4.close(); 439 440 presences = roster.getPresences(getBareJID(1)); 442 count = 0; 443 while (presences.hasNext()) { 444 count++; 445 presences.next(); 446 } 447 assertEquals("Wrong number of returned presences", count, 1); 448 449 Thread.sleep(200); 450 cleanUpRoster(); 451 452 } 453 catch (Exception e) { 454 fail(e.getMessage()); 455 } 456 } 457 458 461 private void cleanUpRoster() { 462 Iterator it = getConnection(0).getRoster().getEntries(); 464 while (it.hasNext()) { 465 RosterEntry entry = (RosterEntry) it.next(); 466 try { 467 getConnection(0).getRoster().removeEntry(entry); 468 } catch (XMPPException e) { 469 e.printStackTrace(); 470 fail(e.getMessage()); 471 } 472 } 473 long initial = System.currentTimeMillis(); 475 while (System.currentTimeMillis() - initial < 2000 && 476 getConnection(0).getRoster().getEntryCount() != 0) { 477 try { 478 Thread.sleep(100); 479 } catch (InterruptedException e) {} 480 } 481 482 initial = System.currentTimeMillis(); 484 while (System.currentTimeMillis() - initial < 2000 && 485 getConnection(1).getRoster().getEntryCount() != 0) { 486 try { 487 Thread.sleep(100); 488 } catch (InterruptedException e) {} 489 } 490 491 assertEquals( 492 "Wrong number of entries in connection 0", 493 0, 494 getConnection(0).getRoster().getEntryCount()); 495 assertEquals( 496 "Wrong number of groups in connection 0", 497 0, 498 getConnection(0).getRoster().getGroupCount()); 499 500 assertEquals( 501 "Wrong number of entries in connection 1", 502 0, 503 getConnection(1).getRoster().getEntryCount()); 504 assertEquals( 505 "Wrong number of groups in connection 1", 506 0, 507 getConnection(1).getRoster().getGroupCount()); 508 509 assertEquals( 510 "Wrong number of entries in connection 2", 511 0, 512 getConnection(2).getRoster().getEntryCount()); 513 assertEquals( 514 "Wrong number of groups in connection 2", 515 0, 516 getConnection(2).getRoster().getGroupCount()); 517 } 518 519 protected int getMaxConnections() { 520 return 3; 521 } 522 523 protected void setUp() throws Exception { 524 super.setUp(); 526 } 527 } | Popular Tags |