1 20 21 package org.jivesoftware.smackx; 22 23 import org.jivesoftware.smack.Chat; 24 import org.jivesoftware.smack.PacketCollector; 25 import org.jivesoftware.smack.XMPPException; 26 import org.jivesoftware.smack.filter.MessageTypeFilter; 27 import org.jivesoftware.smack.packet.Message; 28 import org.jivesoftware.smack.packet.Presence; 29 import org.jivesoftware.smack.test.SmackTestCase; 30 import org.jivesoftware.smackx.packet.OfflineMessageInfo; 31 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 42 public class OfflineMessageManagerTest extends SmackTestCase { 43 44 public OfflineMessageManagerTest(String arg0) { 45 super(arg0); 46 } 47 48 public void testDiscoverFlexibleRetrievalSupport() throws XMPPException { 49 OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1)); 50 assertTrue("Server does not support JEP-13", offlineManager.supportsFlexibleRetrieval()); 51 } 52 53 59 public void testReadAndDelete() { 60 getConnection(1).sendPacket(new Presence(Presence.Type.UNAVAILABLE)); 62 63 try { 64 Thread.sleep(500); 65 66 Chat chat = getConnection(0).createChat(getBareJID(1)); 68 chat.sendMessage("Test 1"); 69 chat.sendMessage("Test 2"); 70 71 Thread.sleep(500); 72 73 OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1)); 75 assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount()); 76 Iterator headers = offlineManager.getHeaders(); 78 assertTrue("No message header was found", headers.hasNext()); 79 List stamps = new ArrayList (); 80 while (headers.hasNext()) { 81 OfflineMessageHeader header = (OfflineMessageHeader) headers.next(); 82 assertEquals("Incorrect sender", getFullJID(0), header.getJid()); 83 assertNotNull("No stamp was found in the message header", header.getStamp()); 84 stamps.add(header.getStamp()); 85 } 86 assertEquals("Wrong number of headers", 2, stamps.size()); 87 Iterator messages = offlineManager.getMessages(stamps); 89 assertTrue("No message was found", messages.hasNext()); 90 stamps = new ArrayList (); 91 while (messages.hasNext()) { 92 Message message = (Message) messages.next(); 93 OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", 94 "http://jabber.org/protocol/offline"); 95 assertNotNull("No offline information was included in the offline message", info); 96 assertNotNull("No stamp was found in the message header", info.getNode()); 97 stamps.add(info.getNode()); 98 } 99 assertEquals("Wrong number of messages", 2, stamps.size()); 100 assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount()); 102 103 PacketCollector collector = getConnection(1).createPacketCollector( 105 new MessageTypeFilter(Message.Type.CHAT)); 106 getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE)); 107 108 Message message = (Message) collector.nextResult(2500); 110 assertNull("An offline message was sent from the server", message); 111 112 offlineManager.deleteMessages(stamps); 114 assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount()); 116 117 } catch (Exception e) { 118 e.printStackTrace(); 119 fail(e.getMessage()); 120 } 121 } 122 123 128 public void testFetchAndPurge() { 129 getConnection(1).sendPacket(new Presence(Presence.Type.UNAVAILABLE)); 131 132 try { 133 Thread.sleep(500); 134 135 Chat chat = getConnection(0).createChat(getBareJID(1)); 137 chat.sendMessage("Test 1"); 138 chat.sendMessage("Test 2"); 139 140 Thread.sleep(500); 141 142 OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1)); 144 assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount()); 145 Iterator messages = offlineManager.getMessages(); 147 assertTrue("No message was found", messages.hasNext()); 148 List stamps = new ArrayList (); 149 while (messages.hasNext()) { 150 Message message = (Message) messages.next(); 151 OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", 152 "http://jabber.org/protocol/offline"); 153 assertNotNull("No offline information was included in the offline message", info); 154 assertNotNull("No stamp was found in the message header", info.getNode()); 155 stamps.add(info.getNode()); 156 } 157 assertEquals("Wrong number of messages", 2, stamps.size()); 158 assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount()); 160 161 PacketCollector collector = getConnection(1).createPacketCollector( 163 new MessageTypeFilter(Message.Type.CHAT)); 164 getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE)); 165 166 Message message = (Message) collector.nextResult(2500); 168 assertNull("An offline message was sent from the server", message); 169 170 offlineManager.deleteMessages(); 172 assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount()); 174 175 } catch (Exception e) { 176 e.printStackTrace(); 177 fail(e.getMessage()); 178 } 179 } 180 181 protected int getMaxConnections() { 182 return 2; 183 } 184 } 185 | Popular Tags |