KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile$
3  * $Revision: 2502 $
4  * $Date: 2005-06-17 19:24:33 -0300 (Fri, 17 Jun 2005) $
5  *
6  * Copyright (C) 2002-2003 Jive Software. All rights reserved.
7  * ====================================================================
8  * The Jive Software License (based on Apache Software License, Version 1.1)
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by
25  * Jive Software (http://www.jivesoftware.com)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Smack" and "Jive Software" must not be used to
30  * endorse or promote products derived from this software without
31  * prior written permission. For written permission, please
32  * contact webmaster@jivesoftware.com.
33  *
34  * 5. Products derived from this software may not be called "Smack",
35  * nor may "Smack" appear in their name, without prior written
36  * permission of Jive Software.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  */

52
53 package org.jivesoftware.smackx;
54
55 import java.util.ArrayList JavaDoc;
56
57 import org.jivesoftware.smack.*;
58 import org.jivesoftware.smack.packet.*;
59 import org.jivesoftware.smack.test.SmackTestCase;
60
61 /**
62  *
63  * Test the MessageEvent extension using the high level API.
64  *
65  * @author Gaston Dombiak
66  */

67 public class MessageEventManagerTest extends SmackTestCase {
68
69     /**
70      * Constructor for MessageEventManagerTest.
71      * @param name
72      */

73     public MessageEventManagerTest(String JavaDoc name) {
74         super(name);
75     }
76
77     /**
78      * High level API test.
79      * This is a simple test to use with a XMPP client and check if the client receives the
80      * message
81      * 1. User_1 will send a message to user_2 requesting to be notified when any of these events
82      * occurs: offline, composing, displayed or delivered
83      */

84     public void testSendMessageEventRequest() {
85         // Create a chat for each connection
86
Chat chat1 = getConnection(0).createChat(getBareJID(1));
87
88         // Create the message to send with the roster
89
Message msg = chat1.createMessage();
90         msg.setSubject("Any subject you want");
91         msg.setBody("An interesting body comes here...");
92         // Add to the message all the notifications requests (offline, delivered, displayed,
93
// composing)
94
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
95
96         // Send the message that contains the notifications request
97
try {
98             chat1.sendMessage(msg);
99         } catch (Exception JavaDoc e) {
100             fail("An error occured sending the message");
101         }
102     }
103
104     /**
105      * High level API test.
106      * This is a simple test to use with a XMPP client, check if the client receives the
107      * message and display in the console any notification
108      * 1. User_1 will send a message to user_2 requesting to be notified when any of these events
109      * occurs: offline, composing, displayed or delivered
110      * 2. User_2 will use a XMPP client (like Exodus) to display the message and compose a reply
111      * 3. User_1 will display any notification that receives
112      */

113     public void testSendMessageEventRequestAndDisplayNotifications() {
114         // Create a chat for each connection
115
Chat chat1 = getConnection(0).createChat(getBareJID(1));
116
117         MessageEventManager messageEventManager = new MessageEventManager(getConnection(0));
118         messageEventManager
119             .addMessageEventNotificationListener(new MessageEventNotificationListener() {
120             public void deliveredNotification(String JavaDoc from, String JavaDoc packetID) {
121                 System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)");
122             }
123
124             public void displayedNotification(String JavaDoc from, String JavaDoc packetID) {
125                 System.out.println("From: " + from + " PacketID: " + packetID + "(displayed)");
126             }
127
128             public void composingNotification(String JavaDoc from, String JavaDoc packetID) {
129                 System.out.println("From: " + from + " PacketID: " + packetID + "(composing)");
130             }
131
132             public void offlineNotification(String JavaDoc from, String JavaDoc packetID) {
133                 System.out.println("From: " + from + " PacketID: " + packetID + "(offline)");
134             }
135
136             public void cancelledNotification(String JavaDoc from, String JavaDoc packetID) {
137                 System.out.println("From: " + from + " PacketID: " + packetID + "(cancelled)");
138             }
139         });
140
141         // Create the message to send with the roster
142
Message msg = chat1.createMessage();
143         msg.setSubject("Any subject you want");
144         msg.setBody("An interesting body comes here...");
145         // Add to the message all the notifications requests (offline, delivered, displayed,
146
// composing)
147
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
148
149         // Send the message that contains the notifications request
150
try {
151             chat1.sendMessage(msg);
152             // Wait a few seconds so that the XMPP client can send any event
153
Thread.sleep(200);
154         } catch (Exception JavaDoc e) {
155             fail("An error occured sending the message");
156         }
157     }
158
159     /**
160      * High level API test.
161      * 1. User_1 will send a message to user_2 requesting to be notified when any of these events
162      * occurs: offline, composing, displayed or delivered
163      * 2. User_2 will receive the message
164      * 3. User_2 will simulate that the message was displayed
165      * 4. User_2 will simulate that he/she is composing a reply
166      * 5. User_2 will simulate that he/she has cancelled the reply
167      */

168     public void testRequestsAndNotifications() {
169         final ArrayList JavaDoc results = new ArrayList JavaDoc();
170         ArrayList JavaDoc resultsExpected = new ArrayList JavaDoc();
171         resultsExpected.add("deliveredNotificationRequested");
172         resultsExpected.add("composingNotificationRequested");
173         resultsExpected.add("displayedNotificationRequested");
174         resultsExpected.add("offlineNotificationRequested");
175         resultsExpected.add("deliveredNotification");
176         resultsExpected.add("displayedNotification");
177         resultsExpected.add("composingNotification");
178         resultsExpected.add("cancelledNotification");
179
180         // Create a chat for each connection
181
Chat chat1 = getConnection(0).createChat(getBareJID(1));
182
183         MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0));
184         messageEventManager1
185             .addMessageEventNotificationListener(new MessageEventNotificationListener() {
186             public void deliveredNotification(String JavaDoc from, String JavaDoc packetID) {
187                 results.add("deliveredNotification");
188             }
189
190             public void displayedNotification(String JavaDoc from, String JavaDoc packetID) {
191                 results.add("displayedNotification");
192             }
193
194             public void composingNotification(String JavaDoc from, String JavaDoc packetID) {
195                 results.add("composingNotification");
196             }
197
198             public void offlineNotification(String JavaDoc from, String JavaDoc packetID) {
199                 results.add("offlineNotification");
200             }
201
202             public void cancelledNotification(String JavaDoc from, String JavaDoc packetID) {
203                 results.add("cancelledNotification");
204             }
205         });
206
207         MessageEventManager messageEventManager2 = new MessageEventManager(getConnection(1));
208         messageEventManager2
209             .addMessageEventRequestListener(new DefaultMessageEventRequestListener() {
210             public void deliveredNotificationRequested(
211                 String JavaDoc from,
212                 String JavaDoc packetID,
213                 MessageEventManager messageEventManager) {
214                 super.deliveredNotificationRequested(from, packetID, messageEventManager);
215                 results.add("deliveredNotificationRequested");
216             }
217
218             public void displayedNotificationRequested(
219                 String JavaDoc from,
220                 String JavaDoc packetID,
221                 MessageEventManager messageEventManager) {
222                 super.displayedNotificationRequested(from, packetID, messageEventManager);
223                 results.add("displayedNotificationRequested");
224             }
225
226             public void composingNotificationRequested(
227                 String JavaDoc from,
228                 String JavaDoc packetID,
229                 MessageEventManager messageEventManager) {
230                 super.composingNotificationRequested(from, packetID, messageEventManager);
231                 results.add("composingNotificationRequested");
232             }
233
234             public void offlineNotificationRequested(
235                 String JavaDoc from,
236                 String JavaDoc packetID,
237                 MessageEventManager messageEventManager) {
238                 super.offlineNotificationRequested(from, packetID, messageEventManager);
239                 results.add("offlineNotificationRequested");
240             }
241         });
242
243         // Create the message to send with the roster
244
Message msg = chat1.createMessage();
245         msg.setSubject("Any subject you want");
246         msg.setBody("An interesting body comes here...");
247         // Add to the message all the notifications requests (offline, delivered, displayed,
248
// composing)
249
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
250
251         // Send the message that contains the notifications request
252
try {
253             chat1.sendMessage(msg);
254             messageEventManager2.sendDisplayedNotification(getBareJID(0), msg.getPacketID());
255             messageEventManager2.sendComposingNotification(getBareJID(0), msg.getPacketID());
256             messageEventManager2.sendCancelledNotification(getBareJID(0), msg.getPacketID());
257             // Wait up to 2 seconds
258
long initial = System.currentTimeMillis();
259             while (System.currentTimeMillis() - initial < 2000 &&
260                     (!results.containsAll(resultsExpected))) {
261                 Thread.sleep(100);
262             }
263             assertTrue(
264                 "Test failed due to bad results (1)" + resultsExpected,
265                 resultsExpected.containsAll(results));
266             assertTrue(
267                 "Test failed due to bad results (2)" + results,
268                 results.containsAll(resultsExpected));
269
270         } catch (Exception JavaDoc e) {
271             fail("An error occured sending the message");
272         }
273     }
274
275     protected int getMaxConnections() {
276         return 2;
277     }
278 }
279
Popular Tags