Java Code Examples for org.jivesoftware.smack.roster.packet.RosterPacket#setStanzaId()

The following examples show how to use org.jivesoftware.smack.roster.packet.RosterPacket#setStanzaId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RosterVersioningTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a non-empty roster result empties the store.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws XmppStringprepException if the provided string is invalid.
 */
@Test(timeout = 5000)
public void testOtherVersionStored() throws XMPPException, SmackException, XmppStringprepException {
    Item vaglafItem = vaglafItem();

    // We expect that the roster request is the only packet sent. This is not part of the specification,
    // but a shortcut in the test implementation.
    Stanza sentPacket = connection.getSentPacket();
    if (sentPacket instanceof RosterPacket) {
        RosterPacket sentRP = (RosterPacket) sentPacket;
        RosterPacket answer = new RosterPacket();
        answer.setStanzaId(sentRP.getStanzaId());
        answer.setType(Type.result);
        answer.setTo(sentRP.getFrom());

        answer.setVersion("newVersion");
        answer.addRosterItem(vaglafItem);

        rosterListener.reset();
        connection.processStanza(answer);
        rosterListener.waitUntilInvocationOrTimeout();
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }

    Roster roster = Roster.getInstanceFor(connection);
    assertEquals("Size of roster", 1, roster.getEntries().size());
    RosterEntry entry = roster.getEntry(vaglafItem.getJid());
    assertNotNull("Roster contains vaglaf entry", entry);
    assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry));

    RosterStore store = roster.getRosterStore();
    assertEquals("Size of store", 1, store.getEntries().size());
    Item item = store.getEntry(vaglafItem.getJid());
    assertNotNull("Store contains vaglaf entry", item);
    assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item);
}
 
Example 2
Source File: RosterTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the roster according to the example in
 * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-login"
 *     >RFC3921: Retrieving One's Roster on Login</a>.
 *
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XmppStringprepException if the provided string is invalid.
 */
private void initRoster() throws InterruptedException, SmackException, XmppStringprepException {
    roster.reload();
    while (true) {
        final Stanza sentPacket = connection.getSentPacket();
        if (sentPacket instanceof RosterPacket && ((IQ) sentPacket).getType() == Type.get) {
            // setup the roster get request
            final RosterPacket rosterRequest = (RosterPacket) sentPacket;
            assertSame("The <query/> element MUST NOT contain any <item/> child elements!",
                    0,
                    rosterRequest.getRosterItemCount());

            // prepare the roster result
            final RosterPacket rosterResult = new RosterPacket();
            rosterResult.setTo(connection.getUser());
            rosterResult.setType(Type.result);
            rosterResult.setStanzaId(rosterRequest.getStanzaId());

            // prepare romeo's roster entry
            final Item romeo = new Item(JidCreate.entityBareFrom("[email protected]"), "Romeo");
            romeo.addGroupName("Friends");
            romeo.setItemType(ItemType.both);
            rosterResult.addRosterItem(romeo);

            // prepare mercutio's roster entry
            final Item mercutio = new Item(JidCreate.entityBareFrom("[email protected]"), "Mercutio");
            mercutio.setItemType(ItemType.from);
            rosterResult.addRosterItem(mercutio);

            // prepare benvolio's roster entry
            final Item benvolio = new Item(JidCreate.entityBareFrom("[email protected]"), "Benvolio");
            benvolio.setItemType(ItemType.both);
            rosterResult.addRosterItem(benvolio);

            // simulate receiving the roster result and exit the loop
            connection.processStanza(rosterResult);
            break;
        }
    }
    roster.waitUntilLoaded();
    rosterListener.waitUntilInvocationOrTimeout();
}