/**
  * High level API test. This is a simple test to use with a XMPP client and check if the client
  * receives user1's roster 1. User_1 will send his/her roster to user_2
  */
 public void testSendRoster() {
   // Send user1's roster to user2
   try {
     RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(getConnection(0));
     rosterExchangeManager.send(getConnection(0).getRoster(), getBareJID(1));
   } catch (Exception e) {
     e.printStackTrace();
     fail("An error occured sending the roster");
   }
 }
 /**
  * High level API test. This is a simple test to use with a XMPP client and check if the client
  * receives user1's roster groups 1. User_1 will send his/her RosterGroups to user_2
  */
 public void testSendRosterGroup() {
   // Send user1's RosterGroups to user2
   try {
     RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(getConnection(0));
     for (Iterator it = getConnection(0).getRoster().getGroups(); it.hasNext(); )
       rosterExchangeManager.send((RosterGroup) it.next(), getBareJID(1));
   } catch (Exception e) {
     e.printStackTrace();
     fail("An error occured sending the roster");
   }
 }
 /**
  * High level API test. This is a simple test to use with a XMPP client and check if the client
  * receives user1's roster groups 1. User_1 will send his/her RosterGroups to user_2
  */
 public void testSendRosterGroup() {
   // Send user1's RosterGroups to user2
   try {
     RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(getConnection(0));
     for (RosterGroup rosterGroup : getConnection(0).getRoster().getGroups()) {
       rosterExchangeManager.send(rosterGroup, getBareJID(1));
     }
   } catch (Exception e) {
     e.printStackTrace();
     fail("An error occured sending the roster");
   }
 }
  /**
   * High level API test. 1. User_1 will send his/her roster to user_2 2. User_2 will automatically
   * add the entries that receives to his/her roster in the corresponding group 3. User_1 will wait
   * several seconds for an ACK from user_2, if none is received then something is wrong
   */
  public void testSendAndAcceptRoster() {
    RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
    RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));

    // Create a RosterExchangeListener that will accept all the received roster entries
    RosterExchangeListener rosterExchangeListener =
        new RosterExchangeListener() {
          public void entriesReceived(String from, Iterator remoteRosterEntries) {
            int received = 0;
            assertNotNull("From is null", from);
            assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
            assertTrue("Roster without entries", remoteRosterEntries.hasNext());
            while (remoteRosterEntries.hasNext()) {
              received++;
              try {
                RemoteRosterEntry remoteRosterEntry =
                    (RemoteRosterEntry) remoteRosterEntries.next();
                getConnection(1)
                    .getRoster()
                    .createEntry(
                        remoteRosterEntry.getUser(),
                        remoteRosterEntry.getName(),
                        remoteRosterEntry.getGroupArrayNames());
              } catch (Exception e) {
                fail(e.toString());
              }
            }
            entriesReceived = received;
          }
        };
    rosterExchangeManager2.addRosterListener(rosterExchangeListener);

    // Send user1's roster to user2
    try {
      entriesSent = getConnection(0).getRoster().getEntryCount();
      entriesReceived = 0;
      rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
      // Wait up to 2 seconds
      long initial = System.currentTimeMillis();
      while (System.currentTimeMillis() - initial < 2000 && (entriesSent != entriesReceived)) {
        Thread.sleep(100);
      }
    } catch (Exception e) {
      fail("An error occured sending the message with the roster");
    }
    assertEquals(
        "Number of sent and received entries does not match", entriesSent, entriesReceived);
    assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
  }