Search in sources :

Example 16 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MultiUserChatTest method testParticipantPresence.

public void testParticipantPresence() {
    try {
        // User2 joins the new room
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        muc2.join("testbot2");
        Thread.sleep(400);
        // User1 checks the presence of user2 in the room
        Presence presence = muc.getOccupantPresence(room + "/testbot2");
        assertNotNull("Presence of user2 in room is missing", presence);
        assertTrue("Presence mode of user2 is wrong", presence.getMode() == null || presence.getMode() == Presence.Mode.available);
        // User2 changes his availability to AWAY
        muc2.changeAvailabilityStatus("Gone to have lunch", Presence.Mode.away);
        Thread.sleep(200);
        // User1 checks the presence of user2 in the room
        presence = muc.getOccupantPresence(room + "/testbot2");
        assertNotNull("Presence of user2 in room is missing", presence);
        assertEquals("Presence mode of user2 is wrong", Presence.Mode.away, presence.getMode());
        assertEquals("Presence status of user2 is wrong", "Gone to have lunch", presence.getStatus());
        // User2 changes his nickname
        muc2.changeNickname("testbotII");
        Thread.sleep(200);
        // User1 checks the presence of user2 in the room
        presence = muc.getOccupantPresence(room + "/testbot2");
        assertNull("Presence of participant testbot2 still exists", presence);
        presence = muc.getOccupantPresence(room + "/testbotII");
        assertNotNull("Presence of participant testbotII does not exist", presence);
        assertEquals("Presence of participant testbotII has a wrong from", room + "/testbotII", presence.getFrom());
        // User2 leaves the room
        muc2.leave();
        Thread.sleep(250);
        // User1 checks the presence of user2 in the room
        presence = muc.getOccupantPresence(room + "/testbotII");
        assertNull("Presence of participant testbotII still exists", presence);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) XMPPException(org.jivesoftware.smack.XMPPException)

Example 17 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MultiUserChatTest method testAnonymousParticipant.

public void testAnonymousParticipant() {
    try {
        // Anonymous user joins the new room
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
        XMPPTCPConnection anonConnection = new XMPPConnection(connectionConfiguration);
        anonConnection.connect();
        anonConnection.loginAnonymously();
        MultiUserChat muc2 = new MultiUserChat(anonConnection, room);
        muc2.join("testbot2");
        Thread.sleep(400);
        // User1 checks the presence of Anonymous user in the room
        Presence presence = muc.getOccupantPresence(room + "/testbot2");
        assertNotNull("Presence of user2 in room is missing", presence);
        assertTrue("Presence mode of user2 is wrong", presence.getMode() == null || presence.getMode() == Presence.Mode.available);
        // Anonymous user leaves the room
        muc2.leave();
        anonConnection.disconnect();
        Thread.sleep(250);
        // User1 checks the presence of Anonymous user in the room
        presence = muc.getOccupantPresence(room + "/testbot2");
        assertNull("Presence of participant testbotII still exists", presence);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 18 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class OfflineMessageManagerTest method testReadAndDelete.

/**
     * While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
     * performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
     * retriving the headers, then the real messages of the headers and finally removing the
     * loaded messages.
     */
public void testReadAndDelete() {
    // Make user2 unavailable
    getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
    try {
        Thread.sleep(500);
        // User1 sends some messages to User2 which is not available at the moment
        Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
        chat.sendMessage("Test 1");
        chat.sendMessage("Test 2");
        Thread.sleep(500);
        // User2 checks the number of offline messages
        OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
        assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
        // Check the message headers
        Iterator<OfflineMessageHeader> headers = offlineManager.getHeaders();
        assertTrue("No message header was found", headers.hasNext());
        List<String> stamps = new ArrayList<String>();
        while (headers.hasNext()) {
            OfflineMessageHeader header = headers.next();
            assertEquals("Incorrect sender", getFullJID(0), header.getJid());
            assertNotNull("No stamp was found in the message header", header.getStamp());
            stamps.add(header.getStamp());
        }
        assertEquals("Wrong number of headers", 2, stamps.size());
        // Get the offline messages
        Iterator<Message> messages = offlineManager.getMessages(stamps);
        assertTrue("No message was found", messages.hasNext());
        stamps = new ArrayList<String>();
        while (messages.hasNext()) {
            Message message = messages.next();
            OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
            assertNotNull("No offline information was included in the offline message", info);
            assertNotNull("No stamp was found in the message header", info.getNode());
            stamps.add(info.getNode());
        }
        assertEquals("Wrong number of messages", 2, stamps.size());
        // Check that the offline messages have not been deleted
        assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
        // User2 becomes available again
        StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
        getConnection(1).sendStanza(new Presence(Presence.Type.available));
        // Check that no offline messages was sent to the user
        Message message = (Message) collector.nextResult(2500);
        assertNull("An offline message was sent from the server", message);
        // Delete the retrieved offline messages
        offlineManager.deleteMessages(stamps);
        // Check that there are no offline message for this user
        assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) ArrayList(java.util.ArrayList) XMPPException(org.jivesoftware.smack.XMPPException) MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Chat(org.jivesoftware.smack.Chat) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 19 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MessageTest method testOfflineMessage.

/**
     * Check that when a client becomes unavailable all messages sent to the client are stored offline. So that when
     * the client becomes available again the offline messages are received.
     */
public void testOfflineMessage() {
    getConnection(0).sendStanza(new Presence(Presence.Type.available));
    getConnection(1).sendStanza(new Presence(Presence.Type.available));
    // Make user2 unavailable
    getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
    try {
        Thread.sleep(500);
        // User1 sends some messages to User2 which is not available at the moment
        Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
        StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
        chat.sendMessage("Test 1");
        chat.sendMessage("Test 2");
        Thread.sleep(500);
        // User2 becomes available again
        getConnection(1).sendStanza(new Presence(Presence.Type.available));
        // Check that offline messages are retrieved by user2 which is now available
        Message message = (Message) collector.nextResult(2500);
        assertNotNull(message);
        message = (Message) collector.nextResult(2000);
        assertNotNull(message);
        message = (Message) collector.nextResult(1000);
        assertNull(message);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 20 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MessageTest method testHugeMessage.

/**
     * Send messages with invalid XML characters to offline users. Check that offline users
     * are receiving them from the server.<p>
     *
     * Test case commented out since some servers may just close the connection while others
     * are more tolerant and accept stanzas with invalid XML characters.
     */
/*public void testOfflineMessageInvalidXML() {
        // Make user2 unavailable
        getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));

        try {
            Thread.sleep(500);

            // User1 sends some messages to User2 which is not available at the moment
            Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
            StanzaCollector collector = getConnection(1).createStanzaCollector(
                    new MessageTypeFilter(Message.Type.chat));
            chat.sendMessage("Test \f 1");
            chat.sendMessage("Test \r 1");

            Thread.sleep(500);

            // User2 becomes available again

            getConnection(1).sendStanza(new Presence(Presence.Type.available));

            // Check that offline messages are retrieved by user2 which is now available
            Message message = (Message) collector.nextResult(2500);
            assertNotNull(message);
            message = (Message) collector.nextResult(2000);
            assertNotNull(message);
            message = (Message) collector.nextResult(1000);
            assertNull(message);

        }
        catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }*/
/**
     * Check that two clients are able to send messages with a body of 4K characters and their
     * connections are not being closed.
     */
public void testHugeMessage() {
    getConnection(0).sendStanza(new Presence(Presence.Type.available));
    getConnection(1).sendStanza(new Presence(Presence.Type.available));
    // User2 becomes available again
    StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // Create message with a body of 4K characters
    Message msg = new Message(getFullJID(1), Message.Type.chat);
    StringBuilder sb = new StringBuilder(5000);
    for (int i = 0; i <= 4000; i++) {
        sb.append('X');
    }
    msg.setBody(sb.toString());
    // Send the first message
    getConnection(0).sendStanza(msg);
    // Check that the connection that sent the message is still connected
    assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
    // Check that the message was received
    Message rcv = (Message) collector.nextResult(1000);
    assertNotNull("No Message was received", rcv);
    // Send the second message
    getConnection(0).sendStanza(msg);
    // Check that the connection that sent the message is still connected
    assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
    // Check that the second message was received
    rcv = (Message) collector.nextResult(1000);
    assertNotNull("No Message was received", rcv);
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)89 Message (org.jivesoftware.smack.packet.Message)17 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 Jid (org.jxmpp.jid.Jid)7 Resourcepart (org.jxmpp.jid.parts.Resourcepart)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 BareJid (org.jxmpp.jid.BareJid)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 WaitFor (jetbrains.communicator.util.WaitFor)4 AccountItem (com.xabber.android.data.account.AccountItem)3 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 XMPPException (org.jivesoftware.smack.XMPPException)3