KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > RosterTest


1 /**
2  * $RCSfile$
3  * $Revision: 2729 $
4  * $Date: 2005-08-26 23:21:24 -0300 (Fri, 26 Aug 2005) $
5  *
6  * Copyright (C) 2002-2003 Jive Software. All rights reserved.
7  * ====================================================================
8  * The Jive Software License (based on Apache Software License, Version 1.1)
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by
25  * Jive Software (http://www.jivesoftware.com)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Smack" and "Jive Software" must not be used to
30  * endorse or promote products derived from this software without
31  * prior written permission. For written permission, please
32  * contact webmaster@jivesoftware.com.
33  *
34  * 5. Products derived from this software may not be called "Smack",
35  * nor may "Smack" appear in their name, without prior written
36  * permission of Jive Software.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  */

52
53 package org.jivesoftware.smack;
54
55 import java.util.Iterator JavaDoc;
56
57 import org.jivesoftware.smack.packet.Presence;
58 import org.jivesoftware.smack.test.SmackTestCase;
59 import org.jivesoftware.smack.util.StringUtils;
60
61 /**
62  * Tests the Roster functionality by creating and removing roster entries.
63  *
64  * @author Gaston Dombiak
65  */

66 public class RosterTest extends SmackTestCase {
67
68     /**
69      * Constructor for RosterTest.
70      * @param name
71      */

72     public RosterTest(String JavaDoc name) {
73         super(name);
74     }
75
76     /**
77      * 1. Create entries in roster groups
78      * 2. Iterate on the groups and remove the entry from each group
79      * 3. Check that the entries are kept as unfiled entries
80      */

81     public void testDeleteAllRosterGroupEntries() {
82         try {
83             // Add a new roster entry
84
Roster roster = getConnection(0).getRoster();
85             roster.createEntry(getBareJID(1), "gato11", new String JavaDoc[] { "Friends", "Family" });
86             roster.createEntry(getBareJID(2), "gato12", new String JavaDoc[] { "Family" });
87
88             // Wait until the server confirms the new entries
89
while (roster.getEntryCount() != 2) {
90                 Thread.sleep(50);
91             }
92
93             Iterator JavaDoc it = roster.getEntries();
94             while (it.hasNext()) {
95                 RosterEntry entry = (RosterEntry) it.next();
96                 Iterator JavaDoc groups = entry.getGroups();
97                 while (groups.hasNext()) {
98                     RosterGroup rosterGroup = (RosterGroup) groups.next();
99                     rosterGroup.removeEntry(entry);
100                 }
101             }
102             // Wait up to 2 seconds
103
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 JavaDoc e) {
140             fail(e.getMessage());
141         }
142     }
143
144     /**
145      * 1. Create entries in roster groups
146      * 2. Iterate on all the entries and remove them from the roster
147      * 3. Check that the number of entries and groups is zero
148      */

149     public void testDeleteAllRosterEntries() {
150         try {
151             // Add a new roster entry
152
Roster roster = getConnection(0).getRoster();
153             roster.createEntry(getBareJID(1), "gato11", new String JavaDoc[] { "Friends" });
154             roster.createEntry(getBareJID(2), "gato12", new String JavaDoc[] { "Family" });
155
156             // Wait up to 2 seconds to receive new roster contacts
157
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             // Wait up to 2 seconds to receive presences of the new roster contacts
165
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 JavaDoc it = roster.getEntries();
175             while (it.hasNext()) {
176                 RosterEntry entry = (RosterEntry) it.next();
177                 roster.removeEntry(entry);
178                 Thread.sleep(250);
179             }
180
181             // Wait up to 2 seconds to receive roster removal notifications
182
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 JavaDoc e) {
200             fail(e.getMessage());
201         }
202     }
203
204     /**
205      * 1. Create unfiled entries
206      * 2. Iterate on all the entries and remove them from the roster
207      * 3. Check that the number of entries and groups is zero
208      */

209     public void testDeleteAllUnfiledRosterEntries() {
210         try {
211             // Add a new roster entry
212
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 JavaDoc 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 JavaDoc e) {
238             fail(e.getMessage());
239         }
240     }
241
242     /**
243      * 1. Create an unfiled entry
244      * 2. Change its name
245      * 3. Check that the name has been modified
246      * 4. Reload the whole roster
247      * 5. Check that the name has been modified
248      */

249     public void testChangeNameToUnfiledEntry() {
250         try {
251             // Add a new roster entry
252
Roster roster = getConnection(0).getRoster();
253             roster.createEntry(getBareJID(1), null, null);
254
255             Thread.sleep(200);
256
257             // Change the roster entry name and check if the change was made
258
Iterator JavaDoc it = roster.getEntries();
259             while (it.hasNext()) {
260                 RosterEntry entry = (RosterEntry) it.next();
261                 entry.setName("gato11");
262                 assertEquals("gato11", entry.getName());
263             }
264             // Reload the roster and check the name again
265
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 JavaDoc e) {
276             fail(e.getMessage());
277         }
278     }
279
280     /**
281      * 1. Create an unfiled entry with no name
282      * 2. Check that the the entry does not belong to any group
283      * 3. Change its name and add it to a group
284      * 4. Check that the name has been modified and that the entry belongs to a group
285      */

286     public void testChangeGroupAndNameToUnfiledEntry() {
287         try {
288             // Add a new roster entry
289
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             // Wait up to 5 seconds to receive presences of the new roster contacts
297
long initial = System.currentTimeMillis();
298             while (System.currentTimeMillis() - initial < 5000 &&
299                     roster.getPresence(getBareJID(0)) == null) {
300                 Thread.sleep(100);
301             }
302             //assertNotNull("Presence not received", roster.getPresence(getBareJID(0)));
303

304             Iterator JavaDoc 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             // Change the roster entry name and check if the change was made
311
roster.createEntry(getBareJID(1), "NewName", new String JavaDoc[] { "Friends" });
312
313             // Reload the roster and check the name again
314
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             // Wait up to 5 seconds to receive presences of the new roster contacts
323
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 JavaDoc e) {
332             fail(e.getMessage());
333         }
334     }
335
336     /**
337      * Test if renaming a roster group works fine.
338      *
339      */

340     public void testRenameRosterGroup() {
341         try {
342             // Add a new roster entry
343
Roster roster = getConnection(0).getRoster();
344             roster.createEntry(getBareJID(1), "gato11", new String JavaDoc[] { "Friends" });
345             roster.createEntry(getBareJID(2), "gato12", new String JavaDoc[] { "Friends" });
346
347             Thread.sleep(200);
348
349             roster.getGroup("Friends").setName("Amigos");
350
351             // Wait up to 2 seconds
352
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 JavaDoc e) {
380             fail(e.getMessage());
381         }
382     }
383
384     /**
385      * Test presence management.
386      */

387     public void testRosterPresences() {
388         try {
389             Presence presence = null;
390
391             // Create another connection for the same user of connection 1
392
XMPPConnection conn4 = new XMPPConnection(getServiceName());
393             conn4.login(getUsername(1), getUsername(1), "Home");
394
395             // Add a new roster entry
396
Roster roster = getConnection(0).getRoster();
397             roster.createEntry(getBareJID(1), "gato11", null);
398
399             // Wait up to 2 seconds
400
long initial = System.currentTimeMillis();
401             while (System.currentTimeMillis() - initial < 2000 &&
402                     (roster.getPresence(getBareJID(1)) == null)) {
403                 Thread.sleep(100);
404             }
405
406             // Check that a presence is returned for a user
407
presence = roster.getPresence(getBareJID(1));
408             assertNotNull("Returned a null Presence for an existing user", presence);
409
410             // Check that the right presence is returned for a user+resource
411
presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home");
412             assertEquals(
413                 "Returned the wrong Presence",
414                 StringUtils.parseResource(presence.getFrom()),
415                 "Home");
416
417             // Check that the right presence is returned for a user+resource
418
presence = roster.getPresenceResource(getFullJID(1));
419             assertEquals(
420                 "Returned the wrong Presence",
421                 StringUtils.parseResource(presence.getFrom()),
422                 "Smack");
423
424             // Check that the no presence is returned for a non-existent user+resource
425
presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack");
426             assertNull("Returned a Presence for a non-existing user", presence);
427
428             // Check that the returned presences are correct
429
Iterator JavaDoc 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             // Close the connection so one presence must go
438
conn4.close();
439
440             // Check that the returned presences are correct
441
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 JavaDoc e) {
454             fail(e.getMessage());
455         }
456     }
457
458     /**
459      * Clean up all the entries in the roster
460      */

461     private void cleanUpRoster() {
462         // Delete all the entries from the roster
463
Iterator JavaDoc 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         // Wait up to 2 seconds to receive roster removal notifications
474
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 JavaDoc e) {}
480         }
481
482         // Wait up to 2 seconds to receive roster removal notifications
483
initial = System.currentTimeMillis();
484         while (System.currentTimeMillis() - initial < 2000 &&
485                 getConnection(1).getRoster().getEntryCount() != 0) {
486             try {
487                 Thread.sleep(100);
488             } catch (InterruptedException JavaDoc 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 JavaDoc {
524         //XMPPConnection.DEBUG_ENABLED = false;
525
super.setUp();
526     }
527 }
Popular Tags