KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > RosterExchangeManagerTest


1 /**
2  * $RCSfile$
3  * $Revision: 2502 $
4  * $Date: 2005-06-17 19:24:33 -0300 (Fri, 17 Jun 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.smackx;
54
55 import java.util.Iterator JavaDoc;
56
57 import org.jivesoftware.smack.*;
58 import org.jivesoftware.smack.test.SmackTestCase;
59
60 /**
61  *
62  * Test the Roster Exchange extension using the high level API
63  *
64  * @author Gaston Dombiak
65  */

66 public class RosterExchangeManagerTest extends SmackTestCase {
67
68     private int entriesSent;
69     private int entriesReceived;
70
71     /**
72      * Constructor for RosterExchangeManagerTest.
73      * @param name
74      */

75     public RosterExchangeManagerTest(String JavaDoc name) {
76         super(name);
77     }
78
79     /**
80      * High level API test.
81      * This is a simple test to use with a XMPP client and check if the client receives user1's
82      * roster
83      * 1. User_1 will send his/her roster to user_2
84      */

85     public void testSendRoster() {
86         // Send user1's roster to user2
87
try {
88             RosterExchangeManager rosterExchangeManager =
89                 new RosterExchangeManager(getConnection(0));
90             rosterExchangeManager.send(getConnection(0).getRoster(), getBareJID(1));
91         }
92         catch (Exception JavaDoc e) {
93             e.printStackTrace();
94             fail("An error occured sending the roster");
95         }
96     }
97
98     /**
99      * High level API test.
100      * This is a simple test to use with a XMPP client and check if the client receives user1's
101      * roster groups
102      * 1. User_1 will send his/her RosterGroups to user_2
103      */

104     public void testSendRosterGroup() {
105         // Send user1's RosterGroups to user2
106
try {
107             RosterExchangeManager rosterExchangeManager =
108                 new RosterExchangeManager(getConnection(0));
109             for (Iterator JavaDoc it = getConnection(0).getRoster().getGroups(); it.hasNext();)
110                 rosterExchangeManager.send((RosterGroup) it.next(), getBareJID(1));
111         }
112         catch (Exception JavaDoc e) {
113             e.printStackTrace();
114             fail("An error occured sending the roster");
115         }
116     }
117
118     /**
119      * High level API test.
120      * 1. User_1 will send his/her roster to user_2
121      * 2. User_2 will receive the entries and iterate over them to check if everything is fine
122      * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
123      * something is wrong
124      */

125     public void testSendAndReceiveRoster() {
126         RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
127         RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
128
129         // Create a RosterExchangeListener that will iterate over the received roster entries
130
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
131             public void entriesReceived(String JavaDoc from, Iterator JavaDoc remoteRosterEntries) {
132                 int received = 0;
133                 assertNotNull("From is null", from);
134                 assertNotNull("rosterEntries is null", remoteRosterEntries);
135                 assertTrue("Roster without entries", remoteRosterEntries.hasNext());
136                 while (remoteRosterEntries.hasNext()) {
137                     received++;
138                     RemoteRosterEntry remoteEntry = (RemoteRosterEntry) remoteRosterEntries.next();
139                     System.out.println(remoteEntry);
140                 }
141                 entriesReceived = received;
142             }
143         };
144         rosterExchangeManager2.addRosterListener(rosterExchangeListener);
145
146         // Send user1's roster to user2
147
try {
148             entriesSent = getConnection(0).getRoster().getEntryCount();
149             entriesReceived = 0;
150             rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
151             // Wait up to 2 seconds
152
long initial = System.currentTimeMillis();
153             while (System.currentTimeMillis() - initial < 2000 &&
154                     (entriesSent != entriesReceived)) {
155                 Thread.sleep(100);
156             }
157         }
158         catch (Exception JavaDoc e) {
159             fail("An error occured sending the message with the roster");
160         }
161         assertEquals(
162             "Number of sent and received entries does not match",
163             entriesSent,
164             entriesReceived);
165     }
166
167     /**
168      * High level API test.
169      * 1. User_1 will send his/her roster to user_2
170      * 2. User_2 will automatically add the entries that receives to his/her roster in the
171      * corresponding group
172      * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
173      * something is wrong
174      */

175     public void testSendAndAcceptRoster() {
176         RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
177         RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
178
179         // Create a RosterExchangeListener that will accept all the received roster entries
180
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
181             public void entriesReceived(String JavaDoc from, Iterator JavaDoc remoteRosterEntries) {
182                 int received = 0;
183                 assertNotNull("From is null", from);
184                 assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
185                 assertTrue("Roster without entries", remoteRosterEntries.hasNext());
186                 while (remoteRosterEntries.hasNext()) {
187                     received++;
188                     try {
189                         RemoteRosterEntry remoteRosterEntry =
190                             (RemoteRosterEntry) remoteRosterEntries.next();
191                         getConnection(1).getRoster().createEntry(
192                             remoteRosterEntry.getUser(),
193                             remoteRosterEntry.getName(),
194                             remoteRosterEntry.getGroupArrayNames());
195                     }
196                     catch (Exception JavaDoc e) {
197                         fail(e.toString());
198                     }
199                 }
200                 entriesReceived = received;
201             }
202         };
203         rosterExchangeManager2.addRosterListener(rosterExchangeListener);
204
205         // Send user1's roster to user2
206
try {
207             entriesSent = getConnection(0).getRoster().getEntryCount();
208             entriesReceived = 0;
209             rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
210             // Wait up to 2 seconds
211
long initial = System.currentTimeMillis();
212             while (System.currentTimeMillis() - initial < 2000 &&
213                     (entriesSent != entriesReceived)) {
214                 Thread.sleep(100);
215             }
216         }
217         catch (Exception JavaDoc e) {
218             fail("An error occured sending the message with the roster");
219         }
220         assertEquals(
221             "Number of sent and received entries does not match",
222             entriesSent,
223             entriesReceived);
224         assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
225     }
226
227     protected void setUp() throws Exception JavaDoc {
228         super.setUp();
229         try {
230             getConnection(0).getRoster().createEntry(
231                 getBareJID(2),
232                 "gato5",
233                 new String JavaDoc[] { "Friends, Coworker" });
234             getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
235             Thread.sleep(100);
236
237         }
238         catch (Exception JavaDoc e) {
239             fail(e.getMessage());
240         }
241     }
242
243     protected int getMaxConnections() {
244         return 4;
245     }
246 }
247
Popular Tags