Search in sources :

Example 41 with Message

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

the class LastActivityManagerTest method testOnline.

/**
	 * This is a test to check if a LastActivity request for idle time is
	 * answered and correct.
	 */
public void testOnline() {
    TCPConnection conn0 = getConnection(0);
    TCPConnection conn1 = getConnection(1);
    // Send a message as the last activity action from connection 1 to
    // connection 0
    conn1.sendStanza(new Message(getBareJID(0)));
    // Wait 1 seconds to have some idle time
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail("Thread sleep interrupted");
    }
    LastActivity lastActivity = null;
    try {
        lastActivity = LastActivityManager.getLastActivity(conn0, getFullJID(1));
    } catch (XMPPException e) {
        e.printStackTrace();
        fail("An error occurred requesting the Last Activity");
    }
    // Asserts that the last activity packet was received
    assertNotNull("No last activity packet", lastActivity);
    // Asserts that there is at least a 1 second of idle time
    assertTrue("The last activity idle time is less than expected: " + lastActivity.getIdleTime(), lastActivity.getIdleTime() >= 1);
}
Also used : Message(org.jivesoftware.smack.packet.Message) TCPConnection(org.jivesoftware.smack.TCPConnection) XMPPException(org.jivesoftware.smack.XMPPException) LastActivity(org.jivesoftware.smackx.packet.LastActivity)

Example 42 with Message

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

the class MultipleRecipientManagerTest method testReplying.

/**
     * Ensures that replying to packets is ok.
     */
public void testReplying() throws XMPPException {
    StanzaCollector collector0 = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    // Send the intial message with multiple recipients
    Message message = new Message();
    message.setBody("Hola");
    List<String> to = Arrays.asList(new String[] { getBareJID(1) });
    List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
    List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
    MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
    // Get the message and ensure it's ok
    Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the message", message1);
    MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
    assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
    assertFalse("Message 1 should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Prepare and send the reply
    Message reply1 = new Message();
    reply1.setBody("This is my reply");
    MultipleRecipientManager.reply(getConnection(1), message1, reply1);
    // Get the reply and ensure it's ok
    reply1 = (Message) collector0.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 0 never received the reply", reply1);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply1);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Send a reply to the reply
    Message reply2 = new Message();
    reply2.setBody("This is my reply to your reply");
    reply2.setFrom(getBareJID(0));
    MultipleRecipientManager.reply(getConnection(0), reply1, reply2);
    // Get the reply and ensure it's ok
    reply2 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the reply", reply2);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply2);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Check that connection2 recevied 3 messages
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 2 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 3 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 4 messages", message1);
    // Check that connection3 recevied only 1 message (was BCC in the first message)
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 2 messages", message1);
    collector0.cancel();
    collector1.cancel();
    collector2.cancel();
    collector3.cancel();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 43 with Message

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

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

the class MamResultProviderTest method checkMamResultProvider.

@Test
public void checkMamResultProvider() throws Exception {
    XmlPullParser parser = PacketParserUtils.getParserFor(exampleMamResultXml);
    MamResultExtension mamResultExtension = new MamResultProvider().parse(parser);
    Assert.assertEquals(mamResultExtension.getQueryId(), "f27");
    Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623");
    GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = calendar.getTime();
    Forwarded forwarded = mamResultExtension.getForwarded();
    Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date);
    Message message = (Message) forwarded.getForwardedStanza();
    Assert.assertEquals(message.getFrom(), "romeo@montague.lit/orchard");
    Assert.assertEquals(message.getTo(), "juliet@capulet.lit/balcony");
    Assert.assertEquals(message.getBody(), "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.");
}
Also used : MamResultProvider(org.jivesoftware.smackx.mam.provider.MamResultProvider) Message(org.jivesoftware.smack.packet.Message) XmlPullParser(org.xmlpull.v1.XmlPullParser) GregorianCalendar(java.util.GregorianCalendar) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) MamResultExtension(org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension) Date(java.util.Date) Test(org.junit.Test)

Example 45 with Message

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

the class MamResultProviderTest method checkResultsParse.

@Test
public void checkResultsParse() throws Exception {
    Message message = (Message) PacketParserUtils.parseStanza(exampleResultMessage);
    MamResultExtension mamResultExtension = MamResultExtension.from(message);
    Assert.assertEquals(mamResultExtension.getQueryId(), "f27");
    Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623");
    GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = calendar.getTime();
    Forwarded forwarded = mamResultExtension.getForwarded();
    Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date);
    Message forwardedMessage = (Message) forwarded.getForwardedStanza();
    Assert.assertEquals(forwardedMessage.getFrom(), "witch@shakespeare.lit");
    Assert.assertEquals(forwardedMessage.getTo(), "macbeth@shakespeare.lit");
    Assert.assertEquals(forwardedMessage.getBody(), "Hail to thee");
}
Also used : Message(org.jivesoftware.smack.packet.Message) GregorianCalendar(java.util.GregorianCalendar) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) MamResultExtension(org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension) Date(java.util.Date) Test(org.junit.Test)

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