KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > packet > MessageEventTest


1 /**
2  * $RCSfile$
3  * $Revision: 2339 $
4  * $Date: 2004-07-12 10:36:13 -0300 (Mon, 12 Jul 2004) $
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.packet;
54
55 import org.jivesoftware.smack.*;
56 import org.jivesoftware.smack.filter.*;
57 import org.jivesoftware.smack.packet.*;
58 import org.jivesoftware.smack.test.SmackTestCase;
59
60 /**
61  *
62  * Test the MessageEvent extension using the low level API
63  *
64  * @author Gaston Dombiak
65  */

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

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

83     public void testSendMessageEventRequest() {
84         // Create a chat for each connection
85
Chat chat1 = getConnection(0).createChat(getBareJID(1));
86
87         // Create the message to send with the roster
88
Message msg = chat1.createMessage();
89         msg.setSubject("Any subject you want");
90         msg.setBody("An interesting body comes here...");
91         // Create a MessageEvent Package and add it to the message
92
MessageEvent messageEvent = new MessageEvent();
93         messageEvent.setComposing(true);
94         messageEvent.setDelivered(true);
95         messageEvent.setDisplayed(true);
96         messageEvent.setOffline(true);
97         msg.addExtension(messageEvent);
98
99         // Send the message that contains the notifications request
100
try {
101             chat1.sendMessage(msg);
102             // Wait half second so that the complete test can run
103
Thread.sleep(200);
104         }
105         catch (Exception JavaDoc e) {
106             fail("An error occured sending the message");
107         }
108     }
109
110     /**
111      * Low level API test.
112      * This is a simple test to use with a XMPP client, check if the client receives the
113      * message and display in the console any notification
114      * 1. User_1 will send a message to user_2 requesting to be notified when any of these events
115      * occurs: offline, composing, displayed or delivered
116      * 2. User_2 will use a XMPP client (like Exodus) to display the message and compose a reply
117      * 3. User_1 will display any notification that receives
118      */

119     public void testSendMessageEventRequestAndDisplayNotifications() {
120         // Create a chat for each connection
121
Chat chat1 = getConnection(0).createChat(getBareJID(1));
122
123         // Create a Listener that listens for Messages with the extension "jabber:x:roster"
124
// This listener will listen on the conn2 and answer an ACK if everything is ok
125
PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event");
126         PacketListener packetListener = new PacketListener() {
127             public void processPacket(Packet packet) {
128                 Message message = (Message) packet;
129                 try {
130                     MessageEvent messageEvent =
131                         (MessageEvent) message.getExtension("x", "jabber:x:event");
132                     assertNotNull("Message without extension \"jabber:x:event\"", messageEvent);
133                     assertTrue(
134                         "Message event is a request not a notification",
135                         !messageEvent.isMessageEventRequest());
136                     System.out.println(messageEvent.toXML());
137                 }
138                 catch (ClassCastException JavaDoc e) {
139                     fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
140                 }
141             }
142         };
143         getConnection(0).addPacketListener(packetListener, packetFilter);
144
145         // Create the message to send with the roster
146
Message msg = chat1.createMessage();
147         msg.setSubject("Any subject you want");
148         msg.setBody("An interesting body comes here...");
149         // Create a MessageEvent Package and add it to the message
150
MessageEvent messageEvent = new MessageEvent();
151         messageEvent.setComposing(true);
152         messageEvent.setDelivered(true);
153         messageEvent.setDisplayed(true);
154         messageEvent.setOffline(true);
155         msg.addExtension(messageEvent);
156
157         // Send the message that contains the notifications request
158
try {
159             chat1.sendMessage(msg);
160             // Wait half second so that the complete test can run
161
Thread.sleep(200);
162         }
163         catch (Exception JavaDoc e) {
164             fail("An error occured sending the message");
165         }
166     }
167
168     protected int getMaxConnections() {
169         return 2;
170     }
171 }
172
Popular Tags