1 52 53 package org.jivesoftware.smackx; 54 55 import java.util.Iterator ; 56 57 import org.jivesoftware.smack.*; 58 import org.jivesoftware.smack.test.SmackTestCase; 59 60 66 public class RosterExchangeManagerTest extends SmackTestCase { 67 68 private int entriesSent; 69 private int entriesReceived; 70 71 75 public RosterExchangeManagerTest(String name) { 76 super(name); 77 } 78 79 85 public void testSendRoster() { 86 try { 88 RosterExchangeManager rosterExchangeManager = 89 new RosterExchangeManager(getConnection(0)); 90 rosterExchangeManager.send(getConnection(0).getRoster(), getBareJID(1)); 91 } 92 catch (Exception e) { 93 e.printStackTrace(); 94 fail("An error occured sending the roster"); 95 } 96 } 97 98 104 public void testSendRosterGroup() { 105 try { 107 RosterExchangeManager rosterExchangeManager = 108 new RosterExchangeManager(getConnection(0)); 109 for (Iterator it = getConnection(0).getRoster().getGroups(); it.hasNext();) 110 rosterExchangeManager.send((RosterGroup) it.next(), getBareJID(1)); 111 } 112 catch (Exception e) { 113 e.printStackTrace(); 114 fail("An error occured sending the roster"); 115 } 116 } 117 118 125 public void testSendAndReceiveRoster() { 126 RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0)); 127 RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1)); 128 129 RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { 131 public void entriesReceived(String from, Iterator 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 try { 148 entriesSent = getConnection(0).getRoster().getEntryCount(); 149 entriesReceived = 0; 150 rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1)); 151 long initial = System.currentTimeMillis(); 153 while (System.currentTimeMillis() - initial < 2000 && 154 (entriesSent != entriesReceived)) { 155 Thread.sleep(100); 156 } 157 } 158 catch (Exception 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 175 public void testSendAndAcceptRoster() { 176 RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0)); 177 RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1)); 178 179 RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { 181 public void entriesReceived(String from, Iterator 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 e) { 197 fail(e.toString()); 198 } 199 } 200 entriesReceived = received; 201 } 202 }; 203 rosterExchangeManager2.addRosterListener(rosterExchangeListener); 204 205 try { 207 entriesSent = getConnection(0).getRoster().getEntryCount(); 208 entriesReceived = 0; 209 rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1)); 210 long initial = System.currentTimeMillis(); 212 while (System.currentTimeMillis() - initial < 2000 && 213 (entriesSent != entriesReceived)) { 214 Thread.sleep(100); 215 } 216 } 217 catch (Exception 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 { 228 super.setUp(); 229 try { 230 getConnection(0).getRoster().createEntry( 231 getBareJID(2), 232 "gato5", 233 new String [] { "Friends, Coworker" }); 234 getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null); 235 Thread.sleep(100); 236 237 } 238 catch (Exception e) { 239 fail(e.getMessage()); 240 } 241 } 242 243 protected int getMaxConnections() { 244 return 4; 245 } 246 } 247 | Popular Tags |