Search in sources :

Example 61 with Message

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

the class DeliveryReceiptTest method receiptManagerListenerTest.

@Test
public void receiptManagerListenerTest() throws Exception {
    DummyConnection c = new DummyConnection();
    c.connect();
    DeliveryReceiptManager drm = DeliveryReceiptManager.getInstanceFor(c);
    TestReceiptReceivedListener rrl = new TestReceiptReceivedListener();
    drm.addReceiptReceivedListener(rrl);
    Message m = new Message(JidCreate.from("romeo@montague.com"), Message.Type.normal);
    m.setFrom(JidCreate.from("julia@capulet.com"));
    m.setStanzaId("reply-id");
    m.addExtension(new DeliveryReceipt("original-test-id"));
    c.processStanza(m);
    rrl.waitUntilInvocationOrTimeout();
}
Also used : Message(org.jivesoftware.smack.packet.Message) DummyConnection(org.jivesoftware.smack.DummyConnection) Test(org.junit.Test)

Example 62 with Message

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

the class HeadersProviderTest method headersInMessageTest.

@Test
public void headersInMessageTest() throws Exception {
    // @formatter:off
    final String messageStanza = "<message from='romeo@shakespeare.lit/orchard' to='juliet@capulet.com' type='chat'>" + "<body>Wherefore are thou?!?</body>" + "<headers xmlns='http://jabber.org/protocol/shim'>" + "<header name='Urgency'>high</header>" + "</headers>" + "</message>";
    // @formatter:on
    XmlPullParser parser = TestUtils.getMessageParser(messageStanza);
    Message message = PacketParserUtils.parseMessage(parser);
    HeadersExtension headers = HeadersExtension.from(message);
    Header header = headers.getHeaders().get(0);
    assertEquals("Urgency", header.getName());
    assertEquals("high", header.getValue());
}
Also used : HeadersExtension(org.jivesoftware.smackx.shim.packet.HeadersExtension) Message(org.jivesoftware.smack.packet.Message) Header(org.jivesoftware.smackx.shim.packet.Header) XmlPullParser(org.xmlpull.v1.XmlPullParser) Test(org.junit.Test)

Example 63 with Message

use of org.jivesoftware.smack.packet.Message 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 64 with Message

use of org.jivesoftware.smack.packet.Message 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)

Example 65 with Message

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

the class MessageTest method testOfflineStorageWithNegativePriority.

/**
     * User0 is connected from 1 resource with a negative priority presence. User1
     * sends a message to the bare JID of User0. Messages should be stored offline.
     * User0 then changes the priority presence to a positive value. Check that
     * offline messages were delivered to the user.
     *
     * @throws Exception if an error occurs.
     */
public void testOfflineStorageWithNegativePriority() throws Exception {
    // Set this connection with negative priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(-1);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen for incoming traffic
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // User1 sends a message to the bare JID of User0
    Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
    chat.sendMessage("Test 1");
    chat.sendMessage("Test 2");
    // Check that messages were sent to resource with highest priority
    Message message = (Message) collector.nextResult(2000);
    assertNull("Messages were not stored offline", message);
    // Set this connection with positive priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(1);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    message = (Message) collector.nextResult(2000);
    assertNotNull("Offline messages were not delivered", message);
    assertEquals("Test 1", message.getBody());
    message = (Message) collector.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Message (org.jivesoftware.smack.packet.Message)166 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)21 XMPPException (org.jivesoftware.smack.XMPPException)15 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 NetworkException (com.xabber.android.data.NetworkException)13 Stanza (org.jivesoftware.smack.packet.Stanza)13 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)13 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)12 AccountItem (com.xabber.android.data.account.AccountItem)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)11 Date (java.util.Date)10 Jid (org.jxmpp.jid.Jid)10 Chat (org.jivesoftware.smack.Chat)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 ArrayList (java.util.ArrayList)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 InputStream (java.io.InputStream)6 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)6