KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > OfflineMessageManagerTest


1 /**
2  * $RCSfile$
3  * $Revision: 2512 $
4  * $Date: 2005-07-26 01:57:36 -0300 (Tue, 26 Jul 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

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 JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * Tests handling of offline messaging using OfflineMessageManager. This server requires the
38  * server to support JEP-0013: Flexible Offline Message Retrieval.
39  *
40  * @author Gaston Dombiak
41  */

42 public class OfflineMessageManagerTest extends SmackTestCase {
43
44     public OfflineMessageManagerTest(String JavaDoc 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     /**
54      * While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
55      * performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
56      * retriving the headers, then the real messages of the headers and finally removing the
57      * loaded messages.
58      */

59     public void testReadAndDelete() {
60         // Make user2 unavailable
61
getConnection(1).sendPacket(new Presence(Presence.Type.UNAVAILABLE));
62
63         try {
64             Thread.sleep(500);
65
66             // User1 sends some messages to User2 which is not available at the moment
67
Chat chat = getConnection(0).createChat(getBareJID(1));
68             chat.sendMessage("Test 1");
69             chat.sendMessage("Test 2");
70
71             Thread.sleep(500);
72
73             // User2 checks the number of offline messages
74
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
75             assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
76             // Check the message headers
77
Iterator JavaDoc headers = offlineManager.getHeaders();
78             assertTrue("No message header was found", headers.hasNext());
79             List JavaDoc stamps = new ArrayList JavaDoc();
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             // Get the offline messages
88
Iterator JavaDoc messages = offlineManager.getMessages(stamps);
89             assertTrue("No message was found", messages.hasNext());
90             stamps = new ArrayList JavaDoc();
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             // Check that the offline messages have not been deleted
101
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
102
103             // User2 becomes available again
104
PacketCollector collector = getConnection(1).createPacketCollector(
105                     new MessageTypeFilter(Message.Type.CHAT));
106             getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE));
107
108             // Check that no offline messages was sent to the user
109
Message message = (Message) collector.nextResult(2500);
110             assertNull("An offline message was sent from the server", message);
111
112             // Delete the retrieved offline messages
113
offlineManager.deleteMessages(stamps);
114             // Check that there are no offline message for this user
115
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
116
117         } catch (Exception JavaDoc e) {
118             e.printStackTrace();
119             fail(e.getMessage());
120         }
121     }
122
123     /**
124      * While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
125      * performs some "Flexible Offline Message Retrieval" by fetching all the offline messages
126      * and then removing all the offline messages.
127      */

128     public void testFetchAndPurge() {
129         // Make user2 unavailable
130
getConnection(1).sendPacket(new Presence(Presence.Type.UNAVAILABLE));
131
132         try {
133             Thread.sleep(500);
134
135             // User1 sends some messages to User2 which is not available at the moment
136
Chat chat = getConnection(0).createChat(getBareJID(1));
137             chat.sendMessage("Test 1");
138             chat.sendMessage("Test 2");
139
140             Thread.sleep(500);
141
142             // User2 checks the number of offline messages
143
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
144             assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
145             // Get all offline messages
146
Iterator JavaDoc messages = offlineManager.getMessages();
147             assertTrue("No message was found", messages.hasNext());
148             List JavaDoc stamps = new ArrayList JavaDoc();
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             // Check that the offline messages have not been deleted
159
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
160
161             // User2 becomes available again
162
PacketCollector collector = getConnection(1).createPacketCollector(
163                     new MessageTypeFilter(Message.Type.CHAT));
164             getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE));
165
166             // Check that no offline messages was sent to the user
167
Message message = (Message) collector.nextResult(2500);
168             assertNull("An offline message was sent from the server", message);
169
170             // Delete all offline messages
171
offlineManager.deleteMessages();
172             // Check that there are no offline message for this user
173
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
174
175         } catch (Exception JavaDoc e) {
176             e.printStackTrace();
177             fail(e.getMessage());
178         }
179     }
180
181     protected int getMaxConnections() {
182         return 2;
183     }
184 }
185
Popular Tags